import { readAuthConfig, parseTtl, clientConfig } from './config.js'; const base = { HB_PUBLIC_URL : 'https://brew.example.org/', HB_OIDC_ISSUER : 'https://idp.example/realms/x', HB_OIDC_CLIENT_ID : 'id', HB_OIDC_CLIENT_SECRET : 's', HB_SESSION_SECRET : 'x'.repeat(32) }; const own = { upstreamSecret: 'eigenes-secret' }; describe('Auth-Konfiguration', ()=>{ test('Standard ohne Angaben: nur naturalcrit (Upstream-Verhalten)', ()=>{ const c = readAuthConfig({}); expect(c.providers).toEqual(['naturalcrit']); expect(clientConfig(c).loginUrl).toBeNull(); }); test('Issuer gesetzt, Provider leer: oidc,naturalcrit mit Auswahlseite', ()=>{ const c = readAuthConfig(base, own); expect(c.providers).toEqual(['oidc', 'naturalcrit']); expect(c.publicUrl).toBe('https://brew.example.org'); expect(c.secureCookies).toBe(true); expect(clientConfig(c).loginUrl).toBe('/auth/login'); expect(c.oidc).toMatchObject({ scopes: 'openid profile email', usernameClaim: 'preferred_username', linkByEmail: false, endSession: false, label: 'Anmelden' }); }); test('nur oidc: direkter Login-Link', ()=>{ const c = readAuthConfig({ ...base, HB_AUTH_PROVIDERS: 'oidc' }); expect(c.providers).toEqual(['oidc']); expect(clientConfig(c).loginUrl).toBe('/auth/oidc/login'); }); test('Startprüfung nennt fehlende Variablen', ()=>{ expect(()=>readAuthConfig({ HB_AUTH_PROVIDERS: 'oidc' })).toThrow(/HB_OIDC_ISSUER.*HB_OIDC_CLIENT_ID.*HB_OIDC_CLIENT_SECRET.*HB_PUBLIC_URL.*HB_SESSION_SECRET/); }); test('HB_NATURALCRIT_RENAME: Standard off, signed erlaubt, unbekannter Wert bricht ab', ()=>{ expect(readAuthConfig({}).naturalcrit).toEqual({ rename: 'off', secret: undefined }); expect(readAuthConfig({ HB_NATURALCRIT_RENAME: ' Signed ' }, own).naturalcrit).toEqual({ rename: 'signed', secret: 'eigenes-secret' }); expect(()=>readAuthConfig({ HB_NATURALCRIT_RENAME: 'upstream' }, own)).toThrow(/HB_NATURALCRIT_RENAME ungültig/); }); test('HB_NATURALCRIT_RENAME=signed mit Standard-secret bricht außerhalb lokaler Umgebungen ab', ()=>{ expect(()=>readAuthConfig({ HB_NATURALCRIT_RENAME: 'signed' }, { upstreamSecret: 'secret' })).toThrow(/eigenes Upstream-"secret"/); expect(()=>readAuthConfig({ HB_NATURALCRIT_RENAME: 'signed' })).toThrow(/eigenes Upstream-"secret"/); expect(readAuthConfig({ HB_NATURALCRIT_RENAME: 'signed' }, { upstreamSecret: 'secret', isLocal: true }).naturalcrit.rename).toBe('signed'); // ohne naturalcrit-Provider ist der Modus wirkungslos und kein Startfehler expect(readAuthConfig({ ...base, HB_AUTH_PROVIDERS: 'oidc', HB_NATURALCRIT_RENAME: 'signed' }).naturalcrit.rename).toBe('signed'); }); test('zu kurzes Session-Secret und unbekannter Provider brechen ab', ()=>{ expect(()=>readAuthConfig({ ...base, HB_SESSION_SECRET: 'kurz' }, own)).toThrow(/32 Byte/); expect(()=>readAuthConfig({ HB_AUTH_PROVIDERS: 'ldap' })).toThrow(/unbekannte/); }); test('http-URL: Cookies nicht Secure', ()=>{ expect(readAuthConfig({ ...base, HB_PUBLIC_URL: 'http://localhost:8000' }, own).secureCookies).toBe(false); }); test('TTL-Formate', ()=>{ expect(parseTtl('7d')).toBe(604800); expect(parseTtl('12h')).toBe(43200); expect(parseTtl('3600')).toBe(3600); expect(parseTtl(undefined)).toBe(604800); expect(()=>parseTtl('bald')).toThrow(); expect(()=>parseTtl('0')).toThrow(); }); test('Secret taucht nicht in der Client-Konfiguration auf', ()=>{ expect(JSON.stringify(clientConfig(readAuthConfig(base, own)))).not.toMatch(/secret|"s"/i); }); describe('Upstream-secret (fälschbare nc_session)', ()=>{ test('Abbruch bei oidc+naturalcrit, Standard-secret, nicht lokal', ()=>{ expect(()=>readAuthConfig(base, { upstreamSecret: 'secret', isLocal: false })).toThrow(/Upstream-"secret".*HB_AUTH_PROVIDERS=oidc/); expect(()=>readAuthConfig(base, { upstreamSecret: '', isLocal: false })).toThrow(/Upstream-"secret"/); expect(()=>readAuthConfig(base)).toThrow(/Upstream-"secret"/); }); test('kein Abbruch: lokal, nur oidc oder eigenes secret', ()=>{ expect(()=>readAuthConfig(base, { upstreamSecret: 'secret', isLocal: true })).not.toThrow(); expect(()=>readAuthConfig({ ...base, HB_AUTH_PROVIDERS: 'oidc' }, { upstreamSecret: 'secret', isLocal: false })).not.toThrow(); expect(()=>readAuthConfig(base, { upstreamSecret: 'eigenes-secret', isLocal: false })).not.toThrow(); }); test('reiner Upstream-Modus bleibt unverändert', ()=>{ expect(()=>readAuthConfig({}, { upstreamSecret: 'secret', isLocal: false })).not.toThrow(); }); }); describe('Issuer-Schema', ()=>{ test('http-Issuer mit https-HB_PUBLIC_URL bricht ab', ()=>{ expect(()=>readAuthConfig({ ...base, HB_OIDC_ISSUER: 'http://idp.example' }, own)).toThrow(/https:\/\//); }); test('http-Issuer mit http-HB_PUBLIC_URL ist erlaubt (Dev/Compose)', ()=>{ expect(()=>readAuthConfig({ ...base, HB_OIDC_ISSUER: 'http://idp:8080', HB_PUBLIC_URL: 'http://localhost:8000' }, own)).not.toThrow(); }); }); });