import { describe, expect, test } from 'bun:test' import { datasourceConfigCredentialKind, mapConfigsAllRow, normalizeDatasourceConfigPayload, parsePositiveConfigId, recordContainsSecrets, redactRawJsonText, } from '../src/admin/pages/datasourceConfigHelpers' describe('datasourceConfigHelpers', () => { test('sets auth_type=oauth_client and omits masked secrets for barentswatch_vessels', () => { const normalized = normalizeDatasourceConfigPayload({ name: 'barentswatch_vessels', source_type: 'api', endpoint: 'https://live.ais.barentswatch.no/v1/latest/combined', client_id: 'user@example.com:client', client_secret: '••••••••', auth_type: 'none', }) expect(normalized.auth_type).toBe('oauth_client') expect(normalized.client_id).toBeUndefined() expect(normalized.client_secret).toBeUndefined() expect(normalized.auth_config).toEqual({ client_id: 'user@example.com:client' }) }) test('free built-in collectors do not receive bogus api_key fields', () => { const mapped = mapConfigsAllRow({ name: 'bgpstream_bgp', requires_credentials: false, credential_provider: null, endpoint: 'https://broker.bgpstream.caida.org/v2', auth_type: 'none', is_active: true, auth_config: {}, auth_configured: {}, }) expect(mapped.api_key).toBeUndefined() expect(mapped.api_key_name).toBeUndefined() expect(mapped.api_key_location).toBeUndefined() expect(datasourceConfigCredentialKind(mapped)).toBe('') }) test('redacts plaintext secrets from raw metadata text while preserving boolean flags', () => { const draftRecord = { name: 'barentswatch_vessels', requires_credentials: true, client_id: 'qklovehs@hotmail.com:planet-collector', client_secret: 'super-secret-value', auth_configured: { client_id: true, client_secret: false }, } expect(recordContainsSecrets(draftRecord)).toBe(true) const redacted = JSON.parse(redactRawJsonText(JSON.stringify(draftRecord), draftRecord)) expect(redacted.client_secret).toBe('••••••••') expect(redacted.client_id).toBe('qklovehs@hotmail.com:planet-collector') expect(redacted.auth_configured.client_secret).toBe(false) }) test('validates saved config IDs before custom sample POST', () => { expect(parsePositiveConfigId(12)).toBe(12) expect(parsePositiveConfigId('7')).toBe(7) expect(parsePositiveConfigId('bgpstream_bgp')).toBeNull() expect(parsePositiveConfigId(null)).toBeNull() }) })