Secure Sakani portal and show full album titles

This commit is contained in:
Leon Tonhäuser committed 2026-11-30 19:14:57 +01:00
1 parent c40ccf77cf
commit 69264ce165
49 files changed
+1362 -601

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
import { eq } from "drizzle-orm";
import { afterAll, expect, test } from "vitest";
import { users } from "@/db/schema";
import { insertAdminCredential, seedAdmin } from "@/modules/auth/admin-bootstrap";
import { startPostgres } from "./postgres";
const testDb = await startPostgres();
afterAll(() => testDb.stop());
const input = { email: "existing@example.test", name: "Admin", password: "dummy seed password" };
const seed = (email: string) =>
seedAdmin({ ...input, email }, (credential) => insertAdminCredential(testDb.database, credential));
test("fails for an existing address without changing its role or verification", async () => {
const [{ id }] = await testDb.database.insert(users)
.values({ email: "Existing@example.test", name: "Editorial" })
.returning({ id: users.id });
await expect(seed(input.email)).rejects.toThrow("email is already registered");
const [account] = await testDb.database.select().from(users).where(eq(users.id, id));
expect(account).toMatchObject({ role: "EDITORIAL", emailVerified: false, name: "Editorial" });
});
test("rolls back the admin user when its credential insert fails", async () => {
const email = "rollback@example.test";
await testDb.pool.query("alter table account add constraint test_reject_credential check (provider_id <> 'credential')");
try {
await expect(seed(email)).rejects.toThrow();
const found = await testDb.database.select().from(users).where(eq(users.email, email));
expect(found).toEqual([]);
} finally {
await testDb.pool.query("alter table account drop constraint test_reject_credential");
}
});
+4 -4
View File
@@ -66,7 +66,7 @@ test("a no-byte new failure retains the attempt and voids its billable usage", a
});
const repeats = createDownloadService({
repository: createRepeatDownloadRepository(testDb.database),
repository: createRepeatDownloadRepository(testDb.database, async () => undefined),
});
await expect(
repeats.createRepeatDownload({
@@ -80,7 +80,7 @@ test("a no-byte new failure retains the attempt and voids its billable usage", a
test("rejects a free repeat until a new-usage original delivered bytes", async () => {
const seed = await testDb.seedEditorialWithUsage();
const repeats = createDownloadService({
repository: createRepeatDownloadRepository(testDb.database),
repository: createRepeatDownloadRepository(testDb.database, async () => undefined),
});
await expect(
@@ -106,7 +106,7 @@ test.each(["completed", "interrupted"] as const)(
await delivery.markInterrupted(originalId, 80);
}
const repeats = createDownloadService({
repository: createRepeatDownloadRepository(testDb.database),
repository: createRepeatDownloadRepository(testDb.database, async () => undefined),
});
const repeat = await repeats.createRepeatDownload({
@@ -136,7 +136,7 @@ test("a later new-use void invalidates a pre-authorized eligible repeat", async
await delivery.markCompleted(originalId, 120);
await delivery.claimAuthorized(seed.editorialId, failingNewId);
const repeats = createDownloadService({
repository: createRepeatDownloadRepository(testDb.database),
repository: createRepeatDownloadRepository(testDb.database, async () => undefined),
});
const repeatKey = randomUUID();
const repeatInput = {
+43 -10
View File
@@ -20,6 +20,7 @@ import {
createDownloadService,
createRepeatDownloadRepository,
} from "@/modules/downloads/service";
import { assertOriginalAccess } from "@/modules/albums/photo-access";
import { requestFingerprint } from "@/modules/usages/types";
import { startPostgres } from "./postgres";
@@ -59,7 +60,8 @@ function createServices() {
immich,
}),
downloadService: createDownloadService({
repository: createRepeatDownloadRepository(database),
repository: createRepeatDownloadRepository(database, (editorialId, assetId, albumId, tx) =>
assertOriginalAccess(editorialId, assetId, albumId, tx, immich)),
}),
immich,
};
@@ -240,7 +242,7 @@ test("rechecks exact album membership after database locks before inserting", as
expect(usageCount.value).toBe(0);
});
test("returns a winner committed while waiting before later mutable state is checked", async () => {
test("rejects an otherwise equivalent winner when the editorial is suspended while waiting", async () => {
const editorialId = await seedEditorial();
const input = newInput(editorialId);
const immich = createServices().immich;
@@ -267,7 +269,7 @@ test("returns a winner committed while waiting before later mutable state is che
try {
const pending = usageService.createNewUsage(input);
await fastMiss;
const winner = await insertWinner(
await insertWinner(
locker,
input,
requestFingerprint({
@@ -284,12 +286,7 @@ test("returns a winner committed while waiting before later mutable state is che
]);
await locker.query("commit");
await expect(pending).resolves.toMatchObject({
id: winner.eventId,
usageId: winner.usageId,
authorizedAt: winner.authorizedAt,
billable: true,
});
await expect(pending).rejects.toMatchObject({ code: "EDITORIAL_NOT_ACTIVE" });
expect(immich.getAlbum).toHaveBeenCalledTimes(1);
expect(immich.getAsset).toHaveBeenCalledTimes(1);
} finally {
@@ -494,7 +491,7 @@ test("rechecks suspension after an idempotent repeat winner commits", async () =
idempotencyKey: randomUUID(),
};
const downloadService = createDownloadService({
repository: createRepeatDownloadRepository(database),
repository: createRepeatDownloadRepository(database, async () => undefined),
});
const eventId = randomUUID();
const authorizedAt = new Date("2026-08-09T11:00:00.000Z");
@@ -584,3 +581,39 @@ async function markDelivered(downloadId: string): Promise<void> {
})
.where(eq(downloadEvents.id, downloadId));
}
test("revocation blocks an idempotent new intent and an eligible repeat while retaining the usage", async () => {
const editorialId = await seedEditorial();
const { usageService, downloadService } = createServices();
const input = newInput(editorialId);
const intent = await usageService.createNewUsage(input);
await database.update(downloadEvents).set({ status: "completed", transferredBytes: 8 })
.where(eq(downloadEvents.id, intent.id));
const repeatInput = { editorialId, usageId: intent.usageId, idempotencyKey: randomUUID() };
await expect(downloadService.createRepeatDownload(repeatInput)).resolves.toMatchObject({ priceCents: 0 });
await database.update(albumGrants).set({ revokedAt: new Date() })
.where(eq(albumGrants.editorialAccountId, editorialId));
await expect(usageService.createNewUsage(input)).rejects.toMatchObject({ code: "ALBUM_NOT_GRANTED" });
await expect(downloadService.createRepeatDownload(repeatInput)).rejects.toMatchObject({ code: "ALBUM_NOT_GRANTED" });
await expect(usageService.listPriorUsages(editorialId, assetId)).resolves.toEqual([
expect.objectContaining({ id: intent.usageId, purpose: input.purpose }),
]);
});
test("calculates ZIP pricing server-side and refuses a price above the confirmed cap", async () => {
const editorialId = await seedEditorial({ priceCents: 1500 });
const { usageService } = createServices();
const input = newInput(editorialId);
await expect(usageService.createNewUsage({ ...input, maxPriceCents: 1499 }))
.rejects.toMatchObject({ code: "INVALID_PRICE" });
await database.update(editorialAccounts).set({
flatrateActive: true, flatratePriceCents: 10000, flatrateAssetLimit: 1,
}).where(eq(editorialAccounts.id, editorialId));
await expect(usageService.createNewUsage({ ...input, maxPriceCents: 1499 }))
.resolves.toMatchObject({ priceCents: 0 });
await expect(usageService.createNewUsage({ ...newInput(editorialId), maxPriceCents: 1500 }))
.resolves.toMatchObject({ priceCents: 1500 });
});