@terreno/api 0.29.0 → 0.30.0
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.
- package/CHANGELOG.md +79 -0
- package/dist/actions.js +17 -8
- package/dist/actions.test.js +8 -3
- package/dist/api.arrayOperations.test.js +16 -8
- package/dist/api.coverage.test.js +2 -1
- package/dist/api.errorPaths.test.js +522 -1
- package/dist/api.errors.test.js +9 -0
- package/dist/api.js +137 -170
- package/dist/api.query.test.js +20 -8
- package/dist/api.test.js +12 -6
- package/dist/auth.coverage.test.d.ts +1 -0
- package/dist/auth.coverage.test.js +202 -0
- package/dist/auth.test.js +159 -0
- package/dist/docLoader.js +5 -1
- package/dist/envConfigurationPlugin.d.ts +1 -1
- package/dist/envConfigurationPlugin.js +0 -2
- package/dist/errors.d.ts +14 -0
- package/dist/errors.js +24 -2
- package/dist/errors.test.js +137 -0
- package/dist/httpClient.js +5 -1
- package/dist/plugins.d.ts +4 -4
- package/dist/plugins.js +51 -9
- package/dist/plugins.test.js +61 -17
- package/dist/realtime/registry.d.ts +3 -2
- package/package.json +2 -2
- package/src/actions.test.ts +8 -3
- package/src/actions.ts +19 -10
- package/src/api.arrayOperations.test.ts +16 -8
- package/src/api.coverage.test.ts +2 -1
- package/src/api.errorPaths.test.ts +313 -3
- package/src/api.errors.test.ts +12 -0
- package/src/api.query.test.ts +20 -8
- package/src/api.test.ts +12 -6
- package/src/api.ts +141 -161
- package/src/auth.coverage.test.ts +101 -0
- package/src/auth.test.ts +125 -2
- package/src/docLoader.ts +6 -2
- package/src/envConfigurationPlugin.ts +2 -6
- package/src/errors.test.ts +106 -1
- package/src/errors.ts +25 -1
- package/src/httpClient.ts +6 -2
- package/src/plugins.test.ts +45 -9
- package/src/plugins.ts +72 -35
- package/src/realtime/changeStreamWatcher.ts +8 -1
- package/src/realtime/registry.ts +3 -4
|
@@ -83,7 +83,8 @@ describe("model array operations", () => {
|
|
|
83
83
|
.post(`/food/${apple._id}/categories`)
|
|
84
84
|
.send({name: "Good Seller", show: false})
|
|
85
85
|
.expect(400);
|
|
86
|
-
expect(res.body.title).toBe(
|
|
86
|
+
expect(res.body.title).toBe("Malformed array operation body");
|
|
87
|
+
expect(res.body.detail).toBe(
|
|
87
88
|
"Malformed body, array operations should have a single, top level key, got: name,show"
|
|
88
89
|
);
|
|
89
90
|
|
|
@@ -106,7 +107,8 @@ describe("model array operations", () => {
|
|
|
106
107
|
.patch(`/food/${apple._id}/categories/xyz`)
|
|
107
108
|
.send({categories: {name: "Good Seller", show: false}})
|
|
108
109
|
.expect(404);
|
|
109
|
-
expect(res.body.title).toBe("
|
|
110
|
+
expect(res.body.title).toBe("Array item not found");
|
|
111
|
+
expect(res.body.detail).toBe("Could not find categories/xyz");
|
|
110
112
|
res = await agent
|
|
111
113
|
.patch(`/food/${apple._id}/categories/${apple.categories[1]._id}`)
|
|
112
114
|
.send({categories: {name: "Good Seller", show: false}})
|
|
@@ -117,7 +119,8 @@ describe("model array operations", () => {
|
|
|
117
119
|
|
|
118
120
|
it("delete array sub-schema item", async () => {
|
|
119
121
|
let res = await agent.delete(`/food/${apple._id}/categories/xyz`).expect(404);
|
|
120
|
-
expect(res.body.title).toBe("
|
|
122
|
+
expect(res.body.title).toBe("Array item not found");
|
|
123
|
+
expect(res.body.detail).toBe("Could not find categories/xyz");
|
|
121
124
|
res = await agent
|
|
122
125
|
.delete(`/food/${apple._id}/categories/${apple.categories[0]._id}`)
|
|
123
126
|
.expect(200);
|
|
@@ -139,7 +142,8 @@ describe("model array operations", () => {
|
|
|
139
142
|
.patch(`/food/${apple._id}/tags/xyz`)
|
|
140
143
|
.send({tags: "unhealthy"})
|
|
141
144
|
.expect(404);
|
|
142
|
-
expect(res.body.title).toBe("
|
|
145
|
+
expect(res.body.title).toBe("Array item not found");
|
|
146
|
+
expect(res.body.detail).toBe("Could not find tags/xyz");
|
|
143
147
|
res = await agent
|
|
144
148
|
.patch(`/food/${apple._id}/tags/healthy`)
|
|
145
149
|
.send({tags: "unhealthy"})
|
|
@@ -149,7 +153,8 @@ describe("model array operations", () => {
|
|
|
149
153
|
|
|
150
154
|
it("delete array item", async () => {
|
|
151
155
|
let res = await agent.delete(`/food/${apple._id}/tags/xyz`).expect(404);
|
|
152
|
-
expect(res.body.title).toBe("
|
|
156
|
+
expect(res.body.title).toBe("Array item not found");
|
|
157
|
+
expect(res.body.detail).toBe("Could not find tags/xyz");
|
|
153
158
|
res = await agent.delete(`/food/${apple._id}/tags/healthy`).expect(200);
|
|
154
159
|
expect(res.body.data.tags).toEqual(["cheap"]);
|
|
155
160
|
});
|
|
@@ -516,7 +521,8 @@ describe("array operation errors", () => {
|
|
|
516
521
|
agent = await authAsUser(app, "notAdmin");
|
|
517
522
|
|
|
518
523
|
const res = await agent.post(`/food/${apple._id}/tags`).send({tags: "organic"}).expect(405);
|
|
519
|
-
expect(res.body.title).
|
|
524
|
+
expect(res.body.title).toBe("Access denied");
|
|
525
|
+
expect(res.body.detail).toContain("Access to PATCH");
|
|
520
526
|
});
|
|
521
527
|
|
|
522
528
|
it("array operation on non-existent document returns 404", async () => {
|
|
@@ -538,7 +544,8 @@ describe("array operation errors", () => {
|
|
|
538
544
|
|
|
539
545
|
const fakeId = "000000000000000000000000";
|
|
540
546
|
const res = await agent.post(`/food/${fakeId}/tags`).send({tags: "organic"}).expect(404);
|
|
541
|
-
expect(res.body.title).
|
|
547
|
+
expect(res.body.title).toBe("Document not found");
|
|
548
|
+
expect(res.body.detail).toContain("Could not find document to PATCH");
|
|
542
549
|
});
|
|
543
550
|
|
|
544
551
|
it("array operation denied when user cannot update specific doc", async () => {
|
|
@@ -561,7 +568,8 @@ describe("array operation errors", () => {
|
|
|
561
568
|
agent = await authAsUser(app, "notAdmin");
|
|
562
569
|
|
|
563
570
|
const res = await agent.post(`/food/${apple._id}/tags`).send({tags: "organic"}).expect(403);
|
|
564
|
-
expect(res.body.title).
|
|
571
|
+
expect(res.body.title).toBe("Update not allowed");
|
|
572
|
+
expect(res.body.detail).toContain("Patch not allowed");
|
|
565
573
|
});
|
|
566
574
|
|
|
567
575
|
it("array operation transform error is handled", async () => {
|
package/src/api.coverage.test.ts
CHANGED
|
@@ -96,7 +96,8 @@ describe("modelRouter error path coverage", () => {
|
|
|
96
96
|
.post(`/food/${apple._id}/eatenBy`)
|
|
97
97
|
.send({eatenBy: "not-an-object-id"})
|
|
98
98
|
.expect(400);
|
|
99
|
-
expect(res.body.title).
|
|
99
|
+
expect(res.body.title).toBe("Validation failed");
|
|
100
|
+
expect(res.body.meta.fields["eatenBy.0"]).toContain("ObjectId");
|
|
100
101
|
});
|
|
101
102
|
|
|
102
103
|
it("returns 400 when a hard delete (no isDeleted plugin) fails", async () => {
|
|
@@ -8,8 +8,9 @@ import type TestAgent from "supertest/lib/agent";
|
|
|
8
8
|
|
|
9
9
|
import {type ModelRouterOptions, modelRouter} from "./api";
|
|
10
10
|
import {addAuthRoutes, setupAuth} from "./auth";
|
|
11
|
+
import {ConflictError} from "./errors";
|
|
11
12
|
import {Permissions} from "./permissions";
|
|
12
|
-
import {type Food, FoodModel, getBaseServer, setupDb, UserModel} from "./tests";
|
|
13
|
+
import {type Food, FoodModel, getBaseServer, RequiredModel, setupDb, UserModel} from "./tests";
|
|
13
14
|
|
|
14
15
|
type SetupAuthUserModel = Parameters<typeof setupAuth>[1];
|
|
15
16
|
const authUserModel = UserModel as unknown as SetupAuthUserModel;
|
|
@@ -155,7 +156,7 @@ describe("modelRouter error paths", () => {
|
|
|
155
156
|
assert.include(res.body.detail, "transform exploded");
|
|
156
157
|
});
|
|
157
158
|
|
|
158
|
-
it("
|
|
159
|
+
it("converts Mongoose save failures during array operations into field errors", async () => {
|
|
159
160
|
mountFood();
|
|
160
161
|
const food = await FoodModel.create({
|
|
161
162
|
calories: 1,
|
|
@@ -168,6 +169,315 @@ describe("modelRouter error paths", () => {
|
|
|
168
169
|
.post(`/food/${food._id}/eatenBy`)
|
|
169
170
|
.send({eatenBy: "not-an-object-id"})
|
|
170
171
|
.expect(400);
|
|
171
|
-
assert.
|
|
172
|
+
assert.equal(res.body.title, "Validation failed");
|
|
173
|
+
assert.include(res.body.meta.fields["eatenBy.0"], "ObjectId");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("wraps non-Mongoose save failures during array operations", async () => {
|
|
177
|
+
mountFood();
|
|
178
|
+
const food = await FoodModel.create({
|
|
179
|
+
calories: 1,
|
|
180
|
+
name: "Unsavable",
|
|
181
|
+
ownerId: new mongoose.Types.ObjectId(),
|
|
182
|
+
} as Partial<Food>);
|
|
183
|
+
|
|
184
|
+
const saveSpy = spyOn(FoodModel.prototype, "save").mockImplementation(() => {
|
|
185
|
+
throw new Error("save exploded");
|
|
186
|
+
});
|
|
187
|
+
try {
|
|
188
|
+
const res = await server.post(`/food/${food._id}/tags`).send({tags: "vegetable"}).expect(400);
|
|
189
|
+
assert.include(res.body.title, "PATCH Pre Update error");
|
|
190
|
+
assert.include(res.body.detail, "save exploded");
|
|
191
|
+
} finally {
|
|
192
|
+
saveSpy.mockRestore();
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
interface Guarded {
|
|
198
|
+
name: string;
|
|
199
|
+
tags: string[];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// A domain error of the shape consumers throw: stable title, kebab-case code, per-occurrence
|
|
203
|
+
// detail, and structured meta the client renders.
|
|
204
|
+
const staffConflictError = (code: string): ConflictError =>
|
|
205
|
+
new ConflictError({
|
|
206
|
+
code,
|
|
207
|
+
detail: "Conflict detected for Dr. Ada on 2026-07-31",
|
|
208
|
+
meta: {date: "2026-07-31", staff: "Dr. Ada"},
|
|
209
|
+
title: "Conflict detected",
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
const assertStaffConflict = (body: Record<string, unknown>, code: string): void => {
|
|
213
|
+
assert.equal(body.status, 409);
|
|
214
|
+
assert.equal(body.title, "Conflict detected");
|
|
215
|
+
assert.equal(body.detail, "Conflict detected for Dr. Ada on 2026-07-31");
|
|
216
|
+
assert.equal(body.code, code);
|
|
217
|
+
assert.deepEqual(body.meta, {date: "2026-07-31", staff: "Dr. Ada"});
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const guardedSchema = new mongoose.Schema<Guarded>(
|
|
221
|
+
{
|
|
222
|
+
name: {description: "The name", type: String},
|
|
223
|
+
tags: {description: "Tags for the document", type: [String]},
|
|
224
|
+
},
|
|
225
|
+
{strict: "throw"}
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
// Stands in for a consumer model that validates in document middleware.
|
|
229
|
+
guardedSchema.pre("save", function () {
|
|
230
|
+
if (this.name === "Conflict" || this.tags?.includes("conflict")) {
|
|
231
|
+
throw staffConflictError("schedule-item-staff-conflict");
|
|
232
|
+
}
|
|
233
|
+
if (this.name === "Boom" || this.tags?.includes("boom")) {
|
|
234
|
+
throw new Error("save exploded");
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const GuardedModel = mongoose.model<Guarded>("PassthroughGuarded", guardedSchema);
|
|
239
|
+
|
|
240
|
+
describe("modelRouter APIError passthrough", () => {
|
|
241
|
+
let app: express.Application;
|
|
242
|
+
let server: TestAgent;
|
|
243
|
+
|
|
244
|
+
beforeEach(async () => {
|
|
245
|
+
await setupDb();
|
|
246
|
+
await GuardedModel.deleteMany({});
|
|
247
|
+
app = getBaseServer();
|
|
248
|
+
setupAuth(app, authUserModel);
|
|
249
|
+
addAuthRoutes(app, authUserModel);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
const mountGuarded = (options: Partial<ModelRouterOptions<Guarded>> = {}): void => {
|
|
253
|
+
app.use(
|
|
254
|
+
"/guarded",
|
|
255
|
+
modelRouter(GuardedModel, {
|
|
256
|
+
allowAnonymous: true,
|
|
257
|
+
permissions: anyPermissions,
|
|
258
|
+
...options,
|
|
259
|
+
})
|
|
260
|
+
);
|
|
261
|
+
server = supertest(app);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
describe("Mongoose document middleware", () => {
|
|
265
|
+
it("keeps an APIError from pre(save) intact through POST /", async () => {
|
|
266
|
+
mountGuarded();
|
|
267
|
+
const res = await server.post("/guarded").send({name: "Conflict"}).expect(409);
|
|
268
|
+
assertStaffConflict(res.body, "schedule-item-staff-conflict");
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("wraps a non-APIError from pre(save) on POST /", async () => {
|
|
272
|
+
mountGuarded();
|
|
273
|
+
const res = await server.post("/guarded").send({name: "Boom"}).expect(400);
|
|
274
|
+
assert.equal(res.body.title, "Create error");
|
|
275
|
+
assert.equal(res.body.code, "create-error");
|
|
276
|
+
assert.include(res.body.detail, "save exploded");
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("keeps an APIError from pre(save) intact through PATCH /:id", async () => {
|
|
280
|
+
mountGuarded();
|
|
281
|
+
const doc = await GuardedModel.create({name: "Fine", tags: []});
|
|
282
|
+
const res = await server.patch(`/guarded/${doc._id}`).send({name: "Conflict"}).expect(409);
|
|
283
|
+
assertStaffConflict(res.body, "schedule-item-staff-conflict");
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("wraps a non-APIError from pre(save) on PATCH /:id", async () => {
|
|
287
|
+
mountGuarded();
|
|
288
|
+
const doc = await GuardedModel.create({name: "Fine", tags: []});
|
|
289
|
+
const res = await server.patch(`/guarded/${doc._id}`).send({name: "Boom"}).expect(400);
|
|
290
|
+
assert.equal(res.body.title, "preUpdate hook save error");
|
|
291
|
+
assert.include(res.body.detail, "save exploded");
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it("keeps an APIError from pre(save) intact through an array operation", async () => {
|
|
295
|
+
mountGuarded();
|
|
296
|
+
const doc = await GuardedModel.create({name: "Fine", tags: []});
|
|
297
|
+
const res = await server
|
|
298
|
+
.post(`/guarded/${doc._id}/tags`)
|
|
299
|
+
.send({tags: "conflict"})
|
|
300
|
+
.expect(409);
|
|
301
|
+
assertStaffConflict(res.body, "schedule-item-staff-conflict");
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("surfaces Mongoose validation errors from create as per-field errors", async () => {
|
|
305
|
+
app.use(
|
|
306
|
+
"/required",
|
|
307
|
+
modelRouter(RequiredModel, {allowAnonymous: true, permissions: anyPermissions})
|
|
308
|
+
);
|
|
309
|
+
server = supertest(app);
|
|
310
|
+
|
|
311
|
+
const res = await server.post("/required").send({about: "no name here"}).expect(400);
|
|
312
|
+
assert.equal(res.body.title, "Validation failed");
|
|
313
|
+
assert.include(res.body.meta.fields.name, "required");
|
|
314
|
+
assert.isTrue(res.body.disableExternalErrorTracking);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
describe("hooks", () => {
|
|
319
|
+
it("keeps an APIError from queryFilter intact", async () => {
|
|
320
|
+
mountGuarded({
|
|
321
|
+
queryFilter: () => {
|
|
322
|
+
throw staffConflictError("query-filter-conflict");
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
const res = await server.get("/guarded").expect(409);
|
|
326
|
+
assertStaffConflict(res.body, "query-filter-conflict");
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("wraps a non-APIError from queryFilter with the caught error's message", async () => {
|
|
330
|
+
mountGuarded({
|
|
331
|
+
queryFilter: () => {
|
|
332
|
+
throw new Error("filter exploded");
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
const res = await server.get("/guarded").expect(400);
|
|
336
|
+
assert.equal(res.body.title, "Query filter error");
|
|
337
|
+
// Previously String(error), which prefixed the class name.
|
|
338
|
+
assert.equal(res.body.detail, "filter exploded");
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it("wraps a thrown non-Error from queryFilter", async () => {
|
|
342
|
+
mountGuarded({
|
|
343
|
+
queryFilter: () => {
|
|
344
|
+
throw "filter string";
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
const res = await server.get("/guarded").expect(400);
|
|
348
|
+
assert.equal(res.body.title, "Query filter error");
|
|
349
|
+
assert.equal(res.body.detail, "filter string");
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it("keeps an APIError from responseHandler intact for every method", async () => {
|
|
353
|
+
mountGuarded({
|
|
354
|
+
responseHandler: () => {
|
|
355
|
+
throw staffConflictError("response-handler-conflict");
|
|
356
|
+
},
|
|
357
|
+
});
|
|
358
|
+
const doc = await GuardedModel.create({name: "Fine", tags: []});
|
|
359
|
+
|
|
360
|
+
for (const request of [
|
|
361
|
+
server.post("/guarded").send({name: "Another"}),
|
|
362
|
+
server.get("/guarded"),
|
|
363
|
+
server.get(`/guarded/${doc._id}`),
|
|
364
|
+
server.patch(`/guarded/${doc._id}`).send({name: "Renamed"}),
|
|
365
|
+
]) {
|
|
366
|
+
const res = await request.expect(409);
|
|
367
|
+
assertStaffConflict(res.body, "response-handler-conflict");
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it("wraps a non-APIError from responseHandler", async () => {
|
|
372
|
+
mountGuarded({
|
|
373
|
+
responseHandler: () => {
|
|
374
|
+
throw new Error("serialize exploded");
|
|
375
|
+
},
|
|
376
|
+
});
|
|
377
|
+
const res = await server.get("/guarded").expect(500);
|
|
378
|
+
assert.equal(res.body.title, "responseHandler error");
|
|
379
|
+
assert.include(res.body.detail, "serialize exploded");
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it("keeps an APIError from postCreate intact", async () => {
|
|
383
|
+
mountGuarded({
|
|
384
|
+
postCreate: () => {
|
|
385
|
+
throw staffConflictError("post-create-conflict");
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
const res = await server.post("/guarded").send({name: "Fine"}).expect(409);
|
|
389
|
+
assertStaffConflict(res.body, "post-create-conflict");
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it("wraps a non-APIError from postCreate", async () => {
|
|
393
|
+
mountGuarded({
|
|
394
|
+
postCreate: () => {
|
|
395
|
+
throw new Error("postCreate exploded");
|
|
396
|
+
},
|
|
397
|
+
});
|
|
398
|
+
const res = await server.post("/guarded").send({name: "Fine"}).expect(400);
|
|
399
|
+
assert.equal(res.body.title, "postCreate hook error");
|
|
400
|
+
assert.include(res.body.detail, "postCreate exploded");
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it("keeps an APIError from postUpdate intact", async () => {
|
|
404
|
+
mountGuarded({
|
|
405
|
+
postUpdate: () => {
|
|
406
|
+
throw staffConflictError("post-update-conflict");
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
const doc = await GuardedModel.create({name: "Fine", tags: []});
|
|
410
|
+
const res = await server.patch(`/guarded/${doc._id}`).send({name: "Renamed"}).expect(409);
|
|
411
|
+
assertStaffConflict(res.body, "post-update-conflict");
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it("wraps a non-APIError from postUpdate", async () => {
|
|
415
|
+
mountGuarded({
|
|
416
|
+
postUpdate: () => {
|
|
417
|
+
throw new Error("postUpdate exploded");
|
|
418
|
+
},
|
|
419
|
+
});
|
|
420
|
+
const doc = await GuardedModel.create({name: "Fine", tags: []});
|
|
421
|
+
const res = await server.patch(`/guarded/${doc._id}`).send({name: "Renamed"}).expect(400);
|
|
422
|
+
assert.equal(res.body.title, "postUpdate hook error");
|
|
423
|
+
assert.include(res.body.detail, "postUpdate exploded");
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it("keeps an APIError from postDelete intact", async () => {
|
|
427
|
+
mountGuarded({
|
|
428
|
+
postDelete: () => {
|
|
429
|
+
throw staffConflictError("post-delete-conflict");
|
|
430
|
+
},
|
|
431
|
+
});
|
|
432
|
+
const doc = await GuardedModel.create({name: "Fine", tags: []});
|
|
433
|
+
const res = await server.delete(`/guarded/${doc._id}`).expect(409);
|
|
434
|
+
assertStaffConflict(res.body, "post-delete-conflict");
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it("wraps a non-APIError from postDelete", async () => {
|
|
438
|
+
mountGuarded({
|
|
439
|
+
postDelete: () => {
|
|
440
|
+
throw new Error("postDelete exploded");
|
|
441
|
+
},
|
|
442
|
+
});
|
|
443
|
+
const doc = await GuardedModel.create({name: "Fine", tags: []});
|
|
444
|
+
const res = await server.delete(`/guarded/${doc._id}`).expect(400);
|
|
445
|
+
assert.equal(res.body.title, "postDelete hook error");
|
|
446
|
+
assert.include(res.body.detail, "postDelete exploded");
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it("keeps an APIError from preUpdate intact during an array operation", async () => {
|
|
450
|
+
mountGuarded({
|
|
451
|
+
preUpdate: () => {
|
|
452
|
+
throw staffConflictError("array-pre-update-conflict");
|
|
453
|
+
},
|
|
454
|
+
});
|
|
455
|
+
const doc = await GuardedModel.create({name: "Fine", tags: []});
|
|
456
|
+
const res = await server.post(`/guarded/${doc._id}/tags`).send({tags: "healthy"}).expect(409);
|
|
457
|
+
assertStaffConflict(res.body, "array-pre-update-conflict");
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
it("keeps an APIError from postUpdate intact during an array operation", async () => {
|
|
461
|
+
mountGuarded({
|
|
462
|
+
postUpdate: () => {
|
|
463
|
+
throw staffConflictError("array-post-update-conflict");
|
|
464
|
+
},
|
|
465
|
+
});
|
|
466
|
+
const doc = await GuardedModel.create({name: "Fine", tags: []});
|
|
467
|
+
const res = await server.post(`/guarded/${doc._id}/tags`).send({tags: "healthy"}).expect(409);
|
|
468
|
+
assertStaffConflict(res.body, "array-post-update-conflict");
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it("wraps a non-APIError from preUpdate during an array operation", async () => {
|
|
472
|
+
mountGuarded({
|
|
473
|
+
preUpdate: () => {
|
|
474
|
+
throw new Error("array preUpdate exploded");
|
|
475
|
+
},
|
|
476
|
+
});
|
|
477
|
+
const doc = await GuardedModel.create({name: "Fine", tags: []});
|
|
478
|
+
const res = await server.post(`/guarded/${doc._id}/tags`).send({tags: "healthy"}).expect(400);
|
|
479
|
+
assert.equal(res.body.title, "preUpdate hook error");
|
|
480
|
+
assert.include(res.body.detail, "array preUpdate exploded");
|
|
481
|
+
});
|
|
172
482
|
});
|
|
173
483
|
});
|
package/src/api.errors.test.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// noExplicitAny: test mock typing
|
|
2
2
|
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
3
3
|
import {describe, expect, it} from "bun:test";
|
|
4
|
+
import type {NextFunction} from "express";
|
|
4
5
|
import mongoose from "mongoose";
|
|
5
6
|
|
|
6
7
|
import {
|
|
@@ -153,5 +154,16 @@ describe("errors module", () => {
|
|
|
153
154
|
apiUnauthorizedMiddleware(err, {} as any, {} as any, next);
|
|
154
155
|
expect(nextCalled).toBe(true);
|
|
155
156
|
});
|
|
157
|
+
|
|
158
|
+
it("calls next for an APIError whose title is Unauthorized", () => {
|
|
159
|
+
const err = new APIError({code: "not-a-member", status: 403, title: "Unauthorized"});
|
|
160
|
+
let nextArg: unknown;
|
|
161
|
+
const next = (error?: unknown) => {
|
|
162
|
+
nextArg = error;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
apiUnauthorizedMiddleware(err, {} as any, {} as any, next as unknown as NextFunction);
|
|
166
|
+
expect(nextArg).toBe(err);
|
|
167
|
+
});
|
|
156
168
|
});
|
|
157
169
|
});
|
package/src/api.query.test.ts
CHANGED
|
@@ -178,12 +178,16 @@ describe("query and list methods", () => {
|
|
|
178
178
|
|
|
179
179
|
it("list page 0 ", async () => {
|
|
180
180
|
const res = await agent.get("/food?limit=1&page=0").expect(400);
|
|
181
|
-
expect(res.body.title).toBe("Invalid page
|
|
181
|
+
expect(res.body.title).toBe("Invalid page");
|
|
182
|
+
expect(res.body.detail).toBe("Invalid page: 0");
|
|
183
|
+
expect(res.body.source).toEqual({parameter: "page"});
|
|
182
184
|
});
|
|
183
185
|
|
|
184
186
|
it("list page with garbage ", async () => {
|
|
185
187
|
const res = await agent.get("/food?limit=1&page=abc").expect(400);
|
|
186
|
-
expect(res.body.title).toBe("Invalid page
|
|
188
|
+
expect(res.body.title).toBe("Invalid page");
|
|
189
|
+
expect(res.body.detail).toBe("Invalid page: abc");
|
|
190
|
+
expect(res.body.source).toEqual({parameter: "page"});
|
|
187
191
|
});
|
|
188
192
|
|
|
189
193
|
it("list page over", async () => {
|
|
@@ -216,7 +220,9 @@ describe("query and list methods", () => {
|
|
|
216
220
|
|
|
217
221
|
it("list returns 400 when created_gte is not a valid date", async () => {
|
|
218
222
|
const res = await agent.get("/food").query({created_gte: "not-a-date", limit: 10}).expect(400);
|
|
219
|
-
expect(res.body.title).toBe("Invalid date
|
|
223
|
+
expect(res.body.title).toBe("Invalid date query parameter");
|
|
224
|
+
expect(res.body.detail).toBe("Invalid date for query parameter created_gte");
|
|
225
|
+
expect(res.body.source).toEqual({parameter: "created_gte"});
|
|
220
226
|
});
|
|
221
227
|
|
|
222
228
|
it("list returns 400 when created_lte is not a valid date", async () => {
|
|
@@ -224,7 +230,9 @@ describe("query and list methods", () => {
|
|
|
224
230
|
.get("/food")
|
|
225
231
|
.query({created_lte: "also-not-a-date", limit: 10})
|
|
226
232
|
.expect(400);
|
|
227
|
-
expect(res.body.title).toBe("Invalid date
|
|
233
|
+
expect(res.body.title).toBe("Invalid date query parameter");
|
|
234
|
+
expect(res.body.detail).toBe("Invalid date for query parameter created_lte");
|
|
235
|
+
expect(res.body.source).toEqual({parameter: "created_lte"});
|
|
228
236
|
});
|
|
229
237
|
|
|
230
238
|
it("applies created_gte alone as a lower Date bound", async () => {
|
|
@@ -275,7 +283,8 @@ describe("query and list methods", () => {
|
|
|
275
283
|
|
|
276
284
|
it("list query params not in list", async () => {
|
|
277
285
|
const res = await agent.get(`/food?ownerId=${admin._id}`).expect(400);
|
|
278
|
-
expect(res.body.title).toBe("
|
|
286
|
+
expect(res.body.title).toBe("Query parameter not allowed");
|
|
287
|
+
expect(res.body.detail).toBe("ownerId is not allowed as a query param.");
|
|
279
288
|
});
|
|
280
289
|
|
|
281
290
|
it("list query by nested param", async () => {
|
|
@@ -477,16 +486,19 @@ describe("query and list methods", () => {
|
|
|
477
486
|
let res = await agent
|
|
478
487
|
.get(`/food?${qs.stringify({$and: [{ownerId: "healthy"}, {tags: "cheap"}]})}`)
|
|
479
488
|
.expect(400);
|
|
480
|
-
expect(res.body.title).toBe("
|
|
489
|
+
expect(res.body.title).toBe("Query parameter not allowed");
|
|
490
|
+
expect(res.body.detail).toBe("ownerId is not allowed as a query param.");
|
|
481
491
|
res = await agent
|
|
482
492
|
.get(`/food?${qs.stringify({$and: [{tags: "cheap"}, {ownerId: "healthy"}]})}`)
|
|
483
493
|
.expect(400);
|
|
484
|
-
expect(res.body.title).toBe("
|
|
494
|
+
expect(res.body.title).toBe("Query parameter not allowed");
|
|
495
|
+
expect(res.body.detail).toBe("ownerId is not allowed as a query param.");
|
|
485
496
|
|
|
486
497
|
res = await agent
|
|
487
498
|
.get(`/food?${qs.stringify({$or: [{tags: "cheap"}, {ownerId: "healthy"}]})}`)
|
|
488
499
|
.expect(400);
|
|
489
|
-
expect(res.body.title).toBe("
|
|
500
|
+
expect(res.body.title).toBe("Query parameter not allowed");
|
|
501
|
+
expect(res.body.detail).toBe("ownerId is not allowed as a query param.");
|
|
490
502
|
});
|
|
491
503
|
|
|
492
504
|
it("query with a number", async () => {
|
package/src/api.test.ts
CHANGED
|
@@ -514,7 +514,8 @@ describe("@terreno/api", () => {
|
|
|
514
514
|
server = supertest(app);
|
|
515
515
|
|
|
516
516
|
const res = await server.get("/food?calories=100").expect(400);
|
|
517
|
-
expect(res.body.title).
|
|
517
|
+
expect(res.body.title).toBe("Query parameter not allowed");
|
|
518
|
+
expect(res.body.detail).toContain("calories is not allowed as a query param");
|
|
518
519
|
});
|
|
519
520
|
|
|
520
521
|
it("queryFilter returning null returns empty array", async () => {
|
|
@@ -626,8 +627,9 @@ describe("@terreno/api", () => {
|
|
|
626
627
|
server = supertest(app);
|
|
627
628
|
|
|
628
629
|
const res = await server.post("/required").send({about: "test"}).expect(400);
|
|
629
|
-
expect(res.body.title).toBe("
|
|
630
|
+
expect(res.body.title).toBe("Validation failed");
|
|
630
631
|
expect(res.body.detail).toContain("Required");
|
|
632
|
+
expect(res.body.meta.fields.name).toContain("required");
|
|
631
633
|
});
|
|
632
634
|
|
|
633
635
|
it("preDelete hook throwing APIError is re-thrown", async () => {
|
|
@@ -1214,7 +1216,8 @@ describe("@terreno/api", () => {
|
|
|
1214
1216
|
agent = await authAsUser(app, "notAdmin");
|
|
1215
1217
|
|
|
1216
1218
|
const res = await agent.post(`/food/${apple._id}/tags`).send({tags: "organic"}).expect(405);
|
|
1217
|
-
expect(res.body.title).
|
|
1219
|
+
expect(res.body.title).toBe("Access denied");
|
|
1220
|
+
expect(res.body.detail).toContain("Access to PATCH");
|
|
1218
1221
|
});
|
|
1219
1222
|
|
|
1220
1223
|
it("array operation on non-existent document returns 404", async () => {
|
|
@@ -1236,7 +1239,8 @@ describe("@terreno/api", () => {
|
|
|
1236
1239
|
|
|
1237
1240
|
const fakeId = "000000000000000000000000";
|
|
1238
1241
|
const res = await agent.post(`/food/${fakeId}/tags`).send({tags: "organic"}).expect(404);
|
|
1239
|
-
expect(res.body.title).
|
|
1242
|
+
expect(res.body.title).toBe("Document not found");
|
|
1243
|
+
expect(res.body.detail).toContain("Could not find document to PATCH");
|
|
1240
1244
|
});
|
|
1241
1245
|
|
|
1242
1246
|
it("array operation denied when user cannot update specific doc", async () => {
|
|
@@ -1259,7 +1263,8 @@ describe("@terreno/api", () => {
|
|
|
1259
1263
|
agent = await authAsUser(app, "notAdmin");
|
|
1260
1264
|
|
|
1261
1265
|
const res = await agent.post(`/food/${apple._id}/tags`).send({tags: "organic"}).expect(403);
|
|
1262
|
-
expect(res.body.title).
|
|
1266
|
+
expect(res.body.title).toBe("Update not allowed");
|
|
1267
|
+
expect(res.body.detail).toContain("Patch not allowed");
|
|
1263
1268
|
});
|
|
1264
1269
|
|
|
1265
1270
|
it("array operation transform error is handled", async () => {
|
|
@@ -1381,8 +1386,9 @@ describe("@terreno/api", () => {
|
|
|
1381
1386
|
|
|
1382
1387
|
// Send without required 'name' field
|
|
1383
1388
|
const res = await server.post("/required").send({about: "test"}).expect(400);
|
|
1384
|
-
expect(res.body.title).toBe("
|
|
1389
|
+
expect(res.body.title).toBe("Validation failed");
|
|
1385
1390
|
expect(res.body.detail).toContain("Required");
|
|
1391
|
+
expect(res.body.meta.fields.name).toContain("required");
|
|
1386
1392
|
});
|
|
1387
1393
|
|
|
1388
1394
|
it("preDelete hook throwing APIError is re-thrown", async () => {
|