@terreno/api 0.31.0 → 56.0.0-beta.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 +122 -2
- package/LICENSE +202 -21
- package/dist/api.d.ts +12 -3
- package/dist/api.js +110 -361
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +100 -1
- package/dist/betterAuth.d.ts +5 -0
- package/dist/betterAuthSetup.d.ts +1 -15
- package/dist/betterAuthSetup.js +29 -8
- package/dist/configurationPlugin.js +2 -2
- package/dist/docLoader.js +12 -9
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +29 -1
- package/dist/expressServer.js +5 -11
- package/dist/githubAuth.js +4 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +29 -1
- package/dist/middleware.d.ts +7 -0
- package/dist/middleware.js +16 -1
- package/dist/openApiCompat.d.ts +10 -1
- package/dist/openApiCompat.js +115 -47
- package/dist/permissions.d.ts +27 -0
- package/dist/permissions.js +42 -1
- package/dist/plugins.d.ts +15 -3
- package/dist/plugins.js +36 -6
- package/dist/realtime/changeStreamWatcher.d.ts +100 -3
- package/dist/realtime/changeStreamWatcher.js +542 -137
- package/dist/realtime/realtimeApp.d.ts +3 -1
- package/dist/realtime/realtimeApp.js +75 -27
- package/dist/realtime/sessionRevalidation.d.ts +90 -0
- package/dist/realtime/sessionRevalidation.js +438 -0
- package/dist/realtime/socketAuth.d.ts +97 -0
- package/dist/realtime/socketAuth.js +272 -0
- package/dist/realtime/socketUser.d.ts +54 -0
- package/dist/realtime/socketUser.js +75 -5
- package/dist/realtime/types.d.ts +40 -0
- package/dist/secretProviders.d.ts +23 -0
- package/dist/secretProviders.js +9 -3
- package/dist/sync/executors.d.ts +198 -0
- package/dist/sync/executors.js +670 -0
- package/dist/sync/loadHarness.js +807 -0
- package/dist/sync/models.d.ts +216 -0
- package/dist/sync/models.js +644 -0
- package/dist/sync/mutationHandler.d.ts +97 -0
- package/dist/sync/mutationHandler.js +886 -0
- package/dist/sync/registry.d.ts +73 -0
- package/dist/sync/registry.js +223 -0
- package/dist/sync/routes.d.ts +62 -0
- package/dist/sync/routes.js +936 -0
- package/dist/sync/scripts/compactTombstones.d.ts +47 -0
- package/dist/sync/scripts/compactTombstones.js +270 -0
- package/dist/sync/serialize.d.ts +20 -0
- package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
- package/dist/sync/socketHandlers.d.ts +54 -0
- package/dist/sync/socketHandlers.js +470 -0
- package/dist/sync/streams.d.ts +85 -0
- package/dist/sync/streams.js +221 -0
- package/dist/sync/syncApp.d.ts +29 -0
- package/dist/sync/syncApp.js +44 -0
- package/dist/sync/syncBatch.d.ts +71 -0
- package/dist/sync/syncBatch.js +252 -0
- package/dist/sync/syncSeqPlugin.d.ts +8 -0
- package/dist/sync/syncSeqPlugin.js +903 -0
- package/dist/sync/types.d.ts +254 -0
- package/dist/sync/types.js +9 -0
- package/dist/terrenoApp.js +90 -27
- package/dist/tests/models.d.ts +11 -27
- package/dist/tests/models.js +3 -5
- package/dist/tests/testHelper.d.ts +3 -9
- package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
- package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
- package/package.json +10 -8
- package/src/__tests__/openApiCompat.test.ts +20 -0
- package/src/actions.test.ts +12 -19
- package/src/actions.ts +2 -2
- package/src/api.coverage.test.ts +9 -7
- package/src/api.errorPaths.test.ts +1 -47
- package/src/api.test.ts +12 -0
- package/src/api.ts +98 -317
- package/src/auth.test.ts +128 -176
- package/src/auth.ts +109 -0
- package/src/betterAuth.ts +6 -0
- package/src/betterAuthSetup.test.ts +34 -0
- package/src/betterAuthSetup.ts +27 -8
- package/src/configurationPlugin.ts +2 -2
- package/src/consentApp.test.ts +12 -12
- package/src/docLoader.ts +3 -1
- package/src/errors.ts +33 -0
- package/src/expressServer.test.ts +7 -1
- package/src/expressServer.ts +5 -11
- package/src/githubAuth.ts +3 -1
- package/src/index.ts +42 -0
- package/src/middleware.test.ts +40 -1
- package/src/middleware.ts +15 -0
- package/src/models/versionConfig.ts +1 -1
- package/src/openApiBuilder.test.ts +9 -24
- package/src/openApiCompat.ts +99 -45
- package/src/openApiValidator.test.ts +0 -41
- package/src/permissions.test.ts +75 -1
- package/src/permissions.ts +49 -0
- package/src/plugins.test.ts +54 -14
- package/src/plugins.ts +47 -9
- package/src/populate.test.ts +16 -25
- package/src/realtime/changeStreamWatcher.ts +552 -114
- package/src/realtime/realtime.test.ts +157 -239
- package/src/realtime/realtimeApp.ts +80 -19
- package/src/realtime/sessionRevalidation.test.ts +423 -0
- package/src/realtime/sessionRevalidation.ts +273 -0
- package/src/realtime/socketAuth.test.ts +355 -0
- package/src/realtime/socketAuth.ts +228 -0
- package/src/realtime/socketUser.ts +69 -0
- package/src/realtime/types.ts +40 -0
- package/src/secretProviders.test.ts +7 -1
- package/src/secretProviders.ts +15 -3
- package/src/sync/ensureSyncIndexes.test.ts +144 -0
- package/src/sync/executors.test.ts +882 -0
- package/src/sync/executors.ts +703 -0
- package/src/sync/integration.test.ts +1160 -0
- package/src/sync/loadHarness.ts +618 -0
- package/src/sync/models.ts +640 -0
- package/src/sync/mutationHandler.test.ts +1252 -0
- package/src/sync/mutationHandler.ts +850 -0
- package/src/sync/registry.ts +232 -0
- package/src/sync/routes.ts +812 -0
- package/src/sync/scripts/compactTombstones.test.ts +249 -0
- package/src/sync/scripts/compactTombstones.ts +145 -0
- package/src/sync/serialize.ts +48 -0
- package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
- package/src/sync/socketHandlers.ts +320 -0
- package/src/sync/streams.ts +188 -0
- package/src/sync/sync.test.ts +582 -0
- package/src/sync/syncApp.ts +44 -0
- package/src/sync/syncBatch.ts +219 -0
- package/src/sync/syncFrontier.test.ts +494 -0
- package/src/sync/syncPhaseC.test.ts +718 -0
- package/src/sync/syncRoutes.test.ts +865 -0
- package/src/sync/syncSeqPlugin.ts +738 -0
- package/src/sync/syncSocket.test.ts +1440 -0
- package/src/sync/types.ts +270 -0
- package/src/terrenoApp.ts +37 -17
- package/src/tests/bunSetup.ts +8 -0
- package/src/tests/models.ts +6 -8
- package/tsconfig.json +4 -4
- package/tsconfig.typedoc.json +7 -1
- package/dist/__tests__/openApiCompat.test.js +0 -317
- package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
- package/dist/__tests__/versionCheckPlugin.test.js +0 -597
- package/dist/actions.openApi.test.d.ts +0 -1
- package/dist/actions.openApi.test.js +0 -253
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -1087
- package/dist/api.arrayOperations.test.d.ts +0 -1
- package/dist/api.arrayOperations.test.js +0 -878
- package/dist/api.asyncHandler.test.d.ts +0 -1
- package/dist/api.asyncHandler.test.js +0 -236
- package/dist/api.coverage.test.d.ts +0 -1
- package/dist/api.coverage.test.js +0 -192
- package/dist/api.errorPaths.test.d.ts +0 -1
- package/dist/api.errorPaths.test.js +0 -887
- package/dist/api.errors.test.d.ts +0 -1
- package/dist/api.errors.test.js +0 -185
- package/dist/api.hooks.test.d.ts +0 -1
- package/dist/api.hooks.test.js +0 -950
- package/dist/api.query.test.d.ts +0 -1
- package/dist/api.query.test.js +0 -948
- package/dist/api.test.d.ts +0 -1
- package/dist/api.test.js +0 -2820
- package/dist/auth.compile.test.d.ts +0 -1
- package/dist/auth.compile.test.js +0 -201
- package/dist/auth.coverage.test.d.ts +0 -1
- package/dist/auth.coverage.test.js +0 -202
- package/dist/auth.test.d.ts +0 -1
- package/dist/auth.test.js +0 -2286
- package/dist/betterAuth.test.d.ts +0 -1
- package/dist/betterAuth.test.js +0 -181
- package/dist/betterAuthApp.test.d.ts +0 -1
- package/dist/betterAuthApp.test.js +0 -242
- package/dist/betterAuthSetup.test.d.ts +0 -1
- package/dist/betterAuthSetup.test.js +0 -762
- package/dist/config.test.d.ts +0 -1
- package/dist/config.test.js +0 -353
- package/dist/configuration.test.d.ts +0 -1
- package/dist/configuration.test.js +0 -980
- package/dist/configurationPlugin.test.d.ts +0 -1
- package/dist/configurationPlugin.test.js +0 -827
- package/dist/consentApp.test.d.ts +0 -1
- package/dist/consentApp.test.js +0 -1411
- package/dist/docLoader.test.d.ts +0 -1
- package/dist/docLoader.test.js +0 -137
- package/dist/envConfigurationPlugin.test.d.ts +0 -1
- package/dist/envConfigurationPlugin.test.js +0 -369
- package/dist/errors.test.d.ts +0 -1
- package/dist/errors.test.js +0 -607
- package/dist/expressServer.test.d.ts +0 -1
- package/dist/expressServer.test.js +0 -1249
- package/dist/githubAuth.test.d.ts +0 -1
- package/dist/githubAuth.test.js +0 -1142
- package/dist/httpClient.test.d.ts +0 -1
- package/dist/httpClient.test.js +0 -1471
- package/dist/logger.test.d.ts +0 -1
- package/dist/logger.test.js +0 -685
- package/dist/middleware.test.d.ts +0 -1
- package/dist/middleware.test.js +0 -241
- package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
- package/dist/notifiers/googleChatNotifier.test.js +0 -298
- package/dist/notifiers/slackNotifier.test.d.ts +0 -1
- package/dist/notifiers/slackNotifier.test.js +0 -297
- package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
- package/dist/notifiers/zoomNotifier.test.js +0 -373
- package/dist/openApi.test.d.ts +0 -1
- package/dist/openApi.test.js +0 -620
- package/dist/openApiBuilder.test.d.ts +0 -1
- package/dist/openApiBuilder.test.js +0 -599
- package/dist/openApiEtag.test.d.ts +0 -1
- package/dist/openApiEtag.test.js +0 -86
- package/dist/openApiValidator.test.d.ts +0 -1
- package/dist/openApiValidator.test.js +0 -795
- package/dist/permissions.middleware.test.d.ts +0 -1
- package/dist/permissions.middleware.test.js +0 -308
- package/dist/permissions.test.d.ts +0 -1
- package/dist/permissions.test.js +0 -439
- package/dist/plugins.test.d.ts +0 -1
- package/dist/plugins.test.js +0 -902
- package/dist/populate.test.d.ts +0 -1
- package/dist/populate.test.js +0 -370
- package/dist/realtime/realtime.test.d.ts +0 -10
- package/dist/realtime/realtime.test.js +0 -4213
- package/dist/realtime/realtimeAppSetup.test.d.ts +0 -1
- package/dist/realtime/realtimeAppSetup.test.js +0 -213
- package/dist/requestContext.test.d.ts +0 -1
- package/dist/requestContext.test.js +0 -412
- package/dist/scriptRunner.test.d.ts +0 -1
- package/dist/scriptRunner.test.js +0 -386
- package/dist/secretProviders.test.d.ts +0 -1
- package/dist/secretProviders.test.js +0 -726
- package/dist/syncConsents.test.d.ts +0 -1
- package/dist/syncConsents.test.js +0 -522
- package/dist/terrenoApp.test.d.ts +0 -1
- package/dist/terrenoApp.test.js +0 -489
- package/dist/tests/bunSetup.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.js +0 -37
- package/dist/tests/testEnv.d.ts +0 -0
- package/dist/tests/testEnv.js +0 -6
- package/dist/transformers.test.d.ts +0 -1
- package/dist/transformers.test.js +0 -423
- package/dist/utils.test.d.ts +0 -1
- package/dist/utils.test.js +0 -242
- package/src/realtime/realtimeAppSetup.test.ts +0 -161
- /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
|
@@ -0,0 +1,882 @@
|
|
|
1
|
+
import {beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
3
|
+
import type express from "express";
|
|
4
|
+
import {model, Schema} from "mongoose";
|
|
5
|
+
|
|
6
|
+
import type {ModelRouterOptions} from "../api";
|
|
7
|
+
import type {User} from "../auth";
|
|
8
|
+
import {APIError} from "../errors";
|
|
9
|
+
import {Permissions} from "../permissions";
|
|
10
|
+
import {createdUpdatedPlugin, type IsDeleted, isDeletedPlugin} from "../plugins";
|
|
11
|
+
import {setupDb} from "../tests";
|
|
12
|
+
import {ExecutorConflictError, executeCreate, executeDelete, executeUpdate} from "./executors";
|
|
13
|
+
|
|
14
|
+
interface ExecStuff extends IsDeleted {
|
|
15
|
+
_id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
ownerId: string;
|
|
18
|
+
created: Date;
|
|
19
|
+
updated?: Date;
|
|
20
|
+
_syncSeq?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const execStuffSchema = new Schema<ExecStuff>({
|
|
24
|
+
_syncSeq: {description: "Per-stream sync sequence used by concurrency tests", type: Number},
|
|
25
|
+
name: {description: "The name of the item", required: true, type: String},
|
|
26
|
+
ownerId: {description: "The user who owns this item", type: String},
|
|
27
|
+
});
|
|
28
|
+
execStuffSchema.plugin(isDeletedPlugin);
|
|
29
|
+
execStuffSchema.plugin(createdUpdatedPlugin);
|
|
30
|
+
const ExecStuffModel = model<ExecStuff>("ExecStuff", execStuffSchema);
|
|
31
|
+
|
|
32
|
+
interface ExecStringIdStuff extends IsDeleted {
|
|
33
|
+
_id: string;
|
|
34
|
+
name: string;
|
|
35
|
+
ownerId: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const execStringIdStuffSchema = new Schema<ExecStringIdStuff>({
|
|
39
|
+
_id: {description: "Offline-generated string identifier", required: true, type: String},
|
|
40
|
+
name: {description: "The name of the item", required: true, type: String},
|
|
41
|
+
ownerId: {description: "The user who owns this item", type: String},
|
|
42
|
+
});
|
|
43
|
+
execStringIdStuffSchema.plugin(isDeletedPlugin);
|
|
44
|
+
const ExecStringIdStuffModel = model<ExecStringIdStuff>(
|
|
45
|
+
"ExecStringIdStuff",
|
|
46
|
+
execStringIdStuffSchema
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
// No isDeletedPlugin — exercises the hard-delete branch of executeDelete.
|
|
50
|
+
interface ExecHardStuff {
|
|
51
|
+
_id: string;
|
|
52
|
+
name: string;
|
|
53
|
+
ownerId?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const execHardSchema = new Schema<ExecHardStuff>({
|
|
57
|
+
name: {description: "The name of the item", required: true, type: String},
|
|
58
|
+
ownerId: {description: "The user who owns this item", type: String},
|
|
59
|
+
});
|
|
60
|
+
const ExecHardModel = model<ExecHardStuff>("ExecHardStuff", execHardSchema);
|
|
61
|
+
|
|
62
|
+
const owner = {_id: "u-owner", admin: false, id: "u-owner"} as unknown as User;
|
|
63
|
+
const stranger = {_id: "u-stranger", admin: false, id: "u-stranger"} as unknown as User;
|
|
64
|
+
|
|
65
|
+
const baseOptions = (
|
|
66
|
+
overrides: Partial<ModelRouterOptions<ExecStuff>> = {}
|
|
67
|
+
): ModelRouterOptions<ExecStuff> =>
|
|
68
|
+
({
|
|
69
|
+
permissions: {
|
|
70
|
+
create: [Permissions.IsAuthenticated],
|
|
71
|
+
delete: [Permissions.IsOwner],
|
|
72
|
+
list: [Permissions.IsAny],
|
|
73
|
+
read: [Permissions.IsAny],
|
|
74
|
+
update: [Permissions.IsOwner],
|
|
75
|
+
},
|
|
76
|
+
...overrides,
|
|
77
|
+
}) as ModelRouterOptions<ExecStuff>;
|
|
78
|
+
|
|
79
|
+
const hardOptions = (
|
|
80
|
+
overrides: Partial<ModelRouterOptions<ExecHardStuff>> = {}
|
|
81
|
+
): ModelRouterOptions<ExecHardStuff> =>
|
|
82
|
+
({
|
|
83
|
+
permissions: {
|
|
84
|
+
create: [Permissions.IsAny],
|
|
85
|
+
delete: [Permissions.IsAny],
|
|
86
|
+
list: [Permissions.IsAny],
|
|
87
|
+
read: [Permissions.IsAny],
|
|
88
|
+
update: [Permissions.IsAny],
|
|
89
|
+
},
|
|
90
|
+
...overrides,
|
|
91
|
+
}) as ModelRouterOptions<ExecHardStuff>;
|
|
92
|
+
|
|
93
|
+
const caught = async (promise: Promise<unknown>): Promise<APIError> => {
|
|
94
|
+
const result = await promise.then(
|
|
95
|
+
() => undefined,
|
|
96
|
+
(error) => error
|
|
97
|
+
);
|
|
98
|
+
expect(result).toBeInstanceOf(APIError);
|
|
99
|
+
return result as APIError;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
describe("executeCreate", () => {
|
|
103
|
+
beforeAll(async () => {
|
|
104
|
+
await setupDb();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
beforeEach(async () => {
|
|
108
|
+
await ExecStuffModel.deleteMany({});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("creates a document without an HTTP request", async () => {
|
|
112
|
+
const {doc} = await executeCreate<ExecStuff>({
|
|
113
|
+
body: {name: "direct", ownerId: owner.id},
|
|
114
|
+
model: ExecStuffModel,
|
|
115
|
+
options: baseOptions(),
|
|
116
|
+
user: owner,
|
|
117
|
+
});
|
|
118
|
+
expect(doc.name).toBe("direct");
|
|
119
|
+
const saved = await ExecStuffModel.findById(doc._id);
|
|
120
|
+
expect(saved?.name).toBe("direct");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("denies unauthenticated users at the method level", async () => {
|
|
124
|
+
const error = await caught(
|
|
125
|
+
executeCreate<ExecStuff>({
|
|
126
|
+
body: {name: "nope"},
|
|
127
|
+
model: ExecStuffModel,
|
|
128
|
+
options: baseOptions(),
|
|
129
|
+
user: undefined,
|
|
130
|
+
})
|
|
131
|
+
);
|
|
132
|
+
expect(error.status).toBe(405);
|
|
133
|
+
expect(error.title).toContain("Access to CREATE on ExecStuff");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("passes a {user} stub request to preCreate when no req is provided", async () => {
|
|
137
|
+
const options = baseOptions({
|
|
138
|
+
preCreate: (body, request) => ({
|
|
139
|
+
...(body as Partial<ExecStuff>),
|
|
140
|
+
ownerId: (request.user as User).id,
|
|
141
|
+
}),
|
|
142
|
+
} as Partial<ModelRouterOptions<ExecStuff>>);
|
|
143
|
+
const {doc} = await executeCreate<ExecStuff>({
|
|
144
|
+
body: {name: "stubbed"},
|
|
145
|
+
model: ExecStuffModel,
|
|
146
|
+
options,
|
|
147
|
+
user: owner,
|
|
148
|
+
});
|
|
149
|
+
expect(doc.ownerId).toBe(owner.id);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("passes the real request through to hooks when provided", async () => {
|
|
153
|
+
const fakeReq = {params: {marker: "real"}, user: owner} as unknown as express.Request;
|
|
154
|
+
let seenReq: express.Request | undefined;
|
|
155
|
+
const options = baseOptions({
|
|
156
|
+
preCreate: (body, request) => {
|
|
157
|
+
seenReq = request;
|
|
158
|
+
return body as ExecStuff;
|
|
159
|
+
},
|
|
160
|
+
} as Partial<ModelRouterOptions<ExecStuff>>);
|
|
161
|
+
await executeCreate<ExecStuff>({
|
|
162
|
+
body: {name: "with req", ownerId: owner.id},
|
|
163
|
+
model: ExecStuffModel,
|
|
164
|
+
options,
|
|
165
|
+
req: fakeReq,
|
|
166
|
+
user: owner,
|
|
167
|
+
});
|
|
168
|
+
expect(seenReq).toBe(fakeReq);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("returns 403 when preCreate returns null", async () => {
|
|
172
|
+
const options = baseOptions({preCreate: () => null});
|
|
173
|
+
const error = await caught(
|
|
174
|
+
executeCreate<ExecStuff>({
|
|
175
|
+
body: {name: "denied"},
|
|
176
|
+
model: ExecStuffModel,
|
|
177
|
+
options,
|
|
178
|
+
user: owner,
|
|
179
|
+
})
|
|
180
|
+
);
|
|
181
|
+
expect(error.status).toBe(403);
|
|
182
|
+
expect(error.title).toBe("Create not allowed");
|
|
183
|
+
expect(error.detail).toBe("preCreate hook returned null");
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("returns 403 when preCreate returns undefined", async () => {
|
|
187
|
+
const options = baseOptions({
|
|
188
|
+
preCreate: () => undefined,
|
|
189
|
+
} as unknown as Partial<ModelRouterOptions<ExecStuff>>);
|
|
190
|
+
const error = await caught(
|
|
191
|
+
executeCreate<ExecStuff>({
|
|
192
|
+
body: {name: "denied"},
|
|
193
|
+
model: ExecStuffModel,
|
|
194
|
+
options,
|
|
195
|
+
user: owner,
|
|
196
|
+
})
|
|
197
|
+
);
|
|
198
|
+
expect(error.status).toBe(403);
|
|
199
|
+
expect(error.detail).toBe("A body must be returned from preCreate");
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("rethrows APIErrors from preCreate unchanged", async () => {
|
|
203
|
+
const options = baseOptions({
|
|
204
|
+
preCreate: () => {
|
|
205
|
+
throw new APIError({status: 418, title: "teapot"});
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
const error = await caught(
|
|
209
|
+
executeCreate<ExecStuff>({
|
|
210
|
+
body: {name: "tea"},
|
|
211
|
+
model: ExecStuffModel,
|
|
212
|
+
options,
|
|
213
|
+
user: owner,
|
|
214
|
+
})
|
|
215
|
+
);
|
|
216
|
+
expect(error.status).toBe(418);
|
|
217
|
+
expect(error.title).toBe("teapot");
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("wraps plain preCreate errors as 400s", async () => {
|
|
221
|
+
const options = baseOptions({
|
|
222
|
+
preCreate: () => {
|
|
223
|
+
throw new Error("boom");
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
const error = await caught(
|
|
227
|
+
executeCreate<ExecStuff>({
|
|
228
|
+
body: {name: "boom"},
|
|
229
|
+
model: ExecStuffModel,
|
|
230
|
+
options,
|
|
231
|
+
user: owner,
|
|
232
|
+
})
|
|
233
|
+
);
|
|
234
|
+
expect(error.status).toBe(400);
|
|
235
|
+
expect(error.title).toBe("preCreate hook error");
|
|
236
|
+
expect(error.detail).toBe("boom");
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("returns 400 when the body is undefined", async () => {
|
|
240
|
+
const error = await caught(
|
|
241
|
+
executeCreate<ExecStuff>({
|
|
242
|
+
body: undefined,
|
|
243
|
+
model: ExecStuffModel,
|
|
244
|
+
options: baseOptions(),
|
|
245
|
+
user: owner,
|
|
246
|
+
})
|
|
247
|
+
);
|
|
248
|
+
expect(error.status).toBe(400);
|
|
249
|
+
expect(error.title).toBe("Invalid request body");
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("returns 400 on Mongoose validation failure", async () => {
|
|
253
|
+
const error = await caught(
|
|
254
|
+
executeCreate<ExecStuff>({
|
|
255
|
+
body: {ownerId: owner.id},
|
|
256
|
+
model: ExecStuffModel,
|
|
257
|
+
options: baseOptions(),
|
|
258
|
+
user: owner,
|
|
259
|
+
})
|
|
260
|
+
);
|
|
261
|
+
expect(error.status).toBe(400);
|
|
262
|
+
expect(error.title).toBe("Validation failed");
|
|
263
|
+
expect(error.meta?.fields).toMatchObject({name: expect.any(String)});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("wraps transformer.transform errors as 400s", async () => {
|
|
267
|
+
const options = baseOptions({
|
|
268
|
+
transformer: {
|
|
269
|
+
transform: () => {
|
|
270
|
+
throw new Error("transform rejected");
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
} as Partial<ModelRouterOptions<ExecStuff>>);
|
|
274
|
+
const error = await caught(
|
|
275
|
+
executeCreate<ExecStuff>({
|
|
276
|
+
body: {name: "x", ownerId: owner.id},
|
|
277
|
+
model: ExecStuffModel,
|
|
278
|
+
options,
|
|
279
|
+
user: owner,
|
|
280
|
+
})
|
|
281
|
+
);
|
|
282
|
+
expect(error.status).toBe(400);
|
|
283
|
+
expect(error.title).toBe("Transform error");
|
|
284
|
+
expect(error.detail).toBe("transform rejected");
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("invokes postCreate with the created document", async () => {
|
|
288
|
+
const postCreate = mock((value: ExecStuff) => {
|
|
289
|
+
expect(value.name).toBe("hooked");
|
|
290
|
+
});
|
|
291
|
+
const options = baseOptions({postCreate} as Partial<ModelRouterOptions<ExecStuff>>);
|
|
292
|
+
const {doc} = await executeCreate<ExecStuff>({
|
|
293
|
+
body: {name: "hooked", ownerId: owner.id},
|
|
294
|
+
model: ExecStuffModel,
|
|
295
|
+
options,
|
|
296
|
+
user: owner,
|
|
297
|
+
});
|
|
298
|
+
expect(postCreate).toHaveBeenCalledTimes(1);
|
|
299
|
+
expect(postCreate.mock.calls[0][0]._id.toString()).toBe(doc._id.toString());
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("wraps postCreate errors as 400s", async () => {
|
|
303
|
+
const options = baseOptions({
|
|
304
|
+
postCreate: () => {
|
|
305
|
+
throw new Error("side effect failed");
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
const error = await caught(
|
|
309
|
+
executeCreate<ExecStuff>({
|
|
310
|
+
body: {name: "hooked", ownerId: owner.id},
|
|
311
|
+
model: ExecStuffModel,
|
|
312
|
+
options,
|
|
313
|
+
user: owner,
|
|
314
|
+
})
|
|
315
|
+
);
|
|
316
|
+
expect(error.status).toBe(400);
|
|
317
|
+
expect(error.title).toBe("postCreate hook error");
|
|
318
|
+
expect(error.detail).toBe("side effect failed");
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
describe("executeUpdate", () => {
|
|
323
|
+
beforeAll(async () => {
|
|
324
|
+
await setupDb();
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
beforeEach(async () => {
|
|
328
|
+
await ExecStuffModel.deleteMany({});
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
const createDoc = async (fields: Partial<ExecStuff> = {}) =>
|
|
332
|
+
ExecStuffModel.create({name: "original", ownerId: owner.id, ...fields});
|
|
333
|
+
|
|
334
|
+
it("updates a document without an HTTP request", async () => {
|
|
335
|
+
const doc = await createDoc();
|
|
336
|
+
const {doc: updated} = await executeUpdate<ExecStuff>({
|
|
337
|
+
body: {name: "changed"},
|
|
338
|
+
id: doc._id.toString(),
|
|
339
|
+
model: ExecStuffModel,
|
|
340
|
+
options: baseOptions(),
|
|
341
|
+
user: owner,
|
|
342
|
+
});
|
|
343
|
+
expect(updated.name).toBe("changed");
|
|
344
|
+
const saved = await ExecStuffModel.findById(doc._id);
|
|
345
|
+
expect(saved?.name).toBe("changed");
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("denies at the method level when update permissions are empty", async () => {
|
|
349
|
+
const doc = await createDoc();
|
|
350
|
+
const options = baseOptions();
|
|
351
|
+
options.permissions = {...options.permissions, update: []};
|
|
352
|
+
const error = await caught(
|
|
353
|
+
executeUpdate<ExecStuff>({
|
|
354
|
+
body: {name: "x"},
|
|
355
|
+
id: doc._id.toString(),
|
|
356
|
+
model: ExecStuffModel,
|
|
357
|
+
options,
|
|
358
|
+
user: owner,
|
|
359
|
+
})
|
|
360
|
+
);
|
|
361
|
+
expect(error.status).toBe(405);
|
|
362
|
+
expect(error.title).toContain("Access to UPDATE on ExecStuff");
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("denies at the object level for non-owners", async () => {
|
|
366
|
+
const doc = await createDoc();
|
|
367
|
+
const error = await caught(
|
|
368
|
+
executeUpdate<ExecStuff>({
|
|
369
|
+
body: {name: "x"},
|
|
370
|
+
id: doc._id.toString(),
|
|
371
|
+
model: ExecStuffModel,
|
|
372
|
+
options: baseOptions(),
|
|
373
|
+
user: stranger,
|
|
374
|
+
})
|
|
375
|
+
);
|
|
376
|
+
expect(error.status).toBe(403);
|
|
377
|
+
expect(error.title).toContain(`Access to GET on ExecStuff:${doc._id.toString()}`);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("returns 404 for a missing document", async () => {
|
|
381
|
+
const error = await caught(
|
|
382
|
+
executeUpdate<ExecStuff>({
|
|
383
|
+
body: {name: "x"},
|
|
384
|
+
id: "000000000000000000000000",
|
|
385
|
+
model: ExecStuffModel,
|
|
386
|
+
options: baseOptions(),
|
|
387
|
+
user: owner,
|
|
388
|
+
})
|
|
389
|
+
);
|
|
390
|
+
expect(error.status).toBe(404);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("returns 403 when preUpdate returns null", async () => {
|
|
394
|
+
const doc = await createDoc();
|
|
395
|
+
const options = baseOptions({preUpdate: () => null});
|
|
396
|
+
const error = await caught(
|
|
397
|
+
executeUpdate<ExecStuff>({
|
|
398
|
+
body: {name: "x"},
|
|
399
|
+
id: doc._id.toString(),
|
|
400
|
+
model: ExecStuffModel,
|
|
401
|
+
options,
|
|
402
|
+
user: owner,
|
|
403
|
+
})
|
|
404
|
+
);
|
|
405
|
+
expect(error.status).toBe(403);
|
|
406
|
+
expect(error.title).toBe("Update not allowed");
|
|
407
|
+
expect(error.detail).toBe(`preUpdate hook on ${doc._id.toString()} returned null`);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
it("returns 403 when preUpdate returns undefined", async () => {
|
|
411
|
+
const doc = await createDoc();
|
|
412
|
+
const options = baseOptions({
|
|
413
|
+
preUpdate: () => undefined,
|
|
414
|
+
} as unknown as Partial<ModelRouterOptions<ExecStuff>>);
|
|
415
|
+
const error = await caught(
|
|
416
|
+
executeUpdate<ExecStuff>({
|
|
417
|
+
body: {name: "x"},
|
|
418
|
+
id: doc._id.toString(),
|
|
419
|
+
model: ExecStuffModel,
|
|
420
|
+
options,
|
|
421
|
+
user: owner,
|
|
422
|
+
})
|
|
423
|
+
);
|
|
424
|
+
expect(error.status).toBe(403);
|
|
425
|
+
expect(error.detail).toBe("A body must be returned from preUpdate");
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it("wraps plain preUpdate errors as 400s", async () => {
|
|
429
|
+
const doc = await createDoc();
|
|
430
|
+
const options = baseOptions({
|
|
431
|
+
preUpdate: () => {
|
|
432
|
+
throw new Error("nope");
|
|
433
|
+
},
|
|
434
|
+
});
|
|
435
|
+
const error = await caught(
|
|
436
|
+
executeUpdate<ExecStuff>({
|
|
437
|
+
body: {name: "x"},
|
|
438
|
+
id: doc._id.toString(),
|
|
439
|
+
model: ExecStuffModel,
|
|
440
|
+
options,
|
|
441
|
+
user: owner,
|
|
442
|
+
})
|
|
443
|
+
);
|
|
444
|
+
expect(error.status).toBe(400);
|
|
445
|
+
expect(error.title).toBe("preUpdate hook error");
|
|
446
|
+
expect(error.detail).toBe(`preUpdate hook error on ${doc._id.toString()}: nope`);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it("strips _updatedAt from the body before preUpdate", async () => {
|
|
450
|
+
const doc = await createDoc();
|
|
451
|
+
let seenBody: Partial<ExecStuff> | undefined;
|
|
452
|
+
const options = baseOptions({
|
|
453
|
+
preUpdate: (body) => {
|
|
454
|
+
seenBody = body;
|
|
455
|
+
return body as ExecStuff;
|
|
456
|
+
},
|
|
457
|
+
} as Partial<ModelRouterOptions<ExecStuff>>);
|
|
458
|
+
const rawBody: Record<string, unknown> = {
|
|
459
|
+
_updatedAt: "2025-01-01T00:00:00.000Z",
|
|
460
|
+
name: "stripped",
|
|
461
|
+
};
|
|
462
|
+
await executeUpdate<ExecStuff>({
|
|
463
|
+
body: rawBody,
|
|
464
|
+
id: doc._id.toString(),
|
|
465
|
+
model: ExecStuffModel,
|
|
466
|
+
options,
|
|
467
|
+
user: owner,
|
|
468
|
+
});
|
|
469
|
+
expect(seenBody).toBeDefined();
|
|
470
|
+
expect((seenBody as Record<string, unknown>)._updatedAt).toBeUndefined();
|
|
471
|
+
expect(rawBody._updatedAt).toBeUndefined();
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
it("throws an ExecutorConflictError on a stale timestamp", async () => {
|
|
475
|
+
const doc = await createDoc();
|
|
476
|
+
const error = await caught(
|
|
477
|
+
executeUpdate<ExecStuff>({
|
|
478
|
+
body: {name: "stale"},
|
|
479
|
+
concurrencyCheck: {
|
|
480
|
+
ifUnmodifiedSince: new Date("2000-01-01T00:00:00.000Z"),
|
|
481
|
+
type: "timestamp",
|
|
482
|
+
},
|
|
483
|
+
id: doc._id.toString(),
|
|
484
|
+
model: ExecStuffModel,
|
|
485
|
+
options: baseOptions(),
|
|
486
|
+
user: owner,
|
|
487
|
+
})
|
|
488
|
+
);
|
|
489
|
+
expect(error).toBeInstanceOf(ExecutorConflictError);
|
|
490
|
+
const conflict = error as ExecutorConflictError;
|
|
491
|
+
expect(conflict.status).toBe(409);
|
|
492
|
+
expect(conflict.conflictType).toBe("timestamp");
|
|
493
|
+
expect((conflict.doc as ExecStuff).name).toBe("original");
|
|
494
|
+
const saved = await ExecStuffModel.findById(doc._id);
|
|
495
|
+
expect(saved?.name).toBe("original");
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
it("applies the update when the timestamp is fresh", async () => {
|
|
499
|
+
const doc = await createDoc();
|
|
500
|
+
const {doc: updated} = await executeUpdate<ExecStuff>({
|
|
501
|
+
body: {name: "fresh"},
|
|
502
|
+
concurrencyCheck: {
|
|
503
|
+
ifUnmodifiedSince: new Date(Date.now() + 60_000),
|
|
504
|
+
type: "timestamp",
|
|
505
|
+
},
|
|
506
|
+
id: doc._id.toString(),
|
|
507
|
+
model: ExecStuffModel,
|
|
508
|
+
options: baseOptions(),
|
|
509
|
+
user: owner,
|
|
510
|
+
});
|
|
511
|
+
expect(updated.name).toBe("fresh");
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
it("returns 400 for an invalid timestamp, using the provided detail", async () => {
|
|
515
|
+
const doc = await createDoc();
|
|
516
|
+
const error = await caught(
|
|
517
|
+
executeUpdate<ExecStuff>({
|
|
518
|
+
body: {name: "x"},
|
|
519
|
+
concurrencyCheck: {
|
|
520
|
+
ifUnmodifiedSince: new Date(Number.NaN),
|
|
521
|
+
invalidTimestampDetail: "custom parse detail",
|
|
522
|
+
type: "timestamp",
|
|
523
|
+
},
|
|
524
|
+
id: doc._id.toString(),
|
|
525
|
+
model: ExecStuffModel,
|
|
526
|
+
options: baseOptions(),
|
|
527
|
+
user: owner,
|
|
528
|
+
})
|
|
529
|
+
);
|
|
530
|
+
expect(error.status).toBe(400);
|
|
531
|
+
expect(error.title).toBe("Invalid conflict-detection timestamp");
|
|
532
|
+
expect(error.detail).toBe("custom parse detail");
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
it("returns 400 for an invalid timestamp with a default detail", async () => {
|
|
536
|
+
const doc = await createDoc();
|
|
537
|
+
const error = await caught(
|
|
538
|
+
executeUpdate<ExecStuff>({
|
|
539
|
+
body: {name: "x"},
|
|
540
|
+
concurrencyCheck: {ifUnmodifiedSince: new Date(Number.NaN), type: "timestamp"},
|
|
541
|
+
id: doc._id.toString(),
|
|
542
|
+
model: ExecStuffModel,
|
|
543
|
+
options: baseOptions(),
|
|
544
|
+
user: owner,
|
|
545
|
+
})
|
|
546
|
+
);
|
|
547
|
+
expect(error.status).toBe(400);
|
|
548
|
+
expect(error.detail).toBe("Conflict-detection timestamp could not be parsed as a date");
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
it("throws an ExecutorConflictError on a seq mismatch", async () => {
|
|
552
|
+
const doc = await createDoc({_syncSeq: 5});
|
|
553
|
+
const error = await caught(
|
|
554
|
+
executeUpdate<ExecStuff>({
|
|
555
|
+
body: {name: "stale seq"},
|
|
556
|
+
concurrencyCheck: {baseSeq: 3, type: "seq"},
|
|
557
|
+
id: doc._id.toString(),
|
|
558
|
+
model: ExecStuffModel,
|
|
559
|
+
options: baseOptions(),
|
|
560
|
+
user: owner,
|
|
561
|
+
})
|
|
562
|
+
);
|
|
563
|
+
expect(error).toBeInstanceOf(ExecutorConflictError);
|
|
564
|
+
const conflict = error as ExecutorConflictError;
|
|
565
|
+
expect(conflict.status).toBe(409);
|
|
566
|
+
expect(conflict.conflictType).toBe("seq");
|
|
567
|
+
expect(conflict.serverSeq).toBe(5);
|
|
568
|
+
expect(conflict.title).toContain("Sync conflict");
|
|
569
|
+
expect((conflict.doc as ExecStuff).name).toBe("original");
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
it("applies the update when baseSeq matches the doc's _syncSeq", async () => {
|
|
573
|
+
const doc = await createDoc({_syncSeq: 5});
|
|
574
|
+
const {doc: updated} = await executeUpdate<ExecStuff>({
|
|
575
|
+
body: {name: "seq ok"},
|
|
576
|
+
concurrencyCheck: {baseSeq: 5, type: "seq"},
|
|
577
|
+
id: doc._id.toString(),
|
|
578
|
+
model: ExecStuffModel,
|
|
579
|
+
options: baseOptions(),
|
|
580
|
+
user: owner,
|
|
581
|
+
});
|
|
582
|
+
expect(updated.name).toBe("seq ok");
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
it("treats a missing _syncSeq as 0 for seq checks", async () => {
|
|
586
|
+
const doc = await createDoc();
|
|
587
|
+
const {doc: updated} = await executeUpdate<ExecStuff>({
|
|
588
|
+
body: {name: "zero seq"},
|
|
589
|
+
concurrencyCheck: {baseSeq: 0, type: "seq"},
|
|
590
|
+
id: doc._id.toString(),
|
|
591
|
+
model: ExecStuffModel,
|
|
592
|
+
options: baseOptions(),
|
|
593
|
+
user: owner,
|
|
594
|
+
});
|
|
595
|
+
expect(updated.name).toBe("zero seq");
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
it("returns 400 when the save fails Mongoose validation", async () => {
|
|
599
|
+
const doc = await createDoc();
|
|
600
|
+
const error = await caught(
|
|
601
|
+
executeUpdate<ExecStuff>({
|
|
602
|
+
body: {name: ""},
|
|
603
|
+
id: doc._id.toString(),
|
|
604
|
+
model: ExecStuffModel,
|
|
605
|
+
options: baseOptions(),
|
|
606
|
+
user: owner,
|
|
607
|
+
})
|
|
608
|
+
);
|
|
609
|
+
expect(error.status).toBe(400);
|
|
610
|
+
expect(error.title).toBe("Validation failed");
|
|
611
|
+
expect(error.meta?.fields).toMatchObject({name: expect.any(String)});
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it("invokes postUpdate with the updated doc, cleaned body, and previous doc", async () => {
|
|
615
|
+
const doc = await createDoc();
|
|
616
|
+
let seen: {updatedName: string; cleanedBody: Partial<ExecStuff>; prevName: string} | undefined;
|
|
617
|
+
const options = baseOptions({
|
|
618
|
+
postUpdate: (value, cleanedBody, _request, prevValue) => {
|
|
619
|
+
seen = {
|
|
620
|
+
cleanedBody,
|
|
621
|
+
prevName: (prevValue as ExecStuff).name,
|
|
622
|
+
updatedName: (value as ExecStuff).name,
|
|
623
|
+
};
|
|
624
|
+
},
|
|
625
|
+
} as Partial<ModelRouterOptions<ExecStuff>>);
|
|
626
|
+
await executeUpdate<ExecStuff>({
|
|
627
|
+
body: {name: "after"},
|
|
628
|
+
id: doc._id.toString(),
|
|
629
|
+
model: ExecStuffModel,
|
|
630
|
+
options,
|
|
631
|
+
user: owner,
|
|
632
|
+
});
|
|
633
|
+
expect(seen?.updatedName).toBe("after");
|
|
634
|
+
expect(seen?.prevName).toBe("original");
|
|
635
|
+
expect(seen?.cleanedBody).toEqual({name: "after"} as Partial<ExecStuff>);
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
it("wraps postUpdate errors as 400s", async () => {
|
|
639
|
+
const doc = await createDoc();
|
|
640
|
+
const options = baseOptions({
|
|
641
|
+
postUpdate: () => {
|
|
642
|
+
throw new Error("notify failed");
|
|
643
|
+
},
|
|
644
|
+
});
|
|
645
|
+
const error = await caught(
|
|
646
|
+
executeUpdate<ExecStuff>({
|
|
647
|
+
body: {name: "after"},
|
|
648
|
+
id: doc._id.toString(),
|
|
649
|
+
model: ExecStuffModel,
|
|
650
|
+
options,
|
|
651
|
+
user: owner,
|
|
652
|
+
})
|
|
653
|
+
);
|
|
654
|
+
expect(error.status).toBe(400);
|
|
655
|
+
expect(error.title).toBe("postUpdate hook error");
|
|
656
|
+
expect(error.detail).toBe(`postUpdate hook error on ${doc._id.toString()}: notify failed`);
|
|
657
|
+
});
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
describe("executeDelete", () => {
|
|
661
|
+
beforeAll(async () => {
|
|
662
|
+
await setupDb();
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
beforeEach(async () => {
|
|
666
|
+
await Promise.all([
|
|
667
|
+
ExecStuffModel.deleteMany({}),
|
|
668
|
+
ExecStringIdStuffModel.deleteMany({}),
|
|
669
|
+
ExecHardModel.deleteMany({}),
|
|
670
|
+
]);
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
const createDoc = async (fields: Partial<ExecStuff> = {}) =>
|
|
674
|
+
ExecStuffModel.create({name: "victim", ownerId: owner.id, ...fields});
|
|
675
|
+
|
|
676
|
+
it("soft deletes when the schema has a Boolean deleted path", async () => {
|
|
677
|
+
const doc = await createDoc();
|
|
678
|
+
const {doc: deleted} = await executeDelete<ExecStuff>({
|
|
679
|
+
id: doc._id.toString(),
|
|
680
|
+
model: ExecStuffModel,
|
|
681
|
+
options: baseOptions(),
|
|
682
|
+
user: owner,
|
|
683
|
+
});
|
|
684
|
+
expect((deleted as ExecStuff).deleted).toBe(true);
|
|
685
|
+
const tombstones = await ExecStuffModel.find({_id: doc._id, deleted: true});
|
|
686
|
+
expect(tombstones).toHaveLength(1);
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
it("returns an authorized string-id tombstone for an idempotent delete", async () => {
|
|
690
|
+
const id = "offline-generated-id";
|
|
691
|
+
await ExecStringIdStuffModel.create({
|
|
692
|
+
_id: id,
|
|
693
|
+
deleted: true,
|
|
694
|
+
name: "already gone",
|
|
695
|
+
ownerId: owner.id,
|
|
696
|
+
});
|
|
697
|
+
const options = baseOptions() as unknown as ModelRouterOptions<ExecStringIdStuff>;
|
|
698
|
+
|
|
699
|
+
const {doc} = await executeDelete<ExecStringIdStuff>({
|
|
700
|
+
id,
|
|
701
|
+
model: ExecStringIdStuffModel,
|
|
702
|
+
options,
|
|
703
|
+
user: owner,
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
assert.equal(doc._id, id);
|
|
707
|
+
assert.isTrue(doc.deleted);
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
it("checks object permissions before returning an idempotent tombstone", async () => {
|
|
711
|
+
const id = "foreign-offline-id";
|
|
712
|
+
await ExecStringIdStuffModel.create({
|
|
713
|
+
_id: id,
|
|
714
|
+
deleted: true,
|
|
715
|
+
name: "not yours",
|
|
716
|
+
ownerId: owner.id,
|
|
717
|
+
});
|
|
718
|
+
const options = baseOptions() as unknown as ModelRouterOptions<ExecStringIdStuff>;
|
|
719
|
+
|
|
720
|
+
const error = await caught(
|
|
721
|
+
executeDelete<ExecStringIdStuff>({
|
|
722
|
+
id,
|
|
723
|
+
model: ExecStringIdStuffModel,
|
|
724
|
+
options,
|
|
725
|
+
user: stranger,
|
|
726
|
+
})
|
|
727
|
+
);
|
|
728
|
+
|
|
729
|
+
assert.equal(error.status, 403);
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
it("hard deletes when the schema has no deleted path", async () => {
|
|
733
|
+
const doc = await ExecHardModel.create({name: "gone"});
|
|
734
|
+
await executeDelete<ExecHardStuff>({
|
|
735
|
+
id: doc._id.toString(),
|
|
736
|
+
model: ExecHardModel,
|
|
737
|
+
options: hardOptions(),
|
|
738
|
+
user: owner,
|
|
739
|
+
});
|
|
740
|
+
const remaining = await ExecHardModel.findById(doc._id);
|
|
741
|
+
expect(remaining).toBeNull();
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
it("denies at the method level when delete permissions are empty", async () => {
|
|
745
|
+
const doc = await createDoc();
|
|
746
|
+
const options = baseOptions();
|
|
747
|
+
options.permissions = {...options.permissions, delete: []};
|
|
748
|
+
const error = await caught(
|
|
749
|
+
executeDelete<ExecStuff>({
|
|
750
|
+
id: doc._id.toString(),
|
|
751
|
+
model: ExecStuffModel,
|
|
752
|
+
options,
|
|
753
|
+
user: owner,
|
|
754
|
+
})
|
|
755
|
+
);
|
|
756
|
+
expect(error.status).toBe(405);
|
|
757
|
+
expect(error.title).toContain("Access to DELETE on ExecStuff");
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
it("denies at the object level for non-owners", async () => {
|
|
761
|
+
const doc = await createDoc();
|
|
762
|
+
const error = await caught(
|
|
763
|
+
executeDelete<ExecStuff>({
|
|
764
|
+
id: doc._id.toString(),
|
|
765
|
+
model: ExecStuffModel,
|
|
766
|
+
options: baseOptions(),
|
|
767
|
+
user: stranger,
|
|
768
|
+
})
|
|
769
|
+
);
|
|
770
|
+
expect(error.status).toBe(403);
|
|
771
|
+
expect(error.title).toContain(`Access to GET on ExecStuff:${doc._id.toString()}`);
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
it("returns 403 when preDelete returns null", async () => {
|
|
775
|
+
const doc = await createDoc();
|
|
776
|
+
const options = baseOptions({preDelete: () => null});
|
|
777
|
+
const error = await caught(
|
|
778
|
+
executeDelete<ExecStuff>({
|
|
779
|
+
id: doc._id.toString(),
|
|
780
|
+
model: ExecStuffModel,
|
|
781
|
+
options,
|
|
782
|
+
user: owner,
|
|
783
|
+
})
|
|
784
|
+
);
|
|
785
|
+
expect(error.status).toBe(403);
|
|
786
|
+
expect(error.title).toBe("Delete not allowed");
|
|
787
|
+
expect(error.detail).toBe(`preDelete hook for ${doc._id.toString()} returned null`);
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
it("returns 403 when preDelete returns undefined", async () => {
|
|
791
|
+
const doc = await createDoc();
|
|
792
|
+
const options = baseOptions({
|
|
793
|
+
preDelete: () => undefined,
|
|
794
|
+
} as unknown as Partial<ModelRouterOptions<ExecStuff>>);
|
|
795
|
+
const error = await caught(
|
|
796
|
+
executeDelete<ExecStuff>({
|
|
797
|
+
id: doc._id.toString(),
|
|
798
|
+
model: ExecStuffModel,
|
|
799
|
+
options,
|
|
800
|
+
user: owner,
|
|
801
|
+
})
|
|
802
|
+
);
|
|
803
|
+
expect(error.status).toBe(403);
|
|
804
|
+
expect(error.detail).toBe("A body must be returned from preDelete");
|
|
805
|
+
});
|
|
806
|
+
|
|
807
|
+
it("wraps plain preDelete errors as 403s", async () => {
|
|
808
|
+
const doc = await createDoc();
|
|
809
|
+
const options = baseOptions({
|
|
810
|
+
preDelete: () => {
|
|
811
|
+
throw new Error("no delete");
|
|
812
|
+
},
|
|
813
|
+
});
|
|
814
|
+
const error = await caught(
|
|
815
|
+
executeDelete<ExecStuff>({
|
|
816
|
+
id: doc._id.toString(),
|
|
817
|
+
model: ExecStuffModel,
|
|
818
|
+
options,
|
|
819
|
+
user: owner,
|
|
820
|
+
})
|
|
821
|
+
);
|
|
822
|
+
expect(error.status).toBe(403);
|
|
823
|
+
expect(error.title).toBe("preDelete hook error");
|
|
824
|
+
expect(error.detail).toBe(`preDelete hook error on ${doc._id.toString()}: no delete`);
|
|
825
|
+
});
|
|
826
|
+
|
|
827
|
+
it("rethrows APIErrors from preDelete unchanged", async () => {
|
|
828
|
+
const doc = await createDoc();
|
|
829
|
+
const options = baseOptions({
|
|
830
|
+
preDelete: () => {
|
|
831
|
+
throw new APIError({status: 422, title: "cannot delete yet"});
|
|
832
|
+
},
|
|
833
|
+
});
|
|
834
|
+
const error = await caught(
|
|
835
|
+
executeDelete<ExecStuff>({
|
|
836
|
+
id: doc._id.toString(),
|
|
837
|
+
model: ExecStuffModel,
|
|
838
|
+
options,
|
|
839
|
+
user: owner,
|
|
840
|
+
})
|
|
841
|
+
);
|
|
842
|
+
expect(error.status).toBe(422);
|
|
843
|
+
expect(error.title).toBe("cannot delete yet");
|
|
844
|
+
});
|
|
845
|
+
|
|
846
|
+
it("invokes postDelete with the tombstoned doc", async () => {
|
|
847
|
+
const doc = await createDoc();
|
|
848
|
+
let seenDeleted: boolean | undefined;
|
|
849
|
+
const options = baseOptions({
|
|
850
|
+
postDelete: (_request, value) => {
|
|
851
|
+
seenDeleted = (value as ExecStuff).deleted;
|
|
852
|
+
},
|
|
853
|
+
} as Partial<ModelRouterOptions<ExecStuff>>);
|
|
854
|
+
await executeDelete<ExecStuff>({
|
|
855
|
+
id: doc._id.toString(),
|
|
856
|
+
model: ExecStuffModel,
|
|
857
|
+
options,
|
|
858
|
+
user: owner,
|
|
859
|
+
});
|
|
860
|
+
expect(seenDeleted).toBe(true);
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
it("wraps postDelete errors as 400s", async () => {
|
|
864
|
+
const doc = await createDoc();
|
|
865
|
+
const options = baseOptions({
|
|
866
|
+
postDelete: () => {
|
|
867
|
+
throw new Error("cascade failed");
|
|
868
|
+
},
|
|
869
|
+
});
|
|
870
|
+
const error = await caught(
|
|
871
|
+
executeDelete<ExecStuff>({
|
|
872
|
+
id: doc._id.toString(),
|
|
873
|
+
model: ExecStuffModel,
|
|
874
|
+
options,
|
|
875
|
+
user: owner,
|
|
876
|
+
})
|
|
877
|
+
);
|
|
878
|
+
expect(error.status).toBe(400);
|
|
879
|
+
expect(error.title).toBe("postDelete hook error");
|
|
880
|
+
expect(error.detail).toBe("cascade failed");
|
|
881
|
+
});
|
|
882
|
+
});
|