// 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(''), { filename: 'page.html', contentType: 'text/html' }); expect(up.status).toBe(400); expect((fs.existsSync(BRANDING) ? fs.readdirSync(BRANDING).length : 0)).toBe(before); }); });