@terreno/api 0.26.0 → 0.28.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/dist/__tests__/openApiCompat.test.js +8 -2
- package/dist/actions.openApi.test.js +1 -0
- package/dist/actions.test.js +3 -1
- package/dist/api.arrayOperations.test.js +1 -0
- package/dist/api.coverage.test.d.ts +1 -0
- package/dist/api.coverage.test.js +193 -0
- package/dist/api.errorPaths.test.d.ts +1 -0
- package/dist/api.errorPaths.test.js +280 -0
- package/dist/api.errors.test.js +7 -7
- package/dist/api.hooks.test.js +66 -8
- package/dist/api.js +245 -206
- package/dist/api.query.test.js +1 -0
- package/dist/api.test.js +11 -4
- package/dist/auth.js +1 -0
- package/dist/auth.test.js +123 -1
- package/dist/betterAuthSetup.js +1 -0
- package/dist/betterAuthSetup.test.js +1 -0
- package/dist/config.js +7 -4
- package/dist/config.test.js +28 -3
- package/dist/configuration.test.js +1 -0
- package/dist/configurationApp.js +1 -0
- package/dist/configurationPlugin.test.js +80 -0
- package/dist/consentApp.test.js +1 -0
- package/dist/docLoader.js +16 -54
- package/dist/docLoader.test.js +2 -1
- package/dist/envConfigurationPlugin.js +1 -0
- package/dist/errors.d.ts +72 -5
- package/dist/errors.js +265 -67
- package/dist/errors.test.js +163 -13
- package/dist/example.js +3 -2
- package/dist/expressServer.test.js +1 -0
- package/dist/githubAuth.js +2 -0
- package/dist/githubAuth.test.js +1 -0
- package/dist/httpClient.d.ts +2 -3
- package/dist/httpClient.js +4 -4
- package/dist/httpClient.test.js +3 -2
- package/dist/logger.test.js +107 -0
- package/dist/notifiers/googleChatNotifier.test.js +2 -1
- package/dist/notifiers/slackNotifier.test.js +1 -1
- package/dist/notifiers/zoomNotifier.test.js +2 -1
- package/dist/openApi.test.js +1 -0
- package/dist/openApiBuilder.js +2 -1
- package/dist/openApiBuilder.test.js +3 -2
- package/dist/openApiValidator.js +1 -0
- package/dist/openApiValidator.test.js +41 -0
- package/dist/permissions.js +0 -2
- package/dist/permissions.middleware.test.js +8 -42
- package/dist/permissions.test.js +1 -0
- package/dist/plugins.js +8 -0
- package/dist/plugins.test.js +5 -2
- package/dist/populate.js +4 -2
- package/dist/populate.test.js +1 -0
- package/dist/realtime/realtime.test.js +13 -3
- package/dist/realtime/types.d.ts +1 -1
- package/dist/requestContext.test.js +28 -0
- package/dist/terrenoApp.js +1 -0
- package/dist/tests/createTestData.js +11 -6
- package/dist/tests/models.js +1 -0
- package/dist/transformers.js +4 -2
- package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +3 -1
- package/package.json +7 -7
- package/src/__tests__/openApiCompat.test.ts +8 -2
- package/src/actions.openApi.test.ts +1 -0
- package/src/actions.test.ts +3 -1
- package/src/api.arrayOperations.test.ts +1 -0
- package/src/api.coverage.test.ts +123 -0
- package/src/api.errorPaths.test.ts +173 -0
- package/src/api.errors.test.ts +7 -7
- package/src/api.hooks.test.ts +25 -1
- package/src/api.query.test.ts +1 -0
- package/src/api.test.ts +11 -4
- package/src/api.ts +106 -57
- package/src/auth.test.ts +92 -3
- package/src/auth.ts +6 -0
- package/src/betterAuthSetup.test.ts +1 -0
- package/src/betterAuthSetup.ts +3 -0
- package/src/config.test.ts +25 -3
- package/src/config.ts +7 -4
- package/src/configuration.test.ts +1 -0
- package/src/configurationApp.ts +2 -0
- package/src/configurationPlugin.test.ts +46 -1
- package/src/consentApp.test.ts +1 -0
- package/src/docLoader.test.ts +2 -1
- package/src/docLoader.ts +17 -20
- package/src/envConfigurationPlugin.ts +2 -0
- package/src/errors.test.ts +178 -13
- package/src/errors.ts +278 -79
- package/src/example.ts +3 -2
- package/src/expressServer.test.ts +1 -0
- package/src/expressServer.ts +4 -2
- package/src/githubAuth.test.ts +1 -0
- package/src/githubAuth.ts +2 -0
- package/src/httpClient.test.ts +3 -2
- package/src/httpClient.ts +4 -4
- package/src/logger.test.ts +78 -0
- package/src/notifiers/googleChatNotifier.test.ts +2 -2
- package/src/notifiers/slackNotifier.test.ts +2 -2
- package/src/notifiers/zoomNotifier.test.ts +2 -2
- package/src/openApi.test.ts +1 -0
- package/src/openApiBuilder.test.ts +7 -6
- package/src/openApiBuilder.ts +2 -1
- package/src/openApiValidator.test.ts +64 -0
- package/src/openApiValidator.ts +1 -0
- package/src/permissions.middleware.test.ts +8 -11
- package/src/permissions.test.ts +1 -0
- package/src/permissions.ts +0 -2
- package/src/plugins.test.ts +5 -2
- package/src/plugins.ts +9 -0
- package/src/populate.test.ts +1 -0
- package/src/populate.ts +4 -2
- package/src/realtime/realtime.test.ts +13 -3
- package/src/realtime/realtimeApp.ts +1 -0
- package/src/realtime/registry.ts +1 -0
- package/src/realtime/types.ts +1 -2
- package/src/requestContext.test.ts +32 -0
- package/src/terrenoApp.ts +1 -0
- package/src/tests/createTestData.ts +11 -6
- package/src/tests/models.ts +1 -0
- package/src/transformers.ts +4 -2
- package/src/vendor/wesleytodd-openapi/lib/generate-doc.js +4 -1
- package/tsconfig.json +4 -4
- /package/{types.d.ts → src/types.d.ts} +0 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// noExplicitAny: test mock typing
|
|
2
|
+
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
3
|
+
import {beforeEach, describe, expect, it} from "bun:test";
|
|
4
|
+
import type express from "express";
|
|
5
|
+
import mongoose from "mongoose";
|
|
6
|
+
import supertest from "supertest";
|
|
7
|
+
import type TestAgent from "supertest/lib/agent";
|
|
8
|
+
|
|
9
|
+
import {modelRouter} from "./api";
|
|
10
|
+
import {addAuthRoutes, setupAuth} from "./auth";
|
|
11
|
+
import {Permissions} from "./permissions";
|
|
12
|
+
import {type Food, FoodModel, getBaseServer, setupDb, UserModel} from "./tests";
|
|
13
|
+
|
|
14
|
+
// A model without the isDeleted plugin so deletes call doc.deleteOne(). The
|
|
15
|
+
// document delete hook throws to exercise the hard-delete error branch.
|
|
16
|
+
const explosiveSchema = new mongoose.Schema<{name: string}>({
|
|
17
|
+
name: {description: "The name of the item", type: String},
|
|
18
|
+
});
|
|
19
|
+
explosiveSchema.pre("deleteOne", {document: true, query: false}, () => {
|
|
20
|
+
throw new Error("deleteOne exploded");
|
|
21
|
+
});
|
|
22
|
+
const ExplosiveModel = mongoose.model<{name: string}>("ExplosiveCoverage", explosiveSchema);
|
|
23
|
+
|
|
24
|
+
describe("modelRouter error path coverage", () => {
|
|
25
|
+
let server: TestAgent;
|
|
26
|
+
let app: express.Application;
|
|
27
|
+
let admin: any;
|
|
28
|
+
let apple: Food;
|
|
29
|
+
|
|
30
|
+
beforeEach(async () => {
|
|
31
|
+
[admin] = await setupDb();
|
|
32
|
+
|
|
33
|
+
apple = await FoodModel.create({
|
|
34
|
+
calories: 100,
|
|
35
|
+
categories: [
|
|
36
|
+
{name: "Fruit", show: true},
|
|
37
|
+
{name: "Popular", show: false},
|
|
38
|
+
],
|
|
39
|
+
created: new Date("2021-12-03T00:00:30.000Z"),
|
|
40
|
+
hidden: false,
|
|
41
|
+
name: "Apple",
|
|
42
|
+
ownerId: admin._id,
|
|
43
|
+
tags: ["healthy", "cheap"],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
app = getBaseServer();
|
|
47
|
+
setupAuth(app, UserModel as any);
|
|
48
|
+
addAuthRoutes(app, UserModel as any);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("returns 400 when an array operation transformer throws a non-APIError", async () => {
|
|
52
|
+
app.use(
|
|
53
|
+
"/food",
|
|
54
|
+
modelRouter(FoodModel, {
|
|
55
|
+
allowAnonymous: true,
|
|
56
|
+
permissions: {
|
|
57
|
+
create: [Permissions.IsAny],
|
|
58
|
+
delete: [Permissions.IsAny],
|
|
59
|
+
list: [Permissions.IsAny],
|
|
60
|
+
read: [Permissions.IsAny],
|
|
61
|
+
update: [Permissions.IsAny],
|
|
62
|
+
},
|
|
63
|
+
transformer: {
|
|
64
|
+
transform: () => {
|
|
65
|
+
throw new Error("array transform boom");
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
server = supertest(app);
|
|
71
|
+
|
|
72
|
+
const res = await server.post(`/food/${apple._id}/tags`).send({tags: "organic"}).expect(403);
|
|
73
|
+
expect(res.body.title).toBe("Transform error");
|
|
74
|
+
expect(res.body.detail).toContain("array transform boom");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("returns 400 when saving an array operation fails validation", async () => {
|
|
78
|
+
app.use(
|
|
79
|
+
"/food",
|
|
80
|
+
modelRouter(FoodModel, {
|
|
81
|
+
allowAnonymous: true,
|
|
82
|
+
permissions: {
|
|
83
|
+
create: [Permissions.IsAny],
|
|
84
|
+
delete: [Permissions.IsAny],
|
|
85
|
+
list: [Permissions.IsAny],
|
|
86
|
+
read: [Permissions.IsAny],
|
|
87
|
+
update: [Permissions.IsAny],
|
|
88
|
+
},
|
|
89
|
+
})
|
|
90
|
+
);
|
|
91
|
+
server = supertest(app);
|
|
92
|
+
|
|
93
|
+
// eatenBy is an ObjectId array; pushing a non-ObjectId value fails the
|
|
94
|
+
// cast when the document is saved.
|
|
95
|
+
const res = await server
|
|
96
|
+
.post(`/food/${apple._id}/eatenBy`)
|
|
97
|
+
.send({eatenBy: "not-an-object-id"})
|
|
98
|
+
.expect(400);
|
|
99
|
+
expect(res.body.title).toContain("PATCH Pre Update error");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("returns 400 when a hard delete (no isDeleted plugin) fails", async () => {
|
|
103
|
+
const doc = await ExplosiveModel.create({name: "boom"});
|
|
104
|
+
app.use(
|
|
105
|
+
"/explosive",
|
|
106
|
+
modelRouter(ExplosiveModel as any, {
|
|
107
|
+
allowAnonymous: true,
|
|
108
|
+
permissions: {
|
|
109
|
+
create: [Permissions.IsAny],
|
|
110
|
+
delete: [Permissions.IsAny],
|
|
111
|
+
list: [Permissions.IsAny],
|
|
112
|
+
read: [Permissions.IsAny],
|
|
113
|
+
update: [Permissions.IsAny],
|
|
114
|
+
},
|
|
115
|
+
})
|
|
116
|
+
);
|
|
117
|
+
server = supertest(app);
|
|
118
|
+
|
|
119
|
+
const res = await server.delete(`/explosive/${doc._id}`).expect(400);
|
|
120
|
+
expect(res.body.title).toBe("Delete error");
|
|
121
|
+
expect(res.body.detail).toContain("deleteOne exploded");
|
|
122
|
+
});
|
|
123
|
+
});
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, it, spyOn} from "bun:test";
|
|
2
|
+
import * as Sentry from "@sentry/bun";
|
|
3
|
+
import {assert} from "chai";
|
|
4
|
+
import type express from "express";
|
|
5
|
+
import mongoose from "mongoose";
|
|
6
|
+
import supertest from "supertest";
|
|
7
|
+
import type TestAgent from "supertest/lib/agent";
|
|
8
|
+
|
|
9
|
+
import {type ModelRouterOptions, modelRouter} from "./api";
|
|
10
|
+
import {addAuthRoutes, setupAuth} from "./auth";
|
|
11
|
+
import {Permissions} from "./permissions";
|
|
12
|
+
import {type Food, FoodModel, getBaseServer, setupDb, UserModel} from "./tests";
|
|
13
|
+
|
|
14
|
+
type SetupAuthUserModel = Parameters<typeof setupAuth>[1];
|
|
15
|
+
const authUserModel = UserModel as unknown as SetupAuthUserModel;
|
|
16
|
+
|
|
17
|
+
interface RestorableSpy {
|
|
18
|
+
mockRestore?: () => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const anyPermissions = {
|
|
22
|
+
create: [Permissions.IsAny],
|
|
23
|
+
delete: [Permissions.IsAny],
|
|
24
|
+
list: [Permissions.IsAny],
|
|
25
|
+
read: [Permissions.IsAny],
|
|
26
|
+
update: [Permissions.IsAny],
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
describe("modelRouter error paths", () => {
|
|
30
|
+
let app: express.Application;
|
|
31
|
+
let server: TestAgent;
|
|
32
|
+
|
|
33
|
+
beforeEach(async () => {
|
|
34
|
+
await setupDb();
|
|
35
|
+
app = getBaseServer();
|
|
36
|
+
setupAuth(app, authUserModel);
|
|
37
|
+
addAuthRoutes(app, authUserModel);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
// Restore any spies installed on the shared FoodModel / Sentry.
|
|
42
|
+
(FoodModel.find as unknown as RestorableSpy).mockRestore?.();
|
|
43
|
+
(FoodModel.findById as unknown as RestorableSpy).mockRestore?.();
|
|
44
|
+
(Sentry.captureMessage as unknown as RestorableSpy).mockRestore?.();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const mountFood = (options: Partial<ModelRouterOptions<Food>> = {}): void => {
|
|
48
|
+
app.use(
|
|
49
|
+
"/food",
|
|
50
|
+
modelRouter(FoodModel, {
|
|
51
|
+
allowAnonymous: true,
|
|
52
|
+
permissions: anyPermissions,
|
|
53
|
+
...options,
|
|
54
|
+
})
|
|
55
|
+
);
|
|
56
|
+
server = supertest(app);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
it("wraps populate failures on create in a Populate error", async () => {
|
|
60
|
+
mountFood({populatePaths: [{fields: ["email"], path: "ownerId"}]});
|
|
61
|
+
|
|
62
|
+
// Creation succeeds, but the follow-up populate query rejects.
|
|
63
|
+
const rejectingQuery = {
|
|
64
|
+
exec: () => Promise.reject(new Error("populate exploded")),
|
|
65
|
+
populate() {
|
|
66
|
+
return this;
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
spyOn(FoodModel, "findById").mockReturnValue(
|
|
70
|
+
rejectingQuery as unknown as ReturnType<typeof FoodModel.findById>
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const res = await server
|
|
74
|
+
.post("/food")
|
|
75
|
+
.send({calories: 5, name: "Broccoli", ownerId: new mongoose.Types.ObjectId()})
|
|
76
|
+
.expect(400);
|
|
77
|
+
assert.include(res.body.title, "Populate error");
|
|
78
|
+
assert.include(res.body.detail, "populate exploded");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("wraps list query failures in a List error", async () => {
|
|
82
|
+
mountFood();
|
|
83
|
+
|
|
84
|
+
const rejectingQuery = {
|
|
85
|
+
exec: () => Promise.reject(new Error("list exploded")),
|
|
86
|
+
limit() {
|
|
87
|
+
return this;
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
spyOn(FoodModel, "find").mockReturnValue(
|
|
91
|
+
rejectingQuery as unknown as ReturnType<typeof FoodModel.find>
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const res = await server.get("/food").expect(500);
|
|
95
|
+
assert.include(res.body.title, "List error");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("swallows Sentry failures when warning about truncated unpaginated lists", async () => {
|
|
99
|
+
mountFood({defaultLimit: 1});
|
|
100
|
+
const ownerId = new mongoose.Types.ObjectId();
|
|
101
|
+
await FoodModel.create([
|
|
102
|
+
{calories: 1, name: "A", ownerId} as Partial<Food>,
|
|
103
|
+
{calories: 2, name: "B", ownerId} as Partial<Food>,
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
const captureSpy = spyOn(Sentry, "captureMessage").mockImplementation(() => {
|
|
107
|
+
throw new Error("sentry down");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const res = await server.get("/food").expect(200);
|
|
111
|
+
// The extra document is sliced off so only `limit` rows are returned.
|
|
112
|
+
assert.lengthOf(res.body.data, 1);
|
|
113
|
+
assert.isTrue(res.body.more);
|
|
114
|
+
assert.isAbove(captureSpy.mock.calls.length, 0);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("wraps deleteOne failures for models without soft delete", async () => {
|
|
118
|
+
const brittleSchema = new mongoose.Schema(
|
|
119
|
+
{name: {description: "The name", type: String}},
|
|
120
|
+
{strict: "throw"}
|
|
121
|
+
);
|
|
122
|
+
brittleSchema.pre("deleteOne", {document: true, query: false}, () => {
|
|
123
|
+
throw new Error("deleteOne exploded");
|
|
124
|
+
});
|
|
125
|
+
const BrittleModel = mongoose.model(`BrittleDelete_${Date.now()}`, brittleSchema);
|
|
126
|
+
|
|
127
|
+
app.use(
|
|
128
|
+
"/brittle",
|
|
129
|
+
modelRouter(BrittleModel, {allowAnonymous: true, permissions: anyPermissions})
|
|
130
|
+
);
|
|
131
|
+
server = supertest(app);
|
|
132
|
+
|
|
133
|
+
const created = await BrittleModel.create({name: "doomed"});
|
|
134
|
+
const res = await server.delete(`/brittle/${created._id}`).expect(400);
|
|
135
|
+
assert.equal(res.body.title, "Delete error");
|
|
136
|
+
assert.include(res.body.detail, "deleteOne exploded");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("wraps non-APIError transformer failures during array operations", async () => {
|
|
140
|
+
mountFood({
|
|
141
|
+
transformer: {
|
|
142
|
+
transform: () => {
|
|
143
|
+
throw new Error("transform exploded");
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
const food = await FoodModel.create({
|
|
148
|
+
calories: 1,
|
|
149
|
+
name: "Tagged",
|
|
150
|
+
ownerId: new mongoose.Types.ObjectId(),
|
|
151
|
+
} as Partial<Food>);
|
|
152
|
+
|
|
153
|
+
const res = await server.post(`/food/${food._id}/tags`).send({tags: "vegetable"}).expect(403);
|
|
154
|
+
assert.equal(res.body.title, "Transform error");
|
|
155
|
+
assert.include(res.body.detail, "transform exploded");
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("wraps save failures during array operations", async () => {
|
|
159
|
+
mountFood();
|
|
160
|
+
const food = await FoodModel.create({
|
|
161
|
+
calories: 1,
|
|
162
|
+
name: "Eaten",
|
|
163
|
+
ownerId: new mongoose.Types.ObjectId(),
|
|
164
|
+
} as Partial<Food>);
|
|
165
|
+
|
|
166
|
+
// eatenBy is an ObjectId array, so pushing an unparseable id fails on save.
|
|
167
|
+
const res = await server
|
|
168
|
+
.post(`/food/${food._id}/eatenBy`)
|
|
169
|
+
.send({eatenBy: "not-an-object-id"})
|
|
170
|
+
.expect(400);
|
|
171
|
+
assert.include(res.body.title, "PATCH Pre Update error");
|
|
172
|
+
});
|
|
173
|
+
});
|
package/src/api.errors.test.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// noExplicitAny: test mock typing
|
|
1
2
|
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
2
3
|
import {describe, expect, it} from "bun:test";
|
|
3
4
|
import mongoose from "mongoose";
|
|
@@ -28,24 +29,23 @@ describe("errors module", () => {
|
|
|
28
29
|
expect(error.status).toBe(500);
|
|
29
30
|
});
|
|
30
31
|
|
|
31
|
-
it("
|
|
32
|
+
it("exposes a wrapped error as the cause, keeping the message stable", () => {
|
|
32
33
|
const originalError = new Error("Original error");
|
|
33
34
|
const apiError = new APIError({
|
|
34
35
|
error: originalError,
|
|
35
36
|
title: "Wrapped error",
|
|
36
37
|
});
|
|
37
|
-
expect(apiError.message).
|
|
38
|
-
expect(
|
|
39
|
-
expect(apiError.message).toContain(originalError.stack as string);
|
|
38
|
+
expect(apiError.message).toBe("Wrapped error");
|
|
39
|
+
expect(apiError.cause).toBe(originalError);
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
it("
|
|
42
|
+
it("keeps detail out of the message", () => {
|
|
43
43
|
const error = new APIError({
|
|
44
44
|
detail: "More details here",
|
|
45
45
|
title: "Test error",
|
|
46
46
|
});
|
|
47
|
-
expect(error.message).
|
|
48
|
-
expect(error.
|
|
47
|
+
expect(error.message).toBe("Test error");
|
|
48
|
+
expect(error.detail).toBe("More details here");
|
|
49
49
|
});
|
|
50
50
|
|
|
51
51
|
it("sets fields in meta when provided", () => {
|
package/src/api.hooks.test.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
// noExplicitAny: test mock typing
|
|
1
2
|
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
2
|
-
import {beforeEach, describe, expect, it} from "bun:test";
|
|
3
|
+
import {beforeEach, describe, expect, it, type mock} from "bun:test";
|
|
4
|
+
import * as Sentry from "@sentry/bun";
|
|
3
5
|
import type express from "express";
|
|
4
6
|
import supertest from "supertest";
|
|
5
7
|
import type TestAgent from "supertest/lib/agent";
|
|
@@ -10,6 +12,8 @@ import {APIError} from "./errors";
|
|
|
10
12
|
import {Permissions} from "./permissions";
|
|
11
13
|
import {authAsUser, type Food, FoodModel, getBaseServer, setupDb, UserModel} from "./tests";
|
|
12
14
|
|
|
15
|
+
const captureExceptionMock = Sentry.captureException as unknown as ReturnType<typeof mock>;
|
|
16
|
+
|
|
13
17
|
describe("pre and post hooks", () => {
|
|
14
18
|
let server: TestAgent;
|
|
15
19
|
let app: express.Application;
|
|
@@ -21,6 +25,7 @@ describe("pre and post hooks", () => {
|
|
|
21
25
|
setupAuth(app, UserModel as any);
|
|
22
26
|
addAuthRoutes(app, UserModel as any);
|
|
23
27
|
agent = await authAsUser(app, "notAdmin");
|
|
28
|
+
captureExceptionMock.mockClear?.();
|
|
24
29
|
});
|
|
25
30
|
|
|
26
31
|
it("pre hooks change data", async () => {
|
|
@@ -229,7 +234,10 @@ describe("pre and post hooks", () => {
|
|
|
229
234
|
.expect(400);
|
|
230
235
|
|
|
231
236
|
expect(res.body.title).toBe("Custom preCreate error");
|
|
237
|
+
// Serialized when true so the frontend can suppress duplicate Sentry reporting; also
|
|
238
|
+
// suppresses Sentry capture in apiErrorMiddleware.
|
|
232
239
|
expect(res.body.disableExternalErrorTracking).toBe(true);
|
|
240
|
+
expect(captureExceptionMock).not.toHaveBeenCalled();
|
|
233
241
|
});
|
|
234
242
|
|
|
235
243
|
it("preCreate hook preserves disableExternalErrorTracking on non-APIError", async () => {
|
|
@@ -262,7 +270,10 @@ describe("pre and post hooks", () => {
|
|
|
262
270
|
.expect(400);
|
|
263
271
|
|
|
264
272
|
expect(res.body.title).toContain("preCreate hook error");
|
|
273
|
+
// Serialized when true so the frontend can suppress duplicate Sentry reporting; also
|
|
274
|
+
// suppresses Sentry capture in apiErrorMiddleware.
|
|
265
275
|
expect(res.body.disableExternalErrorTracking).toBe(true);
|
|
276
|
+
expect(captureExceptionMock).not.toHaveBeenCalled();
|
|
266
277
|
});
|
|
267
278
|
|
|
268
279
|
it("preUpdate hook preserves disableExternalErrorTracking on APIError", async () => {
|
|
@@ -310,7 +321,10 @@ describe("pre and post hooks", () => {
|
|
|
310
321
|
.expect(400);
|
|
311
322
|
|
|
312
323
|
expect(res.body.title).toBe("Custom preUpdate error");
|
|
324
|
+
// Serialized when true so the frontend can suppress duplicate Sentry reporting; also
|
|
325
|
+
// suppresses Sentry capture in apiErrorMiddleware.
|
|
313
326
|
expect(res.body.disableExternalErrorTracking).toBe(true);
|
|
327
|
+
expect(captureExceptionMock).not.toHaveBeenCalled();
|
|
314
328
|
});
|
|
315
329
|
|
|
316
330
|
it("preUpdate hook preserves disableExternalErrorTracking on non-APIError", async () => {
|
|
@@ -356,7 +370,10 @@ describe("pre and post hooks", () => {
|
|
|
356
370
|
.expect(400);
|
|
357
371
|
|
|
358
372
|
expect(res.body.title).toContain("preUpdate hook error");
|
|
373
|
+
// Serialized when true so the frontend can suppress duplicate Sentry reporting; also
|
|
374
|
+
// suppresses Sentry capture in apiErrorMiddleware.
|
|
359
375
|
expect(res.body.disableExternalErrorTracking).toBe(true);
|
|
376
|
+
expect(captureExceptionMock).not.toHaveBeenCalled();
|
|
360
377
|
});
|
|
361
378
|
|
|
362
379
|
it("preDelete hook preserves disableExternalErrorTracking on non-APIError", async () => {
|
|
@@ -397,7 +414,10 @@ describe("pre and post hooks", () => {
|
|
|
397
414
|
const res = await agent.delete(`/food/${spinach._id}`).expect(403);
|
|
398
415
|
|
|
399
416
|
expect(res.body.title).toContain("preDelete hook error");
|
|
417
|
+
// Serialized when true so the frontend can suppress duplicate Sentry reporting; also
|
|
418
|
+
// suppresses Sentry capture in apiErrorMiddleware.
|
|
400
419
|
expect(res.body.disableExternalErrorTracking).toBe(true);
|
|
420
|
+
expect(captureExceptionMock).not.toHaveBeenCalled();
|
|
401
421
|
});
|
|
402
422
|
});
|
|
403
423
|
|
|
@@ -425,6 +445,7 @@ describe("hook error handling", () => {
|
|
|
425
445
|
app = getBaseServer();
|
|
426
446
|
setupAuth(app, UserModel as any);
|
|
427
447
|
addAuthRoutes(app, UserModel as any);
|
|
448
|
+
captureExceptionMock.mockClear?.();
|
|
428
449
|
});
|
|
429
450
|
|
|
430
451
|
it("preCreate returning undefined throws error", async () => {
|
|
@@ -700,6 +721,9 @@ describe("hook error handling", () => {
|
|
|
700
721
|
|
|
701
722
|
const res = await agent.delete(`/food/${spinach._id}`).expect(400);
|
|
702
723
|
expect(res.body.title).toBe("Custom preDelete APIError");
|
|
724
|
+
// Serialized when true so the frontend can suppress duplicate Sentry reporting; also
|
|
725
|
+
// suppresses Sentry capture in apiErrorMiddleware.
|
|
703
726
|
expect(res.body.disableExternalErrorTracking).toBe(true);
|
|
727
|
+
expect(captureExceptionMock).not.toHaveBeenCalled();
|
|
704
728
|
});
|
|
705
729
|
});
|
package/src/api.query.test.ts
CHANGED
package/src/api.test.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// noExplicitAny: test mock typing
|
|
1
2
|
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
2
3
|
// biome-ignore-all lint/suspicious/noImplicitAnyLet: test mock typing
|
|
3
4
|
import {beforeEach, describe, expect, it} from "bun:test";
|
|
@@ -625,7 +626,8 @@ describe("@terreno/api", () => {
|
|
|
625
626
|
server = supertest(app);
|
|
626
627
|
|
|
627
628
|
const res = await server.post("/required").send({about: "test"}).expect(400);
|
|
628
|
-
expect(res.body.title).
|
|
629
|
+
expect(res.body.title).toBe("Create error");
|
|
630
|
+
expect(res.body.detail).toContain("Required");
|
|
629
631
|
});
|
|
630
632
|
|
|
631
633
|
it("preDelete hook throwing APIError is re-thrown", async () => {
|
|
@@ -654,6 +656,7 @@ describe("@terreno/api", () => {
|
|
|
654
656
|
|
|
655
657
|
const res = await agent.delete(`/food/${spinach._id}`).expect(400);
|
|
656
658
|
expect(res.body.title).toBe("Custom preDelete APIError");
|
|
659
|
+
// Serialized when true so the frontend can suppress duplicate Sentry reporting.
|
|
657
660
|
expect(res.body.disableExternalErrorTracking).toBe(true);
|
|
658
661
|
});
|
|
659
662
|
});
|
|
@@ -1378,7 +1381,8 @@ describe("@terreno/api", () => {
|
|
|
1378
1381
|
|
|
1379
1382
|
// Send without required 'name' field
|
|
1380
1383
|
const res = await server.post("/required").send({about: "test"}).expect(400);
|
|
1381
|
-
expect(res.body.title).
|
|
1384
|
+
expect(res.body.title).toBe("Create error");
|
|
1385
|
+
expect(res.body.detail).toContain("Required");
|
|
1382
1386
|
});
|
|
1383
1387
|
|
|
1384
1388
|
it("preDelete hook throwing APIError is re-thrown", async () => {
|
|
@@ -1407,6 +1411,7 @@ describe("@terreno/api", () => {
|
|
|
1407
1411
|
|
|
1408
1412
|
const res = await agent.delete(`/food/${spinach._id}`).expect(400);
|
|
1409
1413
|
expect(res.body.title).toBe("Custom preDelete APIError");
|
|
1414
|
+
// Serialized when true so the frontend can suppress duplicate Sentry reporting.
|
|
1410
1415
|
expect(res.body.disableExternalErrorTracking).toBe(true);
|
|
1411
1416
|
});
|
|
1412
1417
|
});
|
|
@@ -2165,7 +2170,8 @@ describe("@terreno/api", () => {
|
|
|
2165
2170
|
.send({calories: 10, name: "New Food", ownerId: admin._id})
|
|
2166
2171
|
.expect(400);
|
|
2167
2172
|
|
|
2168
|
-
expect(res.body.title).
|
|
2173
|
+
expect(res.body.title).toBe("Transform error");
|
|
2174
|
+
expect(res.body.detail).toContain("generic transform error");
|
|
2169
2175
|
});
|
|
2170
2176
|
});
|
|
2171
2177
|
|
|
@@ -2211,7 +2217,8 @@ describe("@terreno/api", () => {
|
|
|
2211
2217
|
it("wraps non-APIError transform errors in a 403 APIError on update", async () => {
|
|
2212
2218
|
const res = await agent.patch(`/food/${spinach._id}`).send({name: "Updated"}).expect(403);
|
|
2213
2219
|
|
|
2214
|
-
expect(res.body.title).
|
|
2220
|
+
expect(res.body.title).toBe("PATCH failed");
|
|
2221
|
+
expect(res.body.detail).toContain("update transform error");
|
|
2215
2222
|
});
|
|
2216
2223
|
});
|
|
2217
2224
|
});
|