events 3.9.0, website 1.9.0, accounts 1.10.0: the church's details from Accounts everywhere
- Events: the Accounts profile's name, logo, icon, colours and contact details win over Events' own settings while Accounts is connected (pages, emails, WhatsApp, PDFs); Settings shows them read-only with a link to Accounts. The Accounts logo is copied for PDFs (PNG and JPEG). Fixes Events keeping its old church after a demo scenario load. - Website: a background per homepage section in the church's colours; "More info" instead of "Register" for events without registration; the shared colour field. - Accounts: the shared colour field in Church profile. - @crosscode/ui 0.4.0: ColourField and isHexColour. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -37,8 +37,9 @@ const realFetch = global.fetch;
|
||||
|
||||
beforeAll(() => {
|
||||
global.fetch = (async (url: string) => {
|
||||
// The logo, copied for PDFs (utils/logoCopy.js): not part of these tests.
|
||||
if (url !== `https://accounts.hope.test${CHURCH_PATHS.profile}`) return new Response('', { status: 404 });
|
||||
fetches++;
|
||||
expect(url).toBe(`https://accounts.hope.test${CHURCH_PATHS.profile}`);
|
||||
const next = answers.shift() ?? 200;
|
||||
if (next === 'down') throw new Error('connect ECONNREFUSED');
|
||||
return new Response(JSON.stringify(profile), { status: next, headers: { 'content-type': 'application/json' } });
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
// 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<void>): Promise<Record<string, string>> {
|
||||
let body: Record<string, string> = {};
|
||||
await handler({}, { json: (b: Record<string, string>) => { 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');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,75 @@
|
||||
// The logo for PDFs: an uploaded one is used as it is; the church's logo from Accounts (an address
|
||||
// on another site) is copied into uploads/accounts once, and only when it's a PNG or JPEG.
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-require-imports -- CommonJS */
|
||||
const { pdfLogoPath, resetLogoCopies } = require('../src/utils/logoCopy') as { pdfLogoPath(u: string): Promise<string | null>; resetLogoCopies(): void };
|
||||
const { UPLOADS_DIR } = require('../src/utils/uploads') as { UPLOADS_DIR: string };
|
||||
/* eslint-enable @typescript-eslint/no-require-imports */
|
||||
|
||||
const PNG = Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), Buffer.alloc(32)]);
|
||||
const SVG = Buffer.from('<svg xmlns="http://www.w3.org/2000/svg"/>');
|
||||
const COPIES = path.join(UPLOADS_DIR, 'accounts');
|
||||
const realFetch = global.fetch;
|
||||
let served: Record<string, Buffer | number> = {};
|
||||
let fetched: string[] = [];
|
||||
|
||||
beforeAll(() => {
|
||||
global.fetch = (async (url: string) => {
|
||||
fetched.push(url);
|
||||
const body = served[url];
|
||||
if (body === undefined || typeof body === 'number') return new Response('', { status: typeof body === 'number' ? body : 404 });
|
||||
return new Response(new Uint8Array(body), { status: 200 });
|
||||
}) as typeof fetch;
|
||||
});
|
||||
afterAll(() => {
|
||||
global.fetch = realFetch;
|
||||
fs.rmSync(COPIES, { recursive: true, force: true });
|
||||
});
|
||||
beforeEach(() => {
|
||||
fs.rmSync(COPIES, { recursive: true, force: true });
|
||||
resetLogoCopies();
|
||||
served = {};
|
||||
fetched = [];
|
||||
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
});
|
||||
afterEach(() => { jest.restoreAllMocks(); });
|
||||
|
||||
test("the church's PNG logo from Accounts is copied once, and used from the copy after", async () => {
|
||||
served['https://accounts.hope.test/logo.png'] = PNG;
|
||||
const p = await pdfLogoPath('https://accounts.hope.test/logo.png');
|
||||
expect(p && path.dirname(p)).toBe(COPIES);
|
||||
expect(fs.readFileSync(p!)).toEqual(PNG);
|
||||
expect(await pdfLogoPath('https://accounts.hope.test/logo.png')).toBe(p);
|
||||
expect(fetched).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('a new logo replaces the copy of the old one', async () => {
|
||||
served['https://accounts.hope.test/old.png'] = PNG;
|
||||
served['https://accounts.hope.test/new.png'] = PNG;
|
||||
const old = await pdfLogoPath('https://accounts.hope.test/old.png');
|
||||
const next = await pdfLogoPath('https://accounts.hope.test/new.png');
|
||||
expect(next).not.toBe(old);
|
||||
expect(fs.existsSync(old!)).toBe(false);
|
||||
expect(fs.readdirSync(COPIES)).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("an SVG logo or one that can't be fetched gives none (the PDF shows the name), and isn't fetched again at once", async () => {
|
||||
served['https://accounts.hope.test/logo.svg'] = SVG;
|
||||
served['https://accounts.hope.test/gone.png'] = 404;
|
||||
expect(await pdfLogoPath('https://accounts.hope.test/logo.svg')).toBeNull();
|
||||
expect(await pdfLogoPath('https://accounts.hope.test/gone.png')).toBeNull();
|
||||
expect(await pdfLogoPath('https://accounts.hope.test/logo.svg')).toBeNull();
|
||||
expect(fetched).toHaveLength(2);
|
||||
});
|
||||
|
||||
test('a logo uploaded here is used as it is; nothing for no logo', async () => {
|
||||
const dir = path.join(UPLOADS_DIR, 'accounts');
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, 'own.png'), PNG);
|
||||
expect(await pdfLogoPath('/uploads/accounts/own.png')).toBe(path.join(dir, 'own.png'));
|
||||
expect(await pdfLogoPath('')).toBeNull();
|
||||
expect(await pdfLogoPath('/uploads/accounts/missing.png')).toBeNull();
|
||||
expect(fetched).toHaveLength(0);
|
||||
});
|
||||
Reference in New Issue
Block a user