feat: enforce trip membership and role authorization
This commit is contained in:
@@ -9,13 +9,23 @@ describe('TripsController', () => {
|
||||
email: 'a@example.com',
|
||||
};
|
||||
|
||||
function controllerWith(
|
||||
tripsService: object,
|
||||
tripSettingsService: object = {},
|
||||
): TripsController {
|
||||
return new TripsController(
|
||||
tripsService as never,
|
||||
tripSettingsService as never,
|
||||
);
|
||||
}
|
||||
|
||||
it('GET /trips lists trips for the current user', async () => {
|
||||
const tripsService = {
|
||||
listTripsForUser: jest
|
||||
.fn()
|
||||
.mockResolvedValue([{ id: 't1', name: 'Slovenia 2027' }]),
|
||||
};
|
||||
const controller = new TripsController(tripsService as never);
|
||||
const controller = controllerWith(tripsService);
|
||||
|
||||
await expect(controller.list(currentUser)).resolves.toEqual([
|
||||
{ id: 't1', name: 'Slovenia 2027' },
|
||||
@@ -31,7 +41,7 @@ describe('TripsController', () => {
|
||||
version: 1,
|
||||
};
|
||||
const tripsService = { createTrip: jest.fn().mockResolvedValue(created) };
|
||||
const controller = new TripsController(tripsService as never);
|
||||
const controller = controllerWith(tripsService);
|
||||
|
||||
await expect(
|
||||
controller.create(currentUser, { name: 'Slovenia 2027' }),
|
||||
@@ -44,7 +54,7 @@ describe('TripsController', () => {
|
||||
it('GET /trips/:tripId returns a single trip', async () => {
|
||||
const trip = { id: 't1', name: 'Slovenia 2027' };
|
||||
const tripsService = { getTrip: jest.fn().mockResolvedValue(trip) };
|
||||
const controller = new TripsController(tripsService as never);
|
||||
const controller = controllerWith(tripsService);
|
||||
|
||||
await expect(controller.getOne('t1')).resolves.toEqual(trip);
|
||||
});
|
||||
@@ -52,7 +62,7 @@ describe('TripsController', () => {
|
||||
it('PATCH /trips/:tripId forwards the update dto including version', async () => {
|
||||
const updated = { id: 't1', name: 'New name', version: 2 };
|
||||
const tripsService = { updateTrip: jest.fn().mockResolvedValue(updated) };
|
||||
const controller = new TripsController(tripsService as never);
|
||||
const controller = controllerWith(tripsService);
|
||||
|
||||
await expect(
|
||||
controller.update('t1', { name: 'New name', version: 1 }),
|
||||
@@ -65,9 +75,41 @@ describe('TripsController', () => {
|
||||
|
||||
it('DELETE /trips/:tripId deletes the trip', async () => {
|
||||
const tripsService = { deleteTrip: jest.fn().mockResolvedValue(undefined) };
|
||||
const controller = new TripsController(tripsService as never);
|
||||
const controller = controllerWith(tripsService);
|
||||
|
||||
await controller.remove('t1');
|
||||
expect(tripsService.deleteTrip).toHaveBeenCalledWith('t1');
|
||||
});
|
||||
|
||||
it('GET /trips/:tripId/settings returns the trip settings', async () => {
|
||||
const settings = { tripId: 't1', webResearchEnabled: false };
|
||||
const tripSettingsService = {
|
||||
getSettings: jest.fn().mockResolvedValue(settings),
|
||||
};
|
||||
const controller = controllerWith({}, tripSettingsService);
|
||||
|
||||
await expect(controller.getSettings('t1')).resolves.toEqual(settings);
|
||||
expect(tripSettingsService.getSettings).toHaveBeenCalledWith('t1');
|
||||
});
|
||||
|
||||
it('PUT /trips/:tripId/settings replaces the trip settings', async () => {
|
||||
const dto = {
|
||||
webResearchEnabled: true,
|
||||
periodicAgentReviewEnabled: false,
|
||||
notificationEmailEnabled: true,
|
||||
notificationPushEnabled: true,
|
||||
defaultResearchDepth: null,
|
||||
defaultPlanningStyle: null,
|
||||
};
|
||||
const updated = { tripId: 't1', ...dto };
|
||||
const tripSettingsService = {
|
||||
replaceSettings: jest.fn().mockResolvedValue(updated),
|
||||
};
|
||||
const controller = controllerWith({}, tripSettingsService);
|
||||
|
||||
await expect(controller.replaceSettings('t1', dto)).resolves.toEqual(
|
||||
updated,
|
||||
);
|
||||
expect(tripSettingsService.replaceSettings).toHaveBeenCalledWith('t1', dto);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user