Files
TREK/server/tests/unit/nest/demo-write.test.ts
T
Maurice 724c88c201 style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
2026-10-10 21:23:37 +02:00

63 lines
2.3 KiB
TypeScript

/**
* The demo-mode upload block.
*
* The condition was copy-pasted into six upload handlers, each carrying its own
* copy of the 403 body. All six call isDemoWriteBlocked now.
*
* It is a function rather than a guard on purpose, and that is worth a test of
* its own: guards run before the multipart parser, so throwing from one leaves
* the request body unread and Node resets the connection — the client gets
* ECONNRESET instead of the 403. PROFILE-015 in the integration suite is what
* demonstrates it end-to-end.
*/
import { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
import { isDemoEmail } from '../../../src/nest/common/demo';
import { isDemoWriteBlocked, DEMO_WRITE_ERROR } from '../../../src/nest/common/demo-write';
import { describe, it, expect, vi, afterEach } from 'vitest';
vi.mock('../../../src/nest/common/demo', () => ({ isDemoEmail: vi.fn(() => false) }));
const env = new RuntimeEnvService();
afterEach(() => {
delete process.env.DEMO_MODE;
vi.mocked(isDemoEmail).mockReturnValue(false);
});
describe('isDemoWriteBlocked', () => {
it('DEMO-WRITE-001: blocks the demo account while the instance runs in demo mode', () => {
process.env.DEMO_MODE = 'true';
vi.mocked(isDemoEmail).mockReturnValue(true);
expect(isDemoWriteBlocked(env, 'demo@trek.app')).toBe(true);
});
it('DEMO-WRITE-002: lets a normal account through even while demo mode is on', () => {
process.env.DEMO_MODE = 'true';
vi.mocked(isDemoEmail).mockReturnValue(false);
expect(isDemoWriteBlocked(env, 'alice@example.com')).toBe(false);
});
it('DEMO-WRITE-003: lets the demo account through when the instance is not in demo mode', () => {
vi.mocked(isDemoEmail).mockReturnValue(true);
expect(isDemoWriteBlocked(env, 'demo@trek.app')).toBe(false);
});
it('DEMO-WRITE-004: reads the env live, so a toggle takes effect without a restart', () => {
vi.mocked(isDemoEmail).mockReturnValue(true);
expect(isDemoWriteBlocked(env, 'demo@trek.app')).toBe(false);
process.env.DEMO_MODE = 'true';
expect(isDemoWriteBlocked(env, 'demo@trek.app')).toBe(true);
});
it('DEMO-WRITE-005: the 403 body is the one the six endpoints have always sent', () => {
expect(DEMO_WRITE_ERROR).toEqual({
error: 'Uploads are disabled in demo mode. Self-host TREK for full functionality.',
});
});
});