From 741bb651e52a0d6716055e48ec1cf7088d85fcda Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 17 Nov 2026 17:00:56 +0100 Subject: [PATCH] test(db): run the OIDC, passkey, profile, notifications and ORM request context seam suites on the real database module They reset through resetDatabase (the seam suite needs no reset) and seed through the ORM factories instead of the SQLite snapshot mock, so they run in the Postgres lane as well. --- server/scripts/test-mocks-baseline.json | 5 - .../tests/integration/notifications.test.ts | 92 +++++++++---------- server/tests/integration/oidc.test.ts | 20 ++-- .../orm-request-context-seams.test.ts | 11 +-- server/tests/integration/passkey.test.ts | 28 +++--- server/tests/integration/profile.test.ts | 53 +++++------ .../postgres/snapshot-suites-baseline.json | 5 - 7 files changed, 88 insertions(+), 126 deletions(-) diff --git a/server/scripts/test-mocks-baseline.json b/server/scripts/test-mocks-baseline.json index 011778ca4..56aa1d5f3 100644 --- a/server/scripts/test-mocks-baseline.json +++ b/server/scripts/test-mocks-baseline.json @@ -64,17 +64,12 @@ "tests/integration/memories-immich.test.ts": 1, "tests/integration/memories-synology.test.ts": 1, "tests/integration/memories-unified.test.ts": 1, - "tests/integration/notifications.test.ts": 1, "tests/integration/oauth-authorize.test.ts": 1, "tests/integration/oauth.test.ts": 1, - "tests/integration/oidc.test.ts": 1, - "tests/integration/orm-request-context-seams.test.ts": 1, "tests/integration/packing.test.ts": 1, - "tests/integration/passkey.test.ts": 1, "tests/integration/places.test.ts": 1, "tests/integration/plugins/boot-registry-order.test.ts": 1, "tests/integration/plugins/plugin-runtime.test.ts": 1, - "tests/integration/profile.test.ts": 1, "tests/integration/reservations.test.ts": 1, "tests/integration/scheduler-gate.test.ts": 1, "tests/integration/security.test.ts": 1, diff --git a/server/tests/integration/notifications.test.ts b/server/tests/integration/notifications.test.ts index 204896a19..21b287d2b 100644 --- a/server/tests/integration/notifications.test.ts +++ b/server/tests/integration/notifications.test.ts @@ -6,16 +6,17 @@ * in-app notification CRUD, and authentication. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { NotificationChannelPreferences } from '../../src/db/entities/NotificationChannelPreferences.entity'; import { Notifications } from '../../src/db/entities/Notifications.entity'; import { authCookie } from '../helpers/auth'; -import { createUser, createAdmin, disableNotificationPref } from '../helpers/factories'; import type { FactoryOrm } from '../helpers/factories/context'; +import { disableNotificationPref } from '../helpers/factories/notifications'; import { makeNotification } from '../helpers/factories/notifications'; import { findRow, updateRows } from '../helpers/factories/rows'; import { setUserSetting } from '../helpers/factories/settings'; -import { resetTestDb, resetRateLimits } from '../helpers/test-db'; +import { makeAdmin, 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'; @@ -23,10 +24,6 @@ import type { Application } from 'express'; 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()); -}); // The two test-send endpoints must not reach a real SMTP server or URL. Since // the fold they are provider methods, so they are stubbed on the prototype // rather than by module path — everything else in the domain stays real. @@ -52,18 +49,17 @@ beforeAll(async () => { }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm()); await resetRateLimits(nestApp); }); afterAll(async () => { await nestApp.close(); - testDb.close(); }); describe('Notification preferences', () => { it('NOTIF-001 — GET /api/notifications/preferences returns defaults', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).get('/api/notifications/preferences').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); @@ -71,7 +67,7 @@ describe('Notification preferences', () => { }); it('NOTIF-001 — PUT /api/notifications/preferences updates settings', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); // The DTO ratchet enforces the matrix shape the client actually sends // ({ event: { channel: enabled } }); the pre-matrix flat notify_* body @@ -92,7 +88,7 @@ describe('Notification preferences', () => { describe('In-app notifications', () => { it('NOTIF-008 — GET /api/notifications/in-app returns notifications array', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).get('/api/notifications/in-app').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); @@ -100,7 +96,7 @@ describe('In-app notifications', () => { }); it('NOTIF-008 — GET /api/notifications/in-app/unread-count returns count', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).get('/api/notifications/in-app/unread-count').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); @@ -109,7 +105,7 @@ describe('In-app notifications', () => { }); it('NOTIF-009 — PUT /api/notifications/in-app/read-all marks all read', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).put('/api/notifications/in-app/read-all').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); @@ -117,7 +113,7 @@ describe('In-app notifications', () => { }); it('NOTIF-010 — DELETE /api/notifications/in-app/all deletes all notifications', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).delete('/api/notifications/in-app/all').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); @@ -125,14 +121,14 @@ describe('In-app notifications', () => { }); it('NOTIF-011 — PUT /api/notifications/in-app/:id/read on non-existent returns 404', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).put('/api/notifications/in-app/99999/read').set('Cookie', authCookie(user.id)); expect(res.status).toBe(404); }); it('NOTIF-012 — DELETE /api/notifications/in-app/:id on non-existent returns 404', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).delete('/api/notifications/in-app/99999').set('Cookie', authCookie(user.id)); expect(res.status).toBe(404); @@ -145,7 +141,7 @@ describe('In-app notifications', () => { describe('GET /api/notifications/preferences — matrix format', () => { it('NROUTE-002 — returns preferences, channels, event_types, implemented_combos', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).get('/api/notifications/preferences').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); expect(res.body).toHaveProperty('preferences'); @@ -161,28 +157,28 @@ describe('GET /api/notifications/preferences — matrix format', () => { }); it('NROUTE-003 — regular user does not see version_available in event_types', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).get('/api/notifications/preferences').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); expect(res.body.event_types).not.toContain('version_available'); }); it('NROUTE-004 — user preferences endpoint excludes version_available even for admins', async () => { - const { user } = createAdmin(testDb); + const { user } = await makeAdmin(orm()); const res = await request(app).get('/api/notifications/preferences').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); expect(res.body.event_types).not.toContain('version_available'); }); it('NROUTE-004b — admin notification preferences endpoint returns version_available', async () => { - const { user } = createAdmin(testDb); + const { user } = await makeAdmin(orm()); const res = await request(app).get('/api/admin/notification-preferences').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); expect(res.body.event_types).toContain('version_available'); }); it('NROUTE-005 — all preferences default to true for new user with no stored prefs', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).get('/api/notifications/preferences').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); const { preferences } = res.body; @@ -196,7 +192,7 @@ describe('GET /api/notifications/preferences — matrix format', () => { describe('PUT /api/notifications/preferences — matrix format', () => { it('NROUTE-007 — disabling a preference persists and is reflected in subsequent GET', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const putRes = await request(app) .put('/api/notifications/preferences') @@ -211,8 +207,8 @@ describe('PUT /api/notifications/preferences — matrix format', () => { }); it('NROUTE-008 — re-enabling a preference restores default state', async () => { - const { user } = createUser(testDb); - disableNotificationPref(testDb, user.id, 'trip_invite', 'email'); + const { user } = await makeUser(orm()); + await disableNotificationPref(orm(), user.id, 'trip_invite', 'email'); const res = await request(app) .put('/api/notifications/preferences') @@ -231,8 +227,8 @@ describe('PUT /api/notifications/preferences — matrix format', () => { }); it('NROUTE-009 — partial update does not affect other preferences', async () => { - const { user } = createUser(testDb); - disableNotificationPref(testDb, user.id, 'booking_change', 'email'); + const { user } = await makeUser(orm()); + await disableNotificationPref(orm(), user.id, 'booking_change', 'email'); await request(app) .put('/api/notifications/preferences') @@ -248,7 +244,7 @@ describe('PUT /api/notifications/preferences — matrix format', () => { describe('implemented_combos — in-app channel coverage', () => { it('NROUTE-010 — implemented_combos includes inapp for all event types', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).get('/api/notifications/preferences').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); const { implemented_combos } = res.body as { implemented_combos: Record }; @@ -271,7 +267,7 @@ describe('implemented_combos — in-app channel coverage', () => { describe('Notification test endpoints', () => { it('NOTIF-005 — POST /api/notifications/test-smtp requires admin', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); // Send the empty JSON body the client sends ({ email: undefined } → // {}): a completely body-less POST has no content-type, so the DTO pipe @@ -282,14 +278,14 @@ describe('Notification test endpoints', () => { }); it('NOTIF-006 — POST /api/notifications/test-webhook returns 400 when url is missing', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).post('/api/notifications/test-webhook').set('Cookie', authCookie(user.id)).send({}); expect(res.status).toBe(400); }); it('NOTIF-006b — POST /api/notifications/test-webhook returns 400 for invalid URL', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app) .post('/api/notifications/test-webhook') @@ -299,7 +295,7 @@ describe('Notification test endpoints', () => { }); it('NOTIF-005b — admin can call test-smtp and gets a result', async () => { - const { user } = createAdmin(testDb); + const { user } = await makeAdmin(orm()); const res = await request(app) .post('/api/notifications/test-smtp') @@ -311,7 +307,7 @@ describe('Notification test endpoints', () => { }); it('NOTIF-006c — POST /api/notifications/test-webhook with valid URL calls testWebhook', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app) .post('/api/notifications/test-webhook') @@ -323,7 +319,7 @@ describe('Notification test endpoints', () => { }); it('NOTIF-007 — POST /api/notifications/test-ntfy returns 400 when no topic configured', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app).post('/api/notifications/test-ntfy').set('Cookie', authCookie(user.id)).send({}); @@ -332,7 +328,7 @@ describe('Notification test endpoints', () => { }); it('NOTIF-008 — POST /api/notifications/test-ntfy with explicit topic returns 200', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app) .post('/api/notifications/test-ntfy') @@ -344,7 +340,7 @@ describe('Notification test endpoints', () => { }); it('NOTIF-009 — POST /api/notifications/test-ntfy falls back to user saved topic', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); await setUserSetting(orm(), user.id, 'ntfy_topic', 'saved-user-topic'); const res = await request(app).post('/api/notifications/test-ntfy').set('Cookie', authCookie(user.id)).send({}); @@ -394,7 +390,7 @@ async function insertSimpleNotification(recipientId: number): Promise { describe('POST /api/notifications/in-app/:id/respond', () => { it('NROUTE-011 — valid positive response returns success and updated notification', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const id = await insertBooleanNotification(user.id); const res = await request(app) @@ -409,7 +405,7 @@ describe('POST /api/notifications/in-app/:id/respond', () => { }); it('NROUTE-012 — invalid response value returns 400', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const id = await insertBooleanNotification(user.id); const res = await request(app) @@ -421,7 +417,7 @@ describe('POST /api/notifications/in-app/:id/respond', () => { }); it('NROUTE-013 — response on non-existent notification returns 400', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app) .post('/api/notifications/in-app/99999/respond') @@ -432,7 +428,7 @@ describe('POST /api/notifications/in-app/:id/respond', () => { }); it('NROUTE-014 — double response returns 400', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const id = await insertBooleanNotification(user.id); await request(app) @@ -456,7 +452,7 @@ describe('POST /api/notifications/in-app/:id/respond', () => { describe('PUT /api/admin/notification-preferences', () => { it('NROUTE-015 — admin can disable email for version_available, persists in GET', async () => { - const { user } = createAdmin(testDb); + const { user } = await makeAdmin(orm()); const putRes = await request(app) .put('/api/admin/notification-preferences') @@ -472,7 +468,7 @@ describe('PUT /api/admin/notification-preferences', () => { }); it('NROUTE-016 — non-admin is rejected with 403', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const res = await request(app) .put('/api/admin/notification-preferences') @@ -489,7 +485,7 @@ describe('PUT /api/admin/notification-preferences', () => { describe('In-app notifications — CRUD with data', () => { it('NROUTE-017 — GET /in-app returns created notifications', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); await insertSimpleNotification(user.id); await insertSimpleNotification(user.id); @@ -502,7 +498,7 @@ describe('In-app notifications — CRUD with data', () => { }); it('NROUTE-018 — unread count reflects actual unread notifications', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); await insertSimpleNotification(user.id); await insertSimpleNotification(user.id); @@ -513,7 +509,7 @@ describe('In-app notifications — CRUD with data', () => { }); it('NROUTE-019 — mark-read on existing notification succeeds and decrements unread count', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const id = await insertSimpleNotification(user.id); const markRes = await request(app).put(`/api/notifications/in-app/${id}/read`).set('Cookie', authCookie(user.id)); @@ -527,7 +523,7 @@ describe('In-app notifications — CRUD with data', () => { }); it('NROUTE-020 — mark-unread on a read notification succeeds', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const id = await insertSimpleNotification(user.id); // Mark read first await updateRows(orm(), Notifications, { id }, { is_read: 1 }); @@ -541,7 +537,7 @@ describe('In-app notifications — CRUD with data', () => { }); it('NROUTE-021 — DELETE on existing notification removes it', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const id = await insertSimpleNotification(user.id); const res = await request(app).delete(`/api/notifications/in-app/${id}`).set('Cookie', authCookie(user.id)); @@ -552,7 +548,7 @@ describe('In-app notifications — CRUD with data', () => { }); it('NROUTE-022 — unread_only=true filter returns only unread notifications', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm()); const id1 = await insertSimpleNotification(user.id); await insertSimpleNotification(user.id); // Mark first one read diff --git a/server/tests/integration/oidc.test.ts b/server/tests/integration/oidc.test.ts index 74a30f34e..ef824da25 100644 --- a/server/tests/integration/oidc.test.ts +++ b/server/tests/integration/oidc.test.ts @@ -8,17 +8,17 @@ * real on that same instance against the real test DB. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { AuditLog } from '../../src/db/entities/AuditLog.entity'; import { Users } from '../../src/db/entities/Users.entity'; import { OidcService } from '../../src/nest/oidc/oidc.service'; import { authCookie } from '../helpers/auth'; -import { createUser } from '../helpers/factories'; import type { FactoryOrm } from '../helpers/factories/context'; import { findRow, findRows } from '../helpers/factories/rows'; import { setAppSetting } from '../helpers/factories/settings'; +import { makeUser } from '../helpers/factories/users'; import { readUser } from '../helpers/factories/users'; -import { resetTestDb, resetRateLimits } from '../helpers/test-db'; +import { resetDatabase } from '../helpers/reset-database'; +import { resetRateLimits } from '../helpers/test-db'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; @@ -29,11 +29,6 @@ import type { MockInstance } from 'vitest'; // ── DB mock (async vi.mock factory over the migrated schema snapshot) ──────── -vi.mock('../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); - /** Read one cookie's value out of a response's Set-Cookie header, as a browser would. */ function readCookie(res: request.Response, name: string): string | undefined { const raw = res.headers['set-cookie']; @@ -78,7 +73,7 @@ beforeAll(async () => { }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm()); await resetRateLimits(nestApp); vi.clearAllMocks(); @@ -98,7 +93,6 @@ afterEach(() => { afterAll(async () => { await nestApp.close(); - testDb.close(); }); // ── /login ─────────────────────────────────────────────────────────────────── @@ -142,7 +136,7 @@ describe('GET /api/auth/oidc/login', () => { describe('GET /api/auth/oidc/callback', () => { it('OIDC-004: valid code for existing user → redirects to frontend with oidc_code', async () => { - const { user } = createUser(testDb, { email: 'alice@example.com' }); + const { user } = await makeUser(orm(), { email: 'alice@example.com' }); mockDiscover.mockResolvedValueOnce(MOCK_DISCOVERY_DOC); mockExchangeCode.mockResolvedValueOnce({ @@ -171,7 +165,7 @@ describe('GET /api/auth/oidc/callback', () => { }); it('OIDC-004b: a successful login writes the user.login row every other method writes (#2417)', async () => { - const { user } = createUser(testDb, { email: 'audited@example.com' }); + const { user } = await makeUser(orm(), { email: 'audited@example.com' }); mockDiscover.mockResolvedValueOnce(MOCK_DISCOVERY_DOC); mockExchangeCode.mockResolvedValueOnce({ access_token: 'test-access-token', @@ -326,7 +320,7 @@ describe('GET /api/auth/oidc/callback', () => { it('OIDC-010: registration disabled for new user → redirects with registration_disabled error', async () => { // Need at least one existing user so isFirstUser=false - createUser(testDb, { email: 'existing@example.com' }); + await makeUser(orm(), { email: 'existing@example.com' }); // Disable registration await setAppSetting(orm(), 'allow_registration', 'false'); diff --git a/server/tests/integration/orm-request-context-seams.test.ts b/server/tests/integration/orm-request-context-seams.test.ts index 697126ac9..49568d20b 100644 --- a/server/tests/integration/orm-request-context-seams.test.ts +++ b/server/tests/integration/orm-request-context-seams.test.ts @@ -30,7 +30,6 @@ * `applicationConfig`, despite the class's own name). */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { PlaceShadowPicks } from '../../src/db/entities/PlaceShadowPicks.entity'; import { Users } from '../../src/db/entities/Users.entity'; import { PlaceShadowRetentionJob } from '../../src/nest/place-shadow/place-shadow.job'; @@ -38,8 +37,8 @@ import { PluginRuntimeService } from '../../src/nest/plugins/plugin-runtime.serv import { CronRegistrarService } from '../../src/nest/scheduling/cron-registrar.service'; import { StorageService } from '../../src/nest/storage/storage.service'; import { generateToken } from '../helpers/auth'; -import { createUser } from '../helpers/factories'; import { countRows, insertRow } from '../helpers/factories/rows'; +import { makeUser } from '../helpers/factories/users'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; import { SchedulerRegistry } from '@nestjs/schedule'; @@ -48,11 +47,6 @@ import type { Application } from 'express'; import request from 'supertest'; import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'; -vi.mock('../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); - describe('ORM request-context seams populated in production', () => { let app: INestApplication; @@ -62,7 +56,6 @@ describe('ORM request-context seams populated in production', () => { afterAll(async () => { await app.close(); - testDb.close(); }); it('SEAM-001: CronRegistrarService, PluginRuntimeService, PluginSupervisor.resolveOrm() and TrekWsAdapter all resolve the SAME MikroORM app.get(MikroORM) does', () => { @@ -99,7 +92,7 @@ describe('ORM request-context seams populated in production', () => { */ it('SEAM-002: the pre-init /uploads/photos/* route (applyPlatformUploads, bootstrap.ts) forks a request context off the SAME MikroORM app.get(MikroORM) does', async () => { const orm = app.get(MikroORM); - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const storage = app.get(StorageService); let found: unknown; let caught: unknown; diff --git a/server/tests/integration/passkey.test.ts b/server/tests/integration/passkey.test.ts index 585789819..848a1d398 100644 --- a/server/tests/integration/passkey.test.ts +++ b/server/tests/integration/passkey.test.ts @@ -19,24 +19,19 @@ * this fix round runs (Task 4, OAuth). */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { InviteTokens } from '../../src/db/entities/InviteTokens.entity'; import { WebauthnCredentials } from '../../src/db/entities/WebauthnCredentials.entity'; import { authCookie } from '../helpers/auth'; -import { createUser, createAdmin } from '../helpers/factories'; import { findRow, insertRow } from '../helpers/factories/rows'; -import { resetTestDb, resetRateLimits } from '../helpers/test-db'; +import { makeAdmin, 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'; import type { Application } from 'express'; 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'; let nestApp: INestApplication; let app: Application; @@ -49,18 +44,17 @@ beforeAll(async () => { }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); await resetRateLimits(nestApp); }); afterAll(async () => { await nestApp.close(); - testDb.close(); }); describe('Passkey management — non-numeric id parity (Plan 3b Task 3 review, F1)', () => { it('PASSKEY-INT-001 — PATCH /auth/passkey/credentials/abc (non-numeric id) returns the legacy 404, not a 500', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .patch('/api/auth/passkey/credentials/abc') @@ -72,7 +66,7 @@ describe('Passkey management — non-numeric id parity (Plan 3b Task 3 review, F }); it('PASSKEY-INT-002 — PATCH /auth/passkey/credentials/1abc (leading-digit non-numeric id) returns the legacy 404, not a 500', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .patch('/api/auth/passkey/credentials/1abc') @@ -93,7 +87,7 @@ describe('Passkey management — non-numeric id parity (Plan 3b Task 3 review, F // `renamePasskey` fall back to `Number(id)`, which would rename credential // 16 instead of 404ing — verified by hand, recorded in the task report. it('PASSKEY-INT-003 — PATCH /auth/passkey/credentials/0x10 (hex-literal id) is refused by the toRowId narrowing, even though credential 16 exists', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await insertRow(orm, WebauthnCredentials, { id: 16, user: user.id, @@ -114,7 +108,7 @@ describe('Passkey management — non-numeric id parity (Plan 3b Task 3 review, F }); it('PASSKEY-INT-004 — DELETE /auth/passkey/credentials/abc (non-numeric id, correct password) returns the legacy 404, not a 500', async () => { - const { user, password } = createUser(testDb); + const { user, password } = await makeUser(orm); const res = await request(app) .delete('/api/auth/passkey/credentials/abc') @@ -126,7 +120,7 @@ describe('Passkey management — non-numeric id parity (Plan 3b Task 3 review, F }); it('PASSKEY-INT-005 — DELETE /admin/users/abc/passkeys (non-numeric id) returns the legacy 404, not a 500', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); const res = await request(app).delete('/api/admin/users/abc/passkeys').set('Cookie', authCookie(admin.id)); @@ -137,7 +131,7 @@ describe('Passkey management — non-numeric id parity (Plan 3b Task 3 review, F describe('Registration invite deletion — prefixed numeric literal parity (Plan 3b Task 3 review, F2)', () => { it('INVITE-INT-001 — DELETE /admin/invites/0x10 (hex literal) returns the legacy 404 and leaves invite id 16 untouched', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); await insertRow(orm, InviteTokens, { id: 16, token: 'hex-literal-survivor', diff --git a/server/tests/integration/profile.test.ts b/server/tests/integration/profile.test.ts index 41d6208e4..8e2c8775e 100644 --- a/server/tests/integration/profile.test.ts +++ b/server/tests/integration/profile.test.ts @@ -3,26 +3,22 @@ * Covers PROFILE-001 to PROFILE-015. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { StorageReleaseQueue } from '../../src/db/entities/StorageReleaseQueue.entity'; import { Users } from '../../src/db/entities/Users.entity'; import { authCookie } from '../helpers/auth'; -import { createUser, createAdmin, createTrip } from '../helpers/factories'; import { findRow, insertRow } from '../helpers/factories/rows'; +import { makeTrip } from '../helpers/factories/trips'; +import { makeAdmin, makeUser } from '../helpers/factories/users'; import { readUser } from '../helpers/factories/users'; -import { resetTestDb, resetRateLimits } from '../helpers/test-db'; +import { resetDatabase } from '../helpers/reset-database'; +import { resetRateLimits } from '../helpers/test-db'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; import type { Application } from 'express'; import path from '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'; let nestApp: INestApplication; let app: Application; @@ -37,13 +33,12 @@ beforeAll(async () => { }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); await resetRateLimits(nestApp); }); afterAll(async () => { await nestApp.close(); - testDb.close(); }); // ───────────────────────────────────────────────────────────────────────────── @@ -52,7 +47,7 @@ afterAll(async () => { describe('PROFILE-001 — Get current user profile', () => { it('returns user object with expected fields', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app).get('/api/auth/me').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); expect(res.body.user).toMatchObject({ @@ -75,7 +70,7 @@ describe('PROFILE-001 — Get current user profile', () => { // in-memory entity, which still looked right). describe('PUT /api/auth/me/settings (F3)', () => { it('PROFILE-016 — a username+email change persists: the response, a fresh GET, and the raw row all agree', async () => { - const { user } = createUser(testDb, { username: 'before-name', email: 'before@example.test' }); + const { user } = await makeUser(orm, { username: 'before-name', email: 'before@example.test' }); const put = await request(app) .put('/api/auth/me/settings') @@ -102,8 +97,8 @@ describe('PUT /api/auth/me/settings (F3)', () => { // regex, `^[a-zA-Z0-9_.-]+$`, so a non-ASCII collision can never reach // this route.) it("PROFILE-016b — renaming to a non-ASCII email that collides with ANOTHER user still 409s; renaming to one's own exact non-ASCII spelling succeeds", async () => { - createUser(testDb, { email: 'JOSÉ-OTHER@x.com' }); - const { user } = createUser(testDb, { email: 'plain-self@example.test' }); + await makeUser(orm, { email: 'JOSÉ-OTHER@x.com' }); + const { user } = await makeUser(orm, { email: 'plain-self@example.test' }); // Differs from the stored spelling only in ASCII-letter case (the accented // 'É' is kept as-is — SQLite's LOWER() never touches it): SQLite's own @@ -125,7 +120,7 @@ describe('PUT /api/auth/me/settings (F3)', () => { describe('Avatar', () => { it('PROFILE-002 — upload valid JPEG avatar updates avatar_url', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .post('/api/auth/avatar') .set('Cookie', authCookie(user.id)) @@ -139,7 +134,7 @@ describe('Avatar', () => { const fsMod = require('fs') as typeof import('fs'); const pathMod = require('path') as typeof import('path'); const avatarsDir = pathMod.join(__dirname, '../../uploads/avatars'); - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .post('/api/auth/avatar') @@ -164,7 +159,7 @@ describe('Avatar', () => { }); it('PROFILE-003 — uploading non-image (PDF) is rejected', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .post('/api/auth/avatar') .set('Cookie', authCookie(user.id)) @@ -174,7 +169,7 @@ describe('Avatar', () => { }); it('PROFILE-005 — DELETE /api/auth/avatar clears avatar_url', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); // Upload first await request(app).post('/api/auth/avatar').set('Cookie', authCookie(user.id)).attach('avatar', FIXTURE_JPEG); @@ -188,7 +183,7 @@ describe('Avatar', () => { describe('Password change', () => { it('PROFILE-006 — change password with valid credentials succeeds', async () => { - const { user, password } = createUser(testDb); + const { user, password } = await makeUser(orm); const res = await request(app) .put('/api/auth/me/password') .set('Cookie', authCookie(user.id)) @@ -198,7 +193,7 @@ describe('Password change', () => { }); it('PROFILE-007 — wrong current password returns 401', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .put('/api/auth/me/password') .set('Cookie', authCookie(user.id)) @@ -207,7 +202,7 @@ describe('Password change', () => { }); it('PROFILE-008 — weak new password is rejected', async () => { - const { user, password } = createUser(testDb); + const { user, password } = await makeUser(orm); const res = await request(app) .put('/api/auth/me/password') .set('Cookie', authCookie(user.id)) @@ -218,7 +213,7 @@ describe('Password change', () => { describe('Settings', () => { it('PROFILE-009 — PUT /api/settings with key+value persists and GET returns it', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const put = await request(app) .put('/api/settings') @@ -232,13 +227,13 @@ describe('Settings', () => { }); it('PROFILE-009 — PUT /api/settings without key returns 400', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app).put('/api/settings').set('Cookie', authCookie(user.id)).send({ value: 'dark' }); expect(res.status).toBe(400); }); it('PROFILE-010 — POST /api/settings/bulk saves multiple keys atomically', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .post('/api/settings/bulk') @@ -256,7 +251,7 @@ describe('Settings', () => { describe('Account deletion', () => { it('PROFILE-013 — DELETE /api/auth/me removes account, subsequent login fails', async () => { - const { user, password } = createUser(testDb); + const { user, password } = await makeUser(orm); const del = await request(app).delete('/api/auth/me').set('Cookie', authCookie(user.id)); expect(del.status).toBe(200); @@ -267,7 +262,7 @@ describe('Account deletion', () => { }); it('PROFILE-013 — admin cannot delete their own account', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); // Admins are protected from self-deletion const res = await request(app).delete('/api/auth/me').set('Cookie', authCookie(admin.id)); // deleteAccount returns 400 when the user is the last admin @@ -277,8 +272,8 @@ describe('Account deletion', () => { describe('Travel stats', () => { it('PROFILE-014 — GET /api/auth/travel-stats returns stats object', async () => { - const { user } = createUser(testDb); - createTrip(testDb, user.id, { + const { user } = await makeUser(orm); + await makeTrip(orm, user.id, { title: 'France Trip', start_date: '2024-06-01', end_date: '2024-06-05', diff --git a/server/tests/postgres/snapshot-suites-baseline.json b/server/tests/postgres/snapshot-suites-baseline.json index f7f656fd9..0436b2203 100644 --- a/server/tests/postgres/snapshot-suites-baseline.json +++ b/server/tests/postgres/snapshot-suites-baseline.json @@ -10,15 +10,10 @@ "tests/integration/memories-immich.test.ts", "tests/integration/memories-synology.test.ts", "tests/integration/memories-unified.test.ts", - "tests/integration/notifications.test.ts", "tests/integration/oauth-authorize.test.ts", "tests/integration/oauth.test.ts", - "tests/integration/oidc.test.ts", - "tests/integration/orm-request-context-seams.test.ts", "tests/integration/packing.test.ts", - "tests/integration/passkey.test.ts", "tests/integration/places.test.ts", - "tests/integration/profile.test.ts", "tests/integration/reservations.test.ts", "tests/integration/scheduler-gate.test.ts", "tests/integration/security.test.ts",