@terreno/api 56.0.0-beta.0 → 57.0.0-beta.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.
- package/CHANGELOG.md +2 -122
- package/LICENSE +21 -202
- package/dist/api.d.ts +3 -2
- package/dist/auth.js +21 -25
- package/dist/betterAuthSetup.d.ts +15 -1
- package/dist/betterAuthSetup.js +4 -6
- package/dist/configurationPlugin.js +2 -2
- package/dist/expressServer.js +11 -5
- package/dist/githubAuth.js +1 -4
- package/dist/plugins.d.ts +7 -6
- package/dist/plugins.js +24 -8
- package/dist/realtime/changeStreamWatcher.d.ts +1 -1
- package/dist/realtime/changeStreamWatcher.js +0 -2
- package/dist/realtime/realtimeApp.d.ts +1 -1
- package/dist/realtime/realtimeApp.js +16 -8
- package/dist/realtime/types.d.ts +14 -0
- package/dist/sync/executors.js +14 -14
- package/dist/sync/models.js +1 -1
- package/dist/sync/registry.d.ts +4 -4
- package/dist/sync/registry.js +1 -1
- package/dist/sync/socketHandlers.d.ts +1 -1
- package/dist/sync/syncSeqPlugin.js +25 -29
- package/dist/tests/models.d.ts +27 -11
- package/dist/tests/models.js +5 -3
- package/dist/tests/testHelper.d.ts +9 -3
- package/package.json +5 -6
- package/src/actions.test.ts +19 -12
- package/src/actions.ts +2 -2
- package/src/api.coverage.test.ts +7 -9
- package/src/api.errorPaths.test.ts +47 -1
- package/src/api.errors.test.ts +32 -23
- package/src/api.ts +22 -12
- package/src/auth.test.ts +251 -1
- package/src/auth.ts +25 -23
- package/src/betterAuthSetup.ts +7 -7
- package/src/configuration.test.ts +59 -38
- package/src/configurationPlugin.test.ts +72 -42
- package/src/configurationPlugin.ts +2 -2
- package/src/consentApp.test.ts +12 -12
- package/src/expressServer.ts +11 -5
- package/src/githubAuth.ts +1 -3
- package/src/middleware.test.ts +16 -0
- package/src/models/versionConfig.ts +1 -1
- package/src/openApiBuilder.test.ts +24 -9
- package/src/openApiValidator.test.ts +41 -0
- package/src/permissions.middleware.test.ts +73 -81
- package/src/permissions.test.ts +35 -25
- package/src/plugins.test.ts +74 -29
- package/src/plugins.ts +29 -11
- package/src/populate.test.ts +25 -16
- package/src/realtime/changeStreamWatcher.ts +1 -4
- package/src/realtime/realtime.test.ts +239 -0
- package/src/realtime/realtimeApp.ts +15 -8
- package/src/realtime/realtimeAppSetup.test.ts +159 -0
- package/src/realtime/types.ts +14 -0
- package/src/secretProvidersGcpClient.test.ts +66 -0
- package/src/sync/executors.ts +16 -14
- package/src/sync/models.ts +2 -1
- package/src/sync/registry.ts +6 -8
- package/src/sync/socketHandlers.ts +1 -3
- package/src/sync/syncFrontier.test.ts +2 -2
- package/src/sync/syncSeqPlugin.test.ts +446 -0
- package/src/sync/syncSeqPlugin.ts +43 -51
- package/src/tests/models.ts +8 -6
package/src/consentApp.test.ts
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
// noExplicitAny: test mock typing
|
|
2
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
3
1
|
import {afterEach, beforeEach, describe, expect, it} from "bun:test";
|
|
4
2
|
import type express from "express";
|
|
3
|
+
import type {HydratedDocument} from "mongoose";
|
|
5
4
|
import supertest from "supertest";
|
|
6
5
|
import type TestAgent from "supertest/lib/agent";
|
|
7
|
-
import {
|
|
6
|
+
import type {UserModel as UserMongooseModel} from "./auth";
|
|
7
|
+
import {ConsentApp, type ConsentAppOptions} from "./consentApp";
|
|
8
8
|
import {ConsentForm} from "./models/consentForm";
|
|
9
9
|
import {ConsentResponse} from "./models/consentResponse";
|
|
10
10
|
import {TerrenoApp} from "./terrenoApp";
|
|
11
|
-
import {authAsUser, setupDb, UserModel} from "./tests";
|
|
11
|
+
import {authAsUser, setupDb, type User, UserModel} from "./tests";
|
|
12
|
+
import type {ConsentFormDocument} from "./types/consentForm";
|
|
12
13
|
|
|
13
|
-
const buildApp = (consentAppOptions = {}): express.Application =>
|
|
14
|
+
const buildApp = (consentAppOptions: ConsentAppOptions = {}): express.Application =>
|
|
14
15
|
new TerrenoApp({
|
|
15
16
|
skipListen: true,
|
|
16
|
-
userModel: UserModel as
|
|
17
|
+
userModel: UserModel as unknown as UserMongooseModel,
|
|
17
18
|
})
|
|
18
19
|
.register(new ConsentApp(consentAppOptions))
|
|
19
20
|
.build();
|
|
20
21
|
|
|
21
22
|
describe("ConsentApp", () => {
|
|
22
|
-
let admin:
|
|
23
|
-
let notAdmin:
|
|
23
|
+
let admin: HydratedDocument<User>;
|
|
24
|
+
let notAdmin: HydratedDocument<User>;
|
|
24
25
|
let adminAgent: TestAgent;
|
|
25
26
|
let userAgent: TestAgent;
|
|
26
27
|
|
|
@@ -344,8 +345,7 @@ describe("ConsentApp", () => {
|
|
|
344
345
|
|
|
345
346
|
// Build app with resolver that only returns privacy forms
|
|
346
347
|
const filteredApp = buildApp({
|
|
347
|
-
resolveConsentForms: (_user
|
|
348
|
-
forms.filter((f) => f.type === "privacy"),
|
|
348
|
+
resolveConsentForms: (_user, forms) => forms.filter((f) => f.type === "privacy"),
|
|
349
349
|
});
|
|
350
350
|
const filteredAgent = await authAsUser(filteredApp, "notAdmin");
|
|
351
351
|
|
|
@@ -367,7 +367,7 @@ describe("ConsentApp", () => {
|
|
|
367
367
|
|
|
368
368
|
// Build app with resolver that skips admin users
|
|
369
369
|
const filteredApp = buildApp({
|
|
370
|
-
resolveConsentForms: (user
|
|
370
|
+
resolveConsentForms: (user, forms) => (user.admin ? [] : forms),
|
|
371
371
|
});
|
|
372
372
|
const filteredAdmin = await authAsUser(filteredApp, "admin");
|
|
373
373
|
const filteredUser = await authAsUser(filteredApp, "notAdmin");
|
|
@@ -388,7 +388,7 @@ describe("ConsentApp", () => {
|
|
|
388
388
|
});
|
|
389
389
|
|
|
390
390
|
describe("POST /consents/respond", () => {
|
|
391
|
-
let form:
|
|
391
|
+
let form: ConsentFormDocument;
|
|
392
392
|
|
|
393
393
|
beforeEach(async () => {
|
|
394
394
|
form = await ConsentForm.create({
|
package/src/expressServer.ts
CHANGED
|
@@ -7,6 +7,7 @@ import cloneDeep from "lodash/cloneDeep";
|
|
|
7
7
|
import onFinished from "on-finished";
|
|
8
8
|
import passport from "passport";
|
|
9
9
|
import type {ModelRouterOptions} from "./api";
|
|
10
|
+
import {APIError} from "./errors";
|
|
10
11
|
import {type LoggingOptions, logger} from "./logger";
|
|
11
12
|
import {sendToSlack} from "./notifiers/slackNotifier";
|
|
12
13
|
|
|
@@ -16,16 +17,16 @@ const IS_JEST = process.env.JEST_WORKER_ID !== undefined;
|
|
|
16
17
|
|
|
17
18
|
export const setupEnvironment = (): void => {
|
|
18
19
|
if (!process.env.TOKEN_ISSUER) {
|
|
19
|
-
throw new
|
|
20
|
+
throw new APIError({status: 500, title: "TOKEN_ISSUER must be set in env."});
|
|
20
21
|
}
|
|
21
22
|
if (!process.env.TOKEN_SECRET) {
|
|
22
|
-
throw new
|
|
23
|
+
throw new APIError({status: 500, title: "TOKEN_SECRET must be set."});
|
|
23
24
|
}
|
|
24
25
|
if (!process.env.REFRESH_TOKEN_SECRET) {
|
|
25
|
-
throw new
|
|
26
|
+
throw new APIError({status: 500, title: "REFRESH_TOKEN_SECRET must be set."});
|
|
26
27
|
}
|
|
27
28
|
if (!process.env.SESSION_SECRET) {
|
|
28
|
-
throw new
|
|
29
|
+
throw new APIError({status: 500, title: "SESSION_SECRET must be set."});
|
|
29
30
|
}
|
|
30
31
|
if (!process.env.TOKEN_EXPIRES_IN && !IS_JEST) {
|
|
31
32
|
logger.warn("TOKEN_EXPIRES_IN is not set so using default.");
|
|
@@ -190,7 +191,12 @@ export const cronjob = (
|
|
|
190
191
|
try {
|
|
191
192
|
new cron.CronJob(cronSchedule, callback, null, true, "America/Chicago");
|
|
192
193
|
} catch (error) {
|
|
193
|
-
throw new
|
|
194
|
+
throw new APIError({
|
|
195
|
+
cause: error,
|
|
196
|
+
detail: `Failed to create cronjob ${name} with schedule ${cronSchedule}: ${error}`,
|
|
197
|
+
status: 500,
|
|
198
|
+
title: "Failed to create cronjob",
|
|
199
|
+
});
|
|
194
200
|
}
|
|
195
201
|
};
|
|
196
202
|
|
package/src/githubAuth.ts
CHANGED
|
@@ -90,9 +90,7 @@ export const setupGitHubAuth = (
|
|
|
90
90
|
|
|
91
91
|
passport.use(
|
|
92
92
|
"github",
|
|
93
|
-
|
|
94
|
-
// biome-ignore lint/suspicious/noExplicitAny: passport-github2's typed constructor overloads don't match passReqToCallback variant
|
|
95
|
-
new (GitHubStrategy as any)(
|
|
93
|
+
new GitHubStrategy(
|
|
96
94
|
{
|
|
97
95
|
callbackURL: githubOptions.callbackURL,
|
|
98
96
|
clientID: githubOptions.clientId,
|
package/src/middleware.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {beforeEach, describe, expect, it, type Mock, mock} from "bun:test";
|
|
2
2
|
import * as Sentry from "@sentry/bun";
|
|
3
|
+
import {assert} from "chai";
|
|
3
4
|
import express, {type NextFunction, type Request, type Response} from "express";
|
|
4
5
|
import supertest from "supertest";
|
|
5
6
|
|
|
@@ -170,6 +171,21 @@ describe("jsonResponseRequestIdMiddleware", () => {
|
|
|
170
171
|
expect(res.body.requestId).toBeUndefined();
|
|
171
172
|
});
|
|
172
173
|
|
|
174
|
+
it("leaves bodies untouched when no request context or requestId exists", async () => {
|
|
175
|
+
// Without requestContextMiddleware there is no requestId to correlate on, so
|
|
176
|
+
// the middleware must pass payloads through unchanged.
|
|
177
|
+
const app = express();
|
|
178
|
+
app.use(jsonResponseRequestIdMiddleware);
|
|
179
|
+
app.get("/object", (_req, res) => {
|
|
180
|
+
return res.json({hello: "world"});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const res = await supertest(app).get("/object").expect(200);
|
|
184
|
+
|
|
185
|
+
assert.deepEqual(res.body, {hello: "world"});
|
|
186
|
+
assert.isUndefined(res.headers["x-request-id"]);
|
|
187
|
+
});
|
|
188
|
+
|
|
173
189
|
it("uses incoming X-Request-ID on wrapped object responses", async () => {
|
|
174
190
|
const app = buildStackedApp();
|
|
175
191
|
const res = await supertest(app)
|
|
@@ -79,7 +79,7 @@ const versionConfigSchema = new mongoose.Schema<VersionConfigDocument, VersionCo
|
|
|
79
79
|
// Enforce singleton: only one VersionConfig document can exist.
|
|
80
80
|
// The _singleton field is always "config" (required, immutable, enum-constrained)
|
|
81
81
|
// and a unique index guarantees at most one document.
|
|
82
|
-
(versionConfigSchema as mongoose.Schema).add({
|
|
82
|
+
(versionConfigSchema as unknown as mongoose.Schema).add({
|
|
83
83
|
_singleton: {
|
|
84
84
|
default: "config",
|
|
85
85
|
description: "Sentinel field to enforce singleton via unique index",
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// noExplicitAny: test mock typing
|
|
2
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
3
1
|
import {beforeEach, describe, expect, it} from "bun:test";
|
|
4
2
|
import type express from "express";
|
|
5
3
|
import type {Router} from "express";
|
|
@@ -7,12 +5,25 @@ import supertest from "supertest";
|
|
|
7
5
|
import type TestAgent from "supertest/lib/agent";
|
|
8
6
|
|
|
9
7
|
import {type ModelRouterOptions, modelRouter} from "./api";
|
|
8
|
+
import type {UserModel as AuthUserModel} from "./auth";
|
|
10
9
|
import {createOpenApiBuilder, OpenApiMiddlewareBuilder} from "./openApiBuilder";
|
|
11
10
|
import {Permissions} from "./permissions";
|
|
12
11
|
import {TerrenoApp} from "./terrenoApp";
|
|
13
|
-
import {FoodModel, UserModel} from "./tests";
|
|
12
|
+
import {type Food, FoodModel, UserModel} from "./tests";
|
|
13
|
+
|
|
14
|
+
/** The subset of an OpenAPI parameter object the assertions below inspect. */
|
|
15
|
+
interface OpenApiParameter {
|
|
16
|
+
name: string;
|
|
17
|
+
in: string;
|
|
18
|
+
required?: boolean;
|
|
19
|
+
description?: string;
|
|
20
|
+
schema: {type: string};
|
|
21
|
+
}
|
|
14
22
|
|
|
15
|
-
function addRoutesWithBuilder(
|
|
23
|
+
function addRoutesWithBuilder(
|
|
24
|
+
router: Router,
|
|
25
|
+
options?: Partial<ModelRouterOptions<unknown>>
|
|
26
|
+
): void {
|
|
16
27
|
// Add a custom endpoint using the OpenApiMiddlewareBuilder
|
|
17
28
|
const statsMiddleware = createOpenApiBuilder(options ?? {})
|
|
18
29
|
.withTags(["Stats"])
|
|
@@ -124,8 +135,8 @@ function addRoutesWithBuilder(router: Router, options?: Partial<ModelRouterOptio
|
|
|
124
135
|
// Standard modelRouter for food
|
|
125
136
|
router.use(
|
|
126
137
|
"/food",
|
|
127
|
-
modelRouter(FoodModel
|
|
128
|
-
...options,
|
|
138
|
+
modelRouter(FoodModel, {
|
|
139
|
+
...(options as Partial<ModelRouterOptions<Food>>),
|
|
129
140
|
allowAnonymous: true,
|
|
130
141
|
permissions: {
|
|
131
142
|
create: [Permissions.IsAny],
|
|
@@ -149,7 +160,7 @@ describe("OpenApiMiddlewareBuilder", () => {
|
|
|
149
160
|
app = new TerrenoApp({
|
|
150
161
|
configureApp: addRoutesWithBuilder,
|
|
151
162
|
skipListen: true,
|
|
152
|
-
userModel: UserModel as
|
|
163
|
+
userModel: UserModel as unknown as AuthUserModel,
|
|
153
164
|
}).build();
|
|
154
165
|
});
|
|
155
166
|
|
|
@@ -189,7 +200,9 @@ describe("OpenApiMiddlewareBuilder", () => {
|
|
|
189
200
|
expect(statsPath.get.description).toBe("Returns aggregated statistics about food items");
|
|
190
201
|
|
|
191
202
|
// Check query parameter
|
|
192
|
-
const categoryParam = statsPath.get.parameters.find(
|
|
203
|
+
const categoryParam = statsPath.get.parameters.find(
|
|
204
|
+
(p: OpenApiParameter) => p.name === "category"
|
|
205
|
+
);
|
|
193
206
|
expect(categoryParam).toBeDefined();
|
|
194
207
|
expect(categoryParam.in).toBe("query");
|
|
195
208
|
expect(categoryParam.schema.type).toBe("string");
|
|
@@ -269,7 +282,9 @@ describe("OpenApiMiddlewareBuilder", () => {
|
|
|
269
282
|
const categoryPath = res.body.paths["/food/categories/{categoryId}"];
|
|
270
283
|
expect(categoryPath).toBeDefined();
|
|
271
284
|
|
|
272
|
-
const pathParam = categoryPath.get.parameters.find(
|
|
285
|
+
const pathParam = categoryPath.get.parameters.find(
|
|
286
|
+
(p: OpenApiParameter) => p.name === "categoryId"
|
|
287
|
+
);
|
|
273
288
|
expect(pathParam).toBeDefined();
|
|
274
289
|
expect(pathParam.in).toBe("path");
|
|
275
290
|
expect(pathParam.required).toBe(true);
|
|
@@ -563,6 +563,47 @@ describe("openApiValidator", () => {
|
|
|
563
563
|
expect(req.query.page).toBe(5);
|
|
564
564
|
});
|
|
565
565
|
|
|
566
|
+
it("runs only body validation when no query schema is provided", () => {
|
|
567
|
+
configureOpenApiValidator({coerceTypes: true});
|
|
568
|
+
|
|
569
|
+
const middleware = createValidator({
|
|
570
|
+
body: {count: {type: "number"}, name: {required: true, type: "string"}},
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
const req = {
|
|
574
|
+
body: {count: "3", name: "ok"},
|
|
575
|
+
method: "POST",
|
|
576
|
+
path: "/test",
|
|
577
|
+
query: {unchecked: "kept"},
|
|
578
|
+
} as unknown as Request;
|
|
579
|
+
const res = {} as Response;
|
|
580
|
+
|
|
581
|
+
const nextCalls: unknown[][] = [];
|
|
582
|
+
middleware(req, res, ((...args: unknown[]) => {
|
|
583
|
+
nextCalls.push(args);
|
|
584
|
+
}) as NextFunction);
|
|
585
|
+
|
|
586
|
+
expect(nextCalls).toEqual([[]]);
|
|
587
|
+
expect(req.body.count).toBe(3);
|
|
588
|
+
// Query params are untouched when no query schema is configured.
|
|
589
|
+
expect(req.query.unchecked).toBe("kept");
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
it("throws body validation errors when only a body schema is provided", () => {
|
|
593
|
+
configureOpenApiValidator({});
|
|
594
|
+
|
|
595
|
+
const middleware = createValidator({
|
|
596
|
+
body: {name: {required: true, type: "string"}},
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
const req = {body: {}, method: "POST", path: "/test", query: {}} as unknown as Request;
|
|
600
|
+
const res = {} as Response;
|
|
601
|
+
|
|
602
|
+
expect(() => middleware(req, res, (() => {}) as NextFunction)).toThrow(
|
|
603
|
+
"Request validation failed"
|
|
604
|
+
);
|
|
605
|
+
});
|
|
606
|
+
|
|
566
607
|
it("propagates body validation error via next", () => {
|
|
567
608
|
let capturedErrors: ErrorObject[] = [];
|
|
568
609
|
configureOpenApiValidator({
|
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
// noExplicitAny: test mock typing
|
|
2
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
3
1
|
import {describe, expect, it, mock} from "bun:test";
|
|
4
2
|
import type express from "express";
|
|
3
|
+
import type {Model} from "mongoose";
|
|
5
4
|
|
|
6
5
|
import {APIError} from "./errors";
|
|
7
6
|
import {Permissions, permissionMiddleware} from "./permissions";
|
|
8
7
|
|
|
8
|
+
interface MockDoc {
|
|
9
|
+
archived?: boolean;
|
|
10
|
+
deleted?: boolean;
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
foo?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
9
15
|
describe("permissionMiddleware", () => {
|
|
10
16
|
const allPermissions = {
|
|
11
17
|
create: [Permissions.IsAny],
|
|
@@ -24,171 +30,157 @@ describe("permissionMiddleware", () => {
|
|
|
24
30
|
} as unknown as express.Request;
|
|
25
31
|
};
|
|
26
32
|
|
|
27
|
-
|
|
33
|
+
const buildModel = ({
|
|
34
|
+
exec = mock(async (): Promise<MockDoc | null> => null),
|
|
35
|
+
hiddenDoc = null,
|
|
36
|
+
}: {
|
|
37
|
+
exec?: () => Promise<MockDoc | null>;
|
|
38
|
+
hiddenDoc?: MockDoc | null;
|
|
39
|
+
} = {}) => {
|
|
28
40
|
const model = {
|
|
29
|
-
collection: {findOne: mock(async () =>
|
|
30
|
-
findById: mock(() => ({exec
|
|
41
|
+
collection: {findOne: mock(async () => hiddenDoc)},
|
|
42
|
+
findById: mock(() => ({exec})),
|
|
31
43
|
modelName: "MockModel",
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
44
|
+
};
|
|
45
|
+
return {model, modelAsMongoose: model as unknown as Model<MockDoc>};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const buildNext = () => mock((_error?: unknown) => {});
|
|
49
|
+
|
|
50
|
+
const firstError = (next: ReturnType<typeof buildNext>): APIError => {
|
|
51
|
+
const [error] = next.mock.calls[0];
|
|
52
|
+
if (!(error instanceof APIError)) {
|
|
53
|
+
throw new Error(`Expected next to be called with an APIError, got ${String(error)}`);
|
|
54
|
+
}
|
|
55
|
+
return error;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
it("calls next immediately for OPTIONS requests", async () => {
|
|
59
|
+
const {model, modelAsMongoose} = buildModel();
|
|
60
|
+
const middleware = permissionMiddleware(modelAsMongoose, {permissions: allPermissions});
|
|
61
|
+
const next = buildNext();
|
|
35
62
|
|
|
36
|
-
await middleware(buildReq({method: "OPTIONS"}), {} as express.Response, next
|
|
63
|
+
await middleware(buildReq({method: "OPTIONS"}), {} as express.Response, next);
|
|
37
64
|
|
|
38
65
|
expect(next).toHaveBeenCalledTimes(1);
|
|
39
|
-
expect(
|
|
66
|
+
expect(next.mock.calls[0]).toEqual([]);
|
|
40
67
|
expect(model.findById).toHaveBeenCalledTimes(0);
|
|
41
68
|
});
|
|
42
69
|
|
|
43
70
|
it("returns APIError for unsupported HTTP methods", async () => {
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
modelName: "MockModel",
|
|
48
|
-
} as any;
|
|
49
|
-
const middleware = permissionMiddleware(model, {permissions: allPermissions});
|
|
50
|
-
const next = mock(() => {});
|
|
71
|
+
const {modelAsMongoose} = buildModel();
|
|
72
|
+
const middleware = permissionMiddleware(modelAsMongoose, {permissions: allPermissions});
|
|
73
|
+
const next = buildNext();
|
|
51
74
|
|
|
52
|
-
await middleware(buildReq({method: "TRACE"}), {} as express.Response, next
|
|
75
|
+
await middleware(buildReq({method: "TRACE"}), {} as express.Response, next);
|
|
53
76
|
|
|
54
77
|
expect(next).toHaveBeenCalledTimes(1);
|
|
55
|
-
const
|
|
56
|
-
expect(error).toBeInstanceOf(APIError);
|
|
78
|
+
const error = firstError(next);
|
|
57
79
|
expect(error.status).toBe(405);
|
|
58
80
|
expect(error.title).toContain("Method TRACE not allowed");
|
|
59
81
|
});
|
|
60
82
|
|
|
61
83
|
it("wraps query execution failures in a 500 APIError", async () => {
|
|
62
|
-
const exec = mock(async () => {
|
|
84
|
+
const exec = mock(async (): Promise<MockDoc | null> => {
|
|
63
85
|
throw new Error("query failed");
|
|
64
86
|
});
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
modelName: "MockModel",
|
|
69
|
-
} as any;
|
|
70
|
-
const middleware = permissionMiddleware(model, {permissions: allPermissions});
|
|
71
|
-
const next = mock(() => {});
|
|
87
|
+
const {modelAsMongoose} = buildModel({exec});
|
|
88
|
+
const middleware = permissionMiddleware(modelAsMongoose, {permissions: allPermissions});
|
|
89
|
+
const next = buildNext();
|
|
72
90
|
|
|
73
91
|
await middleware(
|
|
74
92
|
buildReq({method: "GET", params: {id: "507f1f77bcf86cd799439011"}}),
|
|
75
93
|
{} as express.Response,
|
|
76
|
-
next
|
|
94
|
+
next
|
|
77
95
|
);
|
|
78
96
|
|
|
79
97
|
expect(exec).toHaveBeenCalledTimes(1);
|
|
80
|
-
const
|
|
81
|
-
expect(error).toBeInstanceOf(APIError);
|
|
98
|
+
const error = firstError(next);
|
|
82
99
|
expect(error.status).toBe(500);
|
|
83
100
|
expect(error.title).toBe("GET error");
|
|
84
101
|
expect(error.detail).toContain("GET failed on 507f1f77bcf86cd799439011");
|
|
85
102
|
});
|
|
86
103
|
|
|
87
104
|
it("returns plain not found when document does not exist", async () => {
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
modelName: "MockModel",
|
|
92
|
-
} as any;
|
|
93
|
-
const middleware = permissionMiddleware(model, {permissions: allPermissions});
|
|
94
|
-
const next = mock(() => {});
|
|
105
|
+
const {modelAsMongoose} = buildModel();
|
|
106
|
+
const middleware = permissionMiddleware(modelAsMongoose, {permissions: allPermissions});
|
|
107
|
+
const next = buildNext();
|
|
95
108
|
|
|
96
109
|
await middleware(
|
|
97
110
|
buildReq({method: "GET", params: {id: "507f1f77bcf86cd799439011"}}),
|
|
98
111
|
{} as express.Response,
|
|
99
|
-
next
|
|
112
|
+
next
|
|
100
113
|
);
|
|
101
114
|
|
|
102
|
-
const
|
|
103
|
-
expect(error).toBeInstanceOf(APIError);
|
|
115
|
+
const error = firstError(next);
|
|
104
116
|
expect(error.status).toBe(404);
|
|
105
117
|
expect(error.title).toBe("Document not found");
|
|
106
118
|
expect(error.detail).toContain("507f1f77bcf86cd799439011");
|
|
107
119
|
});
|
|
108
120
|
|
|
109
121
|
it("returns hidden reason metadata when document is deleted", async () => {
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
modelName: "MockModel",
|
|
114
|
-
} as any;
|
|
115
|
-
const middleware = permissionMiddleware(model, {permissions: allPermissions});
|
|
116
|
-
const next = mock(() => {});
|
|
122
|
+
const {modelAsMongoose} = buildModel({hiddenDoc: {deleted: true}});
|
|
123
|
+
const middleware = permissionMiddleware(modelAsMongoose, {permissions: allPermissions});
|
|
124
|
+
const next = buildNext();
|
|
117
125
|
|
|
118
126
|
await middleware(
|
|
119
127
|
buildReq({method: "GET", params: {id: "507f1f77bcf86cd799439011"}}),
|
|
120
128
|
{} as express.Response,
|
|
121
|
-
next
|
|
129
|
+
next
|
|
122
130
|
);
|
|
123
131
|
|
|
124
|
-
const
|
|
125
|
-
expect(error).toBeInstanceOf(APIError);
|
|
132
|
+
const error = firstError(next);
|
|
126
133
|
expect(error.status).toBe(404);
|
|
127
134
|
expect(error.meta).toEqual({deleted: "true"});
|
|
128
135
|
expect(error.disableExternalErrorTracking).toBe(true);
|
|
129
136
|
});
|
|
130
137
|
|
|
131
138
|
it("returns hidden reason metadata when document is disabled", async () => {
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
modelName: "MockModel",
|
|
136
|
-
} as any;
|
|
137
|
-
const middleware = permissionMiddleware(model, {permissions: allPermissions});
|
|
138
|
-
const next = mock(() => {});
|
|
139
|
+
const {modelAsMongoose} = buildModel({hiddenDoc: {disabled: true}});
|
|
140
|
+
const middleware = permissionMiddleware(modelAsMongoose, {permissions: allPermissions});
|
|
141
|
+
const next = buildNext();
|
|
139
142
|
|
|
140
143
|
await middleware(
|
|
141
144
|
buildReq({method: "GET", params: {id: "507f1f77bcf86cd799439011"}}),
|
|
142
145
|
{} as express.Response,
|
|
143
|
-
next
|
|
146
|
+
next
|
|
144
147
|
);
|
|
145
148
|
|
|
146
|
-
const
|
|
147
|
-
expect(error).toBeInstanceOf(APIError);
|
|
149
|
+
const error = firstError(next);
|
|
148
150
|
expect(error.status).toBe(404);
|
|
149
151
|
expect(error.meta).toEqual({disabled: "true"});
|
|
150
152
|
expect(error.disableExternalErrorTracking).toBe(true);
|
|
151
153
|
});
|
|
152
154
|
|
|
153
155
|
it("returns hidden reason metadata when document is archived", async () => {
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
modelName: "MockModel",
|
|
158
|
-
} as any;
|
|
159
|
-
const middleware = permissionMiddleware(model, {permissions: allPermissions});
|
|
160
|
-
const next = mock(() => {});
|
|
156
|
+
const {modelAsMongoose} = buildModel({hiddenDoc: {archived: true}});
|
|
157
|
+
const middleware = permissionMiddleware(modelAsMongoose, {permissions: allPermissions});
|
|
158
|
+
const next = buildNext();
|
|
161
159
|
|
|
162
160
|
await middleware(
|
|
163
161
|
buildReq({method: "GET", params: {id: "507f1f77bcf86cd799439011"}}),
|
|
164
162
|
{} as express.Response,
|
|
165
|
-
next
|
|
163
|
+
next
|
|
166
164
|
);
|
|
167
165
|
|
|
168
|
-
const
|
|
169
|
-
expect(error).toBeInstanceOf(APIError);
|
|
166
|
+
const error = firstError(next);
|
|
170
167
|
expect(error.status).toBe(404);
|
|
171
168
|
expect(error.meta).toEqual({archived: "true"});
|
|
172
169
|
expect(error.disableExternalErrorTracking).toBe(true);
|
|
173
170
|
});
|
|
174
171
|
|
|
175
172
|
it("returns plain not found when hidden document has no reason", async () => {
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
modelName: "MockModel",
|
|
180
|
-
} as any;
|
|
181
|
-
const middleware = permissionMiddleware(model, {permissions: allPermissions});
|
|
182
|
-
const next = mock(() => {});
|
|
173
|
+
const {modelAsMongoose} = buildModel({hiddenDoc: {foo: "bar"}});
|
|
174
|
+
const middleware = permissionMiddleware(modelAsMongoose, {permissions: allPermissions});
|
|
175
|
+
const next = buildNext();
|
|
183
176
|
|
|
184
177
|
await middleware(
|
|
185
178
|
buildReq({method: "GET", params: {id: "507f1f77bcf86cd799439011"}}),
|
|
186
179
|
{} as express.Response,
|
|
187
|
-
next
|
|
180
|
+
next
|
|
188
181
|
);
|
|
189
182
|
|
|
190
|
-
const
|
|
191
|
-
expect(error).toBeInstanceOf(APIError);
|
|
183
|
+
const error = firstError(next);
|
|
192
184
|
expect(error.status).toBe(404);
|
|
193
185
|
expect(error.title).toBe("Document not found");
|
|
194
186
|
});
|