fix: preserve legacy accounting migration data
This commit is contained in:
1 parent
3de1530074
commit
3cdca5c82c
10 files changed
+559
-177
No files matched your search
@@ -18,6 +18,11 @@ type Audit = {
|
||||
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[] = [];
|
||||
@@ -31,7 +36,7 @@ function createRepository(initial: Grant[] = []) {
|
||||
const active = grants.find(
|
||||
(grant) =>
|
||||
grant.editorialId === input.editorialId &&
|
||||
grant.albumId === input.immichAlbumId &&
|
||||
grant.albumId.toLowerCase() === input.immichAlbumId &&
|
||||
grant.revokedAt === null,
|
||||
);
|
||||
if (active) return false;
|
||||
@@ -39,7 +44,7 @@ function createRepository(initial: Grant[] = []) {
|
||||
const revoked = grants.find(
|
||||
(grant) =>
|
||||
grant.editorialId === input.editorialId &&
|
||||
grant.albumId === input.immichAlbumId &&
|
||||
grant.albumId.toLowerCase() === input.immichAlbumId &&
|
||||
grant.revokedAt !== null,
|
||||
);
|
||||
if (revoked) {
|
||||
@@ -61,7 +66,7 @@ function createRepository(initial: Grant[] = []) {
|
||||
const active = grants.find(
|
||||
(grant) =>
|
||||
grant.editorialId === input.editorialId &&
|
||||
grant.albumId === input.immichAlbumId &&
|
||||
grant.albumId.toLowerCase() === input.immichAlbumId &&
|
||||
grant.revokedAt === null,
|
||||
);
|
||||
if (!active) return false;
|
||||
@@ -84,7 +89,7 @@ function createService(initial: Grant[] = []) {
|
||||
const { repository, grants, audit } = createRepository(initial);
|
||||
const immich = {
|
||||
getAlbum: vi.fn().mockResolvedValue({
|
||||
id: "album-1",
|
||||
id: albumUuid,
|
||||
albumName: "Pressefotos",
|
||||
description: null,
|
||||
albumThumbnailAssetId: "cover-1",
|
||||
@@ -93,7 +98,7 @@ function createService(initial: Grant[] = []) {
|
||||
}),
|
||||
listAlbums: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "album-1",
|
||||
id: albumUuid,
|
||||
albumName: "Pressefotos",
|
||||
description: null,
|
||||
albumThumbnailAssetId: "cover-1",
|
||||
@@ -114,23 +119,80 @@ test("refuses to grant an album that the service user cannot read", async () =>
|
||||
immich.getAlbum.mockRejectedValue({ code: "IMMICH_NOT_FOUND" });
|
||||
|
||||
await expect(
|
||||
service.grantAlbum({ editorialId: "e1", immichAlbumId: "hidden", adminUserId: "a1" }),
|
||||
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("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: "e1", immichAlbumId: "album-1", adminUserId: "a1" });
|
||||
await service.grantAlbum({ editorialId: "e1", immichAlbumId: "album-1", adminUserId: "a1" });
|
||||
await service.grantAlbum({
|
||||
editorialId: editorialUuid,
|
||||
immichAlbumId: albumUuid,
|
||||
adminUserId: adminUuid,
|
||||
});
|
||||
await service.grantAlbum({
|
||||
editorialId: editorialUuid,
|
||||
immichAlbumId: albumUuid,
|
||||
adminUserId: adminUuid,
|
||||
});
|
||||
|
||||
await expect(service.getGrantedAlbumIds("e1")).resolves.toEqual(["album-1"]);
|
||||
await expect(service.getGrantedAlbumIds(editorialUuid)).resolves.toEqual([albumUuid]);
|
||||
expect(audit).toEqual([
|
||||
expect.objectContaining({
|
||||
eventType: "album_granted",
|
||||
actorUserId: "a1",
|
||||
editorialId: "e1",
|
||||
albumId: "album-1",
|
||||
actorUserId: adminUuid,
|
||||
editorialId: editorialUuid,
|
||||
albumId: albumUuid,
|
||||
previousValues: { granted: false },
|
||||
newValues: { granted: true },
|
||||
}),
|
||||
@@ -139,29 +201,41 @@ test("granting an album is idempotent and records one state-change audit event",
|
||||
|
||||
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") },
|
||||
{
|
||||
editorialId: editorialUuid,
|
||||
albumId: albumUuid,
|
||||
revokedAt: new Date("2026-08-08T00:00:00Z"),
|
||||
},
|
||||
]);
|
||||
|
||||
await service.grantAlbum({ editorialId: "e1", immichAlbumId: "album-1", adminUserId: "a1" });
|
||||
await service.grantAlbum({
|
||||
editorialId: editorialUuid,
|
||||
immichAlbumId: albumUuid,
|
||||
adminUserId: adminUuid,
|
||||
});
|
||||
|
||||
expect(grants).toHaveLength(1);
|
||||
await expect(service.getGrantedAlbumIds("e1")).resolves.toEqual(["album-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: "e1", albumId: "album-1", revokedAt: null },
|
||||
{ editorialId: editorialUuid, albumId: albumUuid, revokedAt: null },
|
||||
]);
|
||||
|
||||
await service.revokeAlbum({ editorialId: "e1", immichAlbumId: "album-1", adminUserId: "a1" });
|
||||
await service.revokeAlbum({
|
||||
editorialId: editorialUuid,
|
||||
immichAlbumId: albumUuid,
|
||||
adminUserId: adminUuid,
|
||||
});
|
||||
|
||||
await expect(service.getGrantedAlbumIds("e1")).resolves.not.toContain("album-1");
|
||||
await expect(service.getGrantedAlbumIds(editorialUuid)).resolves.not.toContain(albumUuid);
|
||||
expect(audit).toContainEqual(
|
||||
expect.objectContaining({
|
||||
eventType: "album_revoked",
|
||||
actorUserId: "a1",
|
||||
editorialId: "e1",
|
||||
albumId: "album-1",
|
||||
actorUserId: adminUuid,
|
||||
editorialId: editorialUuid,
|
||||
albumId: albumUuid,
|
||||
previousValues: { granted: true },
|
||||
newValues: { granted: false },
|
||||
}),
|
||||
@@ -176,7 +250,7 @@ test("reports a stable unavailable error when the Immich album list cannot be lo
|
||||
const { service, immich } = createService();
|
||||
immich.listAlbums.mockRejectedValue({ code: "IMMICH_TIMEOUT" });
|
||||
|
||||
await expect(service.listAssignableAlbums("e1")).rejects.toEqual(
|
||||
await expect(service.listAssignableAlbums(editorialUuid)).rejects.toEqual(
|
||||
new AlbumGrantError("IMMICH_UNAVAILABLE"),
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user