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
@@ -4,6 +4,7 @@ import { eq } from "drizzle-orm";
|
||||
import { afterAll, expect, test, vi } from "vitest";
|
||||
|
||||
import { downloadEvents } from "@/db/schema";
|
||||
import { createAlbumGrantRepository, createAlbumGrantService } from "@/modules/albums/grants";
|
||||
import { createUsageService, createUsageServiceRepository } from "@/modules/usages/service";
|
||||
import { requestFingerprint } from "@/modules/usages/types";
|
||||
import { startPostgres } from "./postgres";
|
||||
@@ -17,6 +18,8 @@ const validAlbumId = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
|
||||
const cleanKey = "cccccccc-cccc-4ccc-8ccc-cccccccccccc";
|
||||
const collidingKey = "dddddddd-dddd-4ddd-8ddd-dddddddddddd";
|
||||
const authorizedAt = new Date("2026-08-09T08:00:00.000Z");
|
||||
const repeatAt = new Date("2026-08-09T08:01:00.000Z");
|
||||
const laterNullAt = new Date("2026-08-09T08:02:00.000Z");
|
||||
|
||||
async function insertLegacyUsage({
|
||||
editorialId,
|
||||
@@ -85,8 +88,8 @@ test("0002 preserves dirty rows and reconstructs only provable legacy intents",
|
||||
);
|
||||
await testDb.pool.query(
|
||||
`insert into album_grants (editorial_account_id, immich_album_id)
|
||||
values ($1, $2), ($1, $3)`,
|
||||
[editorialId, validAlbumId.toUpperCase(), "legacy album / 2019"],
|
||||
values ($1, $2), ($1, $3), ($1, $4)`,
|
||||
[editorialId, validAlbumId.toUpperCase(), "legacy album / 2019", validAlbumId],
|
||||
);
|
||||
|
||||
const cleanUsageId = await insertLegacyUsage({
|
||||
@@ -101,6 +104,48 @@ test("0002 preserves dirty rows and reconstructs only provable legacy intents",
|
||||
idempotencyKey: cleanKey.toUpperCase(),
|
||||
});
|
||||
|
||||
const repeatUsageId = await insertLegacyUsage({ editorialId });
|
||||
const repeatInitialEventId = await insertLegacyEvent({ editorialId, usageId: repeatUsageId });
|
||||
const provableRepeatEventId = await insertLegacyEvent({
|
||||
editorialId,
|
||||
usageId: repeatUsageId,
|
||||
note: "Web-Ausgabe",
|
||||
at: repeatAt,
|
||||
});
|
||||
const laterNullEventId = await insertLegacyEvent({
|
||||
editorialId,
|
||||
usageId: repeatUsageId,
|
||||
at: laterNullAt,
|
||||
});
|
||||
|
||||
const tabPurposeUsageId = await insertLegacyUsage({
|
||||
editorialId,
|
||||
purpose: "\tTitelgeschichte",
|
||||
});
|
||||
const tabPurposeEventId = await insertLegacyEvent({
|
||||
editorialId,
|
||||
usageId: tabPurposeUsageId,
|
||||
});
|
||||
const newlineReferenceUsageId = await insertLegacyUsage({
|
||||
editorialId,
|
||||
reference: "Ausgabe 8/26\n",
|
||||
});
|
||||
const newlineReferenceEventId = await insertLegacyEvent({
|
||||
editorialId,
|
||||
usageId: newlineReferenceUsageId,
|
||||
});
|
||||
const nbspNoteUsageId = await insertLegacyUsage({ editorialId });
|
||||
const nbspNoteInitialEventId = await insertLegacyEvent({
|
||||
editorialId,
|
||||
usageId: nbspNoteUsageId,
|
||||
});
|
||||
const nbspNoteEventId = await insertLegacyEvent({
|
||||
editorialId,
|
||||
usageId: nbspNoteUsageId,
|
||||
note: "\u00a0Web-Ausgabe",
|
||||
at: repeatAt,
|
||||
});
|
||||
|
||||
const dirtyAssetId = "legacy-asset/not-a-uuid";
|
||||
const dirtyAlbumId = "legacy album / 2019";
|
||||
const dirtyUsageId = await insertLegacyUsage({
|
||||
@@ -190,7 +235,7 @@ test("0002 preserves dirty rows and reconstructs only provable legacy intents",
|
||||
`select id, idempotency_key, immich_asset_id, intent_kind, request_fingerprint
|
||||
from download_events`,
|
||||
);
|
||||
expect(migratedEvents.rows).toHaveLength(7);
|
||||
expect(migratedEvents.rows).toHaveLength(14);
|
||||
const byId = new Map(migratedEvents.rows.map((row) => [row.id, row]));
|
||||
expect(byId.get(cleanEventId)).toMatchObject({
|
||||
idempotency_key: cleanKey,
|
||||
@@ -210,7 +255,21 @@ test("0002 preserves dirty rows and reconstructs only provable legacy intents",
|
||||
immich_asset_id: dirtyAssetId,
|
||||
intent_kind: "legacy",
|
||||
});
|
||||
expect(byId.get(repeatInitialEventId)?.intent_kind).toBe("new_usage");
|
||||
expect(byId.get(provableRepeatEventId)).toMatchObject({
|
||||
intent_kind: "repeat_download",
|
||||
request_fingerprint: requestFingerprint({
|
||||
kind: "repeat_download",
|
||||
editorialId,
|
||||
usageId: repeatUsageId,
|
||||
note: "Web-Ausgabe",
|
||||
}),
|
||||
});
|
||||
for (const id of [
|
||||
laterNullEventId,
|
||||
tabPurposeEventId,
|
||||
newlineReferenceEventId,
|
||||
nbspNoteEventId,
|
||||
...equalTimeEventIds,
|
||||
contradictoryEventId,
|
||||
...collisionEventIds,
|
||||
@@ -218,6 +277,7 @@ test("0002 preserves dirty rows and reconstructs only provable legacy intents",
|
||||
expect(byId.get(id)?.intent_kind).toBe("legacy");
|
||||
expect(byId.get(id)?.request_fingerprint).toMatch(/^[0-9a-f]{64}$/);
|
||||
}
|
||||
expect(byId.get(nbspNoteInitialEventId)?.intent_kind).toBe("new_usage");
|
||||
expect(collisionEventIds.map((id) => byId.get(id)?.idempotency_key).sort()).toEqual(
|
||||
[collidingKey.toUpperCase(), collidingKey].sort(),
|
||||
);
|
||||
@@ -236,6 +296,60 @@ test("0002 preserves dirty rows and reconstructs only provable legacy intents",
|
||||
);
|
||||
expect(dirtyGrant.rows).toEqual([{ immich_album_id: dirtyAlbumId }]);
|
||||
|
||||
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_canonical_immich_album_id_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/);
|
||||
|
||||
const grantService = createAlbumGrantService({
|
||||
repository: createAlbumGrantRepository(testDb.database),
|
||||
immich: {
|
||||
getAlbum: vi.fn().mockResolvedValue({
|
||||
id: validAlbumId,
|
||||
albumName: "Legacy Album",
|
||||
description: null,
|
||||
albumThumbnailAssetId: null,
|
||||
assetCount: 0,
|
||||
assets: [],
|
||||
}),
|
||||
listAlbums: vi.fn(),
|
||||
},
|
||||
});
|
||||
await grantService.revokeAlbum({
|
||||
editorialId,
|
||||
immichAlbumId: validAlbumId,
|
||||
adminUserId: userId,
|
||||
});
|
||||
let canonicalGrantRows = await testDb.pool.query<{
|
||||
immich_album_id: string;
|
||||
revoked_at: Date | null;
|
||||
}>(
|
||||
"select immich_album_id, revoked_at from album_grants where lower(immich_album_id) = $1",
|
||||
[validAlbumId],
|
||||
);
|
||||
expect(canonicalGrantRows.rows).toHaveLength(2);
|
||||
expect(canonicalGrantRows.rows.every((row) => row.immich_album_id === validAlbumId)).toBe(true);
|
||||
expect(canonicalGrantRows.rows.every((row) => row.revoked_at !== null)).toBe(true);
|
||||
|
||||
await grantService.grantAlbum({
|
||||
editorialId,
|
||||
immichAlbumId: validAlbumId,
|
||||
adminUserId: userId,
|
||||
});
|
||||
canonicalGrantRows = await testDb.pool.query(
|
||||
"select immich_album_id, revoked_at from album_grants where lower(immich_album_id) = $1",
|
||||
[validAlbumId],
|
||||
);
|
||||
expect(canonicalGrantRows.rows.filter((row) => row.revoked_at === null)).toHaveLength(1);
|
||||
|
||||
await expect(
|
||||
usageService.createNewUsage({
|
||||
editorialId,
|
||||
|
||||
Reference in new issue
Block a user