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
@@ -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