mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
Merge dev into fix/prettier
Brings in the Tours addon and the MikroORM follow-ups. Conflicts were resolved to dev's content, then every file dev brought in was run through prettier and eslint --fix so the branch stays uniformly formatted.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
|
||||
import { TRIP_PERMISSION_KEY } from '../../../src/nest/permissions/trip-access.guard';
|
||||
import { PlacesController } from '../../../src/nest/places/places.controller';
|
||||
import type { PlacesService } from '../../../src/nest/places/places.service';
|
||||
import type { StorageService } from '../../../src/nest/storage/storage.service';
|
||||
@@ -7,6 +8,26 @@ import { HttpException } from '@nestjs/common';
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
const { legacyDatabaseAccess } = vi.hoisted(() => ({
|
||||
legacyDatabaseAccess: vi.fn((property: string | symbol): never => {
|
||||
throw new Error(`Unexpected legacy database access: ${String(property)}`);
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('../../../src/config', () => ({
|
||||
ENCRYPTION_KEY: 'test-only-inert-key',
|
||||
JWT_SECRET: 'test-only-inert-secret',
|
||||
updateJwtSecret: vi.fn(),
|
||||
}));
|
||||
vi.mock('../../../src/db/database', () => ({
|
||||
db: new Proxy(
|
||||
{},
|
||||
{
|
||||
get: (_target, property: string | symbol) => legacyDatabaseAccess(property),
|
||||
},
|
||||
),
|
||||
}));
|
||||
|
||||
const storageStub = { put: vi.fn().mockResolvedValue(undefined) } as unknown as StorageService;
|
||||
|
||||
const user = { id: 1, role: 'user', email: 'u@example.test' } as User;
|
||||
@@ -43,6 +64,10 @@ async function thrownAsync(fn: () => Promise<unknown>): Promise<{ status: number
|
||||
beforeEach(() => vi.spyOn(console, 'error').mockImplementation(() => {}));
|
||||
|
||||
describe('PlacesController (parity with the legacy /api/trips/:tripId/places route)', () => {
|
||||
it('does not access the legacy global database initializer', () => {
|
||||
expect(legacyDatabaseAccess).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
// The trip 404 for this handler is TripAccessGuard's now (see
|
||||
// trip-access.guard.test.ts and the places e2e), so it is no longer reachable
|
||||
// by calling the method directly.
|
||||
@@ -412,13 +437,27 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
|
||||
new RuntimeEnvService(),
|
||||
storageStub,
|
||||
).bulkDelete(user, '5', { ids: [] }),
|
||||
).toEqual({ deleted: [], count: 0 });
|
||||
).toEqual({ deleted: [], count: 0, tourPlaceIds: [] });
|
||||
expect(removeMany).not.toHaveBeenCalled();
|
||||
});
|
||||
it('requires place_edit before a bulk deletion can reach the service', async () => {
|
||||
const removeMany = vi.fn();
|
||||
const s = svc({ canEdit: vi.fn().mockReturnValue(false), removeMany } as Partial<PlacesService>);
|
||||
expect(
|
||||
await thrownAsync(() =>
|
||||
new PlacesController(s, new RuntimeEnvService(), storageStub).bulkDelete(user, '5', { ids: [1] }),
|
||||
),
|
||||
).toEqual({ status: 403, body: { error: 'No permission' } });
|
||||
expect(removeMany).not.toHaveBeenCalled();
|
||||
});
|
||||
it('deletes, fires hooks + broadcasts per deleted id', async () => {
|
||||
const removeMany = vi
|
||||
.fn()
|
||||
.mockReturnValue({ deleted: [1, 2], cancelled: { reservationIds: [], budgetItemIds: [] } });
|
||||
.mockReturnValue({
|
||||
deleted: [1, 2],
|
||||
deletedTourPlaceIds: [2],
|
||||
cancelled: { reservationIds: [], budgetItemIds: [] },
|
||||
});
|
||||
const onDeleted = vi.fn();
|
||||
const broadcast = vi.fn();
|
||||
const scopedIds = vi.fn().mockReturnValue([1, 2]);
|
||||
@@ -430,7 +469,7 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
|
||||
{ ids: [1, 2] },
|
||||
'sock',
|
||||
),
|
||||
).toEqual({ deleted: [1, 2], count: 2 });
|
||||
).toEqual({ deleted: [1, 2], count: 2, tourPlaceIds: [2] });
|
||||
expect(onDeleted).toHaveBeenCalledTimes(2);
|
||||
expect(broadcast).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
@@ -464,7 +503,7 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
|
||||
});
|
||||
const removeMany = vi.fn(async () => {
|
||||
order.push('delete');
|
||||
return { deleted: [1], cancelled: { reservationIds: [], budgetItemIds: [] } };
|
||||
return { deleted: [1], deletedTourPlaceIds: [], cancelled: { reservationIds: [], budgetItemIds: [] } };
|
||||
});
|
||||
const s = svc({
|
||||
removeMany,
|
||||
@@ -827,14 +866,25 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
|
||||
expect(onDeleted).toHaveBeenCalledWith(9);
|
||||
expect(onDeleted.mock.invocationCallOrder[0]).toBeLessThan(remove.mock.invocationCallOrder[0]);
|
||||
const s = svc({
|
||||
remove: vi.fn().mockReturnValue({ deleted: true, cancelled: { reservationIds: [], budgetItemIds: [] } }),
|
||||
remove: vi
|
||||
.fn()
|
||||
.mockReturnValue({
|
||||
deleted: true,
|
||||
deletedTourPlaceIds: [9],
|
||||
cancelled: { reservationIds: [], budgetItemIds: [] },
|
||||
}),
|
||||
broadcast: vi.fn(),
|
||||
} as Partial<PlacesService>);
|
||||
expect(await new PlacesController(s, new RuntimeEnvService(), storageStub).remove(user, '5', '9')).toEqual({
|
||||
success: true,
|
||||
tourPlaceIds: [9],
|
||||
});
|
||||
});
|
||||
|
||||
it('requires place_edit for single Place/Tour deletion', () => {
|
||||
expect(Reflect.getMetadata(TRIP_PERMISSION_KEY, PlacesController.prototype.remove)).toBe('place_edit');
|
||||
});
|
||||
|
||||
// invocationCallOrder in the case above only proves the hook was CALLED
|
||||
// first — a synchronously-resolving mock can't tell an awaited hook from a
|
||||
// detached (fire-and-forget) one. A hook stubbed with a real macrotask hop,
|
||||
@@ -848,7 +898,7 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
|
||||
});
|
||||
const remove = vi.fn(async () => {
|
||||
order.push('delete');
|
||||
return { deleted: true, cancelled: { reservationIds: [], budgetItemIds: [] } };
|
||||
return { deleted: true, deletedTourPlaceIds: [], cancelled: { reservationIds: [], budgetItemIds: [] } };
|
||||
});
|
||||
const s = svc({ remove, onDeleted, broadcast: vi.fn() } as Partial<PlacesService>);
|
||||
await new PlacesController(s, new RuntimeEnvService(), storageStub).remove(user, '5', '9');
|
||||
@@ -991,7 +1041,6 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('PUT /:id/image/from-file (#1242)', () => {
|
||||
it('maps every refusal to its status and broadcasts a success', async () => {
|
||||
const make = (result: unknown, extra: Partial<PlacesService> = {}) =>
|
||||
|
||||
Reference in New Issue
Block a user