Files
TREK/server/tests/unit/nest/journey-gallery.service.test.ts
Maurice 3a300a3996 fix(journey): keep gallery deletes to their journey and refuse the owner role
Deleting a gallery photo ignored the journey id in its path and told no
other tab. JourneyGalleryService now holds every photo delete: it checks
the edit right and that each photo belongs to the journey named, queues
the files for release once the rows are gone and broadcasts
journey:updated. The REST routes and the new MCP tool
delete_journey_photos (up to 100 ids, all or nothing) go through it.

The contributor routes cast the role without checking it, so a raw body
could write role 'owner'. The shared contract now takes only editor or
viewer, the service refuses any other role, and the role update never
touches the owner's own row.
2026-11-30 16:55:53 +01:00

162 lines
7.9 KiB
TypeScript

/**
* JourneyGalleryService: photos taken out of a journey for good (#3101). Every
* delete keeps to the journey it names, needs the edit right, hands the files
* to the storage release once the rows are gone and tells everyone on the
* journey, the asking tab left out (the gallery delete told nobody and did not
* read the journey id of its path). The service comes from its module over a
* snapshot test db; the release is a spy, the realtime the module's fake.
*/
import { JourneyEntryPhotos } from '../../../src/db/entities/JourneyEntryPhotos.entity';
import { JourneyPhotos } from '../../../src/db/entities/JourneyPhotos.entity';
import { TrekPhotos } from '../../../src/db/entities/TrekPhotos.entity';
import { JourneyDomainModule } from '../../../src/nest/journey/journey-domain.module';
import { JourneyDomainService } from '../../../src/nest/journey/journey-domain.service';
import { JourneyGalleryService } from '../../../src/nest/journey/journey-gallery.service';
import { StorageReleaseService } from '../../../src/nest/storage/storage-release.service';
import { createSnapshotTestDb } from '../../helpers/db-mock';
import { addJourneyContributor, makeJourneyEntry } from '../../helpers/factories/journeys';
import { countRows } from '../../helpers/factories/rows';
import { makeUser } from '../../helpers/factories/users';
import { releaseNothing } from '../../helpers/storage-release';
import { resetTestDb } from '../../helpers/test-db';
import { createTestModule, type TestModule } from '../../helpers/test-module';
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const db = createSnapshotTestDb();
const release = releaseNothing();
let t: TestModule;
let orm: TestOrm;
let gallery: JourneyGalleryService;
let journeys: JourneyDomainService;
beforeAll(async () => {
t = await createTestModule({
db,
imports: [JourneyDomainModule],
overrides: [{ provide: StorageReleaseService, useValue: release }],
});
gallery = t.get(JourneyGalleryService);
journeys = t.get(JourneyDomainService);
orm = await createTestOrm(db);
});
beforeEach(() => {
resetTestDb(db);
orm.clear();
t.realtime.reset();
release.releaseValues.mockClear();
});
afterAll(async () => {
await orm.close();
await t.close();
db.close();
});
/** Who was told what, as [user, event type, left-out socket], in user order. */
const sent = () =>
t.realtime.broadcastToUserMock.mock.calls
.map(([to, msg, sid]) => [to, (msg as { type: string }).type, sid] as const)
.sort((a, b) => a[0] - b[0]);
async function scene() {
const { user: owner } = await makeUser(orm);
const { user: editor } = await makeUser(orm);
const { user: viewer } = await makeUser(orm);
const journey = await journeys.createJourney(owner.id, { title: 'Iceland' });
await addJourneyContributor(orm, journey.id, editor.id, 'editor');
await addJourneyContributor(orm, journey.id, viewer.id, 'viewer');
const entry = await makeJourneyEntry(orm, journey.id, owner.id, { title: 'Vik' });
const [photo, video] = await journeys.uploadGalleryPhotos(journey.id, owner.id, [
{ path: 'journey/a.jpg' },
{ path: 'journey/b.mp4', thumbnail: 'journey/b-poster.jpg', mediaType: 'video' },
]);
const linked = await journeys.addProviderPhoto(entry.id, owner.id, 'immich', 'asset-1');
if (!photo || !video || !linked) throw new Error('the scene took no photo');
const other = await journeys.createJourney(owner.id, { title: 'Other' });
const [elsewhere] = await journeys.uploadGalleryPhotos(other.id, owner.id, [{ path: 'journey/c.jpg' }]);
if (!elsewhere) throw new Error('the other journey took no photo');
t.realtime.broadcastToUserMock.mockClear();
release.releaseValues.mockClear();
const everyone = [owner.id, editor.id, viewer.id].sort((a, b) => a - b);
return { owner, editor, viewer, journey, entry, photo, video, linked, other, elsewhere, everyone };
}
describe('JourneyGalleryService.deleteFromGallery', () => {
it('GALDEL-001: deletes the photos, releases their files once and tells everyone, the asking tab left out', async () => {
const s = await scene();
const deleted = await gallery.deleteFromGallery(s.journey.id, [s.photo.id, s.video.id], s.editor.id, 'sock-e');
expect(deleted).toEqual([s.photo.id, s.video.id]);
expect(await countRows(orm, JourneyPhotos, { id: { $in: [s.photo.id, s.video.id] } })).toBe(0);
expect(await countRows(orm, TrekPhotos, { id: { $in: [s.photo.photo_id, s.video.photo_id] } })).toBe(0);
expect(release.releaseValues).toHaveBeenCalledTimes(1);
const [values, options] = release.releaseValues.mock.calls[0] as unknown as [Array<string | null>, unknown];
expect(values.filter(Boolean).sort()).toEqual(['journey/a.jpg', 'journey/b-poster.jpg', 'journey/b.mp4']);
expect(options).toEqual({ bare: 'journey' });
expect(sent()).toEqual(s.everyone.map((id) => [id, 'journey:updated', 'sock-e']));
});
it('GALDEL-002: a photo on an entry leaves the entry too; the same id twice is deleted once', async () => {
const s = await scene();
const deleted = await gallery.deleteFromGallery(s.journey.id, [s.linked.id, s.linked.id], s.owner.id);
expect(deleted).toEqual([s.linked.id]);
expect(await countRows(orm, JourneyEntryPhotos, { entry: s.entry.id })).toBe(0);
expect(await countRows(orm, JourneyPhotos, { id: s.linked.id })).toBe(0);
expect(sent()).toEqual(s.everyone.map((id) => [id, 'journey:updated', undefined]));
});
it("GALDEL-003: a photo of another journey refuses the whole call and deletes nothing, the path's own included", async () => {
const s = await scene();
expect(await gallery.deleteFromGallery(s.journey.id, [s.photo.id, s.elsewhere.id], s.owner.id)).toBeNull();
// The other journey's photo named through this journey's path: refused, though the owner may change both.
expect(await gallery.deleteFromGallery(s.journey.id, [s.elsewhere.id], s.owner.id)).toBeNull();
expect(await gallery.deleteFromGallery(s.journey.id, [999999], s.owner.id)).toBeNull();
expect(await countRows(orm, JourneyPhotos, { id: { $in: [s.photo.id, s.elsewhere.id] } })).toBe(2);
expect(release.releaseValues).not.toHaveBeenCalled();
expect(t.realtime.broadcastToUserMock).not.toHaveBeenCalled();
});
it('GALDEL-004: a viewer, a stranger and an empty list are refused', async () => {
const s = await scene();
const { user: stranger } = await makeUser(orm);
expect(await gallery.deleteFromGallery(s.journey.id, [s.photo.id], s.viewer.id)).toBeNull();
expect(await gallery.deleteFromGallery(s.journey.id, [s.photo.id], stranger.id)).toBeNull();
expect(await gallery.deleteFromGallery(s.journey.id, [], s.owner.id)).toBeNull();
expect(await countRows(orm, JourneyPhotos, { id: s.photo.id })).toBe(1);
expect(t.realtime.broadcastToUserMock).not.toHaveBeenCalled();
});
});
describe('JourneyGalleryService.deletePhoto', () => {
it('GALDEL-005: the older route deletes by the photo id alone, releases its file and tells everyone', async () => {
const s = await scene();
expect(await gallery.deletePhoto(s.photo.id, s.owner.id, 'sock-o')).toBe(true);
expect(await countRows(orm, JourneyPhotos, { id: s.photo.id })).toBe(0);
const [values] = release.releaseValues.mock.calls[0] as unknown as [Array<string | null>];
expect(values.filter(Boolean)).toEqual(['journey/a.jpg']);
expect(sent()).toEqual(s.everyone.map((id) => [id, 'journey:updated', 'sock-o']));
});
it('GALDEL-006: a missing photo or one the user may not change deletes nothing and tells nobody', async () => {
const s = await scene();
expect(await gallery.deletePhoto(999999, s.owner.id)).toBe(false);
expect(await gallery.deletePhoto(s.photo.id, s.viewer.id)).toBe(false);
expect(await countRows(orm, JourneyPhotos, { id: s.photo.id })).toBe(1);
expect(release.releaseValues).not.toHaveBeenCalled();
expect(t.realtime.broadcastToUserMock).not.toHaveBeenCalled();
});
});