mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 10:24:05 +00:00
fix(demo): journey and collab uploads are refused for the demo account too
Six upload routes refused the shared demo account in demo mode; journey photos, videos, gallery media and covers, collab note files and chat images did not, so any visitor could put images in front of every other one for the hour. They answer the same 403 now, checked in the handler after the upload is read, and a text-only chat message still goes through. The Demo Mode wiki page lists them, and says document sync is not covered.
This commit is contained in:
@@ -32,8 +32,9 @@ import { Test } from '@nestjs/testing';
|
||||
|
||||
import cookieParser from 'cookie-parser';
|
||||
import type { Server } from 'http';
|
||||
import path from 'node:path';
|
||||
import request from 'supertest';
|
||||
import { describe, it, expect, beforeAll, afterAll, beforeEach, vi, type MockInstance } from 'vitest';
|
||||
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach, vi, type MockInstance } from 'vitest';
|
||||
|
||||
vi.mock('../../src/db/database', async () => {
|
||||
const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock');
|
||||
@@ -329,4 +330,50 @@ describe('Collab e2e (real auth guard + temp SQLite)', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('demo mode', () => {
|
||||
// Over the wire, because a refusal from a guard would reset the connection
|
||||
// while the client is still sending (PROFILE-015): the 403 has to arrive with
|
||||
// its body, and nothing of the upload may be written.
|
||||
const refused = { error: 'Uploads are disabled in demo mode. Self-host TREK for full functionality.' };
|
||||
|
||||
beforeAll(async () => {
|
||||
// Pinned id: sessionCookie(2) signs for exactly this user, the owner of trip 6.
|
||||
await makeUser(orm, { id: 2, username: 'demo', email: 'demo@trek.app', role: 'user', password_version: 0 });
|
||||
await insertRow(orm, Trips, { id: 6, title: 'Demo trip', user: 2 });
|
||||
});
|
||||
beforeEach(() => {
|
||||
vi.stubEnv('DEMO_MODE', 'true');
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it('403 with the demo body for a note file, and no file row', async () => {
|
||||
const res = await request(server)
|
||||
.post('/api/trips/6/collab/notes/1/files')
|
||||
.set('Cookie', sessionCookie(2))
|
||||
.attach('file', path.join(__dirname, '../fixtures/test.pdf'));
|
||||
expect(res.status).toBe(403);
|
||||
expect(res.body).toEqual(refused);
|
||||
expect(await countRows(orm, TripFiles)).toBe(0);
|
||||
});
|
||||
|
||||
it('403 with the demo body for a chat image, and no message row; a text message still posts', async () => {
|
||||
const res = await request(server)
|
||||
.post('/api/trips/6/collab/messages')
|
||||
.set('Cookie', sessionCookie(2))
|
||||
.field('text', 'look')
|
||||
.attach('images', path.join(__dirname, '../fixtures/small-image.jpg'));
|
||||
expect(res.status).toBe(403);
|
||||
expect(res.body).toEqual(refused);
|
||||
expect(await countRows(orm, CollabMessages)).toBe(0);
|
||||
const text = await request(server)
|
||||
.post('/api/trips/6/collab/messages')
|
||||
.set('Cookie', sessionCookie(2))
|
||||
.send({ text: 'hi' });
|
||||
expect(text.status).toBe(201);
|
||||
expect(text.body.message).toMatchObject({ text: 'hi', trip_id: 6, user_id: 2 });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -28,7 +28,7 @@ import cookieParser from 'cookie-parser';
|
||||
import type { Server } from 'http';
|
||||
import path from 'node:path';
|
||||
import request from 'supertest';
|
||||
import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } from 'vitest';
|
||||
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach, vi } from 'vitest';
|
||||
|
||||
const { db } = vi.hoisted(() => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
@@ -749,4 +749,39 @@ describe('Journey e2e (real auth guard + temp SQLite)', () => {
|
||||
expect(jsvc.linkPhotoToEntry).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('demo mode', () => {
|
||||
// Over the wire, because a refusal from a guard would reset the connection
|
||||
// while the client is still sending (PROFILE-015): the 403 has to arrive with
|
||||
// its body, and nothing of the upload may reach the service.
|
||||
const image = path.join(__dirname, '../fixtures/small-image.jpg');
|
||||
const clip = { filename: 'clip.mp4', contentType: 'video/mp4' };
|
||||
|
||||
beforeAll(() => {
|
||||
seedUser(db as never, { id: 2, username: 'demo', email: 'demo@trek.app' });
|
||||
});
|
||||
beforeEach(() => {
|
||||
vi.stubEnv('DEMO_MODE', 'true');
|
||||
jsvc.uploadGalleryPhotos.mockClear();
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it.each([
|
||||
['/api/journeys/entries/3/photos', 'photos'],
|
||||
['/api/journeys/entries/3/video', 'video'],
|
||||
['/api/journeys/9/gallery/photos', 'photos'],
|
||||
['/api/journeys/9/gallery/video', 'video'],
|
||||
['/api/journeys/9/cover', 'cover'],
|
||||
])('JOURNEY-E2E-020: POST %s answers the demo account 403 with the demo body', async (url, field) => {
|
||||
const req = request(server).post(url).set('Cookie', sessionCookie(2));
|
||||
const res = await (field === 'video'
|
||||
? req.attach(field, Buffer.from('not a clip'), clip)
|
||||
: req.attach(field, image));
|
||||
expect(res.status).toBe(403);
|
||||
expect(res.body).toEqual({ error: 'Uploads are disabled in demo mode. Self-host TREK for full functionality.' });
|
||||
expect(jsvc.uploadGalleryPhotos).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user