import { describe, expect, it } from 'vitest' import { readdirSync } from 'node:fs' import { resolve } from 'node:path' import { psqlFailure } from '../scripts/local-db' import { checkInitEnv, GOTRUE_ROLE_MIGRATION, goTruePasswordSql, INIT_SQL, readPhase, readSetupUrls, scramSha256Verifier } from '../scripts/selfhost-setup' const DATABASE_URL = 'postgresql://postgres:secret@db:5432/postgres' const SUPABASE_ADMIN_URL = 'postgres://supabase_admin:secret@db:5432/postgres' describe('readSetupUrls', () => { it('maps each role to its URL', () => { expect(readSetupUrls({ DATABASE_URL, SUPABASE_ADMIN_URL })).toEqual({ postgres: DATABASE_URL, supabase_admin: SUPABASE_ADMIN_URL, }) }) it('names the missing variable', () => { expect(() => readSetupUrls({ DATABASE_URL })).toThrow('SUPABASE_ADMIN_URL is not set') expect(() => readSetupUrls({ DATABASE_URL: ' ', SUPABASE_ADMIN_URL })).toThrow('DATABASE_URL is not set') }) it('rejects values that are not postgres URLs without echoing them', () => { expect(() => readSetupUrls({ DATABASE_URL: 'host=db password=hunter2', SUPABASE_ADMIN_URL })).toThrow('DATABASE_URL is not a valid URL') expect(() => readSetupUrls({ DATABASE_URL: 'host=db password=hunter2', SUPABASE_ADMIN_URL })).not.toThrow(/hunter2/) expect(() => readSetupUrls({ DATABASE_URL: 'mysql://root:pw@db/capgo', SUPABASE_ADMIN_URL })).toThrow('DATABASE_URL must start with postgresql://') }) }) describe('readPhase', () => { it('defaults to every pending migration and accepts setup-db', () => { expect(readPhase([])).toBe('migrate') expect(readPhase(['setup-db'])).toBe('setup-db') }) it('rejects anything else', () => { expect(() => readPhase(['setup'])).toThrow('Usage') expect(() => readPhase(['setup-db', 'extra'])).toThrow('Usage') }) }) describe('gotrue_cf password', () => { // RFC 7677's example password and salt; the verifier was computed with Python's hashlib. const salt = Buffer.from('W22ZaJ0SNY7soEsUEjb6gQ==', 'base64') const verifier = 'SCRAM-SHA-256$4096:W22ZaJ0SNY7soEsUEjb6gQ==$WG5d8oPm3OtcPnkdi4Uo7BkeZkBFzpcXkuLmtbsT4qY=:wfPLwcE6nTWhTAmQ7tl2KeoiWGPlZqQxSrmfPwDl2dU=' it('computes the SCRAM-SHA-256 verifier Postgres stores', () => { expect(scramSha256Verifier('pencil', salt)).toBe(verifier) }) it('uses a new 16-byte salt each time', () => { const first = scramSha256Verifier('pencil') expect(first).toMatch(/^SCRAM-SHA-256\$4096:[A-Za-z0-9+/]{22}==\$[A-Za-z0-9+/]{43}=:[A-Za-z0-9+/]{43}=$/) expect(scramSha256Verifier('pencil')).not.toBe(first) }) it('sends the verifier, never the password', () => { const sql = goTruePasswordSql({ GOTRUE_DB_PASSWORD: 'hunter2-secret' }) expect(sql).toMatch(/^ALTER ROLE gotrue_cf WITH LOGIN PASSWORD 'SCRAM-SHA-256\$4096:[A-Za-z0-9+/=$:]+';\n$/) expect(sql).not.toContain('hunter2-secret') }) it('needs GOTRUE_DB_PASSWORD', () => { expect(() => goTruePasswordSql({})).toThrow('GOTRUE_DB_PASSWORD is not set') expect(() => goTruePasswordSql({ GOTRUE_DB_PASSWORD: '' })).toThrow('GOTRUE_DB_PASSWORD is not set') }) it('refuses a password clients would prepare differently', () => { // U+1680: SASLprep maps it to a space, so GoTrue's login would not match the verifier. expect(() => goTruePasswordSql({ GOTRUE_DB_PASSWORD: 'pass word' })).toThrow('printable ASCII') expect(() => goTruePasswordSql({ GOTRUE_DB_PASSWORD: 'pass\nword' })).toThrow('printable ASCII') }) }) describe('checkInitEnv', () => { const env = { CAPGO_INTERNAL_URL: 'http://proxy', API_SECRET: 'secret' } it('accepts an origin and a secret', () => { expect(() => checkInitEnv(env)).not.toThrow() expect(() => checkInitEnv({ ...env, CAPGO_INTERNAL_URL: 'https://capgo.example.com:8443' })).not.toThrow() }) it('names the missing variable', () => { expect(() => checkInitEnv({ API_SECRET: 'secret' })).toThrow('CAPGO_INTERNAL_URL is not set') expect(() => checkInitEnv({ CAPGO_INTERNAL_URL: 'http://proxy' })).toThrow('API_SECRET is not set') }) it('refuses anything that is not an origin', () => { for (const value of ['proxy', 'ftp://proxy', 'http://proxy/', 'http://proxy/api']) expect(() => checkInitEnv({ ...env, CAPGO_INTERNAL_URL: value })).toThrow('must be an origin') }) }) describe('INIT_SQL', () => { it('reads the secret from the environment and leaves an existing plan alone', () => { expect(INIT_SQL).toContain('\\getenv apikey API_SECRET') expect(INIT_SQL).toContain('9007199254740991') expect(INIT_SQL).toContain('WHERE NOT EXISTS (SELECT 1 FROM public.plans WHERE name = \'Solo\')') }) }) describe('GOTRUE_ROLE_MIGRATION', () => { it('is the migration that creates gotrue_cf and auth_cf', () => { const files = readdirSync(resolve(import.meta.dirname, '../supabase/migrations')) expect(files).toContain(`${GOTRUE_ROLE_MIGRATION}_gotrue_cf_role_auth_cf_schema.sql`) }) }) describe('psqlFailure', () => { it('names the migration being applied and keeps only the error lines', () => { const stdout = 'Applying 1_a.sql\nApplying 2_b.sql\n' const stderr = 'psql::12: ERROR: relation "x" does not exist\nnoise\nLINE 1: SELECT * FROM x\n' expect(psqlFailure('migrations', stdout, stderr)).toBe( 'Applying 2_b.sql failed:\npsql::12: ERROR: relation "x" does not exist\nLINE 1: SELECT * FROM x', ) }) it('falls back to the label and raw stderr', () => { expect(psqlFailure('bootstrap.sql', '', 'could not connect')).toBe('bootstrap.sql failed:\ncould not connect') }) })