From 78bfa8183ada8c29a8f2fb12e55d776ecc8033a6 Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 17 Nov 2026 18:10:13 +0100 Subject: [PATCH] test(db): run the share, system notices and trip access primitives suites on the real database module They reset through resetDatabase and seed through the ORM factories on the booted app's ORM instead of the SQLite snapshot mock, so they run in the Postgres lane as well. --- server/scripts/test-mocks-baseline.json | 3 - server/tests/integration/share.test.ts | 188 +++++++++--------- .../tests/integration/systemNotices.test.ts | 51 ++--- .../trip-access-primitives.test.ts | 173 ++++++++-------- .../postgres/snapshot-suites-baseline.json | 3 - 5 files changed, 189 insertions(+), 229 deletions(-) diff --git a/server/scripts/test-mocks-baseline.json b/server/scripts/test-mocks-baseline.json index dc2d83490..698bf5036 100644 --- a/server/scripts/test-mocks-baseline.json +++ b/server/scripts/test-mocks-baseline.json @@ -60,9 +60,6 @@ "tests/integration/legacy-upgrade-v244.test.ts": 1, "tests/integration/plugins/boot-registry-order.test.ts": 1, "tests/integration/plugins/plugin-runtime.test.ts": 1, - "tests/integration/share.test.ts": 1, - "tests/integration/systemNotices.test.ts": 1, - "tests/integration/trip-access-primitives.test.ts": 1, "tests/integration/trips.test.ts": 1, "tests/integration/vacay.test.ts": 1, "tests/setup.ts": 1, diff --git a/server/tests/integration/share.test.ts b/server/tests/integration/share.test.ts index 211c0541b..8799e5757 100644 --- a/server/tests/integration/share.test.ts +++ b/server/tests/integration/share.test.ts @@ -3,7 +3,6 @@ * Covers SHARE-001 to SHARE-009. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { db as sharedDb } from '../../src/db/database'; import { AppSettings } from '../../src/db/entities/AppSettings.entity'; import { CollectionPlaces } from '../../src/db/entities/CollectionPlaces.entity'; @@ -20,21 +19,20 @@ import { DEFAULT_UPLOADS_ROOT, GLOBAL_TEMP_DIR } from '../../src/nest/storage/st import type { StorageRegistryService, ResolvedCategory } from '../../src/nest/storage/storage-registry.service'; import { StorageService } from '../../src/nest/storage/storage.service'; import { authCookie } from '../helpers/auth'; -import { - createUser, - createTrip, - addTripMember, - createDay, - createPlace, - createDayAssignment, - createDayNote, -} from '../helpers/factories'; import { makeCollabMessage } from '../helpers/factories/collab'; import type { FactoryOrm } from '../helpers/factories/context'; +import { makeDayAssignment } from '../helpers/factories/itinerary'; +import { makeDayNote } from '../helpers/factories/itinerary'; import { makePackingItem } from '../helpers/factories/packing'; +import { makePlace } from '../helpers/factories/places'; import { deleteRows, insertRow, updateRows } from '../helpers/factories/rows'; import { setAppSetting } from '../helpers/factories/settings'; -import { resetTestDb, resetRateLimits } from '../helpers/test-db'; +import { addTripMember } from '../helpers/factories/trips'; +import { makeDay } from '../helpers/factories/trips'; +import { makeTrip } from '../helpers/factories/trips'; +import { makeUser } from '../helpers/factories/users'; +import { resetDatabase } from '../helpers/reset-database'; +import { resetRateLimits } from '../helpers/test-db'; import { createTestOrm } from '../helpers/test-orm'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; @@ -44,12 +42,7 @@ import crypto from 'node:crypto'; import fs from 'node:fs'; import path from 'node:path'; import request from 'supertest'; -import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest'; - -vi.mock('../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); +import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'; // A real instance over the same connection the app uses — these cases write a // cache entry and then read it back through the HTTP route, so the stub @@ -91,19 +84,18 @@ beforeAll(async () => { }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); await resetRateLimits(nestApp); }); afterAll(async () => { await nestApp.close(); - testDb.close(); }); describe('Share link CRUD', () => { it('SHARE-001 — POST creates share link with default permissions', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const res = await request(app).post(`/api/trips/${trip.id}/share-link`).set('Cookie', authCookie(user.id)).send({}); expect(res.status).toBe(201); @@ -112,8 +104,8 @@ describe('Share link CRUD', () => { }); it('SHARE-002 — POST creates share link with custom permissions', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const res = await request(app) .post(`/api/trips/${trip.id}/share-link`) @@ -124,8 +116,8 @@ describe('Share link CRUD', () => { }); it('SHARE-003 — POST again updates share link permissions', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const first = await request(app) .post(`/api/trips/${trip.id}/share-link`) @@ -141,8 +133,8 @@ describe('Share link CRUD', () => { }); it('SHARE-004 — GET returns share link status', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await request(app).post(`/api/trips/${trip.id}/share-link`).set('Cookie', authCookie(user.id)).send({}); @@ -152,8 +144,8 @@ describe('Share link CRUD', () => { }); it('SHARE-004 — GET returns null token when no share link exists', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const res = await request(app).get(`/api/trips/${trip.id}/share-link`).set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); @@ -161,8 +153,8 @@ describe('Share link CRUD', () => { }); it('SHARE-005 — DELETE removes share link', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await request(app).post(`/api/trips/${trip.id}/share-link`).set('Cookie', authCookie(user.id)).send({}); @@ -177,8 +169,8 @@ describe('Share link CRUD', () => { describe('Shared trip access', () => { it('SHARE-006 — GET /shared/:token returns trip data with all sections', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Paris Adventure' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Paris Adventure' }); const create = await request(app) .post(`/api/trips/${trip.id}/share-link`) @@ -193,8 +185,8 @@ describe('Shared trip access', () => { }); it('SHARE-007 — GET /shared/:token hides budget when share_budget=false', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const create = await request(app) .post(`/api/trips/${trip.id}/share-link`) @@ -211,8 +203,8 @@ describe('Shared trip access', () => { // Regression: a co-member's private packing item (#858) must never reach a public share. it('SHARE-026 — hides private packing items from the public payload', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await makePackingItem(orm, trip.id, { name: 'Private thing', category: 'Misc', @@ -242,12 +234,12 @@ describe('Shared trip access', () => { // just hidden in the client. When the owner disables the map, the itinerary and // every place (with coordinates) must be withheld from the public payload. it('SHARE-024 — hides itinerary and places when share_map=false', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Secret Route' }); - const day = createDay(testDb, trip.id, { date: '2025-06-01' }); - const place = createPlace(testDb, trip.id, { name: 'Safehouse', lat: 12.3456, lng: 65.4321 }); - createDayAssignment(testDb, day.id, place.id); - createDayNote(testDb, day.id, trip.id, { text: 'Do not share' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Secret Route' }); + const day = await makeDay(orm, trip.id, { date: '2025-06-01' }); + const place = await makePlace(orm, trip.id, { name: 'Safehouse', lat: 12.3456, lng: 65.4321 }); + await makeDayAssignment(orm, day.id, place.id); + await makeDayNote(orm, day.id, trip.id, { text: 'Do not share' }); const create = await request(app) .post(`/api/trips/${trip.id}/share-link`) @@ -274,9 +266,9 @@ describe('Shared trip access', () => { }); it('SHARE-009 — non-member cannot create share link', async () => { - const { user: owner } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); const res = await request(app) .post(`/api/trips/${trip.id}/share-link`) @@ -289,10 +281,10 @@ describe('Shared trip access', () => { // Reading the link hands out the credential for the anonymous /api/shared // page, which outlives the membership that was used to fetch it. Under the // default policy share_manage stays with the owner. - const { user: owner } = createUser(testDb); - const { user: member } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, member.id); + const { user: owner } = await makeUser(orm); + const { user: member } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + await addTripMember(orm, trip.id, member.id); await request(app).post(`/api/trips/${trip.id}/share-link`).set('Cookie', authCookie(owner.id)).send({}); const res = await request(app).get(`/api/trips/${trip.id}/share-link`).set('Cookie', authCookie(member.id)); @@ -302,9 +294,9 @@ describe('Shared trip access', () => { }); it('SHARE-027 — the owner still reads it, and a non-member still gets 404 rather than 403', async () => { - const { user: owner } = createUser(testDb); - const { user: stranger } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const { user: stranger } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); await request(app).post(`/api/trips/${trip.id}/share-link`).set('Cookie', authCookie(owner.id)).send({}); const mine = await request(app).get(`/api/trips/${trip.id}/share-link`).set('Cookie', authCookie(owner.id)); @@ -317,10 +309,10 @@ describe('Shared trip access', () => { }); it('SHARE-028 — a member may read it once the instance lowers share_manage to trip_member', async () => { - const { user: owner } = createUser(testDb); - const { user: member } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, member.id); + const { user: owner } = await makeUser(orm); + const { user: member } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + await addTripMember(orm, trip.id, member.id); await request(app).post(`/api/trips/${trip.id}/share-link`).set('Cookie', authCookie(owner.id)).send({}); const { invalidatePermissionsCache } = await import('../../src/nest/permissions/permissions-cache'); @@ -340,11 +332,11 @@ describe('Shared trip access', () => { describe('Shared trip — day assignments and notes', () => { it('SHARE-010 — shared trip with days and assignments includes place data in assignments', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Rome Trip' }); - const day = createDay(testDb, trip.id, { date: '2025-06-01' }); - const place = createPlace(testDb, trip.id, { name: 'Colosseum', lat: 41.89, lng: 12.49 }); - createDayAssignment(testDb, day.id, place.id, { notes: 'Amazing site' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Rome Trip' }); + const day = await makeDay(orm, trip.id, { date: '2025-06-01' }); + const place = await makePlace(orm, trip.id, { name: 'Colosseum', lat: 41.89, lng: 12.49 }); + await makeDayAssignment(orm, day.id, place.id, { notes: 'Amazing site' }); const create = await request(app) .post(`/api/trips/${trip.id}/share-link`) @@ -363,10 +355,10 @@ describe('Shared trip — day assignments and notes', () => { }); it('SHARE-011 — shared trip with day notes includes notes in response', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Notes Trip' }); - const day = createDay(testDb, trip.id, { date: '2025-07-01' }); - createDayNote(testDb, day.id, trip.id, { text: 'Meet at the station' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Notes Trip' }); + const day = await makeDay(orm, trip.id, { date: '2025-07-01' }); + await makeDayNote(orm, day.id, trip.id, { text: 'Meet at the station' }); const create = await request(app) .post(`/api/trips/${trip.id}/share-link`) @@ -383,8 +375,8 @@ describe('Shared trip — day assignments and notes', () => { }); it('SHARE-012 — share_collab=true includes collab messages in response', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await makeCollabMessage(orm, trip.id, user.id, { text: 'Hello team!', deleted: 0 }); const create = await request(app) @@ -401,9 +393,9 @@ describe('Shared trip — day assignments and notes', () => { }); it('SHARE-013 — assignments empty when days have no assignments', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - createDay(testDb, trip.id, { date: '2025-08-01' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + await makeDay(orm, trip.id, { date: '2025-08-01' }); const create = await request(app) .post(`/api/trips/${trip.id}/share-link`) @@ -420,11 +412,11 @@ describe('Shared trip — day assignments and notes', () => { describe('Shared trip — ordering parity (issue #981)', () => { it('SHARE-014 — assignments with same order_index are ordered by created_at (tiebreaker)', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id, { date: '2025-09-01' }); - const place1 = createPlace(testDb, trip.id, { name: 'First Created' }); - const place2 = createPlace(testDb, trip.id, { name: 'Second Created' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id, { date: '2025-09-01' }); + const place1 = await makePlace(orm, trip.id, { name: 'First Created' }); + const place2 = await makePlace(orm, trip.id, { name: 'Second Created' }); // Both with order_index = 0 (schema default) but different created_at await insertRow(orm, DayAssignments, { @@ -453,9 +445,9 @@ describe('Shared trip — ordering parity (issue #981)', () => { }); it('SHARE-015 — reservations include day_positions map from reservation_day_positions table', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id, { date: '2025-09-01' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id, { date: '2025-09-01' }); const reservationId = await insertRow(orm, Reservations, { trip: trip.id, @@ -486,8 +478,8 @@ describe('Shared trip — ordering parity (issue #981)', () => { describe('Shared trip — display currency (issue #1361)', () => { it("SHARE-021 — baseCurrency resolves from the share owner's default_currency setting", async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); // Trip keeps the EUR default; the owner's Costs display currency is CAD. await insertRow(orm, Settings, { user: user.id, key: 'default_currency', value: JSON.stringify('CAD') }); @@ -504,8 +496,8 @@ describe('Shared trip — display currency (issue #1361)', () => { }); it('SHARE-022 — baseCurrency falls back to the trip currency when the owner has no setting', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await updateRows(orm, Trips, { id: trip.id }, { currency: 'GBP' }); const { @@ -521,8 +513,8 @@ describe('Shared trip — display currency (issue #1361)', () => { }); it('SHARE-023 — baseCurrency uses the admin instance default when the owner has no per-user setting', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); // EUR trip default, no user setting + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); // EUR trip default, no user setting await insertRow(orm, AppSettings, { key: 'default_user_setting_default_currency', value: JSON.stringify('USD') }); const { @@ -540,8 +532,8 @@ describe('Shared trip — display currency (issue #1361)', () => { describe('Shared trip: CARTO tile key (issue #2054)', () => { it("SHARE-029: the payload carries the owner's carto_api_key, behind it the admin instance default", async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await insertRow(orm, AppSettings, { key: 'default_user_setting_carto_api_key', value: 'instance-key' }); const { @@ -576,9 +568,9 @@ describe('Shared trip — place photos in shared links (issue #1100)', () => { }); async function setupSharedPlaceWithPhoto() { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id, { name: 'Photo Place' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id, { name: 'Photo Place' }); await updateRows(orm, Places, { id: place.id }, { image_url: PROXY_URL, google_place_id: PLACE_ID }); const { @@ -597,12 +589,12 @@ describe('Shared trip — place photos in shared links (issue #1100)', () => { }); it('SHARE-017 — shared payload rewrites assignment place image_url too', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id, { date: '2025-10-01' }); - const place = createPlace(testDb, trip.id, { name: 'Assigned Photo Place' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id, { date: '2025-10-01' }); + const place = await makePlace(orm, trip.id, { name: 'Assigned Photo Place' }); await updateRows(orm, Places, { id: place.id }, { image_url: PROXY_URL }); - createDayAssignment(testDb, day.id, place.id, {}); + await makeDayAssignment(orm, day.id, place.id, {}); const { body: { token }, @@ -654,9 +646,9 @@ describe('Shared trip — place photos in shared links (issue #1100)', () => { // so the proxy must stream nothing when the owner disabled the map, even // with a valid token + cached bytes. it('SHARE-025 — public place-photo proxy streams nothing when share_map=false', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id, { name: 'Hidden Photo Place' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id, { name: 'Hidden Photo Place' }); await updateRows(orm, Places, { id: place.id }, { image_url: PROXY_URL, google_place_id: PLACE_ID }); const { body: { token }, diff --git a/server/tests/integration/systemNotices.test.ts b/server/tests/integration/systemNotices.test.ts index fedd6084d..ec55bc05d 100644 --- a/server/tests/integration/systemNotices.test.ts +++ b/server/tests/integration/systemNotices.test.ts @@ -4,7 +4,6 @@ */ import { ADDON_IDS } from '../../src/addons'; import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { Addons } from '../../src/db/entities/Addons.entity'; import { UserNoticeDismissals } from '../../src/db/entities/UserNoticeDismissals.entity'; import { Users } from '../../src/db/entities/Users.entity'; @@ -12,25 +11,18 @@ import { SYSTEM_NOTICES } from '../../src/systemNotices/registry'; import { getCurrentAppVersion } from '../../src/systemNotices/service'; import type { SystemNotice } from '../../src/systemNotices/types'; import { authCookie } from '../helpers/auth'; -import { createUser, createAdmin } from '../helpers/factories'; import type { FactoryOrm } from '../helpers/factories/context'; import { insertRow, updateRows } from '../helpers/factories/rows'; import { setAppSetting } from '../helpers/factories/settings'; -import { resetTestDb } from '../helpers/test-db'; +import { makeAdmin } from '../helpers/factories/users'; +import { makeUser } from '../helpers/factories/users'; +import { resetDatabase } from '../helpers/reset-database'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; import type { Application } from 'express'; import request from 'supertest'; -import { describe, it, expect, vi, beforeAll, beforeEach, afterAll, afterEach } from 'vitest'; - -// ───────────────────────────────────────────────────────────────────────────── -// Bare in-memory DB — schema applied in beforeAll after mocks register -// ───────────────────────────────────────────────────────────────────────────── -vi.mock('../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); +import { describe, it, expect, beforeAll, beforeEach, afterAll, afterEach } from 'vitest'; let nestApp: INestApplication; let app: Application; @@ -70,13 +62,12 @@ beforeAll(async () => { orm = nestApp.get(MikroORM); }); -beforeEach(() => { - resetTestDb(testDb); +beforeEach(async () => { + await resetDatabase(orm); }); afterAll(async () => { await nestApp.close(); - testDb.close(); }); // ───────────────────────────────────────────────────────────────────────────── @@ -90,7 +81,7 @@ describe('GET /api/system-notices/active', () => { }); it('returns no login/version-gated notices for an established user', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); // login_count > 1 means firstLogin does not match; first_seen_version >= 3.0.0 means // existingUserBeforeVersion('3.0.0') does not match either. Notices that gate on // nothing but the install being self-hosted still apply, and which ones those are @@ -108,7 +99,7 @@ describe('GET /api/system-notices/active', () => { it('returns firstLogin notice for user with login_count <= 1', async () => { SYSTEM_NOTICES.push(TEST_NOTICE); try { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); // Set login_count to 1 (first login) await updateRows(orm, Users, { id: user.id }, { login_count: 1 }); @@ -131,7 +122,7 @@ describe('GET /api/system-notices/active', () => { it('an addonEnabled-gated notice appears only while the addon is on', async () => { SYSTEM_NOTICES.push(TEST_NOTICE_ADDON); try { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await updateRows(orm, Addons, { id: ADDON_IDS.JOURNEY }, { enabled: false }); const off = await request(app).get('/api/system-notices/active').set('Cookie', authCookie(user.id)); @@ -152,7 +143,7 @@ describe('GET /api/system-notices/active', () => { it('does not return firstLogin notice for user with login_count > 1', async () => { SYSTEM_NOTICES.push(TEST_NOTICE); try { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await updateRows(orm, Users, { id: user.id }, { login_count: 5, first_seen_version: '3.0.0' }); const res = await request(app).get('/api/system-notices/active').set('Cookie', authCookie(user.id)); @@ -167,7 +158,7 @@ describe('GET /api/system-notices/active', () => { it('filters out dismissed notices', async () => { SYSTEM_NOTICES.push(TEST_NOTICE); try { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await updateRows(orm, Users, { id: user.id }, { login_count: 1 }); // Dismiss the notice directly in DB @@ -206,7 +197,7 @@ describe('GET /api/system-notices/active', () => { }; SYSTEM_NOTICES.push(RECURRING); try { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await updateRows(orm, Users, { id: user.id }, { login_count: 5, first_seen_version: '3.0.0' }); const shows = async () => { @@ -246,7 +237,7 @@ describe('GET /api/system-notices/active', () => { // reload of the same version. Driven through the real registry entry and the real // dismiss route, so neither half can drift away from the other unnoticed. it('shows the release notes once per update, however often they were closed before', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await updateRows(orm, Users, { id: user.id }, { login_count: 5, first_seen_version: '3.0.0' }); // The client announces the release layout and the version it was built as; a @@ -295,7 +286,7 @@ describe('GET /api/system-notices/active', () => { // version. So the notice waits for a client that announces the release layout AND // was built for the version now running; the reload brings one. it('keeps the release notes from a client that does not announce the release layout for this version', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await updateRows(orm, Users, { id: user.id }, { login_count: 5, first_seen_version: '3.0.0' }); const fetchActive = (query: Record) => @@ -329,7 +320,7 @@ describe('POST /api/system-notices/:id/dismiss', () => { }); it('returns 404 for unknown notice id', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .post('/api/system-notices/nonexistent-id/dismiss') .set('Cookie', authCookie(user.id)); @@ -340,7 +331,7 @@ describe('POST /api/system-notices/:id/dismiss', () => { it('returns 204 for valid notice id', async () => { SYSTEM_NOTICES.push(TEST_NOTICE); try { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .post(`/api/system-notices/${TEST_NOTICE.id}/dismiss`) .set('Cookie', authCookie(user.id)); @@ -354,7 +345,7 @@ describe('POST /api/system-notices/:id/dismiss', () => { it('is idempotent — second dismiss also returns 204', async () => { SYSTEM_NOTICES.push(TEST_NOTICE); try { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const first = await request(app) .post(`/api/system-notices/${TEST_NOTICE.id}/dismiss`) .set('Cookie', authCookie(user.id)); @@ -373,7 +364,7 @@ describe('POST /api/system-notices/:id/dismiss', () => { it('dismiss appears in GET /active as filtered out', async () => { SYSTEM_NOTICES.push(TEST_NOTICE); try { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await updateRows(orm, Users, { id: user.id }, { login_count: 1 }); // Confirm TEST_NOTICE is visible before dismiss @@ -404,7 +395,7 @@ describe('POST /api/system-notices/:id/dismiss', () => { * high enough to suppress the firstLogin and v3-upgrade notice conditions. */ async function setupCollisionAdmin() { - const { user } = createAdmin(testDb); + const { user } = await makeAdmin(orm); await updateRows(orm, Users, { id: user.id }, { login_count: 5, first_seen_version: '3.0.0' }); return user; } @@ -455,7 +446,7 @@ describe('v3014-whitespace-collision notice', () => { }); it('SN-COLLISION-4 — hidden for non-admin user even when collision flag is set', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await updateRows(orm, Users, { id: user.id }, { login_count: 5, first_seen_version: '3.0.0' }); await setAppSetting(orm, 'whitespace_migration_collision', 'true'); @@ -466,7 +457,7 @@ describe('v3014-whitespace-collision notice', () => { }); it('SN-COLLISION-5 — hidden for user whose first_seen_version is >= 3.0.14 (new account)', async () => { - const { user } = createAdmin(testDb); + const { user } = await makeAdmin(orm); await updateRows(orm, Users, { id: user.id }, { login_count: 5, first_seen_version: '3.0.14' }); await setAppSetting(orm, 'whitespace_migration_collision', 'true'); diff --git a/server/tests/integration/trip-access-primitives.test.ts b/server/tests/integration/trip-access-primitives.test.ts index 0ab1608db..43afd1409 100644 --- a/server/tests/integration/trip-access-primitives.test.ts +++ b/server/tests/integration/trip-access-primitives.test.ts @@ -13,23 +13,21 @@ * path rather than a stand-in. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { Addons } from '../../src/db/entities/Addons.entity'; import { Categories } from '../../src/db/entities/Categories.entity'; import { Trips } from '../../src/db/entities/Trips.entity'; import { closeMcpSessions } from '../../src/mcp/index'; import { authCookie, generateToken } from '../helpers/auth'; -import { - createUser, - createTrip, - createDay, - createPlace, - addTripMember, - createDayAssignment, -} from '../helpers/factories'; import type { FactoryOrm } from '../helpers/factories/context'; +import { makeDayAssignment } from '../helpers/factories/itinerary'; +import { makePlace } from '../helpers/factories/places'; import { findRow, updateRows } from '../helpers/factories/rows'; -import { resetTestDb, resetRateLimits } from '../helpers/test-db'; +import { addTripMember } from '../helpers/factories/trips'; +import { makeDay } from '../helpers/factories/trips'; +import { makeTrip } from '../helpers/factories/trips'; +import { makeUser } from '../helpers/factories/users'; +import { resetDatabase } from '../helpers/reset-database'; +import { resetRateLimits } from '../helpers/test-db'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; @@ -37,20 +35,9 @@ import type { Application } from 'express'; import request from 'supertest'; import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest'; -// task-0a-review F4: this mock no longer intercepts any of the four -// primitives — `db/database.ts` holds none of their bodies any more, so -// `buildDbMock`'s own `canAccessTrip`/`isOwner`/`getPlaceWithTags` -// properties are dead code for this file's purposes. It stays only for what -// every other integration/e2e/WS suite in this program uses it for: an -// isolated snapshot db that `getRawConnection`/`db` swap the shared -// better-sqlite3 handle to, which the REAL `MikroORM` binds to underneath -// (`db/orm-driver.ts`) — so `TripsRepository`/`TripMembersRepository` -// /`PlacesRepository` read the SAME rows this file seeds through `testDb`. -vi.mock('../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); - +// The suite seeds through the booted app's own ORM, the one +// `TripsRepository`/`TripMembersRepository`/`PlacesRepository` read through, +// on whichever engine the app runs. let nestApp: INestApplication; let app: Application; let orm: FactoryOrm; @@ -61,13 +48,12 @@ beforeAll(async () => { orm = nestApp.get(MikroORM); }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); await resetRateLimits(nestApp); }); afterAll(async () => { closeMcpSessions(); await nestApp.close(); - testDb.close(); }); // ───────────────────────────────────────────────────────────────────────── @@ -76,8 +62,8 @@ afterAll(async () => { describe('canAccessTrip (async) — TripAccessGuard on GET /api/trips/:tripId/days', () => { it('PRIM-CAT-001 — owner: 200 with the trip days', async () => { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id, { start_date: '2026-06-01', end_date: '2026-06-02' }); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id, { start_date: '2026-06-01', end_date: '2026-06-02' }); const res = await request(app).get(`/api/trips/${trip.id}/days`).set('Cookie', authCookie(owner.id)); expect(res.status).toBe(200); @@ -85,10 +71,10 @@ describe('canAccessTrip (async) — TripAccessGuard on GET /api/trips/:tripId/da }); it('PRIM-CAT-002 — member: 200 with the trip days', async () => { - const { user: owner } = createUser(testDb); - const { user: member } = createUser(testDb); - const trip = createTrip(testDb, owner.id, { start_date: '2026-06-01', end_date: '2026-06-01' }); - addTripMember(testDb, trip.id, member.id); + const { user: owner } = await makeUser(orm); + const { user: member } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id, { start_date: '2026-06-01', end_date: '2026-06-01' }); + await addTripMember(orm, trip.id, member.id); const res = await request(app).get(`/api/trips/${trip.id}/days`).set('Cookie', authCookie(member.id)); expect(res.status).toBe(200); @@ -96,9 +82,9 @@ describe('canAccessTrip (async) — TripAccessGuard on GET /api/trips/:tripId/da }); it('PRIM-CAT-003 — non-member: 404 { error: "Trip not found" } — never 403 (never confirms the id exists)', async () => { - const { user: owner } = createUser(testDb); - const { user: stranger } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const { user: stranger } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); const res = await request(app).get(`/api/trips/${trip.id}/days`).set('Cookie', authCookie(stranger.id)); expect(res.status).toBe(404); @@ -106,8 +92,8 @@ describe('canAccessTrip (async) — TripAccessGuard on GET /api/trips/:tripId/da }); it('PRIM-CAT-004 — anonymous: 401', async () => { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); const res = await request(app).get(`/api/trips/${trip.id}/days`); expect(res.status).toBe(401); @@ -158,8 +144,8 @@ describe('isOwner (async) — the delete_trip MCP tool', () => { }); it('PRIM-ISOWN-001 — owner: 200, deletes the trip', async () => { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); const sessionId = await mcpSession(owner.id); const res = await request(app) @@ -192,8 +178,8 @@ describe('isOwner (async) — the delete_trip MCP tool', () => { it('PRIM-ISOWN-005 — the repository read inside isOwner runs inside the /mcp request context (MCP-CTX style, no cannotUseGlobalContext)', async () => { const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); try { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); const sessionId = await mcpSession(owner.id); const res = await request(app) @@ -214,10 +200,10 @@ describe('isOwner (async) — the delete_trip MCP tool', () => { }); it('PRIM-ISOWN-002 — member (not owner): refused with the same "no access" message as a stranger, trip untouched', async () => { - const { user: owner } = createUser(testDb); - const { user: member } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, member.id); + const { user: owner } = await makeUser(orm); + const { user: member } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + await addTripMember(orm, trip.id, member.id); const sessionId = await mcpSession(member.id); const res = await request(app) @@ -235,9 +221,9 @@ describe('isOwner (async) — the delete_trip MCP tool', () => { }); it('PRIM-ISOWN-003 — non-member/stranger: the identical refusal isOwner gives a member, trip untouched', async () => { - const { user: owner } = createUser(testDb); - const { user: stranger } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const { user: stranger } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); const sessionId = await mcpSession(stranger.id); const res = await request(app) @@ -255,8 +241,8 @@ describe('isOwner (async) — the delete_trip MCP tool', () => { }); it('PRIM-ISOWN-004 — anonymous: 401, no session established', async () => { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); const res = await request(app).post('/mcp').send(deleteTripBody(trip.id)); expect(res.status).toBe(401); @@ -269,12 +255,12 @@ describe('isOwner (async) — the delete_trip MCP tool', () => { describe('rosterUserIds (async) — PUT .../assignments/:id/participants', () => { it('PRIM-ROSTER-001 — owner: 200, off-roster ids are dropped silently (no 400)', async () => { - const { user: owner } = createUser(testDb); - const { user: outsider } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user: owner } = await makeUser(orm); + const { user: outsider } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); const res = await request(app) .put(`/api/trips/${trip.id}/assignments/${assignment.id}/participants`) @@ -286,14 +272,14 @@ describe('rosterUserIds (async) — PUT .../assignments/:id/participants', () => }); it('PRIM-ROSTER-002 — member with edit rights: 200, keeps only roster ids (owner + member)', async () => { - const { user: owner } = createUser(testDb); - const { user: member } = createUser(testDb); - const { user: outsider } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, member.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user: owner } = await makeUser(orm); + const { user: member } = await makeUser(orm); + const { user: outsider } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + await addTripMember(orm, trip.id, member.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); const res = await request(app) .put(`/api/trips/${trip.id}/assignments/${assignment.id}/participants`) @@ -307,12 +293,12 @@ describe('rosterUserIds (async) — PUT .../assignments/:id/participants', () => }); it('PRIM-ROSTER-003 — non-member: 404 { error: "Trip not found" }, guard refuses before rosterUserIds runs', async () => { - const { user: owner } = createUser(testDb); - const { user: stranger } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user: owner } = await makeUser(orm); + const { user: stranger } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); const res = await request(app) .put(`/api/trips/${trip.id}/assignments/${assignment.id}/participants`) @@ -324,11 +310,11 @@ describe('rosterUserIds (async) — PUT .../assignments/:id/participants', () => }); it('PRIM-ROSTER-004 — anonymous: 401', async () => { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); const res = await request(app) .put(`/api/trips/${trip.id}/assignments/${assignment.id}/participants`) @@ -344,13 +330,13 @@ describe('rosterUserIds (async) — PUT .../assignments/:id/participants', () => describe('getPlaceWithTags (async) — GET /api/trips/:tripId/places/:id', () => { it('PRIM-GPWT-001 — owner: 200 with the hydrated place (category/tags/ratings shape)', async () => { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); // createPlace defaults category_id to the first seeded category when no // override is given (tests/helpers/factories.ts) — read back whatever it // actually assigned rather than assuming none, so this pins the real // production shape instead of a factory default that happens to be null. - const place = createPlace(testDb, trip.id, { name: 'Eiffel Tower' }); + const place = await makePlace(orm, trip.id, { name: 'Eiffel Tower' }); const category = place.category_id ? await findRow(orm, Categories, { id: place.category_id }) : null; const expectedCategory = category ? { id: category.id, name: category.name, color: category.color, icon: category.icon } @@ -359,11 +345,8 @@ describe('getPlaceWithTags (async) — GET /api/trips/:tripId/places/:id', () => const res = await request(app).get(`/api/trips/${trip.id}/places/${place.id}`).set('Cookie', authCookie(owner.id)); expect(res.status).toBe(200); // Plan 3c Task 0b: `getPlaceWithTags` is `PlacesRepository - // .findWithTagsAndRatings` now, reached for real (this file's `vi.mock` - // of `src/db/database` only swaps the underlying connection to an - // isolated snapshot db — see the top-of-file note — it no longer stands - // in for the primitive itself, which db/database.ts doesn't hold any - // more). The full production shape, including the ratings aggregate the + // .findWithTagsAndRatings` now, reached for real on the app's own + // database. The full production shape, including the ratings aggregate the // old mocked stand-in omitted, is asserted directly rather than only a // subset. expect(res.body.place).toMatchObject({ @@ -378,11 +361,11 @@ describe('getPlaceWithTags (async) — GET /api/trips/:tripId/places/:id', () => }); it('PRIM-GPWT-002 — member: 200 with the same hydrated place', async () => { - const { user: owner } = createUser(testDb); - const { user: member } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, member.id); - const place = createPlace(testDb, trip.id); + const { user: owner } = await makeUser(orm); + const { user: member } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + await addTripMember(orm, trip.id, member.id); + const place = await makePlace(orm, trip.id); const res = await request(app).get(`/api/trips/${trip.id}/places/${place.id}`).set('Cookie', authCookie(member.id)); expect(res.status).toBe(200); @@ -390,10 +373,10 @@ describe('getPlaceWithTags (async) — GET /api/trips/:tripId/places/:id', () => }); it('PRIM-GPWT-003 — non-member: 404 { error: "Trip not found" } from the guard, before getPlaceWithTags runs', async () => { - const { user: owner } = createUser(testDb); - const { user: stranger } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - const place = createPlace(testDb, trip.id); + const { user: owner } = await makeUser(orm); + const { user: stranger } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + const place = await makePlace(orm, trip.id); const res = await request(app) .get(`/api/trips/${trip.id}/places/${place.id}`) @@ -403,9 +386,9 @@ describe('getPlaceWithTags (async) — GET /api/trips/:tripId/places/:id', () => }); it('PRIM-GPWT-004 — anonymous: 401', async () => { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - const place = createPlace(testDb, trip.id); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + const place = await makePlace(orm, trip.id); const res = await request(app).get(`/api/trips/${trip.id}/places/${place.id}`); expect(res.status).toBe(401); diff --git a/server/tests/postgres/snapshot-suites-baseline.json b/server/tests/postgres/snapshot-suites-baseline.json index d103cc9a7..734990f3c 100644 --- a/server/tests/postgres/snapshot-suites-baseline.json +++ b/server/tests/postgres/snapshot-suites-baseline.json @@ -1,7 +1,4 @@ [ - "tests/integration/share.test.ts", - "tests/integration/systemNotices.test.ts", - "tests/integration/trip-access-primitives.test.ts", "tests/integration/trips.test.ts", "tests/integration/vacay.test.ts", "tests/unit/mcp/demo-tool-gate.test.ts",