fix: enforce one active logical album grant
This commit is contained in:
1 parent
74ab2eb85f
commit
c400167b8f
9 files changed
+577
-45
No files matched your search
@@ -8,11 +8,12 @@ const migration = await readFile(
|
||||
"utf8",
|
||||
);
|
||||
|
||||
test("0002 retains legacy Immich text and gates only new non-canonical rows", () => {
|
||||
test("0002 retains legacy Immich text and accepts case-variant historical UUIDs", () => {
|
||||
expect(migration).not.toMatch(/ALTER COLUMN "immich_[^"]+" SET DATA TYPE uuid/);
|
||||
expect(migration).toContain(
|
||||
'ADD CONSTRAINT "album_grants_canonical_immich_album_id_check"',
|
||||
'ADD CONSTRAINT "album_grants_immich_album_id_uuid_check"',
|
||||
);
|
||||
expect(migration).toContain('CHECK ("album_grants"."immich_album_id" ~*');
|
||||
expect(migration).toContain(
|
||||
'ADD CONSTRAINT "usages_canonical_immich_asset_id_check"',
|
||||
);
|
||||
@@ -25,6 +26,25 @@ test("0002 retains legacy Immich text and gates only new non-canonical rows", ()
|
||||
expect(migration.match(/NOT VALID/g)).toHaveLength(4);
|
||||
});
|
||||
|
||||
test("0002 consolidates active case duplicates before adding their partial unique index", () => {
|
||||
const consolidation = migration.indexOf('ranked_active_album_grants AS');
|
||||
const activeIndex = migration.indexOf(
|
||||
'CREATE UNIQUE INDEX "album_grants_editorial_active_album_unique"',
|
||||
);
|
||||
|
||||
expect(consolidation).toBeGreaterThan(-1);
|
||||
expect(migration).toContain(
|
||||
'PARTITION BY "editorial_account_id", lower("immich_album_id")',
|
||||
);
|
||||
expect(migration).toContain(
|
||||
'ORDER BY ("immich_album_id" = lower("immich_album_id")) DESC, "granted_at", "id"',
|
||||
);
|
||||
expect(activeIndex).toBeGreaterThan(consolidation);
|
||||
expect(migration).toContain(
|
||||
'ON "album_grants" ("editorial_account_id", lower("immich_album_id")) WHERE "revoked_at" IS NULL',
|
||||
);
|
||||
});
|
||||
|
||||
test("0002 uses an explicit legacy kind instead of random UUID ordering for ambiguity", () => {
|
||||
expect(migration).not.toContain('ORDER BY "authorized_at", "id"');
|
||||
expect(migration).toContain("ELSE 'legacy'");
|
||||
|
||||
@@ -301,13 +301,13 @@ test("0002 preserves dirty rows and reconstructs only provable legacy intents",
|
||||
"insert into album_grants (editorial_account_id, immich_album_id) values ($1, $2)",
|
||||
[editorialId, "new-invalid-album"],
|
||||
),
|
||||
).rejects.toThrow(/album_grants_canonical_immich_album_id_check/);
|
||||
).rejects.toThrow(/album_grants_immich_album_id_uuid_check/);
|
||||
await expect(
|
||||
testDb.pool.query(
|
||||
"update album_grants set revoked_at = now() where immich_album_id = $1",
|
||||
[dirtyAlbumId],
|
||||
),
|
||||
).rejects.toThrow(/album_grants_canonical_immich_album_id_check/);
|
||||
).rejects.toThrow(/album_grants_immich_album_id_uuid_check/);
|
||||
|
||||
const grantService = createAlbumGrantService({
|
||||
repository: createAlbumGrantRepository(testDb.database),
|
||||
@@ -336,7 +336,9 @@ test("0002 preserves dirty rows and reconstructs only provable legacy intents",
|
||||
[validAlbumId],
|
||||
);
|
||||
expect(canonicalGrantRows.rows).toHaveLength(2);
|
||||
expect(canonicalGrantRows.rows.every((row) => row.immich_album_id === validAlbumId)).toBe(true);
|
||||
expect(canonicalGrantRows.rows.map((row) => row.immich_album_id).sort()).toEqual(
|
||||
[validAlbumId.toUpperCase(), validAlbumId].sort(),
|
||||
);
|
||||
expect(canonicalGrantRows.rows.every((row) => row.revoked_at !== null)).toBe(true);
|
||||
|
||||
await grantService.grantAlbum({
|
||||
|
||||
@@ -0,0 +1,277 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
import { afterAll, expect, test, vi } from "vitest";
|
||||
|
||||
import { createAlbumGrantRepository, createAlbumGrantService } from "@/modules/albums/grants";
|
||||
import { startPostgres } from "./postgres";
|
||||
|
||||
const testDb = await startPostgres({ through: "0001_editorial_approval_notice.sql" });
|
||||
|
||||
afterAll(() => testDb.stop());
|
||||
|
||||
const directAlbumId = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
|
||||
const revokeAlbumId = "cccccccc-cccc-4ccc-8ccc-cccccccccccc";
|
||||
const regrantAlbumId = "dddddddd-dddd-4ddd-8ddd-dddddddddddd";
|
||||
const listAlbumId = "eeeeeeee-eeee-4eee-8eee-eeeeeeeeeeee";
|
||||
const migrationAlbumId = "ffffffff-ffff-4fff-8fff-ffffffffffff";
|
||||
const earlierGrant = new Date("2026-08-07T08:00:00.000Z");
|
||||
const laterGrant = new Date("2026-08-08T08:00:00.000Z");
|
||||
const revokedAt = new Date("2026-08-09T08:00:00.000Z");
|
||||
|
||||
test("grant mutations and migration preserve one deterministic active logical grant", async () => {
|
||||
const adminUserId = randomUUID();
|
||||
const editorialId = randomUUID();
|
||||
await testDb.pool.query(
|
||||
`insert into users (id, name, email, email_verified)
|
||||
values ($1, 'Grant Admin', $2, true)`,
|
||||
[adminUserId, `${adminUserId}@example.com`],
|
||||
);
|
||||
await testDb.pool.query(
|
||||
`insert into editorial_accounts (
|
||||
id, user_id, name, contact_name, login_email, status, current_price_cents
|
||||
) values ($1, $2, 'Grant Redaktion', 'Grant Kontakt', $3, 'active', 2300)`,
|
||||
[editorialId, adminUserId, `${adminUserId}@example.com`],
|
||||
);
|
||||
|
||||
await insertGrantRows(editorialId, adminUserId, [
|
||||
{
|
||||
id: "10000000-0000-4000-8000-000000000001",
|
||||
albumId: directAlbumId.toUpperCase(),
|
||||
grantedAt: earlierGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
{
|
||||
id: "10000000-0000-4000-8000-000000000002",
|
||||
albumId: directAlbumId,
|
||||
grantedAt: laterGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
]);
|
||||
await insertGrantRows(editorialId, adminUserId, [
|
||||
{
|
||||
id: "20000000-0000-4000-8000-000000000001",
|
||||
albumId: revokeAlbumId.toUpperCase(),
|
||||
grantedAt: earlierGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
{
|
||||
id: "20000000-0000-4000-8000-000000000002",
|
||||
albumId: revokeAlbumId,
|
||||
grantedAt: laterGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
]);
|
||||
await insertGrantRows(editorialId, adminUserId, [
|
||||
{
|
||||
id: "30000000-0000-4000-8000-000000000003",
|
||||
albumId: regrantAlbumId.toUpperCase(),
|
||||
grantedAt: laterGrant,
|
||||
revokedAt,
|
||||
},
|
||||
{
|
||||
id: "30000000-0000-4000-8000-000000000002",
|
||||
albumId: "Dddddddd-dddd-4ddd-8ddd-dddddddddddd",
|
||||
grantedAt: earlierGrant,
|
||||
revokedAt,
|
||||
},
|
||||
{
|
||||
id: "30000000-0000-4000-8000-000000000001",
|
||||
albumId: "dddddddD-dddd-4ddd-8ddd-dddddddddddd",
|
||||
grantedAt: earlierGrant,
|
||||
revokedAt,
|
||||
},
|
||||
]);
|
||||
await insertGrantRows(editorialId, adminUserId, [
|
||||
{
|
||||
id: "40000000-0000-4000-8000-000000000001",
|
||||
albumId: listAlbumId.toUpperCase(),
|
||||
grantedAt: earlierGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
{
|
||||
id: "40000000-0000-4000-8000-000000000002",
|
||||
albumId: listAlbumId,
|
||||
grantedAt: laterGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
]);
|
||||
await insertGrantRows(editorialId, adminUserId, [
|
||||
{
|
||||
id: "50000000-0000-4000-8000-000000000001",
|
||||
albumId: migrationAlbumId.toUpperCase(),
|
||||
grantedAt: earlierGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
{
|
||||
id: "50000000-0000-4000-8000-000000000002",
|
||||
albumId: migrationAlbumId,
|
||||
grantedAt: laterGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
]);
|
||||
|
||||
const grantService = createAlbumGrantService({
|
||||
repository: createAlbumGrantRepository(testDb.database),
|
||||
immich: {
|
||||
getAlbum: vi.fn().mockImplementation(async (id: string) => ({
|
||||
id,
|
||||
albumName: "Grant Album",
|
||||
description: null,
|
||||
albumThumbnailAssetId: null,
|
||||
assetCount: 0,
|
||||
assets: [],
|
||||
})),
|
||||
listAlbums: vi.fn(),
|
||||
},
|
||||
});
|
||||
|
||||
const listedBeforeCleanup = await grantService.getGrantedAlbumIds(editorialId);
|
||||
expect(listedBeforeCleanup.filter((id) => id === listAlbumId)).toEqual([listAlbumId]);
|
||||
|
||||
await grantService.grantAlbum({
|
||||
editorialId,
|
||||
immichAlbumId: directAlbumId,
|
||||
adminUserId,
|
||||
});
|
||||
await grantService.grantAlbum({
|
||||
editorialId,
|
||||
immichAlbumId: directAlbumId,
|
||||
adminUserId,
|
||||
});
|
||||
expect(await activeGrantRows(editorialId, directAlbumId)).toEqual([
|
||||
{
|
||||
id: "10000000-0000-4000-8000-000000000002",
|
||||
immich_album_id: directAlbumId,
|
||||
},
|
||||
]);
|
||||
await expect(auditCount(editorialId, directAlbumId, "album_granted")).resolves.toBe(0);
|
||||
|
||||
await grantService.revokeAlbum({
|
||||
editorialId,
|
||||
immichAlbumId: revokeAlbumId,
|
||||
adminUserId,
|
||||
});
|
||||
await expect(activeGrantRows(editorialId, revokeAlbumId)).resolves.toEqual([]);
|
||||
await expect(auditCount(editorialId, revokeAlbumId, "album_revoked")).resolves.toBe(1);
|
||||
|
||||
await grantService.grantAlbum({
|
||||
editorialId,
|
||||
immichAlbumId: regrantAlbumId,
|
||||
adminUserId,
|
||||
});
|
||||
await grantService.grantAlbum({
|
||||
editorialId,
|
||||
immichAlbumId: regrantAlbumId,
|
||||
adminUserId,
|
||||
});
|
||||
expect(await activeGrantRows(editorialId, regrantAlbumId)).toEqual([
|
||||
{
|
||||
id: "30000000-0000-4000-8000-000000000001",
|
||||
immich_album_id: regrantAlbumId,
|
||||
},
|
||||
]);
|
||||
await expect(auditCount(editorialId, regrantAlbumId, "album_granted")).resolves.toBe(1);
|
||||
|
||||
await testDb.applyRemainingMigrations();
|
||||
|
||||
expect(await allGrantRows(editorialId, migrationAlbumId)).toEqual([
|
||||
{
|
||||
id: "50000000-0000-4000-8000-000000000001",
|
||||
immich_album_id: migrationAlbumId.toUpperCase(),
|
||||
revoked: true,
|
||||
},
|
||||
{
|
||||
id: "50000000-0000-4000-8000-000000000002",
|
||||
immich_album_id: migrationAlbumId,
|
||||
revoked: false,
|
||||
},
|
||||
]);
|
||||
|
||||
await expect(
|
||||
testDb.pool.query(
|
||||
`insert into album_grants (
|
||||
editorial_account_id, immich_album_id, granted_by_user_id
|
||||
) values ($1, $2, $3)`,
|
||||
[editorialId, migrationAlbumId.toUpperCase(), adminUserId],
|
||||
),
|
||||
).rejects.toThrow(/album_grants_editorial_active_album_unique/);
|
||||
await expect(
|
||||
testDb.pool.query(
|
||||
`insert into album_grants (
|
||||
editorial_account_id, immich_album_id, revoked_at, granted_by_user_id
|
||||
) values ($1, $2, now(), $3)`,
|
||||
[editorialId, migrationAlbumId.toUpperCase(), adminUserId],
|
||||
),
|
||||
).resolves.toBeDefined();
|
||||
await expect(
|
||||
testDb.pool.query(
|
||||
"insert into album_grants (editorial_account_id, immich_album_id) values ($1, $2)",
|
||||
[editorialId, "new-invalid-album"],
|
||||
),
|
||||
).rejects.toThrow(/album_grants_immich_album_id_uuid_check/);
|
||||
await expect(
|
||||
testDb.pool.query(
|
||||
"update album_grants set revoked_at = revoked_at where id = $1",
|
||||
["50000000-0000-4000-8000-000000000001"],
|
||||
),
|
||||
).resolves.toBeDefined();
|
||||
});
|
||||
|
||||
type GrantSeed = {
|
||||
id: string;
|
||||
albumId: string;
|
||||
grantedAt: Date;
|
||||
revokedAt: Date | null;
|
||||
};
|
||||
|
||||
async function insertGrantRows(editorialId: string, adminUserId: string, rows: GrantSeed[]) {
|
||||
for (const row of rows) {
|
||||
await testDb.pool.query(
|
||||
`insert into album_grants (
|
||||
id, editorial_account_id, immich_album_id, granted_at, revoked_at, granted_by_user_id
|
||||
) values ($1, $2, $3, $4, $5, $6)`,
|
||||
[row.id, editorialId, row.albumId, row.grantedAt, row.revokedAt, adminUserId],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function activeGrantRows(editorialId: string, albumId: string) {
|
||||
const result = await testDb.pool.query<{
|
||||
id: string;
|
||||
immich_album_id: string;
|
||||
}>(
|
||||
`select id, immich_album_id
|
||||
from album_grants
|
||||
where editorial_account_id = $1 and lower(immich_album_id) = $2 and revoked_at is null
|
||||
order by id`,
|
||||
[editorialId, albumId],
|
||||
);
|
||||
return result.rows;
|
||||
}
|
||||
|
||||
async function allGrantRows(editorialId: string, albumId: string) {
|
||||
const result = await testDb.pool.query<{
|
||||
id: string;
|
||||
immich_album_id: string;
|
||||
revoked: boolean;
|
||||
}>(
|
||||
`select id, immich_album_id, revoked_at is not null as revoked
|
||||
from album_grants
|
||||
where editorial_account_id = $1 and lower(immich_album_id) = $2
|
||||
order by id`,
|
||||
[editorialId, albumId],
|
||||
);
|
||||
return result.rows;
|
||||
}
|
||||
|
||||
async function auditCount(editorialId: string, albumId: string, eventType: string) {
|
||||
const result = await testDb.pool.query<{ count: string }>(
|
||||
`select count(*)::text as count
|
||||
from audit_events
|
||||
where event_type = $1
|
||||
and entity_id = $2
|
||||
and new_values->>'editorialId' = $3`,
|
||||
[eventType, albumId, editorialId],
|
||||
);
|
||||
return Number(result.rows[0]?.count ?? 0);
|
||||
}
|
||||
Reference in new issue
Block a user