system-settings.test.js (3410B)
1 import test from 'node:test'; 2 import assert from 'node:assert/strict'; 3 import fs from 'node:fs/promises'; 4 import os from 'node:os'; 5 import path from 'node:path'; 6 import { 7 closeRealms, 8 getSystemDatabase, 9 initRealms, 10 } from '../src/lib/realm.js'; 11 import { 12 getAdminSystemSettings, 13 getSystemBranding, 14 updateAdminSystemSettings, 15 } from '../src/lib/system-settings.js'; 16 17 async function withSystemDatabase(fn) { 18 const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'auth-system-settings-')); 19 try { 20 await initRealms({ 21 database: { 22 dialect: 'sqlite', 23 path: path.join(dir, 'system.sqlite'), 24 realms: path.join(dir, 'realms'), 25 }, 26 }); 27 await fn(getSystemDatabase()); 28 } finally { 29 await closeRealms(); 30 await fs.rm(dir, { recursive: true, force: true }); 31 } 32 } 33 34 test('system settings expose and persist global branding', async () => { 35 await withSystemDatabase(async (systemDb) => { 36 assert.deepEqual(await getSystemBranding(systemDb), {}); 37 38 const initial = await getAdminSystemSettings(systemDb); 39 assert.equal(initial.logo_url, null); 40 assert.equal(initial.primary_color, null); 41 assert.equal(typeof initial.updated_at, 'number'); 42 43 const updated = await updateAdminSystemSettings({ 44 systemDb, 45 body: { primary_color: '#25A' }, 46 }); 47 assert.equal(updated.primary_color, '#2255aa'); 48 assert.equal(updated.logo_url, null); 49 50 assert.deepEqual(await getSystemBranding(systemDb), { primary_color: '#2255aa' }); 51 }); 52 }); 53 54 test('system settings patch unknown branding keys and clear values', async () => { 55 await withSystemDatabase(async (systemDb) => { 56 await systemDb.run( 57 `UPDATE system_settings 58 SET branding = {branding} 59 WHERE id = 1`, 60 { 61 branding: JSON.stringify({ 62 primary_color: '#2255aa', 63 custom_asset: 'https://system.test/custom.png', 64 }), 65 } 66 ); 67 68 const updated = await updateAdminSystemSettings({ 69 systemDb, 70 body: { logo_url: 'https://system.test/logo.svg' }, 71 }); 72 assert.equal(updated.primary_color, '#2255aa'); 73 assert.equal(updated.logo_url, 'https://system.test/logo.svg'); 74 75 const row = await systemDb.get('SELECT branding FROM system_settings WHERE id = 1'); 76 assert.deepEqual(JSON.parse(row.branding), { 77 primary_color: '#2255aa', 78 custom_asset: 'https://system.test/custom.png', 79 logo_url: 'https://system.test/logo.svg', 80 }); 81 82 const cleared = await updateAdminSystemSettings({ 83 systemDb, 84 body: { logo_url: '', primary_color: null }, 85 }); 86 assert.equal(cleared.logo_url, null); 87 assert.equal(cleared.primary_color, null); 88 assert.deepEqual(JSON.parse((await systemDb.get('SELECT branding FROM system_settings WHERE id = 1')).branding), { 89 custom_asset: 'https://system.test/custom.png', 90 }); 91 }); 92 }); 93 94 test('system settings validate branding input', async () => { 95 await withSystemDatabase(async (systemDb) => { 96 for (const body of [ 97 {}, 98 { display_name: 'Auth' }, 99 { primary_color: 'red' }, 100 { logo_url: 'http://system.test/logo.svg' }, 101 ]) { 102 await assert.rejects( 103 () => updateAdminSystemSettings({ systemDb, body }), 104 (error) => { 105 assert.equal(error.status, 400); 106 assert.equal(error.code, 'invalid_request'); 107 return true; 108 } 109 ); 110 } 111 }); 112 });