diff --git a/server/tests/helpers/stored-objects.ts b/server/tests/helpers/stored-objects.ts new file mode 100644 index 000000000..b90177403 --- /dev/null +++ b/server/tests/helpers/stored-objects.ts @@ -0,0 +1,40 @@ +import type { StorageService } from '../../src/nest/storage/storage.service'; +import type { ServedCategory } from '../../src/nest/storage/storage.types'; + +export interface StoredObjects { + /** Every object stored through the facade since tracking began, in order. */ + readonly stored: ReadonlyArray<{ category: ServedCategory; name: string }>; + /** Deletes exactly those objects and stops tracking. */ + cleanup(): Promise; +} + +/** + * Records what a suite stores, so it can remove exactly that afterwards. + * + * Every suite that boots the app writes into the same server/uploads tree, and + * on the Postgres lane several of them run at once. A suite that cleaned up by + * removing a whole category folder (uploads/files, uploads/covers, ...) took + * the files of whichever suite was running beside it, which then failed with + * "storage object not found" halfway through a test. `put` is the facade's one + * write path, so wrapping it on the app's own instance sees every upload, + * thumbnail and derived copy the suite causes. + */ +export function trackStoredObjects(storage: StorageService): StoredObjects { + const stored: Array<{ category: ServedCategory; name: string }> = []; + const put = storage.put.bind(storage); + storage.put = async (...args: Parameters) => { + const result = await put(...args); + stored.push({ category: args[0], name: args[1] }); + return result; + }; + return { + stored, + async cleanup() { + storage.put = put; + for (const { category, name } of stored.splice(0)) { + // Already gone is fine: a test may have deleted its own object. + await storage.delete(category, name).catch(() => undefined); + } + }, + }; +} diff --git a/server/tests/integration/collections.test.ts b/server/tests/integration/collections.test.ts index aa46d3b28..a133a0a4e 100644 --- a/server/tests/integration/collections.test.ts +++ b/server/tests/integration/collections.test.ts @@ -10,11 +10,13 @@ import { buildApp } from '../../src/bootstrap'; import { Addons } from '../../src/db/entities/Addons.entity'; import { CollectionPlaces } from '../../src/db/entities/CollectionPlaces.entity'; import { Collections } from '../../src/db/entities/Collections.entity'; +import { StorageService } from '../../src/nest/storage/storage.service'; import { authCookie } from '../helpers/auth'; import type { FactoryOrm } from '../helpers/factories/context'; import { insertRow, upsertRow } from '../helpers/factories/rows'; import { makeUser } from '../helpers/factories/users'; import { resetDatabase } from '../helpers/reset-database'; +import { trackStoredObjects, type StoredObjects } from '../helpers/stored-objects'; import { resetRateLimits } from '../helpers/test-db'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; @@ -26,6 +28,8 @@ import request from 'supertest'; import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'; let nestApp: INestApplication; +// Only what this suite stored is removed: other suites share uploads/ and may run beside it. +let stored: StoredObjects; let app: Application; let orm: FactoryOrm; const FIXTURE_IMG = path.join(__dirname, '../fixtures/small-image.jpg'); @@ -42,6 +46,7 @@ async function createCollectionPlace(collectionId: number, ownerId: number): Pro beforeAll(async () => { nestApp = await buildApp(); + stored = trackStoredObjects(nestApp.get(StorageService)); app = nestApp.getHttpAdapter().getInstance(); orm = nestApp.get(MikroORM); }); @@ -62,9 +67,8 @@ beforeEach(async () => { }); afterAll(async () => { + await stored.cleanup(); await nestApp.close(); - fs.rmSync(coversDir, { recursive: true, force: true }); - fs.rmSync(placesDir, { recursive: true, force: true }); }); describe('Collection cover upload', () => { diff --git a/server/tests/integration/files.test.ts b/server/tests/integration/files.test.ts index 98290eb8c..0bf1823e1 100644 --- a/server/tests/integration/files.test.ts +++ b/server/tests/integration/files.test.ts @@ -8,6 +8,7 @@ * - FILE-009 (ephemeral token download) is covered via the /api/auth/resource-token endpoint */ import { buildApp } from '../../src/bootstrap'; +import { StorageService } from '../../src/nest/storage/storage.service'; import { authCookie, generateToken } from '../helpers/auth'; import type { FactoryOrm } from '../helpers/factories/context'; import { makePlace } from '../helpers/factories/places'; @@ -16,6 +17,7 @@ import { setAppSetting } from '../helpers/factories/settings'; import { makeTrip } from '../helpers/factories/trips'; import { makeUser } from '../helpers/factories/users'; import { resetDatabase } from '../helpers/reset-database'; +import { trackStoredObjects, type StoredObjects } from '../helpers/stored-objects'; import { resetRateLimits } from '../helpers/test-db'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; @@ -27,6 +29,8 @@ import request from 'supertest'; import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'; let nestApp: INestApplication; +// Only what this suite stored is removed: other suites share uploads/ and may run beside it. +let stored: StoredObjects; let app: Application; let orm: FactoryOrm; const FIXTURE_PDF = path.join(__dirname, '../fixtures/test.pdf'); @@ -37,6 +41,7 @@ const uploadsDir = path.join(__dirname, '../../uploads/files'); beforeAll(async () => { nestApp = await buildApp(); + stored = trackStoredObjects(nestApp.get(StorageService)); app = nestApp.getHttpAdapter().getInstance(); orm = nestApp.get(MikroORM); if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true }); @@ -52,8 +57,8 @@ beforeEach(async () => { }); afterAll(async () => { + await stored.cleanup(); await nestApp.close(); - fs.rmSync(uploadsDir, { recursive: true, force: true }); }); // Helper to upload a file and return the file object diff --git a/server/tests/integration/journey.test.ts b/server/tests/integration/journey.test.ts index 7e0561a78..ad829aacf 100644 --- a/server/tests/integration/journey.test.ts +++ b/server/tests/integration/journey.test.ts @@ -6,6 +6,7 @@ import { buildApp } from '../../src/bootstrap'; import { Addons } from '../../src/db/entities/Addons.entity'; import { TrekPhotos } from '../../src/db/entities/TrekPhotos.entity'; import { invalidatePermissionsCache } from '../../src/nest/permissions/permissions-cache'; +import { StorageService } from '../../src/nest/storage/storage.service'; import { authCookie } from '../helpers/auth'; import { jpegWithExif } from '../helpers/exif-jpeg'; import { addJourneyContributor, makeJourney, makeJourneyEntry } from '../helpers/factories/journeys'; @@ -14,6 +15,7 @@ import { setAppSetting } from '../helpers/factories/settings'; import { makeTrip } from '../helpers/factories/trips'; import { makeUser } from '../helpers/factories/users'; import { resetDatabase } from '../helpers/reset-database'; +import { trackStoredObjects, type StoredObjects } from '../helpers/stored-objects'; import { resetRateLimits } from '../helpers/test-db'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; @@ -1031,9 +1033,25 @@ describe('Journey upload parity', () => { const FIXTURE_IMG = pathMod.join(__dirname, '../fixtures/small-image.jpg'); const journeyDir = pathMod.join(__dirname, '../../uploads/journey'); - afterAll(() => { - fsMod.rmSync(journeyDir, { recursive: true, force: true }); + // Only what these tests store is removed: other suites share uploads/ and may run beside them. + let stored: StoredObjects; + beforeAll(() => { + stored = trackStoredObjects(nestApp.get(StorageService)); }); + afterAll(async () => { + await stored.cleanup(); + }); + + /** + * The objects stored since `since` that still exist. Counting the files in + * uploads/journey would also count what a suite beside this one writes there. + */ + async function stillStored(since: number): Promise { + const storage = nestApp.get(StorageService); + const objects = stored.stored.slice(since); + const exists = await Promise.all(objects.map(({ category, name }) => storage.exists(category, name))); + return objects.filter((_, i) => exists[i]).map(({ name }) => name); + } it('JOURNEY-P01 — entry photo upload stores journey/ with a lowercased extension', async () => { const { user } = await makeUser(orm); @@ -1098,7 +1116,7 @@ describe('Journey upload parity', () => { const { user: other } = await makeUser(orm); const journey = await makeJourney(orm, owner.id); - const before = fsMod.existsSync(journeyDir) ? fsMod.readdirSync(journeyDir).length : 0; + const since = stored.stored.length; const res = await request(app) .post(`/api/journeys/${journey.id}/gallery/photos`) .set('Cookie', authCookie(other.id)) @@ -1107,8 +1125,8 @@ describe('Journey upload parity', () => { expect(res.body.error).toBe('Not allowed'); // The commit runs before the permission answer is known, so the refusal has // to delete the object again. Nothing sweeps orphans. - const after = fsMod.existsSync(journeyDir) ? fsMod.readdirSync(journeyDir).length : 0; - expect(after).toBe(before); + expect(stored.stored.length).toBeGreaterThan(since); + expect(await stillStored(since)).toEqual([]); }); it('JOURNEY-P06 — video+poster upload: poster is ALWAYS stored as .jpg (stored-XSS pin)', async () => { @@ -1130,15 +1148,14 @@ describe('Journey upload parity', () => { const { user } = await makeUser(orm); const journey = await makeJourney(orm, user.id); - const before = fsMod.existsSync(journeyDir) ? fsMod.readdirSync(journeyDir).length : 0; + const since = stored.stored.length; const res = await request(app) .post(`/api/journeys/${journey.id}/gallery/video`) .set('Cookie', authCookie(user.id)) .attach('poster', fsMod.readFileSync(FIXTURE_IMG), { filename: 'poster.png', contentType: 'image/png' }); expect(res.status).toBe(400); expect(res.body.error).toBe('No video uploaded'); - const after = fsMod.existsSync(journeyDir) ? fsMod.readdirSync(journeyDir).length : 0; - expect(after).toBe(before); + expect(await stillStored(since)).toEqual([]); }); it('JOURNEY-P08 — non-contributor video upload is 403 and BOTH files are removed', async () => { @@ -1146,7 +1163,7 @@ describe('Journey upload parity', () => { const { user: other } = await makeUser(orm); const journey = await makeJourney(orm, owner.id); - const before = fsMod.existsSync(journeyDir) ? fsMod.readdirSync(journeyDir).length : 0; + const since = stored.stored.length; const res = await request(app) .post(`/api/journeys/${journey.id}/gallery/video`) .set('Cookie', authCookie(other.id)) @@ -1154,8 +1171,9 @@ describe('Journey upload parity', () => { .attach('poster', fsMod.readFileSync(FIXTURE_IMG), { filename: 'poster.png', contentType: 'image/png' }); expect(res.status).toBe(403); expect(res.body.error).toBe('Not allowed'); - const after = fsMod.existsSync(journeyDir) ? fsMod.readdirSync(journeyDir).length : 0; - expect(after).toBe(before); + // The bytes were written before the refusal, and are gone again. + expect(stored.stored.length).toBeGreaterThan(since); + expect(await stillStored(since)).toEqual([]); }); it('JOURNEY-P09 — cover upload stores journey/ into cover_image', async () => { diff --git a/server/tests/integration/security.test.ts b/server/tests/integration/security.test.ts index c65e89a5c..d20d60778 100644 --- a/server/tests/integration/security.test.ts +++ b/server/tests/integration/security.test.ts @@ -10,6 +10,7 @@ import { buildApp } from '../../src/bootstrap'; import { AuditLog } from '../../src/db/entities/AuditLog.entity'; import { TripFiles } from '../../src/db/entities/TripFiles.entity'; +import { StorageService } from '../../src/nest/storage/storage.service'; import { authCookie, authHeader, generateToken } from '../helpers/auth'; import type { FactoryOrm } from '../helpers/factories/context'; import { findRows, updateRows } from '../helpers/factories/rows'; @@ -17,6 +18,7 @@ import { setAppSetting } from '../helpers/factories/settings'; import { makeTrip } from '../helpers/factories/trips'; import { makeUser, readUser } from '../helpers/factories/users'; import { resetDatabase } from '../helpers/reset-database'; +import { trackStoredObjects, type StoredObjects } from '../helpers/stored-objects'; import { resetRateLimits } from '../helpers/test-db'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; @@ -28,6 +30,8 @@ import request from 'supertest'; import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'; let nestApp: INestApplication; +// Only what this suite stored is removed: other suites share uploads/ and may run beside it. +let stored: StoredObjects; let app: Application; /** The app's own ORM, which the factories seed and read through. */ let orm: FactoryOrm; @@ -36,6 +40,7 @@ const uploadsDir = path.join(__dirname, '../../uploads/files'); beforeAll(async () => { nestApp = await buildApp(); + stored = trackStoredObjects(nestApp.get(StorageService)); orm = nestApp.get(MikroORM); app = nestApp.getHttpAdapter().getInstance(); if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true }); @@ -49,8 +54,8 @@ beforeEach(async () => { }); afterAll(async () => { + await stored.cleanup(); await nestApp.close(); - fs.rmSync(uploadsDir, { recursive: true, force: true }); }); describe('Authentication security', () => { diff --git a/server/tests/integration/trips.test.ts b/server/tests/integration/trips.test.ts index dd4e8e014..1226451b4 100644 --- a/server/tests/integration/trips.test.ts +++ b/server/tests/integration/trips.test.ts @@ -20,6 +20,7 @@ import { TripMembers } from '../../src/db/entities/TripMembers.entity'; import { Trips } from '../../src/db/entities/Trips.entity'; import { Users } from '../../src/db/entities/Users.entity'; import { invalidatePermissionsCache } from '../../src/nest/permissions/permissions-cache'; +import { StorageService } from '../../src/nest/storage/storage.service'; import { authCookie } from '../helpers/auth'; import { makeBudgetItem } from '../helpers/factories/budget'; import type { FactoryOrm } from '../helpers/factories/context'; @@ -32,6 +33,7 @@ import { setAppSetting } from '../helpers/factories/settings'; import { addTripMember, makeTrip, readTripDays } from '../helpers/factories/trips'; import { makeAdmin, makeUser, readUser } from '../helpers/factories/users'; import { resetDatabase } from '../helpers/reset-database'; +import { trackStoredObjects, type StoredObjects } from '../helpers/stored-objects'; import { resetRateLimits } from '../helpers/test-db'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; @@ -1271,8 +1273,13 @@ describe('Trip cover upload parity', () => { const FIXTURE_IMG = pathMod.join(__dirname, '../fixtures/small-image.jpg'); const coversDir = pathMod.join(__dirname, '../../uploads/covers'); - afterAll(() => { - fsMod.rmSync(coversDir, { recursive: true, force: true }); + // Only what these tests store is removed: other suites share uploads/ and may run beside them. + let stored: StoredObjects; + beforeAll(() => { + stored = trackStoredObjects(nestApp.get(StorageService)); + }); + afterAll(async () => { + await stored.cleanup(); }); it('TRIP-P01 — cover upload returns /uploads/covers/ and writes the file', async () => {