feat: add editorial album permissions
This commit is contained in:
1 parent
0fe2c80cda
commit
4af86f6eb9
5 files changed
+633
No files matched your search
@@ -0,0 +1,182 @@
|
||||
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>;
|
||||
};
|
||||
|
||||
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.find(
|
||||
(grant) =>
|
||||
grant.editorialId === input.editorialId &&
|
||||
grant.albumId === input.immichAlbumId &&
|
||||
grant.revokedAt === null,
|
||||
);
|
||||
if (active) return false;
|
||||
|
||||
const revoked = grants.find(
|
||||
(grant) =>
|
||||
grant.editorialId === input.editorialId &&
|
||||
grant.albumId === input.immichAlbumId &&
|
||||
grant.revokedAt !== null,
|
||||
);
|
||||
if (revoked) {
|
||||
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.find(
|
||||
(grant) =>
|
||||
grant.editorialId === input.editorialId &&
|
||||
grant.albumId === input.immichAlbumId &&
|
||||
grant.revokedAt === null,
|
||||
);
|
||||
if (!active) return false;
|
||||
active.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: "album-1",
|
||||
albumName: "Pressefotos",
|
||||
description: null,
|
||||
albumThumbnailAssetId: "cover-1",
|
||||
assetCount: 4,
|
||||
assets: [],
|
||||
}),
|
||||
listAlbums: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "album-1",
|
||||
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: "e1", immichAlbumId: "hidden", adminUserId: "a1" }),
|
||||
).rejects.toMatchObject({ code: "ALBUM_NOT_ASSIGNABLE" });
|
||||
});
|
||||
|
||||
test("granting an album is idempotent and records one state-change audit event", async () => {
|
||||
const { service, audit } = createService();
|
||||
|
||||
await service.grantAlbum({ editorialId: "e1", immichAlbumId: "album-1", adminUserId: "a1" });
|
||||
await service.grantAlbum({ editorialId: "e1", immichAlbumId: "album-1", adminUserId: "a1" });
|
||||
|
||||
await expect(service.getGrantedAlbumIds("e1")).resolves.toEqual(["album-1"]);
|
||||
expect(audit).toEqual([
|
||||
expect.objectContaining({
|
||||
eventType: "album_granted",
|
||||
actorUserId: "a1",
|
||||
editorialId: "e1",
|
||||
albumId: "album-1",
|
||||
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: "e1", albumId: "album-1", revokedAt: new Date("2026-08-08T00:00:00Z") },
|
||||
]);
|
||||
|
||||
await service.grantAlbum({ editorialId: "e1", immichAlbumId: "album-1", adminUserId: "a1" });
|
||||
|
||||
expect(grants).toHaveLength(1);
|
||||
await expect(service.getGrantedAlbumIds("e1")).resolves.toEqual(["album-1"]);
|
||||
});
|
||||
|
||||
test("revocation immediately removes the album from visible IDs and records the actor", async () => {
|
||||
const { service, audit } = createService([
|
||||
{ editorialId: "e1", albumId: "album-1", revokedAt: null },
|
||||
]);
|
||||
|
||||
await service.revokeAlbum({ editorialId: "e1", immichAlbumId: "album-1", adminUserId: "a1" });
|
||||
|
||||
await expect(service.getGrantedAlbumIds("e1")).resolves.not.toContain("album-1");
|
||||
expect(audit).toContainEqual(
|
||||
expect.objectContaining({
|
||||
eventType: "album_revoked",
|
||||
actorUserId: "a1",
|
||||
editorialId: "e1",
|
||||
albumId: "album-1",
|
||||
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("e1")).rejects.toEqual(
|
||||
new AlbumGrantError("IMMICH_UNAVAILABLE"),
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user