287 lines
8.8 KiB
TypeScript
287 lines
8.8 KiB
TypeScript
import { expect, test, vi } from "vitest";
|
|
|
|
import {
|
|
ALBUM_GRANT_LOCK_ORDER,
|
|
AlbumGrantError,
|
|
createAlbumGrantService,
|
|
type AlbumGrantRepository,
|
|
} from "./grants";
|
|
import { EDITORIAL_MUTATION_LOCK_ORDER } from "@/modules/editorials/service";
|
|
|
|
type Grant = { editorialId: string; albumId: string; revokedAt: Date | null };
|
|
type Audit = {
|
|
eventType: string;
|
|
actorUserId: string;
|
|
editorialId: string;
|
|
albumId: string;
|
|
previousValues: Record<string, unknown>;
|
|
newValues: Record<string, unknown>;
|
|
};
|
|
|
|
const editorialUuid = "1111111a-1111-4111-8111-11111111111a";
|
|
const albumUuid = "2222222b-2222-4222-8222-22222222222b";
|
|
const adminUuid = "3333333c-3333-4333-8333-33333333333c";
|
|
const unavailableAlbumUuid = "4444444d-4444-4444-8444-44444444444d";
|
|
|
|
function createRepository(initial: Grant[] = []) {
|
|
const grants = [...initial];
|
|
const audit: Audit[] = [];
|
|
const repository: AlbumGrantRepository = {
|
|
async listActiveAlbumIds(editorialId) {
|
|
return grants
|
|
.filter((grant) => grant.editorialId === editorialId && grant.revokedAt === null)
|
|
.map((grant) => grant.albumId);
|
|
},
|
|
async grant(input) {
|
|
const active = grants.filter(
|
|
(grant) =>
|
|
grant.editorialId === input.editorialId &&
|
|
grant.albumId.toLowerCase() === input.immichAlbumId &&
|
|
grant.revokedAt === null,
|
|
);
|
|
if (active.length > 0) {
|
|
for (const grant of active) grant.albumId = input.immichAlbumId;
|
|
return false;
|
|
}
|
|
|
|
const revoked = grants.find(
|
|
(grant) =>
|
|
grant.editorialId === input.editorialId &&
|
|
grant.albumId.toLowerCase() === input.immichAlbumId &&
|
|
grant.revokedAt !== null,
|
|
);
|
|
if (revoked) {
|
|
revoked.albumId = input.immichAlbumId;
|
|
revoked.revokedAt = null;
|
|
} else {
|
|
grants.push({ editorialId: input.editorialId, albumId: input.immichAlbumId, revokedAt: null });
|
|
}
|
|
audit.push({
|
|
eventType: "album_granted",
|
|
actorUserId: input.adminUserId,
|
|
editorialId: input.editorialId,
|
|
albumId: input.immichAlbumId,
|
|
previousValues: { granted: false },
|
|
newValues: { granted: true },
|
|
});
|
|
return true;
|
|
},
|
|
async revoke(input) {
|
|
const active = grants.filter(
|
|
(grant) =>
|
|
grant.editorialId === input.editorialId &&
|
|
grant.albumId.toLowerCase() === input.immichAlbumId &&
|
|
grant.revokedAt === null,
|
|
);
|
|
if (active.length === 0) return false;
|
|
for (const grant of active) {
|
|
grant.albumId = input.immichAlbumId;
|
|
grant.revokedAt = new Date();
|
|
}
|
|
audit.push({
|
|
eventType: "album_revoked",
|
|
actorUserId: input.adminUserId,
|
|
editorialId: input.editorialId,
|
|
albumId: input.immichAlbumId,
|
|
previousValues: { granted: true },
|
|
newValues: { granted: false },
|
|
});
|
|
return true;
|
|
},
|
|
};
|
|
return { repository, grants, audit };
|
|
}
|
|
|
|
function createService(initial: Grant[] = []) {
|
|
const { repository, grants, audit } = createRepository(initial);
|
|
const immich = {
|
|
getAlbum: vi.fn().mockResolvedValue({
|
|
id: albumUuid,
|
|
albumName: "Pressefotos",
|
|
description: null,
|
|
albumThumbnailAssetId: "cover-1",
|
|
assetCount: 4,
|
|
assets: [],
|
|
}),
|
|
listAlbums: vi.fn().mockResolvedValue([
|
|
{
|
|
id: albumUuid,
|
|
albumName: "Pressefotos",
|
|
description: null,
|
|
albumThumbnailAssetId: "cover-1",
|
|
assetCount: 4,
|
|
},
|
|
]),
|
|
};
|
|
return {
|
|
service: createAlbumGrantService({ repository, immich }),
|
|
immich,
|
|
grants,
|
|
audit,
|
|
};
|
|
}
|
|
|
|
test("refuses to grant an album that the service user cannot read", async () => {
|
|
const { service, immich } = createService();
|
|
immich.getAlbum.mockRejectedValue({ code: "IMMICH_NOT_FOUND" });
|
|
|
|
await expect(
|
|
service.grantAlbum({
|
|
editorialId: editorialUuid,
|
|
immichAlbumId: unavailableAlbumUuid,
|
|
adminUserId: adminUuid,
|
|
}),
|
|
).rejects.toMatchObject({ code: "ALBUM_NOT_ASSIGNABLE" });
|
|
});
|
|
|
|
test("canonicalizes UUID grant input before Immich and repository side effects", async () => {
|
|
const { service, immich, grants } = createService();
|
|
|
|
await service.grantAlbum({
|
|
editorialId: editorialUuid.toUpperCase(),
|
|
immichAlbumId: albumUuid.toUpperCase(),
|
|
adminUserId: adminUuid.toUpperCase(),
|
|
});
|
|
|
|
expect(immich.getAlbum).toHaveBeenCalledWith(albumUuid);
|
|
expect(grants).toEqual([
|
|
{ editorialId: editorialUuid, albumId: albumUuid, revokedAt: null },
|
|
]);
|
|
});
|
|
|
|
test("treats an uppercase historical album grant as the canonical active grant", async () => {
|
|
const { service, grants, audit } = createService([
|
|
{ editorialId: editorialUuid, albumId: albumUuid.toUpperCase(), revokedAt: null },
|
|
]);
|
|
|
|
await expect(service.getGrantedAlbumIds(editorialUuid)).resolves.toEqual([albumUuid]);
|
|
await service.grantAlbum({
|
|
editorialId: editorialUuid,
|
|
immichAlbumId: albumUuid,
|
|
adminUserId: adminUuid,
|
|
});
|
|
|
|
expect(grants).toHaveLength(1);
|
|
expect(audit).toHaveLength(0);
|
|
});
|
|
|
|
test("revokes and regrants case-duplicate historical rows as one canonical grant", async () => {
|
|
const { service, grants, audit } = createService([
|
|
{ editorialId: editorialUuid, albumId: albumUuid.toUpperCase(), revokedAt: null },
|
|
{ editorialId: editorialUuid, albumId: albumUuid, revokedAt: null },
|
|
]);
|
|
|
|
await service.revokeAlbum({
|
|
editorialId: editorialUuid,
|
|
immichAlbumId: albumUuid,
|
|
adminUserId: adminUuid,
|
|
});
|
|
expect(grants.filter((grant) => grant.revokedAt === null)).toHaveLength(0);
|
|
expect(grants.every((grant) => grant.albumId === albumUuid)).toBe(true);
|
|
|
|
await service.grantAlbum({
|
|
editorialId: editorialUuid,
|
|
immichAlbumId: albumUuid,
|
|
adminUserId: adminUuid,
|
|
});
|
|
expect(grants.filter((grant) => grant.revokedAt === null)).toHaveLength(1);
|
|
expect(audit.map((event) => event.eventType)).toEqual(["album_revoked", "album_granted"]);
|
|
});
|
|
|
|
test("rejects a non-UUID grant before external side effects", async () => {
|
|
const { service, immich, grants } = createService();
|
|
|
|
await expect(
|
|
service.grantAlbum({
|
|
editorialId: editorialUuid,
|
|
immichAlbumId: "legacy-album",
|
|
adminUserId: adminUuid,
|
|
}),
|
|
).rejects.toMatchObject({ code: "INVALID_ALBUM_GRANT_INPUT" });
|
|
expect(immich.getAlbum).not.toHaveBeenCalled();
|
|
expect(grants).toHaveLength(0);
|
|
});
|
|
|
|
test("granting an album is idempotent and records one state-change audit event", async () => {
|
|
const { service, audit } = createService();
|
|
|
|
await service.grantAlbum({
|
|
editorialId: editorialUuid,
|
|
immichAlbumId: albumUuid,
|
|
adminUserId: adminUuid,
|
|
});
|
|
await service.grantAlbum({
|
|
editorialId: editorialUuid,
|
|
immichAlbumId: albumUuid,
|
|
adminUserId: adminUuid,
|
|
});
|
|
|
|
await expect(service.getGrantedAlbumIds(editorialUuid)).resolves.toEqual([albumUuid]);
|
|
expect(audit).toEqual([
|
|
expect.objectContaining({
|
|
eventType: "album_granted",
|
|
actorUserId: adminUuid,
|
|
editorialId: editorialUuid,
|
|
albumId: albumUuid,
|
|
previousValues: { granted: false },
|
|
newValues: { granted: true },
|
|
}),
|
|
]);
|
|
});
|
|
|
|
test("a revoked album can be re-granted without creating another current grant", async () => {
|
|
const { service, grants } = createService([
|
|
{
|
|
editorialId: editorialUuid,
|
|
albumId: albumUuid,
|
|
revokedAt: new Date("2026-08-08T00:00:00Z"),
|
|
},
|
|
]);
|
|
|
|
await service.grantAlbum({
|
|
editorialId: editorialUuid,
|
|
immichAlbumId: albumUuid,
|
|
adminUserId: adminUuid,
|
|
});
|
|
|
|
expect(grants).toHaveLength(1);
|
|
await expect(service.getGrantedAlbumIds(editorialUuid)).resolves.toEqual([albumUuid]);
|
|
});
|
|
|
|
test("revocation immediately removes the album from visible IDs and records the actor", async () => {
|
|
const { service, audit } = createService([
|
|
{ editorialId: editorialUuid, albumId: albumUuid, revokedAt: null },
|
|
]);
|
|
|
|
await service.revokeAlbum({
|
|
editorialId: editorialUuid,
|
|
immichAlbumId: albumUuid,
|
|
adminUserId: adminUuid,
|
|
});
|
|
|
|
await expect(service.getGrantedAlbumIds(editorialUuid)).resolves.not.toContain(albumUuid);
|
|
expect(audit).toContainEqual(
|
|
expect.objectContaining({
|
|
eventType: "album_revoked",
|
|
actorUserId: adminUuid,
|
|
editorialId: editorialUuid,
|
|
albumId: albumUuid,
|
|
previousValues: { granted: true },
|
|
newValues: { granted: false },
|
|
}),
|
|
);
|
|
});
|
|
|
|
test("uses the same lock order as approval so concurrent approval and revocation serialize", () => {
|
|
expect(ALBUM_GRANT_LOCK_ORDER).toEqual(EDITORIAL_MUTATION_LOCK_ORDER);
|
|
});
|
|
|
|
test("reports a stable unavailable error when the Immich album list cannot be loaded", async () => {
|
|
const { service, immich } = createService();
|
|
immich.listAlbums.mockRejectedValue({ code: "IMMICH_TIMEOUT" });
|
|
|
|
await expect(service.listAssignableAlbums(editorialUuid)).rejects.toEqual(
|
|
new AlbumGrantError("IMMICH_UNAVAILABLE"),
|
|
);
|
|
});
|