// The church's own details from Accounts (name, logo, icon, colours, contact) win over this site's // settings of the same name: on the public settings, in Settings (read-only), and for everything // that reads them through the settings cache (emails, PDFs). Without Accounts, the site's own stand. import fs from 'fs'; import path from 'path'; type Link = { installId: string; product: string; url: string; secret: string; updatedAt: Date }; const mockLinks: Link[] = []; const mockRows: { key: string; value: string }[] = []; jest.mock('../src/config/db', () => ({ productLink: { findFirst: async ({ where }: { where: { product: string } }) => mockLinks.find((l) => l.product === where.product) ?? null, }, appSetting: { findMany: async (q?: { where?: { key?: { in?: string[] } } }) => mockRows.filter((r) => !q?.where?.key?.in || q.where.key.in.includes(r.key)), }, })); process.env.JWT_SECRET = 'a-test-secret-that-is-long-enough-for-the-encryption'; import { CHURCH_PATHS } from '@crosscode/contracts'; import { churchProfile, churchSettings, resetChurchProfile } from '../src/accounts/church'; import { churchLook } from '../src/accounts/look'; /* eslint-disable @typescript-eslint/no-require-imports -- CommonJS */ const settingsCache = require('../src/utils/settingsCache'); const { getSettings, getAllSettings } = require('../src/controllers/settingsController'); /* eslint-enable @typescript-eslint/no-require-imports */ const SAMPLES = path.join(path.dirname(require.resolve('@crosscode/contracts/package.json')), 'samples'); const sample = () => JSON.parse(fs.readFileSync(path.join(SAMPLES, 'church.json'), 'utf8')); let profile = sample(); const realFetch = global.fetch; beforeAll(() => { global.fetch = (async (url: string) => { if (url !== `https://accounts.hope.test${CHURCH_PATHS.profile}`) return new Response('', { status: 404 }); // the logo (logoCopy tests) return new Response(JSON.stringify(profile), { status: 200, headers: { 'content-type': 'application/json' } }); }) as typeof fetch; }); afterAll(() => { global.fetch = realFetch; }); beforeEach(() => { resetChurchProfile(); mockLinks.length = 0; mockRows.length = 0; mockRows.push( { key: 'org_name', value: 'Old Demo Church' }, { key: 'org_tagline', value: 'Our own tagline' }, { key: 'primary_color', value: '#111111' }, { key: 'secondary_color', value: '#222222' }, { key: 'logo_url', value: '/uploads/old-logo.png' }, ); settingsCache.invalidate(); profile = sample(); }); const connectAccounts = () => mockLinks.push({ installId: 'acc', product: 'accounts', url: 'https://accounts.hope.test/', secret: 'x', updatedAt: new Date() }); async function call(handler: (req: unknown, res: unknown) => Promise): Promise> { let body: Record = {}; await handler({}, { json: (b: Record) => { body = b; }, status() { return this; } }); return body; } describe("the church's details from Accounts", () => { test('only what the profile has', () => { profile.branding.iconUrl = null; profile.branding.colours.accent = null; const s = churchSettings(profile); expect(s.org_name).toBe(profile.name); expect(s.primary_color).toBe(profile.branding.colours.primary); expect('favicon_url' in s).toBe(false); expect('accent_color' in s).toBe(false); expect(churchSettings(null)).toEqual({}); }); test('without Accounts, the site keeps its own', async () => { const s = await call(getSettings); expect(s.org_name).toBe('Old Demo Church'); expect((await call(getAllSettings)).accounts_managed).toBe(''); }); test("with Accounts, the public settings show the church's, and keep what the profile doesn't set", async () => { connectAccounts(); const s = await call(getSettings); expect(s.org_name).toBe(profile.name); expect(s.primary_color).toBe(profile.branding.colours.primary); expect(s.logo_url).toBe(profile.branding.logoUrl); expect(s.org_tagline).toBe('Our own tagline'); expect(s.secondary_color).toBe('#222222'); }); test('Settings marks the ones Accounts sets', async () => { connectAccounts(); const all = await call(getAllSettings); expect(all.org_name).toBe(profile.name); expect(all.accounts_managed.split(',')).toEqual(expect.arrayContaining(['org_name', 'primary_color', 'logo_url'])); expect(all.accounts_managed.split(',')).not.toContain('secondary_color'); }); test('the settings cache (emails, PDFs) uses them once the profile is in, and the look sent to Accounts stays the site\'s own', async () => { connectAccounts(); await settingsCache.warmCache(); await churchProfile(); expect(settingsCache.getSettingSync('org_name', 'Cross Code')).toBe(profile.name); expect(await settingsCache.getStoredSetting('org_name')).toBe('Old Demo Church'); expect((await churchLook()).name).toBe('Old Demo Church'); }); test('a church that changes its name in Accounts (a new demo church) is shown after church.changed', async () => { connectAccounts(); await churchProfile(); profile.name = 'Grace Fellowship'; // eslint-disable-next-line @typescript-eslint/no-require-imports require('../src/accounts/church').churchChanged(); await churchProfile(); expect(settingsCache.getSettingSync('org_name')).toBe('Grace Fellowship'); expect((await call(getSettings)).org_name).toBe('Grace Fellowship'); }); test('disconnected from Accounts: back to the site\'s own', async () => { connectAccounts(); await churchProfile(); mockLinks.length = 0; await churchProfile(); expect(settingsCache.getSettingSync('org_name')).toBe('Old Demo Church'); }); });