@terreno/admin-backend 0.10.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,874 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ var bun_test_1 = require("bun:test");
43
+ var node_stream_1 = require("node:stream");
44
+ var bucketBehavior = {};
45
+ bun_test_1.mock.module("@google-cloud/storage", function () {
46
+ var Storage = /** @class */ (function () {
47
+ function Storage() {
48
+ }
49
+ Storage.prototype.bucket = function (name) {
50
+ var _this = this;
51
+ var _a;
52
+ void name;
53
+ return {
54
+ file: function (path) {
55
+ if (bucketBehavior.fileFactory) {
56
+ return bucketBehavior.fileFactory(path);
57
+ }
58
+ return {
59
+ createReadStream: (0, bun_test_1.mock)(function () {
60
+ var stream = new node_stream_1.PassThrough();
61
+ setImmediate(function () {
62
+ stream.end("filecontent");
63
+ });
64
+ return stream;
65
+ }),
66
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
67
+ return [2 /*return*/, [{}]];
68
+ }); }); }),
69
+ getMetadata: (0, bun_test_1.mock)(function () { return __awaiter(_this, void 0, void 0, function () {
70
+ return __generator(this, function (_a) {
71
+ return [2 /*return*/, [
72
+ { contentType: "application/pdf", etag: "etag", size: 11, updated: "now" },
73
+ ]];
74
+ });
75
+ }); }),
76
+ metadata: {},
77
+ name: path,
78
+ save: (0, bun_test_1.mock)(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
79
+ return [2 /*return*/, undefined];
80
+ }); }); }),
81
+ };
82
+ },
83
+ getFiles: (_a = bucketBehavior.getFiles) !== null && _a !== void 0 ? _a : (0, bun_test_1.mock)(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
84
+ return [2 /*return*/, [[], null, { prefixes: [] }]];
85
+ }); }); }),
86
+ };
87
+ };
88
+ return Storage;
89
+ }());
90
+ return { Storage: Storage };
91
+ });
92
+ var api_1 = require("@terreno/api");
93
+ var tests_1 = require("@terreno/api/src/tests");
94
+ var supertest_1 = __importDefault(require("supertest"));
95
+ var documentStorageApp_1 = require("./documentStorageApp");
96
+ var buildApp = function (options) {
97
+ var app = (0, tests_1.getBaseServer)();
98
+ (0, api_1.setupAuth)(app, tests_1.UserModel);
99
+ (0, api_1.addAuthRoutes)(app, tests_1.UserModel);
100
+ new documentStorageApp_1.DocumentStorageApp(options).register(app);
101
+ app.use(api_1.apiUnauthorizedMiddleware);
102
+ app.use(api_1.apiErrorMiddleware);
103
+ return app;
104
+ };
105
+ (0, bun_test_1.describe)("DocumentStorageApp", function () {
106
+ var app;
107
+ var adminAgent;
108
+ var notAdminAgent;
109
+ (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
110
+ return __generator(this, function (_a) {
111
+ switch (_a.label) {
112
+ case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
113
+ case 1:
114
+ _a.sent();
115
+ bucketBehavior.getFiles = undefined;
116
+ bucketBehavior.fileFactory = undefined;
117
+ app = buildApp({ bucketName: "test-bucket", folderPrefix: "tenant/" });
118
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
119
+ case 2:
120
+ adminAgent = _a.sent();
121
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
122
+ case 3:
123
+ notAdminAgent = _a.sent();
124
+ return [2 /*return*/];
125
+ }
126
+ });
127
+ }); });
128
+ (0, bun_test_1.afterEach)(function () {
129
+ bucketBehavior.getFiles = undefined;
130
+ bucketBehavior.fileFactory = undefined;
131
+ });
132
+ (0, bun_test_1.describe)("GET /documents/", function () {
133
+ (0, bun_test_1.it)("lists files and folders", function () { return __awaiter(void 0, void 0, void 0, function () {
134
+ var res;
135
+ return __generator(this, function (_a) {
136
+ switch (_a.label) {
137
+ case 0:
138
+ bucketBehavior.getFiles = (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () {
139
+ return __generator(this, function (_a) {
140
+ return [2 /*return*/, [
141
+ [
142
+ {
143
+ metadata: { contentType: "application/pdf", size: "2048", updated: "2024-01-01" },
144
+ name: "tenant/report.pdf",
145
+ },
146
+ {
147
+ metadata: { contentType: "image/png", size: "500", updated: "2024-01-02" },
148
+ name: "tenant/logo.png",
149
+ },
150
+ // Should be excluded because it equals fullPrefix
151
+ { metadata: { size: "0", updated: "" }, name: "tenant/" },
152
+ ],
153
+ null,
154
+ { prefixes: ["tenant/archive/", "tenant/photos/"] },
155
+ ]];
156
+ });
157
+ }); });
158
+ return [4 /*yield*/, adminAgent.get("/documents/").expect(200)];
159
+ case 1:
160
+ res = _a.sent();
161
+ (0, bun_test_1.expect)(res.body.files).toHaveLength(2);
162
+ (0, bun_test_1.expect)(res.body.files[0].name).toBe("report.pdf");
163
+ (0, bun_test_1.expect)(res.body.files[0].fullPath).toBe("report.pdf");
164
+ (0, bun_test_1.expect)(res.body.files[0].size).toBe(2048);
165
+ (0, bun_test_1.expect)(res.body.folders).toEqual(["archive/", "photos/"]);
166
+ (0, bun_test_1.expect)(res.body.prefix).toBe("");
167
+ return [2 /*return*/];
168
+ }
169
+ });
170
+ }); });
171
+ (0, bun_test_1.it)("supports a prefix query param", function () { return __awaiter(void 0, void 0, void 0, function () {
172
+ var res;
173
+ return __generator(this, function (_a) {
174
+ switch (_a.label) {
175
+ case 0:
176
+ bucketBehavior.getFiles = (0, bun_test_1.mock)(function (opts) { return __awaiter(void 0, void 0, void 0, function () {
177
+ return __generator(this, function (_a) {
178
+ (0, bun_test_1.expect)(opts.prefix).toBe("tenant/archive/");
179
+ return [2 /*return*/, [[], null, { prefixes: [] }]];
180
+ });
181
+ }); });
182
+ return [4 /*yield*/, adminAgent.get("/documents/?prefix=archive/").expect(200)];
183
+ case 1:
184
+ res = _a.sent();
185
+ (0, bun_test_1.expect)(res.body.prefix).toBe("archive/");
186
+ return [2 /*return*/];
187
+ }
188
+ });
189
+ }); });
190
+ (0, bun_test_1.it)("returns empty folders array when no prefixes present", function () { return __awaiter(void 0, void 0, void 0, function () {
191
+ var res;
192
+ return __generator(this, function (_a) {
193
+ switch (_a.label) {
194
+ case 0:
195
+ bucketBehavior.getFiles = (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
196
+ return [2 /*return*/, [[], null, undefined]];
197
+ }); }); });
198
+ return [4 /*yield*/, adminAgent.get("/documents/").expect(200)];
199
+ case 1:
200
+ res = _a.sent();
201
+ (0, bun_test_1.expect)(res.body.folders).toEqual([]);
202
+ (0, bun_test_1.expect)(res.body.files).toEqual([]);
203
+ return [2 /*return*/];
204
+ }
205
+ });
206
+ }); });
207
+ (0, bun_test_1.it)("returns 403 for non-admins", function () { return __awaiter(void 0, void 0, void 0, function () {
208
+ return __generator(this, function (_a) {
209
+ switch (_a.label) {
210
+ case 0: return [4 /*yield*/, notAdminAgent.get("/documents/").expect(403)];
211
+ case 1:
212
+ _a.sent();
213
+ return [2 /*return*/];
214
+ }
215
+ });
216
+ }); });
217
+ (0, bun_test_1.it)("returns 401 when unauthenticated", function () { return __awaiter(void 0, void 0, void 0, function () {
218
+ return __generator(this, function (_a) {
219
+ switch (_a.label) {
220
+ case 0: return [4 /*yield*/, (0, supertest_1.default)(app).get("/documents/").expect(401)];
221
+ case 1:
222
+ _a.sent();
223
+ return [2 /*return*/];
224
+ }
225
+ });
226
+ }); });
227
+ });
228
+ (0, bun_test_1.describe)("POST /documents/ (upload)", function () {
229
+ (0, bun_test_1.it)("rejects when no file is provided", function () { return __awaiter(void 0, void 0, void 0, function () {
230
+ var res;
231
+ return __generator(this, function (_a) {
232
+ switch (_a.label) {
233
+ case 0: return [4 /*yield*/, adminAgent.post("/documents/").expect(400)];
234
+ case 1:
235
+ res = _a.sent();
236
+ (0, bun_test_1.expect)(res.body.title).toInclude("No file provided");
237
+ return [2 /*return*/];
238
+ }
239
+ });
240
+ }); });
241
+ (0, bun_test_1.it)("sanitizes filenames and uploads to the bucket", function () { return __awaiter(void 0, void 0, void 0, function () {
242
+ var saved, res;
243
+ var _a;
244
+ return __generator(this, function (_b) {
245
+ switch (_b.label) {
246
+ case 0:
247
+ saved = {};
248
+ bucketBehavior.fileFactory = function (path) {
249
+ return ({
250
+ createReadStream: (0, bun_test_1.mock)(function () { return new node_stream_1.PassThrough(); }),
251
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
252
+ return [2 /*return*/, [{}]];
253
+ }); }); }),
254
+ getMetadata: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
255
+ return [2 /*return*/, [{}]];
256
+ }); }); }),
257
+ metadata: {},
258
+ name: path,
259
+ save: (0, bun_test_1.mock)(function (body) { return __awaiter(void 0, void 0, void 0, function () {
260
+ return __generator(this, function (_a) {
261
+ saved.path = path;
262
+ saved.body = body;
263
+ return [2 /*return*/];
264
+ });
265
+ }); }),
266
+ });
267
+ };
268
+ return [4 /*yield*/, adminAgent
269
+ .post("/documents/")
270
+ .attach("file", Buffer.from("hello"), {
271
+ contentType: "application/pdf",
272
+ filename: "my doc!.pdf",
273
+ })
274
+ .field("prefix", "archive/")
275
+ .expect(200)];
276
+ case 1:
277
+ res = _b.sent();
278
+ (0, bun_test_1.expect)(res.body.name).toBe("my_doc_.pdf");
279
+ (0, bun_test_1.expect)(res.body.fullPath).toBe("archive/my_doc_.pdf");
280
+ (0, bun_test_1.expect)(saved.path).toBe("tenant/archive/my_doc_.pdf");
281
+ (0, bun_test_1.expect)((_a = saved.body) === null || _a === void 0 ? void 0 : _a.toString()).toBe("hello");
282
+ return [2 /*return*/];
283
+ }
284
+ });
285
+ }); });
286
+ (0, bun_test_1.it)("rejects disallowed mime types via multer fileFilter", function () { return __awaiter(void 0, void 0, void 0, function () {
287
+ return __generator(this, function (_a) {
288
+ switch (_a.label) {
289
+ case 0: return [4 /*yield*/, adminAgent
290
+ .post("/documents/")
291
+ .attach("file", Buffer.from("exec"), {
292
+ contentType: "application/x-sh",
293
+ filename: "bad.sh",
294
+ })
295
+ .expect(400)];
296
+ case 1:
297
+ _a.sent();
298
+ return [2 /*return*/];
299
+ }
300
+ });
301
+ }); });
302
+ (0, bun_test_1.it)("uses options.allowedMimeTypes when provided", function () { return __awaiter(void 0, void 0, void 0, function () {
303
+ var agent;
304
+ return __generator(this, function (_a) {
305
+ switch (_a.label) {
306
+ case 0:
307
+ app = buildApp({
308
+ allowedMimeTypes: ["text/plain"],
309
+ bucketName: "test-bucket",
310
+ });
311
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
312
+ case 1:
313
+ agent = _a.sent();
314
+ return [4 /*yield*/, agent
315
+ .post("/documents/")
316
+ .attach("file", Buffer.from("pdf"), {
317
+ contentType: "application/pdf",
318
+ filename: "x.pdf",
319
+ })
320
+ .expect(400)];
321
+ case 2:
322
+ _a.sent();
323
+ return [4 /*yield*/, agent
324
+ .post("/documents/")
325
+ .attach("file", Buffer.from("text"), {
326
+ contentType: "text/plain",
327
+ filename: "x.txt",
328
+ })
329
+ .expect(200)];
330
+ case 3:
331
+ _a.sent();
332
+ return [2 /*return*/];
333
+ }
334
+ });
335
+ }); });
336
+ (0, bun_test_1.it)("returns 403 for non-admins", function () { return __awaiter(void 0, void 0, void 0, function () {
337
+ return __generator(this, function (_a) {
338
+ switch (_a.label) {
339
+ case 0: return [4 /*yield*/, notAdminAgent
340
+ .post("/documents/")
341
+ .attach("file", Buffer.from("x"), { contentType: "text/plain", filename: "x.txt" })
342
+ .expect(403)];
343
+ case 1:
344
+ _a.sent();
345
+ return [2 /*return*/];
346
+ }
347
+ });
348
+ }); });
349
+ });
350
+ (0, bun_test_1.describe)("GET /documents/download/*filepath", function () {
351
+ (0, bun_test_1.it)("handles a successful download request by invoking pipeline", function () { return __awaiter(void 0, void 0, void 0, function () {
352
+ var res;
353
+ return __generator(this, function (_a) {
354
+ switch (_a.label) {
355
+ case 0:
356
+ // Use Readable.from so pipeline() sees a well-behaved stream that emits
357
+ // a single buffer and ends cleanly, giving a deterministic 200.
358
+ bucketBehavior.fileFactory = function (path) {
359
+ return ({
360
+ createReadStream: (0, bun_test_1.mock)(function () {
361
+ var stream = new node_stream_1.PassThrough();
362
+ setImmediate(function () {
363
+ stream.end(Buffer.from("DOWNLOADED"));
364
+ });
365
+ return stream;
366
+ }),
367
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
368
+ return [2 /*return*/, [{}]];
369
+ }); }); }),
370
+ getMetadata: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
371
+ return [2 /*return*/, [{ contentType: "text/plain", size: 10, updated: "now" }]];
372
+ }); }); }),
373
+ metadata: {},
374
+ name: path,
375
+ save: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
376
+ return [2 /*return*/, undefined];
377
+ }); }); }),
378
+ });
379
+ };
380
+ return [4 /*yield*/, adminAgent.get("/documents/download/readme.txt").expect(200)];
381
+ case 1:
382
+ res = _a.sent();
383
+ (0, bun_test_1.expect)(res.headers["content-type"]).toInclude("text/plain");
384
+ (0, bun_test_1.expect)(res.headers["content-disposition"]).toInclude("readme.txt");
385
+ (0, bun_test_1.expect)(res.headers["content-length"]).toBe("10");
386
+ (0, bun_test_1.expect)(res.text).toBe("DOWNLOADED");
387
+ return [2 /*return*/];
388
+ }
389
+ });
390
+ }); });
391
+ (0, bun_test_1.it)("defaults content-type to application/octet-stream when not provided", function () { return __awaiter(void 0, void 0, void 0, function () {
392
+ var res;
393
+ return __generator(this, function (_a) {
394
+ switch (_a.label) {
395
+ case 0:
396
+ bucketBehavior.fileFactory = function () {
397
+ return ({
398
+ createReadStream: (0, bun_test_1.mock)(function () {
399
+ var stream = new node_stream_1.PassThrough();
400
+ setImmediate(function () {
401
+ stream.end(Buffer.from("blob"));
402
+ });
403
+ return stream;
404
+ }),
405
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
406
+ return [2 /*return*/, [{}]];
407
+ }); }); }),
408
+ getMetadata: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
409
+ return [2 /*return*/, [{}]];
410
+ }); }); }),
411
+ metadata: {},
412
+ name: "blob",
413
+ save: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
414
+ return [2 /*return*/, undefined];
415
+ }); }); }),
416
+ });
417
+ };
418
+ return [4 /*yield*/, adminAgent.get("/documents/download/some.bin").expect(200)];
419
+ case 1:
420
+ res = _a.sent();
421
+ (0, bun_test_1.expect)(res.headers["content-type"]).toInclude("application/octet-stream");
422
+ // No Content-Length header when size is missing from metadata
423
+ (0, bun_test_1.expect)(res.headers["content-length"]).toBeUndefined();
424
+ return [2 /*return*/];
425
+ }
426
+ });
427
+ }); });
428
+ (0, bun_test_1.it)("returns 404 when metadata lookup fails with code 404", function () { return __awaiter(void 0, void 0, void 0, function () {
429
+ var res;
430
+ return __generator(this, function (_a) {
431
+ switch (_a.label) {
432
+ case 0:
433
+ bucketBehavior.fileFactory = function () {
434
+ return ({
435
+ createReadStream: (0, bun_test_1.mock)(function () { return new node_stream_1.PassThrough(); }),
436
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
437
+ return [2 /*return*/, [{}]];
438
+ }); }); }),
439
+ getMetadata: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () {
440
+ var err;
441
+ return __generator(this, function (_a) {
442
+ err = new Error("not found");
443
+ err.code = 404;
444
+ throw err;
445
+ });
446
+ }); }),
447
+ metadata: {},
448
+ name: "x",
449
+ save: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
450
+ return [2 /*return*/, undefined];
451
+ }); }); }),
452
+ });
453
+ };
454
+ return [4 /*yield*/, adminAgent.get("/documents/download/missing.pdf").expect(404)];
455
+ case 1:
456
+ res = _a.sent();
457
+ (0, bun_test_1.expect)(res.body.title).toInclude("File not found");
458
+ return [2 /*return*/];
459
+ }
460
+ });
461
+ }); });
462
+ (0, bun_test_1.it)("returns 500 for other metadata errors", function () { return __awaiter(void 0, void 0, void 0, function () {
463
+ var res;
464
+ return __generator(this, function (_a) {
465
+ switch (_a.label) {
466
+ case 0:
467
+ bucketBehavior.fileFactory = function () {
468
+ return ({
469
+ createReadStream: (0, bun_test_1.mock)(function () { return new node_stream_1.PassThrough(); }),
470
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
471
+ return [2 /*return*/, [{}]];
472
+ }); }); }),
473
+ getMetadata: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () {
474
+ var err;
475
+ return __generator(this, function (_a) {
476
+ err = new Error("broken");
477
+ err.code = 500;
478
+ throw err;
479
+ });
480
+ }); }),
481
+ metadata: {},
482
+ name: "x",
483
+ save: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
484
+ return [2 /*return*/, undefined];
485
+ }); }); }),
486
+ });
487
+ };
488
+ return [4 /*yield*/, adminAgent.get("/documents/download/boom.pdf").expect(500)];
489
+ case 1:
490
+ res = _a.sent();
491
+ (0, bun_test_1.expect)(res.body.title).toInclude("Failed to access file");
492
+ return [2 /*return*/];
493
+ }
494
+ });
495
+ }); });
496
+ (0, bun_test_1.it)("logs and swallows stream errors once headers have been flushed", function () { return __awaiter(void 0, void 0, void 0, function () {
497
+ var createReadStreamMock, status, caught, res, err_1;
498
+ return __generator(this, function (_a) {
499
+ switch (_a.label) {
500
+ case 0:
501
+ createReadStreamMock = (0, bun_test_1.mock)(function () {
502
+ var stream = new node_stream_1.PassThrough();
503
+ setImmediate(function () {
504
+ stream.write(Buffer.from("partial"));
505
+ setImmediate(function () { return stream.destroy(new Error("pipe broken")); });
506
+ });
507
+ return stream;
508
+ });
509
+ bucketBehavior.fileFactory = function () {
510
+ return ({
511
+ createReadStream: createReadStreamMock,
512
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
513
+ return [2 /*return*/, [{}]];
514
+ }); }); }),
515
+ getMetadata: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
516
+ return [2 /*return*/, [{ contentType: "text/plain", updated: "now" }]];
517
+ }); }); }),
518
+ metadata: {},
519
+ name: "x",
520
+ save: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
521
+ return [2 /*return*/, undefined];
522
+ }); }); }),
523
+ });
524
+ };
525
+ _a.label = 1;
526
+ case 1:
527
+ _a.trys.push([1, 3, , 4]);
528
+ return [4 /*yield*/, adminAgent.get("/documents/download/problem.txt")];
529
+ case 2:
530
+ res = _a.sent();
531
+ status = res.status;
532
+ return [3 /*break*/, 4];
533
+ case 3:
534
+ err_1 = _a.sent();
535
+ caught = err_1;
536
+ return [3 /*break*/, 4];
537
+ case 4:
538
+ // Either supertest sees a completed response (200/500) or the socket is
539
+ // reset after partial data. Both outcomes prove we exercised the route
540
+ // and its pipeline-error catch branch without crashing the process.
541
+ if (caught) {
542
+ (0, bun_test_1.expect)(String(caught)).toMatch(/ECONNRESET|socket|aborted/i);
543
+ }
544
+ else {
545
+ (0, bun_test_1.expect)(status).toBeDefined();
546
+ (0, bun_test_1.expect)([200, 500]).toContain(status);
547
+ }
548
+ (0, bun_test_1.expect)(createReadStreamMock).toHaveBeenCalled();
549
+ return [2 /*return*/];
550
+ }
551
+ });
552
+ }); });
553
+ (0, bun_test_1.it)("returns 403 for non-admins", function () { return __awaiter(void 0, void 0, void 0, function () {
554
+ return __generator(this, function (_a) {
555
+ switch (_a.label) {
556
+ case 0: return [4 /*yield*/, notAdminAgent.get("/documents/download/doc.pdf").expect(403)];
557
+ case 1:
558
+ _a.sent();
559
+ return [2 /*return*/];
560
+ }
561
+ });
562
+ }); });
563
+ });
564
+ (0, bun_test_1.describe)("POST /documents/folder", function () {
565
+ (0, bun_test_1.it)("rejects missing folder name", function () { return __awaiter(void 0, void 0, void 0, function () {
566
+ var res;
567
+ return __generator(this, function (_a) {
568
+ switch (_a.label) {
569
+ case 0: return [4 /*yield*/, adminAgent.post("/documents/folder").send({}).expect(400)];
570
+ case 1:
571
+ res = _a.sent();
572
+ (0, bun_test_1.expect)(res.body.title).toInclude("Folder name is required");
573
+ return [2 /*return*/];
574
+ }
575
+ });
576
+ }); });
577
+ (0, bun_test_1.it)("sanitizes folder names and writes a placeholder blob", function () { return __awaiter(void 0, void 0, void 0, function () {
578
+ var saved, res;
579
+ return __generator(this, function (_a) {
580
+ switch (_a.label) {
581
+ case 0:
582
+ saved = {};
583
+ bucketBehavior.fileFactory = function (path) {
584
+ return ({
585
+ createReadStream: (0, bun_test_1.mock)(function () { return new node_stream_1.PassThrough(); }),
586
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
587
+ return [2 /*return*/, [{}]];
588
+ }); }); }),
589
+ getMetadata: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
590
+ return [2 /*return*/, [{}]];
591
+ }); }); }),
592
+ metadata: {},
593
+ name: path,
594
+ save: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () {
595
+ return __generator(this, function (_a) {
596
+ saved.path = path;
597
+ return [2 /*return*/];
598
+ });
599
+ }); }),
600
+ });
601
+ };
602
+ return [4 /*yield*/, adminAgent
603
+ .post("/documents/folder")
604
+ .send({ folderName: "My Folder!", prefix: "archive/" })
605
+ .expect(200)];
606
+ case 1:
607
+ res = _a.sent();
608
+ (0, bun_test_1.expect)(res.body.folder).toBe("archive/My_Folder_/");
609
+ (0, bun_test_1.expect)(saved.path).toBe("tenant/archive/My_Folder_/");
610
+ return [2 /*return*/];
611
+ }
612
+ });
613
+ }); });
614
+ (0, bun_test_1.it)("treats prefix as optional", function () { return __awaiter(void 0, void 0, void 0, function () {
615
+ var res;
616
+ return __generator(this, function (_a) {
617
+ switch (_a.label) {
618
+ case 0: return [4 /*yield*/, adminAgent.post("/documents/folder").send({ folderName: "Root" }).expect(200)];
619
+ case 1:
620
+ res = _a.sent();
621
+ (0, bun_test_1.expect)(res.body.folder).toBe("Root/");
622
+ return [2 /*return*/];
623
+ }
624
+ });
625
+ }); });
626
+ });
627
+ (0, bun_test_1.describe)("DELETE /documents/folder/*folderpath", function () {
628
+ (0, bun_test_1.it)("deletes all files under the given folder", function () { return __awaiter(void 0, void 0, void 0, function () {
629
+ var deleted, res;
630
+ return __generator(this, function (_a) {
631
+ switch (_a.label) {
632
+ case 0:
633
+ deleted = [];
634
+ bucketBehavior.getFiles = (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () {
635
+ return __generator(this, function (_a) {
636
+ return [2 /*return*/, [
637
+ [
638
+ {
639
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () {
640
+ return __generator(this, function (_a) {
641
+ deleted.push("tenant/archive/a.pdf");
642
+ return [2 /*return*/, [{}]];
643
+ });
644
+ }); }),
645
+ metadata: {},
646
+ name: "tenant/archive/a.pdf",
647
+ },
648
+ {
649
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () {
650
+ return __generator(this, function (_a) {
651
+ deleted.push("tenant/archive/b.pdf");
652
+ return [2 /*return*/, [{}]];
653
+ });
654
+ }); }),
655
+ metadata: {},
656
+ name: "tenant/archive/b.pdf",
657
+ },
658
+ ],
659
+ null,
660
+ { prefixes: [] },
661
+ ]];
662
+ });
663
+ }); });
664
+ return [4 /*yield*/, adminAgent.delete("/documents/folder/archive").expect(200)];
665
+ case 1:
666
+ res = _a.sent();
667
+ (0, bun_test_1.expect)(res.body.success).toBe(true);
668
+ (0, bun_test_1.expect)(deleted.sort()).toEqual(["tenant/archive/a.pdf", "tenant/archive/b.pdf"]);
669
+ return [2 /*return*/];
670
+ }
671
+ });
672
+ }); });
673
+ });
674
+ (0, bun_test_1.describe)("DELETE /documents/*filepath", function () {
675
+ (0, bun_test_1.it)("deletes a single file", function () { return __awaiter(void 0, void 0, void 0, function () {
676
+ var deletedPath, res;
677
+ return __generator(this, function (_a) {
678
+ switch (_a.label) {
679
+ case 0:
680
+ bucketBehavior.fileFactory = function (path) {
681
+ return ({
682
+ createReadStream: (0, bun_test_1.mock)(function () { return new node_stream_1.PassThrough(); }),
683
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () {
684
+ return __generator(this, function (_a) {
685
+ deletedPath = path;
686
+ return [2 /*return*/, [{}]];
687
+ });
688
+ }); }),
689
+ getMetadata: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
690
+ return [2 /*return*/, [{}]];
691
+ }); }); }),
692
+ metadata: {},
693
+ name: path,
694
+ save: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
695
+ return [2 /*return*/, undefined];
696
+ }); }); }),
697
+ });
698
+ };
699
+ return [4 /*yield*/, adminAgent.delete("/documents/report.pdf").expect(200)];
700
+ case 1:
701
+ res = _a.sent();
702
+ (0, bun_test_1.expect)(res.body.success).toBe(true);
703
+ (0, bun_test_1.expect)(deletedPath).toBe("tenant/report.pdf");
704
+ return [2 /*return*/];
705
+ }
706
+ });
707
+ }); });
708
+ (0, bun_test_1.it)("returns 403 for non-admins", function () { return __awaiter(void 0, void 0, void 0, function () {
709
+ return __generator(this, function (_a) {
710
+ switch (_a.label) {
711
+ case 0: return [4 /*yield*/, notAdminAgent.delete("/documents/x.pdf").expect(403)];
712
+ case 1:
713
+ _a.sent();
714
+ return [2 /*return*/];
715
+ }
716
+ });
717
+ }); });
718
+ });
719
+ (0, bun_test_1.describe)("configuration edge cases", function () {
720
+ (0, bun_test_1.it)("throws a 503 when no bucketName is configured", function () { return __awaiter(void 0, void 0, void 0, function () {
721
+ var originalBucket, agent, res;
722
+ return __generator(this, function (_a) {
723
+ switch (_a.label) {
724
+ case 0:
725
+ originalBucket = process.env.GCS_BUCKET;
726
+ process.env.GCS_BUCKET = "";
727
+ app = buildApp({ bucketName: "" });
728
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
729
+ case 1:
730
+ agent = _a.sent();
731
+ return [4 /*yield*/, agent.get("/documents/").expect(503)];
732
+ case 2:
733
+ res = _a.sent();
734
+ (0, bun_test_1.expect)(res.body.title).toInclude("Storage not configured");
735
+ if (originalBucket !== undefined) {
736
+ process.env.GCS_BUCKET = originalBucket;
737
+ }
738
+ else {
739
+ delete process.env.GCS_BUCKET;
740
+ }
741
+ return [2 /*return*/];
742
+ }
743
+ });
744
+ }); });
745
+ (0, bun_test_1.it)("falls back to GCS_BUCKET env var when bucketName is falsy", function () { return __awaiter(void 0, void 0, void 0, function () {
746
+ var originalBucket, agent;
747
+ return __generator(this, function (_a) {
748
+ switch (_a.label) {
749
+ case 0:
750
+ originalBucket = process.env.GCS_BUCKET;
751
+ process.env.GCS_BUCKET = "env-bucket";
752
+ app = buildApp({ bucketName: "" });
753
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
754
+ case 1:
755
+ agent = _a.sent();
756
+ bucketBehavior.getFiles = (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
757
+ return [2 /*return*/, [[], null, { prefixes: [] }]];
758
+ }); }); });
759
+ return [4 /*yield*/, agent.get("/documents/").expect(200)];
760
+ case 2:
761
+ _a.sent();
762
+ if (originalBucket !== undefined) {
763
+ process.env.GCS_BUCKET = originalBucket;
764
+ }
765
+ else {
766
+ delete process.env.GCS_BUCKET;
767
+ }
768
+ return [2 /*return*/];
769
+ }
770
+ });
771
+ }); });
772
+ (0, bun_test_1.it)("defaults folderPrefix to empty string", function () { return __awaiter(void 0, void 0, void 0, function () {
773
+ var agent;
774
+ return __generator(this, function (_a) {
775
+ switch (_a.label) {
776
+ case 0:
777
+ app = buildApp({ bucketName: "b" });
778
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
779
+ case 1:
780
+ agent = _a.sent();
781
+ bucketBehavior.getFiles = (0, bun_test_1.mock)(function (opts) { return __awaiter(void 0, void 0, void 0, function () {
782
+ return __generator(this, function (_a) {
783
+ (0, bun_test_1.expect)(opts.prefix).toBe("");
784
+ return [2 /*return*/, [[], null, { prefixes: [] }]];
785
+ });
786
+ }); });
787
+ return [4 /*yield*/, agent.get("/documents/").expect(200)];
788
+ case 2:
789
+ _a.sent();
790
+ return [2 /*return*/];
791
+ }
792
+ });
793
+ }); });
794
+ (0, bun_test_1.it)("defaults basePath to /documents when not provided", function () { return __awaiter(void 0, void 0, void 0, function () {
795
+ var agent;
796
+ return __generator(this, function (_a) {
797
+ switch (_a.label) {
798
+ case 0:
799
+ app = buildApp({ bucketName: "b" });
800
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
801
+ case 1:
802
+ agent = _a.sent();
803
+ bucketBehavior.getFiles = (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
804
+ return [2 /*return*/, [[], null, { prefixes: [] }]];
805
+ }); }); });
806
+ return [4 /*yield*/, agent.get("/documents/").expect(200)];
807
+ case 2:
808
+ _a.sent();
809
+ return [2 /*return*/];
810
+ }
811
+ });
812
+ }); });
813
+ (0, bun_test_1.it)("supports a custom basePath", function () { return __awaiter(void 0, void 0, void 0, function () {
814
+ var agent;
815
+ return __generator(this, function (_a) {
816
+ switch (_a.label) {
817
+ case 0:
818
+ app = buildApp({ basePath: "/files", bucketName: "b" });
819
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
820
+ case 1:
821
+ agent = _a.sent();
822
+ bucketBehavior.getFiles = (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
823
+ return [2 /*return*/, [[], null, { prefixes: [] }]];
824
+ }); }); });
825
+ return [4 /*yield*/, agent.get("/files/").expect(200)];
826
+ case 2:
827
+ _a.sent();
828
+ return [2 /*return*/];
829
+ }
830
+ });
831
+ }); });
832
+ (0, bun_test_1.it)("accepts a maxFileSize option without rejecting normal-sized files", function () { return __awaiter(void 0, void 0, void 0, function () {
833
+ var agent, saved, res;
834
+ return __generator(this, function (_a) {
835
+ switch (_a.label) {
836
+ case 0:
837
+ app = buildApp({ bucketName: "b", maxFileSize: 1024 });
838
+ return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
839
+ case 1:
840
+ agent = _a.sent();
841
+ saved = {};
842
+ bucketBehavior.fileFactory = function (path) {
843
+ return ({
844
+ createReadStream: (0, bun_test_1.mock)(function () { return new node_stream_1.PassThrough(); }),
845
+ delete: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
846
+ return [2 /*return*/, [{}]];
847
+ }); }); }),
848
+ getMetadata: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
849
+ return [2 /*return*/, [{}]];
850
+ }); }); }),
851
+ metadata: {},
852
+ name: path,
853
+ save: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () {
854
+ return __generator(this, function (_a) {
855
+ saved.path = path;
856
+ return [2 /*return*/];
857
+ });
858
+ }); }),
859
+ });
860
+ };
861
+ return [4 /*yield*/, agent.post("/documents/").attach("file", Buffer.from("small"), {
862
+ contentType: "text/plain",
863
+ filename: "small.txt",
864
+ })];
865
+ case 2:
866
+ res = _a.sent();
867
+ (0, bun_test_1.expect)(res.status).toBe(200);
868
+ (0, bun_test_1.expect)(saved.path).toBe("small.txt");
869
+ return [2 /*return*/];
870
+ }
871
+ });
872
+ }); });
873
+ });
874
+ });