Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AttachmentUpload.test.ts

128 lines5.1 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodePath from "node:path";
4​
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import { describe, expect, it } from "@effect/vitest";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as TestClock from "effect/testing/TestClock";
10​
11import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
12import * as ServerConfig from "../config.ts";
13import { parseThreadSegmentFromAttachmentId } from "../attachmentStore.ts";
14import {
15 ATTACHMENT_UPLOAD_ROUTE_PREFIX,
16 deletePendingAttachment,
17 issueAttachmentUploadUrl,
18 storeAttachmentUpload,
19 validateAttachmentUploadToken,
20} from "./AttachmentUpload.ts";
21​
22const testLayer = ServerSecretStore.layer.pipe(
23 Layer.provideMerge(ServerConfig.layerTest(process.cwd(), { prefix: "t3-attachment-upload-" })),
24 Layer.provideMerge(NodeServices.layer),
25);
26​
27const uploadInput = {
28 name: "screenshot.png",
29 mimeType: "image/png",
30 sizeBytes: 6,
31} as const;
32​
33describe("AttachmentUpload", () => {
34 it.effect("signs the attachment metadata and validates the upload token", () =>
35 Effect.gen(function* () {
36 const issued = yield* issueAttachmentUploadUrl(uploadInput);
37 expect(parseThreadSegmentFromAttachmentId(issued.attachmentId)).toBe("pending");
38​
39 const token = issued.relativeUrl.slice(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/`.length);
40 expect(yield* validateAttachmentUploadToken(token)).toMatchObject({
41 kind: "attachment-upload",
42 attachmentId: issued.attachmentId,
43 name: "screenshot.png",
44 mimeType: "image/png",
45 sizeBytes: 6,
46 });
47 }).pipe(Effect.provide(testLayer)),
48 );
49​
50 it.effect("rejects tampered and malformed upload tokens", () =>
51 Effect.gen(function* () {
52 const issued = yield* issueAttachmentUploadUrl(uploadInput);
53 const token = issued.relativeUrl.slice(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/`.length);
54 const [payload, signature] = token.split(".");
55​
56 expect(yield* validateAttachmentUploadToken(`${payload}x.${signature}`)).toBeNull();
57 expect(yield* validateAttachmentUploadToken(`${token}.extra`)).toBeNull();
58 expect(yield* validateAttachmentUploadToken("garbage")).toBeNull();
59 }).pipe(Effect.provide(testLayer)),
60 );
61​
62 it.effect("rejects expired upload tokens", () =>
63 Effect.gen(function* () {
64 const issued = yield* issueAttachmentUploadUrl(uploadInput);
65 const token = issued.relativeUrl.slice(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/`.length);
66​
67 yield* TestClock.adjust("11 minutes");
68 expect(yield* validateAttachmentUploadToken(token)).toBeNull();
69 }).pipe(Effect.provide(testLayer)),
70 );
71​
72 it.effect("removes expired pending uploads while issuing a new upload URL", () =>
73 Effect.gen(function* () {
74 const config = yield* ServerConfig.ServerConfig;
75 const staleId = "pending-00000000-0000-4000-8000-0000000000cc";
76 const stalePath = NodePath.join(config.attachmentsDir, `${staleId}.png`);
77 NodeFS.writeFileSync(stalePath, Buffer.from("pixels"));
78 NodeFS.utimesSync(stalePath, 0, 0);
79​
80 yield* TestClock.adjust("25 hours");
81 yield* issueAttachmentUploadUrl(uploadInput);
82​
83 expect(NodeFS.existsSync(stalePath)).toBe(false);
84 }).pipe(Effect.provide(testLayer)),
85 );
86​
87 it.effect("stores the expected bytes without leaving temporary files", () =>
88 Effect.gen(function* () {
89 const config = yield* ServerConfig.ServerConfig;
90 const issued = yield* issueAttachmentUploadUrl(uploadInput);
91 const token = issued.relativeUrl.slice(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/`.length);
92 const claims = yield* validateAttachmentUploadToken(token);
93 if (!claims) {
94 throw new Error("Expected valid upload claims.");
95 }
96​
97 expect(yield* storeAttachmentUpload(claims, new Uint8Array([1, 2, 3]))).toMatchObject({
98 ok: false,
99 status: 400,
100 });
101 expect(yield* storeAttachmentUpload(claims, new Uint8Array(6))).toEqual({ ok: true });
102 expect(
103 NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${issued.attachmentId}.png`)),
104 ).toBe(true);
105 expect(
106 NodeFS.readdirSync(config.attachmentsDir).filter((entry) => entry.endsWith(".part")),
107 ).toEqual([]);
108 }).pipe(Effect.provide(testLayer)),
109 );
110​
111 it.effect("deletes pending uploads without deleting thread-owned copies", () =>
112 Effect.gen(function* () {
113 const config = yield* ServerConfig.ServerConfig;
114 const uuid = "00000000-0000-4000-8000-0000000000dd";
115 const pendingPath = NodePath.join(config.attachmentsDir, `pending-${uuid}.png`);
116 const claimedPath = NodePath.join(config.attachmentsDir, `thread-1-${uuid}.png`);
117 NodeFS.writeFileSync(pendingPath, Buffer.from("pixels"));
118 NodeFS.writeFileSync(claimedPath, Buffer.from("pixels"));
119​
120 yield* deletePendingAttachment(`pending-${uuid}`);
121 yield* deletePendingAttachment(`pending-${uuid}`);
122 yield* deletePendingAttachment(`thread-1-${uuid}`);
123​
124 expect(NodeFS.existsSync(pendingPath)).toBe(false);
125 expect(NodeFS.existsSync(claimedPath)).toBe(true);
126 }).pipe(Effect.provide(testLayer)),
127 );
128});