mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
382ba79584
A visit is a place planned on a day of one of the reader's trips; nothing is kept or confirmed by hand. A place keeps a log of changes to its details, links the tours and bookings it is about, and carries tags private to the reader, which a copy into a trip takes along only when picked. Contracts, routes, MCP tools and migrations for both engines.
198 lines
8.8 KiB
TypeScript
198 lines
8.8 KiB
TypeScript
/**
|
|
* The bookings and tours a place is about (PLC-18) through the real
|
|
* AddonGuard, JwtAuthGuard and TripAccessGuard against a migrated and seeded
|
|
* temp SQLite db, every answer checked against its response contract.
|
|
*/
|
|
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
|
|
import { ZodValidationPipe } from '../../src/nest/common/zod-validation.pipe';
|
|
import { PlaceActivitiesModule } from '../../src/nest/place-activities/place-activities.module';
|
|
import { RealtimeModule } from '../../src/nest/realtime/realtime.module';
|
|
import { RealtimeService } from '../../src/nest/realtime/realtime.service';
|
|
import { createSnapshotTestDb } from '../helpers/db-mock';
|
|
import { setAddonEnabled } from '../helpers/factories/settings';
|
|
import { addTripMember } from '../helpers/factories/trips';
|
|
import { FakeRealtimeService } from '../helpers/fake-realtime';
|
|
import { oneByOne } from '../helpers/one-by-one';
|
|
import { seedPlaceActivities, type PlaceActivitiesFixture } from '../helpers/place-activities-fixture';
|
|
import { resetTestDb } from '../helpers/test-db';
|
|
import { createTestMikroOrmModule, createTestOrm, type TestOrm } from '../helpers/test-orm';
|
|
import { TestUnitOfWorkModule } from '../helpers/test-uow';
|
|
import { sessionCookie } from './harness';
|
|
import type { INestApplication } from '@nestjs/common';
|
|
import { Test } from '@nestjs/testing';
|
|
|
|
import cookieParser from 'cookie-parser';
|
|
import type { Server } from 'http';
|
|
import request from 'supertest';
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
|
|
|
const db = createSnapshotTestDb();
|
|
const realtime = new FakeRealtimeService();
|
|
|
|
let app: INestApplication;
|
|
let server: Server;
|
|
let orm: TestOrm;
|
|
let f: PlaceActivitiesFixture;
|
|
|
|
const as = (userId: number) => ({ Cookie: sessionCookie(userId) });
|
|
const tripBase = (tripId: number, placeId: number) => `/api/trips/${tripId}/places/${placeId}/activities`;
|
|
const savedBase = (id: number | string) => `/api/addons/collections/places/${id}/activities`;
|
|
|
|
beforeAll(async () => {
|
|
const moduleRef = await Test.createTestingModule({
|
|
imports: [
|
|
await TestUnitOfWorkModule.forRoot(db),
|
|
await createTestMikroOrmModule(db),
|
|
RealtimeModule,
|
|
PlaceActivitiesModule,
|
|
],
|
|
})
|
|
.overrideProvider(RealtimeService)
|
|
.useValue(realtime)
|
|
.compile();
|
|
app = moduleRef.createNestApplication();
|
|
app.use(cookieParser());
|
|
app.useGlobalPipes(new ZodValidationPipe());
|
|
app.useGlobalFilters(new TrekExceptionFilter());
|
|
await app.init();
|
|
server = app.getHttpServer() as Server;
|
|
orm = await createTestOrm(db);
|
|
});
|
|
beforeEach(async () => {
|
|
resetTestDb(db);
|
|
orm.clear();
|
|
realtime.reset();
|
|
f = await seedPlaceActivities(orm);
|
|
await setAddonEnabled(orm, 'collections', true);
|
|
await setAddonEnabled(orm, 'tours', true);
|
|
});
|
|
afterAll(async () => {
|
|
await app.close();
|
|
await orm.close();
|
|
db.close();
|
|
});
|
|
|
|
describe('Place activities e2e (real guards + temp SQLite)', () => {
|
|
it('answers 401 without a session and 404 for a trip out of reach or a place of another trip', async () => {
|
|
expect((await request(server).get(tripBase(f.alps.id, f.places.cafe.id))).status).toBe(401);
|
|
const hidden = await request(server).get(tripBase(f.elsewhere.id, f.places.hidden.id)).set(as(f.me.id));
|
|
expect(hidden.status).toBe(404);
|
|
expect(hidden.body).toEqual({ error: 'Trip not found' });
|
|
const foreign = await request(server).get(tripBase(f.alps.id, f.places.forum.id)).set(as(f.me.id));
|
|
expect(foreign.status).toBe(404);
|
|
expect(foreign.body).toEqual({ error: 'Place not found' });
|
|
});
|
|
|
|
it('links a booking and a tour to a trip place, lists them with the candidates, and unlinks them', async () => {
|
|
const cafe = f.places.cafe.id;
|
|
const candidates = await request(server)
|
|
.get(`${tripBase(f.alps.id, cafe)}/candidates`)
|
|
.set(as(f.me.id));
|
|
expect(candidates.status).toBe(200);
|
|
expect(candidates.body.candidates.map((c: { title: string }) => c.title)).toEqual(
|
|
expect.arrayContaining(['Guided tour', 'Old town walk', 'Ridge walk']),
|
|
);
|
|
|
|
const link = await request(server)
|
|
.post(tripBase(f.alps.id, cafe))
|
|
.set(as(f.me.id))
|
|
.set('X-Socket-Id', '42')
|
|
.send({ reservation_id: f.bookings.guided.id });
|
|
expect(link.status).toBe(200);
|
|
expect(realtime.broadcastMock).toHaveBeenCalledWith(f.alps.id, 'placeActivities:changed', { placeId: cafe }, '42');
|
|
await request(server).post(tripBase(f.alps.id, cafe)).set(as(f.me.id)).send({ tour_id: f.tours.walk.id });
|
|
|
|
const list = await request(server).get(tripBase(f.alps.id, cafe)).set(as(f.me.id));
|
|
expect(list.status).toBe(200);
|
|
expect(list.body.can_link).toBe(true);
|
|
expect(list.body.activities.map((a: { key: string; explicit: boolean }) => [a.key, a.explicit])).toEqual([
|
|
[`reservation:${f.bookings.guided.id}`, true],
|
|
[`reservation:${f.bookings.dinner.id}`, false],
|
|
[`tour:${f.tours.walk.id}`, true],
|
|
]);
|
|
|
|
const booking = await request(server)
|
|
.delete(`${tripBase(f.alps.id, cafe)}/reservations/${f.bookings.guided.id}`)
|
|
.set(as(f.me.id));
|
|
expect(booking.status).toBe(200);
|
|
const tour = await request(server)
|
|
.delete(`${tripBase(f.alps.id, cafe)}/tours/${f.tours.walk.id}`)
|
|
.set(as(f.me.id));
|
|
expect(tour.status).toBe(200);
|
|
expect(tour.body.activities.map((a: { title: string }) => a.title)).toEqual(['Dinner']);
|
|
});
|
|
|
|
it('refuses a body it does not take, a booking that belongs already and a bad id', async () => {
|
|
const cafe = f.places.cafe.id;
|
|
const refused = await oneByOne(
|
|
[{}, { reservation_id: 1, tour_id: 2 }, { reservation_id: '0x5' }, { tour_id: 1.5 }, { tour_id: 1, x: 1 }],
|
|
(body) => request(server).post(tripBase(f.alps.id, cafe)).set(as(f.me.id)).send(body),
|
|
);
|
|
expect(refused.map((res) => res.status)).toEqual([400, 400, 400, 400, 400]);
|
|
const own = await request(server)
|
|
.post(tripBase(f.alps.id, cafe))
|
|
.set(as(f.me.id))
|
|
.send({ reservation_id: f.bookings.dinner.id });
|
|
expect(own.status).toBe(409);
|
|
expect(own.body).toMatchObject({ error: 'This booking already belongs to the place' });
|
|
const missing = await oneByOne(['abc', '0x5'], (id) =>
|
|
request(server)
|
|
.delete(`${tripBase(f.alps.id, cafe)}/reservations/${id}`)
|
|
.set(as(f.me.id)),
|
|
);
|
|
for (const res of missing) {
|
|
expect(res.status).toBe(404);
|
|
expect(res.body).toEqual({ error: 'This activity is not linked to the place' });
|
|
}
|
|
});
|
|
|
|
it('lets a member read what a place is about', async () => {
|
|
await addTripMember(orm, f.alps.id, f.friend.id);
|
|
const res = await request(server).get(tripBase(f.alps.id, f.places.hotel.id)).set(as(f.friend.id));
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.activities).toEqual([expect.objectContaining({ title: 'Hotel Sonne', implicit_via: 'place' })]);
|
|
});
|
|
|
|
it('links a kept tour to a saved place behind the Collections addon, and reads an entry of All places', async () => {
|
|
await setAddonEnabled(orm, 'collections', false);
|
|
expect((await request(server).get(savedBase(f.saved.savedCafe.id)).set(as(f.me.id))).status).toBe(404);
|
|
await setAddonEnabled(orm, 'collections', true);
|
|
|
|
const link = await request(server)
|
|
.post(savedBase(f.saved.savedCafe.id))
|
|
.set(as(f.me.id))
|
|
.send({ collection_tour_id: f.kept.keptRidge.id });
|
|
expect(link.status).toBe(200);
|
|
expect(link.body.activities).toEqual([expect.objectContaining({ title: 'Kept ridge', can_unlink: true })]);
|
|
const candidates = await request(server)
|
|
.get(`${savedBase(f.saved.savedCafe.id)}/candidates`)
|
|
.set(as(f.me.id));
|
|
expect(candidates.body.candidates.map((c: { title: string }) => c.title)).toEqual(['Kept lake loop']);
|
|
|
|
const entry = await request(server)
|
|
.get(`/api/addons/collections/library/places/list/${f.saved.savedCafe.id}/activities`)
|
|
.set(as(f.me.id));
|
|
expect(entry.status).toBe(200);
|
|
expect(entry.body.activities.map((a: { title: string }) => a.title).sort()).toEqual(['Dinner', 'Kept ridge']);
|
|
expect(entry.body.sources).toHaveLength(2);
|
|
|
|
const unlink = await request(server)
|
|
.delete(`${savedBase(f.saved.savedCafe.id)}/tours/${f.kept.keptRidge.id}`)
|
|
.set(as(f.me.id));
|
|
expect(unlink.status).toBe(200);
|
|
expect(unlink.body).toEqual({ activities: [], can_link: true });
|
|
|
|
const viewer = await request(server)
|
|
.post(savedBase(f.saved.pantheon.id))
|
|
.set(as(f.me.id))
|
|
.send({ collection_tour_id: f.kept.keptRome.id });
|
|
expect(viewer.status).toBe(403);
|
|
const secret = await request(server).get(savedBase(f.saved.secret.id)).set(as(f.me.id));
|
|
expect(secret.status).toBe(404);
|
|
expect(secret.body).toEqual({ error: 'Place not found' });
|
|
const bad = await request(server).get(savedBase('0x5')).set(as(f.me.id));
|
|
expect(bad.status).toBe(404);
|
|
expect(bad.body).toEqual({ error: 'Place not found' });
|
|
});
|
|
});
|