Files
crosscode-platform/apps/events/backend/tests/brandingUploads.test.ts
T
joshuaandClaude Opus 5.5 8856d6b35d Events 3.3.0, Accounts 1.3.0, contracts 1.11.0: Accounts looks like Events, backups; sign-out and upload fixes
- Accounts: account and admin pages in Cross Events' layout (top bar, sidebar,
  a page per section, dark footer); quick links to the church's sites
  (GET /me/apps); nightly pg_dump backups with Admin -> Backups (run, keep,
  download, audited; heartbeat lastBackupAt); a fuller built-in privacy notice
  with headings and bullets; a fixed favicon. /backups/ ignored (anchored, so
  src/backups is kept).
- Contracts 1.11.0: people.removeDemo (only @example.com, never an admin).
- Events: uploads served from the folder they're saved in (a backend started
  outside backend/ answered 404 for a logo it had just saved); Branding checks
  the file loads before saving; logout waits for the sign-out call so Accounts
  signs out too; set-demo removes the last demo's people from Accounts.
Tests: Accounts 62 + e2e 8, Events 782, contracts 225; npm run check passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 11:19:57 +02:00

75 lines
3.5 KiB
TypeScript

// Branding uploads end to end (3.3.0): the real v2 upload routes (multer saving to disk) and the
// real file serving, as app.js mounts them. The logo and favicon saved are the files the returned
// address serves, wherever the backend was started from (before 3.3.0 a backend started outside
// backend/ answered 404 for a logo it had just saved). Only the database is a stand-in (no admin
// yet, so the upload is allowed as during setup).
import express from 'express';
import fs from 'fs';
import os from 'os';
import path from 'path';
import request from 'supertest';
import { v2Router } from '../src/v2';
/* eslint-disable @typescript-eslint/no-require-imports -- CommonJS */
const { serveUploads } = require('../src/utils/uploads');
/* eslint-enable @typescript-eslint/no-require-imports */
function site() {
const app = express();
app.use(express.json());
app.use('/api/v2', v2Router());
app.use('/api/uploads', serveUploads());
return app;
}
// Any model, any call: nothing found, no users (as before setup).
jest.mock('../src/config/db', () => {
const anything: unknown = new Proxy(function () { /* callable */ }, {
get: (_t, key) => (key === 'then' ? undefined : key === 'count' ? async () => 0 : anything),
apply: async () => null,
});
return anything;
});
// The real upload folder of this checkout (as multer writes it), cleaned up after.
const BRANDING = path.join(__dirname, '..', 'public', 'uploads', 'branding');
const made: string[] = [];
afterAll(() => { for (const f of made) fs.rmSync(path.join(BRANDING, f), { force: true }); });
const PNG = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==', 'base64');
describe('branding uploads', () => {
const startedIn = process.cwd();
// PM2 or a script may start the backend from anywhere: the served files mustn't depend on it.
beforeAll(() => process.chdir(fs.mkdtempSync(path.join(os.tmpdir(), 'events-cwd-'))));
afterAll(() => process.chdir(startedIn));
test.each([['logo', 'logo.png'], ['favicon', 'icon.png']])('a %s upload is served at the address it answers with', async (kind, name) => {
const app = site();
const up = await request(app).post(`/api/v2/uploads/${kind}`).attach('image', PNG, { filename: name, contentType: 'image/png' });
expect(up.status).toBe(200);
const saved = String(up.body.data.path);
expect(saved).toMatch(new RegExp(`^/uploads/branding/${kind}-[\\w-]+\\.png$`));
made.push(path.basename(saved));
expect(fs.existsSync(path.join(BRANDING, path.basename(saved)))).toBe(true);
const got = await request(app).get(new URL(up.body.data.url).pathname);
expect(got.status).toBe(200);
expect(Buffer.compare(got.body as Buffer, PNG)).toBe(0);
});
test('app.js serves uploads from that same folder, never one relative to where it started', () => {
const source = fs.readFileSync(path.join(__dirname, '..', 'src', 'app.js'), 'utf8');
expect(source).toContain("require('./utils/uploads').serveUploads()");
expect(source).not.toMatch(/express\.static\(\s*['"`]/);
});
test('a file that isn\'t an image is refused, and nothing is saved', async () => {
const app = site();
const before = fs.existsSync(BRANDING) ? fs.readdirSync(BRANDING).length : 0;
const up = await request(app).post('/api/v2/uploads/logo').attach('image', Buffer.from('<html>'), { filename: 'page.html', contentType: 'text/html' });
expect(up.status).toBe(400);
expect((fs.existsSync(BRANDING) ? fs.readdirSync(BRANDING).length : 0)).toBe(before);
});
});