fix: harden legacy normalization and grants
This commit is contained in:
1 parent
3cdca5c82c
commit
74ab2eb85f
5 files changed
+198
-20
No files matched your search
@@ -33,13 +33,16 @@ function createRepository(initial: Grant[] = []) {
|
||||
.map((grant) => grant.albumId);
|
||||
},
|
||||
async grant(input) {
|
||||
const active = grants.find(
|
||||
const active = grants.filter(
|
||||
(grant) =>
|
||||
grant.editorialId === input.editorialId &&
|
||||
grant.albumId.toLowerCase() === input.immichAlbumId &&
|
||||
grant.revokedAt === null,
|
||||
);
|
||||
if (active) return false;
|
||||
if (active.length > 0) {
|
||||
for (const grant of active) grant.albumId = input.immichAlbumId;
|
||||
return false;
|
||||
}
|
||||
|
||||
const revoked = grants.find(
|
||||
(grant) =>
|
||||
@@ -48,6 +51,7 @@ function createRepository(initial: Grant[] = []) {
|
||||
grant.revokedAt !== null,
|
||||
);
|
||||
if (revoked) {
|
||||
revoked.albumId = input.immichAlbumId;
|
||||
revoked.revokedAt = null;
|
||||
} else {
|
||||
grants.push({ editorialId: input.editorialId, albumId: input.immichAlbumId, revokedAt: null });
|
||||
@@ -63,14 +67,17 @@ function createRepository(initial: Grant[] = []) {
|
||||
return true;
|
||||
},
|
||||
async revoke(input) {
|
||||
const active = grants.find(
|
||||
const active = grants.filter(
|
||||
(grant) =>
|
||||
grant.editorialId === input.editorialId &&
|
||||
grant.albumId.toLowerCase() === input.immichAlbumId &&
|
||||
grant.revokedAt === null,
|
||||
);
|
||||
if (!active) return false;
|
||||
active.revokedAt = new Date();
|
||||
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,
|
||||
@@ -158,6 +165,29 @@ test("treats an uppercase historical album grant as the canonical active grant",
|
||||
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();
|
||||
|
||||
|
||||
Reference in new issue
Block a user