@terreno/api 57.0.0-beta.1 → 57.1.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/.ai/skills/mongoose-schema-safety/SKILL.md +8 -0
- package/dist/actions.d.ts +7 -2
- package/dist/actions.js +60 -25
- package/dist/adminModelRouter.d.ts +14 -0
- package/dist/adminModelRouter.js +199 -0
- package/dist/adminTypes.d.ts +156 -0
- package/dist/adminTypes.js +5 -0
- package/dist/api.d.ts +44 -3
- package/dist/api.js +74 -26
- package/dist/auth.d.ts +18 -1
- package/dist/auth.js +122 -20
- package/dist/consentApp.d.ts +2 -0
- package/dist/consentApp.js +98 -0
- package/dist/index.d.ts +22 -1
- package/dist/index.js +51 -7
- package/dist/mcp/auth.d.ts +11 -0
- package/dist/mcp/auth.js +133 -0
- package/dist/mcp/createMCPRequest.d.ts +16 -0
- package/dist/mcp/createMCPRequest.js +26 -0
- package/dist/mcp/handlers.d.ts +8 -0
- package/dist/mcp/handlers.js +793 -0
- package/dist/mcp/query.d.ts +32 -0
- package/dist/mcp/query.js +232 -0
- package/dist/mcp/registry.d.ts +21 -0
- package/dist/mcp/registry.js +54 -0
- package/dist/mcp/schemaGenerator.d.ts +14 -0
- package/dist/mcp/schemaGenerator.js +379 -0
- package/dist/mcp/server.d.ts +8 -0
- package/dist/mcp/server.js +133 -0
- package/dist/mcp/toolGenerator.d.ts +14 -0
- package/dist/mcp/toolGenerator.js +246 -0
- package/dist/mcp/types.d.ts +85 -0
- package/dist/mcp/types.js +2 -0
- package/dist/openApi.d.ts +1 -1
- package/dist/openApi.js +26 -8
- package/dist/permissions.js +15 -15
- package/dist/rbac/access.d.ts +3 -0
- package/dist/rbac/access.js +294 -0
- package/dist/rbac/auditModel.d.ts +31 -0
- package/dist/rbac/auditModel.js +112 -0
- package/dist/rbac/backfillAdmins.d.ts +18 -0
- package/dist/rbac/backfillAdmins.js +139 -0
- package/dist/rbac/fieldViews.d.ts +3 -0
- package/dist/rbac/fieldViews.js +201 -0
- package/dist/rbac/middleware.d.ts +21 -0
- package/dist/rbac/middleware.js +108 -0
- package/dist/rbac/modelRouterAccess.d.ts +42 -0
- package/dist/rbac/modelRouterAccess.js +388 -0
- package/dist/rbac/permissionUtils.d.ts +8 -0
- package/dist/rbac/permissionUtils.js +209 -0
- package/dist/rbac/realtimeAccess.d.ts +18 -0
- package/dist/rbac/realtimeAccess.js +173 -0
- package/dist/rbac/resolve.d.ts +28 -0
- package/dist/rbac/resolve.js +387 -0
- package/dist/rbac/roleManager.d.ts +20 -0
- package/dist/rbac/roleManager.js +741 -0
- package/dist/rbac/roleModel.d.ts +42 -0
- package/dist/rbac/roleModel.js +267 -0
- package/dist/rbac/routes.d.ts +9 -0
- package/dist/rbac/routes.js +211 -0
- package/dist/rbac/scopes.d.ts +15 -0
- package/dist/rbac/scopes.js +155 -0
- package/dist/rbac/statements.d.ts +20 -0
- package/dist/rbac/statements.js +115 -0
- package/dist/rbac/types.d.ts +202 -0
- package/dist/rbac/types.js +2 -0
- package/dist/rbac/userPlugin.d.ts +12 -0
- package/dist/rbac/userPlugin.js +41 -0
- package/dist/realtime/changeStreamWatcher.d.ts +1 -1
- package/dist/realtime/changeStreamWatcher.js +48 -39
- package/dist/realtime/realtimeApp.js +4 -8
- package/dist/realtime/registry.d.ts +5 -0
- package/dist/realtime/registry.js +13 -1
- package/dist/scrubAdminFields.d.ts +17 -0
- package/dist/scrubAdminFields.js +192 -0
- package/dist/sync/executors.d.ts +1 -1
- package/dist/sync/executors.js +63 -40
- package/dist/sync/loadHarness.js +4 -5
- package/dist/sync/registry.d.ts +5 -0
- package/dist/sync/registry.js +13 -1
- package/dist/sync/routes.js +41 -36
- package/dist/sync/serialize.js +5 -6
- package/dist/terrenoApp.d.ts +18 -0
- package/dist/terrenoApp.js +46 -3
- package/dist/terrenoPlugin.d.ts +8 -1
- package/dist/tests/createTestData.js +8 -0
- package/dist/tests/models.d.ts +1 -0
- package/dist/tests/models.js +5 -0
- package/dist/tests/testHelper.js +1 -2
- package/dist/transformers.js +16 -1
- package/package.json +16 -12
- package/src/__snapshots__/openApi.test.ts.snap +192 -0
- package/src/actions.test.ts +194 -1
- package/src/actions.ts +65 -8
- package/src/adminModelRouter.ts +154 -0
- package/src/adminTypes.ts +183 -0
- package/src/api.test.ts +66 -1
- package/src/api.ts +131 -12
- package/src/auth.test.ts +135 -1
- package/src/auth.ts +81 -3
- package/src/consentApp.test.ts +24 -0
- package/src/consentApp.ts +100 -0
- package/src/index.ts +56 -0
- package/src/mcp/auth.test.ts +197 -0
- package/src/mcp/auth.ts +100 -0
- package/src/mcp/createMCPRequest.test.ts +47 -0
- package/src/mcp/createMCPRequest.ts +32 -0
- package/src/mcp/customTools.test.ts +54 -0
- package/src/mcp/handlers.ts +639 -0
- package/src/mcp/integration.test.ts +1493 -0
- package/src/mcp/query.test.ts +172 -0
- package/src/mcp/query.ts +179 -0
- package/src/mcp/registry.ts +70 -0
- package/src/mcp/schemaGenerator.test.ts +182 -0
- package/src/mcp/schemaGenerator.ts +354 -0
- package/src/mcp/server.test.ts +226 -0
- package/src/mcp/server.ts +86 -0
- package/src/mcp/toolGenerator.test.ts +284 -0
- package/src/mcp/toolGenerator.ts +199 -0
- package/src/mcp/types.ts +95 -0
- package/src/openApi.test.ts +107 -0
- package/src/openApi.ts +32 -1
- package/src/permissions.ts +3 -2
- package/src/rbac/access.test.ts +357 -0
- package/src/rbac/access.ts +201 -0
- package/src/rbac/auditModel.ts +103 -0
- package/src/rbac/backfillAdmins.ts +54 -0
- package/src/rbac/fieldViews.test.ts +39 -0
- package/src/rbac/fieldViews.ts +122 -0
- package/src/rbac/middleware.ts +55 -0
- package/src/rbac/modelRouterAccess.test.ts +336 -0
- package/src/rbac/modelRouterAccess.ts +331 -0
- package/src/rbac/permissionUtils.test.ts +112 -0
- package/src/rbac/permissionUtils.ts +83 -0
- package/src/rbac/realtimeAccess.test.ts +242 -0
- package/src/rbac/realtimeAccess.ts +138 -0
- package/src/rbac/resolve.test.ts +358 -0
- package/src/rbac/resolve.ts +285 -0
- package/src/rbac/roleManager.test.ts +915 -0
- package/src/rbac/roleManager.ts +440 -0
- package/src/rbac/roleModel.test.ts +114 -0
- package/src/rbac/roleModel.ts +196 -0
- package/src/rbac/routes.ts +170 -0
- package/src/rbac/scopes.test.ts +105 -0
- package/src/rbac/scopes.ts +81 -0
- package/src/rbac/statements.test.ts +65 -0
- package/src/rbac/statements.ts +62 -0
- package/src/rbac/types.ts +198 -0
- package/src/rbac/userPlugin.test.ts +55 -0
- package/src/rbac/userPlugin.ts +25 -0
- package/src/realtime/changeStreamWatcher.ts +20 -15
- package/src/realtime/realtime.test.ts +25 -1
- package/src/realtime/realtimeApp.ts +2 -5
- package/src/realtime/registry.ts +15 -0
- package/src/scrubAdminFields.test.ts +113 -0
- package/src/scrubAdminFields.ts +158 -0
- package/src/sync/executors.ts +31 -1
- package/src/sync/loadHarness.ts +8 -7
- package/src/sync/models.test.ts +262 -0
- package/src/sync/registry.ts +15 -0
- package/src/sync/routes.ts +25 -11
- package/src/sync/scripts/compactTombstones.test.ts +4 -6
- package/src/sync/serialize.ts +17 -9
- package/src/sync/sync.test.ts +52 -0
- package/src/sync/syncRoutes.test.ts +5 -2
- package/src/sync/syncSocket.test.ts +28 -0
- package/src/terrenoApp.test.ts +100 -0
- package/src/terrenoApp.ts +65 -3
- package/src/terrenoPlugin.ts +10 -1
- package/src/tests/bunSetup.ts +2 -0
- package/src/tests/createTestData.ts +5 -1
- package/src/tests/models.ts +6 -0
- package/src/tests/testHelper.ts +1 -3
- package/src/transformers.test.ts +12 -0
- package/src/transformers.ts +7 -1
|
@@ -0,0 +1,1493 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
+
import type express from "express";
|
|
3
|
+
import mongoose, {Schema} from "mongoose";
|
|
4
|
+
|
|
5
|
+
import type {User} from "../auth";
|
|
6
|
+
import {APIError} from "../errors";
|
|
7
|
+
import {OwnerQueryFilter, Permissions} from "../permissions";
|
|
8
|
+
import {createAccess} from "../rbac/access";
|
|
9
|
+
import {terrenoStatements} from "../rbac/statements";
|
|
10
|
+
import {setupDb} from "../tests";
|
|
11
|
+
import {handleCreate, handleDelete, handleList, handleRead, handleUpdate} from "./handlers";
|
|
12
|
+
import {
|
|
13
|
+
clearMCPRegistry,
|
|
14
|
+
getMCPRegistry,
|
|
15
|
+
registerMCPModel,
|
|
16
|
+
updateMCPRegistryOptions,
|
|
17
|
+
} from "./registry";
|
|
18
|
+
import {generateAllTools} from "./toolGenerator";
|
|
19
|
+
import type {MCPRegistryEntry, MCPRequest} from "./types";
|
|
20
|
+
|
|
21
|
+
// Test model
|
|
22
|
+
const todoSchema = new Schema({
|
|
23
|
+
completed: {default: false, description: "Whether the todo is completed", type: Boolean},
|
|
24
|
+
dueDate: {description: "When the todo is due", type: Date},
|
|
25
|
+
metadata: {description: "Arbitrary metadata", type: Schema.Types.Mixed},
|
|
26
|
+
ownerId: {description: "Owner of the todo", ref: "MCPOwner", type: Schema.Types.ObjectId},
|
|
27
|
+
title: {description: "Todo title", required: true, type: String},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const ownerSchema = new Schema({
|
|
31
|
+
email: {description: "Owner email", type: String},
|
|
32
|
+
tier: {description: "Owner subscription tier", type: String},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const getOrCreateModel = (name: string, schema: Schema): mongoose.Model<TestDocFields> => {
|
|
36
|
+
try {
|
|
37
|
+
return mongoose.model<TestDocFields>(name);
|
|
38
|
+
} catch {
|
|
39
|
+
return mongoose.model<TestDocFields>(name, schema);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** Loose document shape shared by the test models. */
|
|
44
|
+
interface TestDocFields {
|
|
45
|
+
_id: mongoose.Types.ObjectId;
|
|
46
|
+
[key: string]: unknown;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const TodoModel = getOrCreateModel("MCPTodo", todoSchema);
|
|
50
|
+
const OwnerModel = getOrCreateModel("MCPOwner", ownerSchema);
|
|
51
|
+
const userWriteSchema = new Schema({
|
|
52
|
+
email: {type: String},
|
|
53
|
+
roles: {default: [], type: [String]},
|
|
54
|
+
});
|
|
55
|
+
const UserWriteModel = getOrCreateModel("MCPUserWrite", userWriteSchema);
|
|
56
|
+
|
|
57
|
+
interface TestUser {
|
|
58
|
+
_id: mongoose.Types.ObjectId;
|
|
59
|
+
admin: boolean;
|
|
60
|
+
id: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const makeUser = (admin = false): TestUser => {
|
|
64
|
+
const _id = new mongoose.Types.ObjectId();
|
|
65
|
+
return {_id, admin, id: _id.toString()};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** Test users stand in for UserDocument, which needs a consumer model to construct. */
|
|
69
|
+
const asUser = (user: TestUser): User => user as unknown as User;
|
|
70
|
+
|
|
71
|
+
const adminUser = makeUser(true);
|
|
72
|
+
const normalUser = makeUser();
|
|
73
|
+
const otherUser = makeUser();
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Tool results are JSON text. Tests assert on deeply nested response shapes, so the
|
|
77
|
+
* parsed value is deliberately loose rather than modelled per assertion.
|
|
78
|
+
*/
|
|
79
|
+
// noExplicitAny: parsed tool JSON is asserted at arbitrary depth
|
|
80
|
+
// biome-ignore lint/suspicious/noExplicitAny: parsed tool JSON is asserted at arbitrary depth
|
|
81
|
+
type ParsedToolResult = Record<string, any>;
|
|
82
|
+
|
|
83
|
+
const parseResult = (result: {content: Array<{text: string}>}): ParsedToolResult => {
|
|
84
|
+
return JSON.parse(result.content[0].text);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const expectStubRequest = ({
|
|
88
|
+
body,
|
|
89
|
+
request,
|
|
90
|
+
user,
|
|
91
|
+
}: {
|
|
92
|
+
body: Record<string, unknown>;
|
|
93
|
+
request: MCPRequest | undefined;
|
|
94
|
+
user: TestUser;
|
|
95
|
+
}): void => {
|
|
96
|
+
expect(request?.isMCPRequest).toBe(true);
|
|
97
|
+
expect(request?.method).toBe("MCP");
|
|
98
|
+
expect(request?.body).toEqual(body);
|
|
99
|
+
expect(request?.headers).toEqual({});
|
|
100
|
+
expect(request?.query).toEqual({});
|
|
101
|
+
expect(request?.params).toEqual({});
|
|
102
|
+
expect((request?.user as unknown as TestUser | undefined)?.id).toBe(user.id);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const createEntry = (): MCPRegistryEntry => ({
|
|
106
|
+
config: {
|
|
107
|
+
maxLimit: 10,
|
|
108
|
+
methods: ["create", "list", "read", "update", "delete"],
|
|
109
|
+
},
|
|
110
|
+
model: TodoModel,
|
|
111
|
+
modelName: "MCPTodo",
|
|
112
|
+
options: {
|
|
113
|
+
permissions: {
|
|
114
|
+
create: [Permissions.IsAuthenticated],
|
|
115
|
+
delete: [Permissions.IsOwner],
|
|
116
|
+
list: [Permissions.IsAuthenticated],
|
|
117
|
+
read: [Permissions.IsAuthenticated],
|
|
118
|
+
update: [Permissions.IsOwner],
|
|
119
|
+
},
|
|
120
|
+
preCreate: (body, req) => ({
|
|
121
|
+
...(body as Record<string, unknown>),
|
|
122
|
+
ownerId: (req.user as unknown as TestUser | undefined)?._id,
|
|
123
|
+
}),
|
|
124
|
+
queryFields: ["completed", "ownerId", "title"],
|
|
125
|
+
queryFilter: OwnerQueryFilter,
|
|
126
|
+
sort: "-title",
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("MCP Integration", () => {
|
|
131
|
+
let entry: MCPRegistryEntry;
|
|
132
|
+
|
|
133
|
+
beforeEach(async () => {
|
|
134
|
+
await TodoModel.deleteMany({});
|
|
135
|
+
await OwnerModel.deleteMany({});
|
|
136
|
+
await UserWriteModel.deleteMany({});
|
|
137
|
+
clearMCPRegistry();
|
|
138
|
+
entry = createEntry();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe("handleCreate", () => {
|
|
142
|
+
it("creates a document with authenticated user", async () => {
|
|
143
|
+
const result = await handleCreate(entry, {title: "Test todo"}, asUser(normalUser));
|
|
144
|
+
const parsed = parseResult(result);
|
|
145
|
+
|
|
146
|
+
expect(parsed.data).toBeDefined();
|
|
147
|
+
expect(parsed.data.title).toBe("Test todo");
|
|
148
|
+
expect(parsed.data.completed).toBe(false);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("sets ownerId via preCreate", async () => {
|
|
152
|
+
const result = await handleCreate(entry, {title: "Owned todo"}, asUser(normalUser));
|
|
153
|
+
const parsed = parseResult(result);
|
|
154
|
+
|
|
155
|
+
expect(String(parsed.data.ownerId)).toBe(normalUser.id);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("denies create without user", async () => {
|
|
159
|
+
const result = await handleCreate(entry, {title: "Test"});
|
|
160
|
+
const parsed = parseResult(result);
|
|
161
|
+
|
|
162
|
+
expect(parsed.error).toBeDefined();
|
|
163
|
+
expect(parsed.error).toContain("Permission denied");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("rejects create fields outside the RBAC write mask", async () => {
|
|
167
|
+
await setupDb();
|
|
168
|
+
const access = createAccess({
|
|
169
|
+
connection: mongoose.connection,
|
|
170
|
+
defaultRoles: [
|
|
171
|
+
{
|
|
172
|
+
displayName: "Writer",
|
|
173
|
+
name: "mcp-writer",
|
|
174
|
+
permissions: {todo: ["create", "read", "update", "list"]},
|
|
175
|
+
},
|
|
176
|
+
],
|
|
177
|
+
fieldViews: {
|
|
178
|
+
todo: {
|
|
179
|
+
createView: "deny",
|
|
180
|
+
select: async () => "public",
|
|
181
|
+
views: {
|
|
182
|
+
public: {omit: [], read: "*", write: ["title"]},
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
statements: {
|
|
187
|
+
...terrenoStatements,
|
|
188
|
+
todo: ["create", "read", "update", "delete", "list"],
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
await access.roles.seedDefaults();
|
|
192
|
+
const writer = {...normalUser, roles: ["mcp-writer"]};
|
|
193
|
+
const result = await handleCreate(
|
|
194
|
+
{
|
|
195
|
+
...entry,
|
|
196
|
+
options: {
|
|
197
|
+
...entry.options,
|
|
198
|
+
access: {resource: "todo"},
|
|
199
|
+
accessControl: access as never,
|
|
200
|
+
preCreate: undefined,
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
{completed: true, title: "Secret"},
|
|
204
|
+
asUser(writer)
|
|
205
|
+
);
|
|
206
|
+
const parsed = parseResult(result);
|
|
207
|
+
expect(parsed.error).toContain("Validation failed");
|
|
208
|
+
expect(await TodoModel.countDocuments()).toBe(0);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("drops User roles on MCP create when accessControl is set", async () => {
|
|
212
|
+
const writeEntry: MCPRegistryEntry = {
|
|
213
|
+
config: {methods: ["create"]},
|
|
214
|
+
model: UserWriteModel,
|
|
215
|
+
modelName: "User",
|
|
216
|
+
options: {
|
|
217
|
+
accessControl: {} as never,
|
|
218
|
+
permissions: {
|
|
219
|
+
create: [Permissions.IsAuthenticated],
|
|
220
|
+
delete: [],
|
|
221
|
+
list: [],
|
|
222
|
+
read: [],
|
|
223
|
+
update: [],
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
const result = await handleCreate(
|
|
228
|
+
writeEntry,
|
|
229
|
+
{email: "mcp-roles@example.com", roles: ["superadmin"]},
|
|
230
|
+
asUser(normalUser)
|
|
231
|
+
);
|
|
232
|
+
const parsed = parseResult(result);
|
|
233
|
+
expect(parsed.data.email).toBe("mcp-roles@example.com");
|
|
234
|
+
expect(parsed.data.roles ?? []).toEqual([]);
|
|
235
|
+
const stored = await UserWriteModel.findOne({email: "mcp-roles@example.com"}).lean();
|
|
236
|
+
expect(stored?.roles ?? []).toEqual([]);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("strips REST excludeFromCreate fields before persist", async () => {
|
|
240
|
+
const writeEntry: MCPRegistryEntry = {
|
|
241
|
+
...entry,
|
|
242
|
+
options: {
|
|
243
|
+
...entry.options,
|
|
244
|
+
preCreate: undefined,
|
|
245
|
+
validation: {excludeFromCreate: ["ownerId"]},
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
const result = await handleCreate(
|
|
249
|
+
writeEntry,
|
|
250
|
+
{ownerId: otherUser._id.toString(), title: "No owner hijack"},
|
|
251
|
+
asUser(normalUser)
|
|
252
|
+
);
|
|
253
|
+
const parsed = parseResult(result);
|
|
254
|
+
|
|
255
|
+
expect(parsed.data.title).toBe("No owner hijack");
|
|
256
|
+
expect(parsed.data.ownerId).toBeUndefined();
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("does not restore excludeFromCreate fields when preCreate spreads req.body", async () => {
|
|
260
|
+
const writeEntry: MCPRegistryEntry = {
|
|
261
|
+
...entry,
|
|
262
|
+
options: {
|
|
263
|
+
...entry.options,
|
|
264
|
+
preCreate: (_body, req: express.Request) => ({...req.body}),
|
|
265
|
+
validation: {excludeFromCreate: ["ownerId"]},
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
const result = await handleCreate(
|
|
269
|
+
writeEntry,
|
|
270
|
+
{ownerId: otherUser._id.toString(), title: "Hook spread"},
|
|
271
|
+
asUser(normalUser)
|
|
272
|
+
);
|
|
273
|
+
const parsed = parseResult(result);
|
|
274
|
+
|
|
275
|
+
expect(parsed.data.title).toBe("Hook spread");
|
|
276
|
+
expect(parsed.data.ownerId).toBeUndefined();
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("strips MCP excludeFields from the create persist payload", async () => {
|
|
280
|
+
const writeEntry: MCPRegistryEntry = {
|
|
281
|
+
...entry,
|
|
282
|
+
config: {...entry.config, excludeFields: ["ownerId"]},
|
|
283
|
+
options: {...entry.options, preCreate: undefined},
|
|
284
|
+
};
|
|
285
|
+
const result = await handleCreate(
|
|
286
|
+
writeEntry,
|
|
287
|
+
{ownerId: otherUser._id.toString(), title: "No hidden write"},
|
|
288
|
+
asUser(normalUser)
|
|
289
|
+
);
|
|
290
|
+
const parsed = parseResult(result);
|
|
291
|
+
|
|
292
|
+
expect(parsed.data.title).toBe("No hidden write");
|
|
293
|
+
expect(parsed.data.ownerId).toBeUndefined();
|
|
294
|
+
const stored = await TodoModel.findOne({title: "No hidden write"}).lean();
|
|
295
|
+
expect(stored?.ownerId).toBeUndefined();
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("strips nested excludeFields from the create persist payload", async () => {
|
|
299
|
+
const writeEntry: MCPRegistryEntry = {
|
|
300
|
+
...entry,
|
|
301
|
+
config: {...entry.config, excludeFields: ["secret", "metadata.items.token"]},
|
|
302
|
+
options: {...entry.options, preCreate: undefined},
|
|
303
|
+
};
|
|
304
|
+
const result = await handleCreate(
|
|
305
|
+
writeEntry,
|
|
306
|
+
{
|
|
307
|
+
metadata: {
|
|
308
|
+
items: [{name: "one", token: "t1"}],
|
|
309
|
+
nested: {secret: "hidden", visible: "shown"},
|
|
310
|
+
secret: "hidden",
|
|
311
|
+
},
|
|
312
|
+
title: "Nested deny",
|
|
313
|
+
},
|
|
314
|
+
asUser(normalUser)
|
|
315
|
+
);
|
|
316
|
+
const parsed = parseResult(result);
|
|
317
|
+
|
|
318
|
+
expect(parsed.data.title).toBe("Nested deny");
|
|
319
|
+
const stored = await TodoModel.findOne({title: "Nested deny"}).lean();
|
|
320
|
+
expect(stored?.metadata).toEqual({
|
|
321
|
+
items: [{name: "one"}],
|
|
322
|
+
nested: {visible: "shown"},
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("strips literal dotted keys that match excludeFields from create", async () => {
|
|
327
|
+
const writeEntry: MCPRegistryEntry = {
|
|
328
|
+
...entry,
|
|
329
|
+
config: {...entry.config, excludeFields: ["metadata.nested.token"]},
|
|
330
|
+
options: {...entry.options, preCreate: undefined},
|
|
331
|
+
};
|
|
332
|
+
const result = await handleCreate(
|
|
333
|
+
writeEntry,
|
|
334
|
+
{
|
|
335
|
+
"metadata.nested.token": "stolen",
|
|
336
|
+
title: "Literal dotted key",
|
|
337
|
+
},
|
|
338
|
+
asUser(normalUser)
|
|
339
|
+
);
|
|
340
|
+
const parsed = parseResult(result);
|
|
341
|
+
|
|
342
|
+
expect(result.isError).not.toBe(true);
|
|
343
|
+
expect(parsed.data.title).toBe("Literal dotted key");
|
|
344
|
+
const stored = await TodoModel.findOne({title: "Literal dotted key"}).lean();
|
|
345
|
+
expect(stored?.metadata).toBeUndefined();
|
|
346
|
+
expect((stored as Record<string, unknown> | null)?.["metadata.nested.token"]).toBeUndefined();
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
describe("handleList", () => {
|
|
351
|
+
beforeEach(async () => {
|
|
352
|
+
await TodoModel.create([
|
|
353
|
+
{completed: false, ownerId: normalUser._id, title: "Todo 1"},
|
|
354
|
+
{completed: true, ownerId: normalUser._id, title: "Todo 2"},
|
|
355
|
+
{completed: false, ownerId: otherUser._id, title: "Todo 3"},
|
|
356
|
+
]);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("lists documents filtered by owner", async () => {
|
|
360
|
+
const result = await handleList(entry, {}, asUser(normalUser));
|
|
361
|
+
const parsed = parseResult(result);
|
|
362
|
+
|
|
363
|
+
expect(parsed.data).toHaveLength(2);
|
|
364
|
+
expect(parsed.total).toBe(2);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("returns a structured error when queryFilter throws", async () => {
|
|
368
|
+
const throwingEntry: MCPRegistryEntry = {
|
|
369
|
+
...entry,
|
|
370
|
+
options: {
|
|
371
|
+
...entry.options,
|
|
372
|
+
queryFilter: async () => {
|
|
373
|
+
throw new APIError({status: 400, title: "Invalid list filter"});
|
|
374
|
+
},
|
|
375
|
+
},
|
|
376
|
+
};
|
|
377
|
+
const result = await handleList(throwingEntry, {}, asUser(normalUser));
|
|
378
|
+
const parsed = parseResult(result);
|
|
379
|
+
|
|
380
|
+
expect(result.isError).toBe(true);
|
|
381
|
+
expect(parsed.error).toBe("Invalid list filter");
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it("admin can see all via query filter (returns admin's own items)", async () => {
|
|
385
|
+
// OwnerQueryFilter filters by ownerId = user.id, even for admins
|
|
386
|
+
const result = await handleList(entry, {}, asUser(adminUser));
|
|
387
|
+
const parsed = parseResult(result);
|
|
388
|
+
|
|
389
|
+
// Admin has no todos
|
|
390
|
+
expect(parsed.data).toHaveLength(0);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("enforces maxLimit", async () => {
|
|
394
|
+
// Create more items than maxLimit
|
|
395
|
+
const items = Array.from({length: 15}, (_, i) => ({
|
|
396
|
+
ownerId: normalUser._id,
|
|
397
|
+
title: `Item ${i}`,
|
|
398
|
+
}));
|
|
399
|
+
await TodoModel.create(items);
|
|
400
|
+
|
|
401
|
+
const result = await handleList(entry, {limit: 100}, asUser(normalUser));
|
|
402
|
+
const parsed = parseResult(result);
|
|
403
|
+
|
|
404
|
+
// maxLimit is 10
|
|
405
|
+
expect(parsed.data.length).toBeLessThanOrEqual(10);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it("supports pagination", async () => {
|
|
409
|
+
const result = await handleList(entry, {limit: 1, page: 2}, asUser(normalUser));
|
|
410
|
+
const parsed = parseResult(result);
|
|
411
|
+
|
|
412
|
+
expect(parsed.data).toHaveLength(1);
|
|
413
|
+
expect(parsed.page).toBe(2);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it("clamps a page below 1 to the first page", async () => {
|
|
417
|
+
const result = await handleList(entry, {limit: 1, page: -3}, asUser(normalUser));
|
|
418
|
+
const parsed = parseResult(result);
|
|
419
|
+
|
|
420
|
+
expect(parsed.page).toBe(1);
|
|
421
|
+
expect(parsed.data).toHaveLength(1);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("supports filtering by query fields", async () => {
|
|
425
|
+
const result = await handleList(entry, {completed: true}, asUser(normalUser));
|
|
426
|
+
const parsed = parseResult(result);
|
|
427
|
+
|
|
428
|
+
expect(parsed.data).toHaveLength(1);
|
|
429
|
+
expect(parsed.data[0].title).toBe("Todo 2");
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it("denies list without user", async () => {
|
|
433
|
+
const result = await handleList(entry, {});
|
|
434
|
+
const parsed = parseResult(result);
|
|
435
|
+
|
|
436
|
+
expect(parsed.error).toBeDefined();
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
describe("query operators", () => {
|
|
440
|
+
it("supports $in on an allowed field", async () => {
|
|
441
|
+
const result = await handleList(
|
|
442
|
+
entry,
|
|
443
|
+
{title: {$in: ["Todo 1", "Todo 2"]}},
|
|
444
|
+
asUser(normalUser)
|
|
445
|
+
);
|
|
446
|
+
const parsed = parseResult(result);
|
|
447
|
+
|
|
448
|
+
expect(parsed.data).toHaveLength(2);
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it("supports $ne on an allowed field", async () => {
|
|
452
|
+
const result = await handleList(entry, {completed: {$ne: true}}, asUser(normalUser));
|
|
453
|
+
const parsed = parseResult(result);
|
|
454
|
+
|
|
455
|
+
expect(parsed.data).toHaveLength(1);
|
|
456
|
+
expect(parsed.data[0].title).toBe("Todo 1");
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
it("supports $or across allowed fields", async () => {
|
|
460
|
+
const result = await handleList(
|
|
461
|
+
entry,
|
|
462
|
+
{$or: [{title: "Todo 1"}, {completed: true}]},
|
|
463
|
+
asUser(normalUser)
|
|
464
|
+
);
|
|
465
|
+
const parsed = parseResult(result);
|
|
466
|
+
|
|
467
|
+
expect(parsed.data).toHaveLength(2);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("still applies the owner query filter alongside $or", async () => {
|
|
471
|
+
const result = await handleList(entry, {$or: [{title: "Todo 3"}]}, asUser(normalUser));
|
|
472
|
+
const parsed = parseResult(result);
|
|
473
|
+
|
|
474
|
+
// Todo 3 belongs to otherUser, so the owner filter excludes it
|
|
475
|
+
expect(parsed.data).toHaveLength(0);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
it("rejects operators that can execute code", async () => {
|
|
479
|
+
const result = await handleList(
|
|
480
|
+
entry,
|
|
481
|
+
{title: {$where: "return true"}},
|
|
482
|
+
asUser(normalUser)
|
|
483
|
+
);
|
|
484
|
+
const parsed = parseResult(result);
|
|
485
|
+
|
|
486
|
+
expect(parsed.error).toContain("$where");
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it("rejects a $or branch referencing a field outside queryFields", async () => {
|
|
490
|
+
const result = await handleList(entry, {$or: [{metadata: "leak"}]}, asUser(normalUser));
|
|
491
|
+
const parsed = parseResult(result);
|
|
492
|
+
|
|
493
|
+
expect(parsed.error).toContain("metadata");
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
it("rejects $and with a non-array value", async () => {
|
|
497
|
+
const result = await handleList(entry, {$and: {title: "Todo 1"}}, asUser(normalUser));
|
|
498
|
+
const parsed = parseResult(result);
|
|
499
|
+
|
|
500
|
+
expect(parsed.error).toContain("$and");
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
it("ignores filters on fields outside queryFields", async () => {
|
|
504
|
+
const result = await handleList(entry, {metadata: "ignored"}, asUser(normalUser));
|
|
505
|
+
const parsed = parseResult(result);
|
|
506
|
+
|
|
507
|
+
expect(parsed.data).toHaveLength(2);
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
describe("handleRead", () => {
|
|
513
|
+
it("reads a document by ID", async () => {
|
|
514
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Read me"});
|
|
515
|
+
const result = await handleRead(entry, {id: doc._id.toString()}, asUser(normalUser));
|
|
516
|
+
const parsed = parseResult(result);
|
|
517
|
+
|
|
518
|
+
expect(parsed.data.title).toBe("Read me");
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it("returns not found for invalid ID", async () => {
|
|
522
|
+
const fakeId = new mongoose.Types.ObjectId().toString();
|
|
523
|
+
const result = await handleRead(entry, {id: fakeId}, asUser(normalUser));
|
|
524
|
+
const parsed = parseResult(result);
|
|
525
|
+
|
|
526
|
+
expect(parsed.error).toBeDefined();
|
|
527
|
+
expect(parsed.error).toContain("not found");
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
it("returns not found for a non-ObjectId id without throwing", async () => {
|
|
531
|
+
const result = await handleRead(entry, {id: "not-an-object-id"}, asUser(normalUser));
|
|
532
|
+
const parsed = parseResult(result);
|
|
533
|
+
|
|
534
|
+
expect(result.isError).toBe(true);
|
|
535
|
+
expect(parsed.error).toContain("not found");
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
it("reads a document by uppercase hex ObjectId", async () => {
|
|
539
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Upper id"});
|
|
540
|
+
const result = await handleRead(
|
|
541
|
+
entry,
|
|
542
|
+
{id: doc._id.toString().toUpperCase()},
|
|
543
|
+
asUser(normalUser)
|
|
544
|
+
);
|
|
545
|
+
const parsed = parseResult(result);
|
|
546
|
+
|
|
547
|
+
expect(parsed.data.title).toBe("Upper id");
|
|
548
|
+
});
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
describe("handleUpdate", () => {
|
|
552
|
+
it("updates a document", async () => {
|
|
553
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Update me"});
|
|
554
|
+
const result = await handleUpdate(
|
|
555
|
+
entry,
|
|
556
|
+
{id: doc._id.toString(), title: "Updated"},
|
|
557
|
+
asUser(normalUser)
|
|
558
|
+
);
|
|
559
|
+
const parsed = parseResult(result);
|
|
560
|
+
|
|
561
|
+
expect(parsed.data.title).toBe("Updated");
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
it("denies update by non-owner", async () => {
|
|
565
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Not yours"});
|
|
566
|
+
const result = await handleUpdate(
|
|
567
|
+
entry,
|
|
568
|
+
{id: doc._id.toString(), title: "Hijacked"},
|
|
569
|
+
asUser(otherUser)
|
|
570
|
+
);
|
|
571
|
+
const parsed = parseResult(result);
|
|
572
|
+
|
|
573
|
+
expect(parsed.error).toBeDefined();
|
|
574
|
+
expect(parsed.error).toContain("Permission denied");
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
it("admin can update any document", async () => {
|
|
578
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Admin update"});
|
|
579
|
+
const result = await handleUpdate(
|
|
580
|
+
entry,
|
|
581
|
+
{id: doc._id.toString(), title: "Admin updated"},
|
|
582
|
+
asUser(adminUser)
|
|
583
|
+
);
|
|
584
|
+
const parsed = parseResult(result);
|
|
585
|
+
|
|
586
|
+
expect(parsed.data.title).toBe("Admin updated");
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
it("returns not found for a non-ObjectId id without throwing", async () => {
|
|
590
|
+
const result = await handleUpdate(
|
|
591
|
+
entry,
|
|
592
|
+
{id: "not-an-object-id", title: "Nope"},
|
|
593
|
+
asUser(normalUser)
|
|
594
|
+
);
|
|
595
|
+
const parsed = parseResult(result);
|
|
596
|
+
|
|
597
|
+
expect(result.isError).toBe(true);
|
|
598
|
+
expect(parsed.error).toContain("not found");
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
it("strips REST excludeFromUpdate fields before persist", async () => {
|
|
602
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Keep owner"});
|
|
603
|
+
const writeEntry: MCPRegistryEntry = {
|
|
604
|
+
...entry,
|
|
605
|
+
options: {
|
|
606
|
+
...entry.options,
|
|
607
|
+
validation: {excludeFromUpdate: ["ownerId"]},
|
|
608
|
+
},
|
|
609
|
+
};
|
|
610
|
+
const result = await handleUpdate(
|
|
611
|
+
writeEntry,
|
|
612
|
+
{id: doc._id.toString(), ownerId: otherUser._id.toString(), title: "Still mine"},
|
|
613
|
+
asUser(normalUser)
|
|
614
|
+
);
|
|
615
|
+
const parsed = parseResult(result);
|
|
616
|
+
|
|
617
|
+
expect(parsed.data.title).toBe("Still mine");
|
|
618
|
+
expect(String(parsed.data.ownerId)).toBe(normalUser.id);
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
it("does not restore excludeFromUpdate fields when preUpdate spreads req.body", async () => {
|
|
622
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Keep owner"});
|
|
623
|
+
const writeEntry: MCPRegistryEntry = {
|
|
624
|
+
...entry,
|
|
625
|
+
options: {
|
|
626
|
+
...entry.options,
|
|
627
|
+
preUpdate: (_body, req: express.Request) => ({...req.body}),
|
|
628
|
+
validation: {excludeFromUpdate: ["ownerId"]},
|
|
629
|
+
},
|
|
630
|
+
};
|
|
631
|
+
const result = await handleUpdate(
|
|
632
|
+
writeEntry,
|
|
633
|
+
{id: doc._id.toString(), ownerId: otherUser._id.toString(), title: "Still mine"},
|
|
634
|
+
asUser(normalUser)
|
|
635
|
+
);
|
|
636
|
+
const parsed = parseResult(result);
|
|
637
|
+
|
|
638
|
+
expect(parsed.data.title).toBe("Still mine");
|
|
639
|
+
expect(String(parsed.data.ownerId)).toBe(normalUser.id);
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
it("rejects update fields outside the RBAC write mask", async () => {
|
|
643
|
+
await setupDb();
|
|
644
|
+
const access = createAccess({
|
|
645
|
+
connection: mongoose.connection,
|
|
646
|
+
defaultRoles: [
|
|
647
|
+
{
|
|
648
|
+
displayName: "Writer",
|
|
649
|
+
name: "mcp-updater",
|
|
650
|
+
permissions: {todo: ["create", "read", "update", "list"]},
|
|
651
|
+
},
|
|
652
|
+
],
|
|
653
|
+
fieldViews: {
|
|
654
|
+
todo: {
|
|
655
|
+
select: async () => "public",
|
|
656
|
+
views: {
|
|
657
|
+
public: {omit: [], read: "*", write: ["title"]},
|
|
658
|
+
},
|
|
659
|
+
},
|
|
660
|
+
},
|
|
661
|
+
statements: {
|
|
662
|
+
...terrenoStatements,
|
|
663
|
+
todo: ["create", "read", "update", "delete", "list"],
|
|
664
|
+
},
|
|
665
|
+
});
|
|
666
|
+
await access.roles.seedDefaults();
|
|
667
|
+
const writer = {...normalUser, roles: ["mcp-updater"]};
|
|
668
|
+
const doc = await TodoModel.create({
|
|
669
|
+
completed: false,
|
|
670
|
+
ownerId: normalUser._id,
|
|
671
|
+
title: "Keep owner",
|
|
672
|
+
});
|
|
673
|
+
const result = await handleUpdate(
|
|
674
|
+
{
|
|
675
|
+
...entry,
|
|
676
|
+
options: {
|
|
677
|
+
...entry.options,
|
|
678
|
+
access: {resource: "todo"},
|
|
679
|
+
accessControl: access as never,
|
|
680
|
+
},
|
|
681
|
+
},
|
|
682
|
+
{completed: true, id: doc._id.toString(), title: "Still mine"},
|
|
683
|
+
asUser(writer)
|
|
684
|
+
);
|
|
685
|
+
const parsed = parseResult(result);
|
|
686
|
+
expect(parsed.error).toContain("Validation failed");
|
|
687
|
+
expect((await TodoModel.findById(doc._id))?.completed).toBe(false);
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
it("drops User roles on MCP update when accessControl is set", async () => {
|
|
691
|
+
const doc = await UserWriteModel.create({email: "mcp-roles-update@example.com", roles: []});
|
|
692
|
+
const writeEntry: MCPRegistryEntry = {
|
|
693
|
+
config: {methods: ["update"]},
|
|
694
|
+
model: UserWriteModel,
|
|
695
|
+
modelName: "User",
|
|
696
|
+
options: {
|
|
697
|
+
accessControl: {} as never,
|
|
698
|
+
permissions: {
|
|
699
|
+
create: [],
|
|
700
|
+
delete: [],
|
|
701
|
+
list: [],
|
|
702
|
+
read: [],
|
|
703
|
+
update: [Permissions.IsAuthenticated],
|
|
704
|
+
},
|
|
705
|
+
},
|
|
706
|
+
};
|
|
707
|
+
const result = await handleUpdate(
|
|
708
|
+
writeEntry,
|
|
709
|
+
{email: "mcp-roles-update@example.com", id: doc._id.toString(), roles: ["superadmin"]},
|
|
710
|
+
asUser(normalUser)
|
|
711
|
+
);
|
|
712
|
+
const parsed = parseResult(result);
|
|
713
|
+
expect(parsed.data.email).toBe("mcp-roles-update@example.com");
|
|
714
|
+
expect(parsed.data.roles ?? []).toEqual([]);
|
|
715
|
+
expect((await UserWriteModel.findById(doc._id).lean())?.roles ?? []).toEqual([]);
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
it("strips MCP excludeFields from the update persist payload", async () => {
|
|
719
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Keep owner"});
|
|
720
|
+
const writeEntry: MCPRegistryEntry = {
|
|
721
|
+
...entry,
|
|
722
|
+
config: {...entry.config, excludeFields: ["ownerId"]},
|
|
723
|
+
};
|
|
724
|
+
const result = await handleUpdate(
|
|
725
|
+
writeEntry,
|
|
726
|
+
{id: doc._id.toString(), ownerId: otherUser._id.toString(), title: "Still mine"},
|
|
727
|
+
asUser(normalUser)
|
|
728
|
+
);
|
|
729
|
+
const parsed = parseResult(result);
|
|
730
|
+
|
|
731
|
+
expect(parsed.data.title).toBe("Still mine");
|
|
732
|
+
expect(parsed.data.ownerId).toBeUndefined();
|
|
733
|
+
const stored = await TodoModel.findById(doc._id).lean();
|
|
734
|
+
expect(String(stored?.ownerId)).toBe(normalUser.id);
|
|
735
|
+
});
|
|
736
|
+
|
|
737
|
+
it("strips nested excludeFields from the update persist payload", async () => {
|
|
738
|
+
const doc = await TodoModel.create({
|
|
739
|
+
metadata: {items: [{name: "one", token: "keep-me"}]},
|
|
740
|
+
ownerId: normalUser._id,
|
|
741
|
+
title: "Nested update",
|
|
742
|
+
});
|
|
743
|
+
const writeEntry: MCPRegistryEntry = {
|
|
744
|
+
...entry,
|
|
745
|
+
config: {...entry.config, excludeFields: ["metadata.items.token", "secret"]},
|
|
746
|
+
};
|
|
747
|
+
const result = await handleUpdate(
|
|
748
|
+
writeEntry,
|
|
749
|
+
{
|
|
750
|
+
id: doc._id.toString(),
|
|
751
|
+
metadata: {items: [{name: "one", token: "stolen"}], secret: "nope", visible: "ok"},
|
|
752
|
+
title: "Nested updated",
|
|
753
|
+
},
|
|
754
|
+
asUser(normalUser)
|
|
755
|
+
);
|
|
756
|
+
const parsed = parseResult(result);
|
|
757
|
+
|
|
758
|
+
expect(parsed.data.title).toBe("Nested updated");
|
|
759
|
+
const stored = await TodoModel.findById(doc._id).lean();
|
|
760
|
+
expect(stored?.metadata).toEqual({items: [{name: "one"}], visible: "ok"});
|
|
761
|
+
});
|
|
762
|
+
|
|
763
|
+
it("strips literal dotted keys that match excludeFields from update", async () => {
|
|
764
|
+
const doc = await TodoModel.create({
|
|
765
|
+
ownerId: normalUser._id,
|
|
766
|
+
title: "Keep nested secret",
|
|
767
|
+
});
|
|
768
|
+
const writeEntry: MCPRegistryEntry = {
|
|
769
|
+
...entry,
|
|
770
|
+
config: {...entry.config, excludeFields: ["metadata.nested.token"]},
|
|
771
|
+
};
|
|
772
|
+
const result = await handleUpdate(
|
|
773
|
+
writeEntry,
|
|
774
|
+
{
|
|
775
|
+
id: doc._id.toString(),
|
|
776
|
+
"metadata.nested.token": "stolen",
|
|
777
|
+
title: "Literal dotted update",
|
|
778
|
+
},
|
|
779
|
+
asUser(normalUser)
|
|
780
|
+
);
|
|
781
|
+
const parsed = parseResult(result);
|
|
782
|
+
|
|
783
|
+
expect(result.isError).not.toBe(true);
|
|
784
|
+
expect(parsed.data.title).toBe("Literal dotted update");
|
|
785
|
+
const stored = await TodoModel.findById(doc._id).lean();
|
|
786
|
+
expect(stored?.metadata).toBeUndefined();
|
|
787
|
+
expect((stored as Record<string, unknown> | null)?.["metadata.nested.token"]).toBeUndefined();
|
|
788
|
+
});
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
describe("handleDelete", () => {
|
|
792
|
+
it("deletes a document", async () => {
|
|
793
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Delete me"});
|
|
794
|
+
const result = await handleDelete(entry, {id: doc._id.toString()}, asUser(normalUser));
|
|
795
|
+
const parsed = parseResult(result);
|
|
796
|
+
|
|
797
|
+
expect(parsed.success).toBe(true);
|
|
798
|
+
|
|
799
|
+
// Verify deleted
|
|
800
|
+
const found = await TodoModel.findById(doc._id);
|
|
801
|
+
expect(found).toBeNull();
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
it("returns not found for a non-ObjectId id without throwing", async () => {
|
|
805
|
+
const result = await handleDelete(entry, {id: "not-an-object-id"}, asUser(normalUser));
|
|
806
|
+
const parsed = parseResult(result);
|
|
807
|
+
|
|
808
|
+
expect(result.isError).toBe(true);
|
|
809
|
+
expect(parsed.error).toContain("not found");
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
it("denies delete by non-owner", async () => {
|
|
813
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Protected"});
|
|
814
|
+
const result = await handleDelete(entry, {id: doc._id.toString()}, asUser(otherUser));
|
|
815
|
+
const parsed = parseResult(result);
|
|
816
|
+
|
|
817
|
+
expect(parsed.error).toBeDefined();
|
|
818
|
+
});
|
|
819
|
+
|
|
820
|
+
it("populates the document before the object-level permission check", async () => {
|
|
821
|
+
const owner = await OwnerModel.create({email: "owner@example.com", tier: "pro"});
|
|
822
|
+
const doc = await TodoModel.create({ownerId: owner._id, title: "Populated delete"});
|
|
823
|
+
|
|
824
|
+
let seenTier: unknown;
|
|
825
|
+
const entryWithPopulate: MCPRegistryEntry = {
|
|
826
|
+
...entry,
|
|
827
|
+
options: {
|
|
828
|
+
...entry.options,
|
|
829
|
+
permissions: {
|
|
830
|
+
...entry.options.permissions,
|
|
831
|
+
// Inspect a populated ref, which only works if handleDelete populated first
|
|
832
|
+
delete: [
|
|
833
|
+
(_method, _user, obj) => {
|
|
834
|
+
if (!obj) {
|
|
835
|
+
return true;
|
|
836
|
+
}
|
|
837
|
+
seenTier = (obj as {ownerId?: {tier?: string}}).ownerId?.tier;
|
|
838
|
+
return seenTier === "pro";
|
|
839
|
+
},
|
|
840
|
+
],
|
|
841
|
+
},
|
|
842
|
+
populatePaths: [{path: "ownerId"}],
|
|
843
|
+
},
|
|
844
|
+
};
|
|
845
|
+
|
|
846
|
+
const result = await handleDelete(
|
|
847
|
+
entryWithPopulate,
|
|
848
|
+
{id: doc._id.toString()},
|
|
849
|
+
asUser(normalUser)
|
|
850
|
+
);
|
|
851
|
+
const parsed = parseResult(result);
|
|
852
|
+
|
|
853
|
+
expect(seenTier).toBe("pro");
|
|
854
|
+
expect(parsed.success).toBe(true);
|
|
855
|
+
});
|
|
856
|
+
});
|
|
857
|
+
|
|
858
|
+
describe("anonymous access", () => {
|
|
859
|
+
/** Read-only helpers pass for list/read with no user, so only allowAnonymous gates them. */
|
|
860
|
+
const readOnlyEntry = (allowAnonymous?: boolean): MCPRegistryEntry => ({
|
|
861
|
+
...entry,
|
|
862
|
+
options: {
|
|
863
|
+
...entry.options,
|
|
864
|
+
allowAnonymous,
|
|
865
|
+
permissions: {
|
|
866
|
+
...entry.options.permissions,
|
|
867
|
+
list: [Permissions.IsAuthenticatedOrReadOnly],
|
|
868
|
+
read: [Permissions.IsAuthenticatedOrReadOnly],
|
|
869
|
+
},
|
|
870
|
+
queryFilter: undefined,
|
|
871
|
+
},
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
beforeEach(async () => {
|
|
875
|
+
await TodoModel.create({ownerId: normalUser._id, title: "Not for anonymous"});
|
|
876
|
+
});
|
|
877
|
+
|
|
878
|
+
it("refuses an anonymous list when allowAnonymous is not set", async () => {
|
|
879
|
+
const parsed = parseResult(await handleList(readOnlyEntry(), {}));
|
|
880
|
+
|
|
881
|
+
expect(parsed.error).toContain("authentication required");
|
|
882
|
+
});
|
|
883
|
+
|
|
884
|
+
it("refuses an anonymous read when allowAnonymous is not set", async () => {
|
|
885
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Hidden"});
|
|
886
|
+
|
|
887
|
+
const parsed = parseResult(await handleRead(readOnlyEntry(), {id: doc._id.toString()}));
|
|
888
|
+
|
|
889
|
+
expect(parsed.error).toContain("authentication required");
|
|
890
|
+
});
|
|
891
|
+
|
|
892
|
+
it("allows an anonymous list when the router opts in", async () => {
|
|
893
|
+
const parsed = parseResult(await handleList(readOnlyEntry(true), {}));
|
|
894
|
+
|
|
895
|
+
expect(parsed.error).toBeUndefined();
|
|
896
|
+
expect(parsed.data.length).toBeGreaterThan(0);
|
|
897
|
+
});
|
|
898
|
+
|
|
899
|
+
it("refuses anonymous writes even when allowAnonymous is set", async () => {
|
|
900
|
+
const anonymousWrites: MCPRegistryEntry = {
|
|
901
|
+
...readOnlyEntry(true),
|
|
902
|
+
options: {
|
|
903
|
+
...readOnlyEntry(true).options,
|
|
904
|
+
permissions: {
|
|
905
|
+
...readOnlyEntry(true).options.permissions,
|
|
906
|
+
create: [Permissions.IsAuthenticatedOrReadOnly],
|
|
907
|
+
},
|
|
908
|
+
},
|
|
909
|
+
};
|
|
910
|
+
|
|
911
|
+
const parsed = parseResult(await handleCreate(anonymousWrites, {title: "Anon"}));
|
|
912
|
+
|
|
913
|
+
expect(parsed.error).toContain("Permission denied");
|
|
914
|
+
});
|
|
915
|
+
});
|
|
916
|
+
|
|
917
|
+
describe("populate", () => {
|
|
918
|
+
const entryWithPopulate = (): MCPRegistryEntry => ({
|
|
919
|
+
...entry,
|
|
920
|
+
options: {...entry.options, populatePaths: [{fields: ["tier"], path: "ownerId"}]},
|
|
921
|
+
});
|
|
922
|
+
|
|
923
|
+
it("populates a declared path when requested", async () => {
|
|
924
|
+
const owner = await OwnerModel.create({email: "owner@example.com", tier: "pro"});
|
|
925
|
+
const doc = await TodoModel.create({ownerId: owner._id, title: "Populated"});
|
|
926
|
+
|
|
927
|
+
const parsed = parseResult(
|
|
928
|
+
await handleRead(
|
|
929
|
+
entryWithPopulate(),
|
|
930
|
+
{id: doc._id.toString(), populate: "ownerId"},
|
|
931
|
+
asUser(adminUser)
|
|
932
|
+
)
|
|
933
|
+
);
|
|
934
|
+
|
|
935
|
+
expect(parsed.data.ownerId.tier).toBe("pro");
|
|
936
|
+
// populatePaths declares fields: ["tier"], so email must not come along
|
|
937
|
+
expect(parsed.data.ownerId.email).toBeUndefined();
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
it("rejects a populate path the model router did not declare", async () => {
|
|
941
|
+
const owner = await OwnerModel.create({email: "secret@example.com", tier: "pro"});
|
|
942
|
+
const doc = await TodoModel.create({ownerId: owner._id, title: "Undeclared"});
|
|
943
|
+
|
|
944
|
+
const parsed = parseResult(
|
|
945
|
+
await handleRead(
|
|
946
|
+
entryWithPopulate(),
|
|
947
|
+
{id: doc._id.toString(), populate: "metadata"},
|
|
948
|
+
asUser(adminUser)
|
|
949
|
+
)
|
|
950
|
+
);
|
|
951
|
+
|
|
952
|
+
expect(parsed.error).toContain("metadata is not a populate-able path");
|
|
953
|
+
expect(parsed.error).toContain("ownerId");
|
|
954
|
+
});
|
|
955
|
+
|
|
956
|
+
it("rejects any populate path when the model declares none", async () => {
|
|
957
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "No paths"});
|
|
958
|
+
|
|
959
|
+
const parsed = parseResult(
|
|
960
|
+
await handleRead(entry, {id: doc._id.toString(), populate: "ownerId"}, asUser(normalUser))
|
|
961
|
+
);
|
|
962
|
+
|
|
963
|
+
expect(parsed.error).toContain("no populate-able paths");
|
|
964
|
+
});
|
|
965
|
+
|
|
966
|
+
it("rejects an undeclared populate path on list", async () => {
|
|
967
|
+
const parsed = parseResult(
|
|
968
|
+
await handleList(entryWithPopulate(), {populate: "metadata"}, asUser(normalUser))
|
|
969
|
+
);
|
|
970
|
+
|
|
971
|
+
expect(parsed.error).toContain("not a populate-able path");
|
|
972
|
+
});
|
|
973
|
+
|
|
974
|
+
it("falls back to the declared paths when populate is omitted", async () => {
|
|
975
|
+
const owner = await OwnerModel.create({email: "default@example.com", tier: "basic"});
|
|
976
|
+
const doc = await TodoModel.create({ownerId: owner._id, title: "Default populate"});
|
|
977
|
+
|
|
978
|
+
const parsed = parseResult(
|
|
979
|
+
await handleRead(entryWithPopulate(), {id: doc._id.toString()}, asUser(adminUser))
|
|
980
|
+
);
|
|
981
|
+
|
|
982
|
+
expect(parsed.data.ownerId.tier).toBe("basic");
|
|
983
|
+
});
|
|
984
|
+
|
|
985
|
+
it("omits the populate parameter from tools when nothing is populate-able", () => {
|
|
986
|
+
const tools = generateAllTools([entry]);
|
|
987
|
+
const readTool = tools.find((t) => t.name === "mcptodos_read");
|
|
988
|
+
|
|
989
|
+
expect(readTool?.inputSchema.properties?.populate).toBeUndefined();
|
|
990
|
+
});
|
|
991
|
+
|
|
992
|
+
it("advertises the declared populate paths on tools that support them", () => {
|
|
993
|
+
const tools = generateAllTools([entryWithPopulate()]);
|
|
994
|
+
const readTool = tools.find((t) => t.name === "mcptodos_read");
|
|
995
|
+
const populateParam = readTool?.inputSchema.properties?.populate as
|
|
996
|
+
| {description?: string}
|
|
997
|
+
| undefined;
|
|
998
|
+
|
|
999
|
+
expect(populateParam?.description).toContain("ownerId");
|
|
1000
|
+
});
|
|
1001
|
+
});
|
|
1002
|
+
|
|
1003
|
+
describe("lifecycle hook failures", () => {
|
|
1004
|
+
it("reports a preCreate hook that returns null", async () => {
|
|
1005
|
+
const blocked: MCPRegistryEntry = {
|
|
1006
|
+
...entry,
|
|
1007
|
+
options: {...entry.options, preCreate: () => null},
|
|
1008
|
+
};
|
|
1009
|
+
|
|
1010
|
+
const parsed = parseResult(await handleCreate(blocked, {title: "No"}, asUser(normalUser)));
|
|
1011
|
+
|
|
1012
|
+
expect(parsed.error).toContain("Create not allowed");
|
|
1013
|
+
});
|
|
1014
|
+
|
|
1015
|
+
it("reports a preCreate hook that throws", async () => {
|
|
1016
|
+
const failing: MCPRegistryEntry = {
|
|
1017
|
+
...entry,
|
|
1018
|
+
options: {
|
|
1019
|
+
...entry.options,
|
|
1020
|
+
preCreate: () => {
|
|
1021
|
+
throw new Error("boom");
|
|
1022
|
+
},
|
|
1023
|
+
},
|
|
1024
|
+
};
|
|
1025
|
+
|
|
1026
|
+
const parsed = parseResult(await handleCreate(failing, {title: "No"}, asUser(normalUser)));
|
|
1027
|
+
|
|
1028
|
+
expect(parsed.error).toContain("preCreate hook failed: boom");
|
|
1029
|
+
});
|
|
1030
|
+
|
|
1031
|
+
it("preserves APIError titles thrown from preCreate", async () => {
|
|
1032
|
+
const failing: MCPRegistryEntry = {
|
|
1033
|
+
...entry,
|
|
1034
|
+
options: {
|
|
1035
|
+
...entry.options,
|
|
1036
|
+
preCreate: () => {
|
|
1037
|
+
throw new APIError({status: 400, title: "Title already taken"});
|
|
1038
|
+
},
|
|
1039
|
+
},
|
|
1040
|
+
};
|
|
1041
|
+
|
|
1042
|
+
const result = await handleCreate(failing, {title: "No"}, asUser(normalUser));
|
|
1043
|
+
const parsed = parseResult(result);
|
|
1044
|
+
|
|
1045
|
+
expect(result.isError).toBe(true);
|
|
1046
|
+
expect(parsed.error).toBe("Title already taken");
|
|
1047
|
+
});
|
|
1048
|
+
|
|
1049
|
+
it("reports a postCreate hook that throws", async () => {
|
|
1050
|
+
const failing: MCPRegistryEntry = {
|
|
1051
|
+
...entry,
|
|
1052
|
+
options: {
|
|
1053
|
+
...entry.options,
|
|
1054
|
+
postCreate: () => {
|
|
1055
|
+
throw new Error("after");
|
|
1056
|
+
},
|
|
1057
|
+
},
|
|
1058
|
+
};
|
|
1059
|
+
|
|
1060
|
+
const parsed = parseResult(await handleCreate(failing, {title: "Yes"}, asUser(normalUser)));
|
|
1061
|
+
|
|
1062
|
+
expect(parsed.error).toContain("postCreate hook failed: after");
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
it("reports a preUpdate hook that returns null", async () => {
|
|
1066
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Blocked update"});
|
|
1067
|
+
const blocked: MCPRegistryEntry = {
|
|
1068
|
+
...entry,
|
|
1069
|
+
options: {...entry.options, preUpdate: () => null},
|
|
1070
|
+
};
|
|
1071
|
+
|
|
1072
|
+
const parsed = parseResult(
|
|
1073
|
+
await handleUpdate(blocked, {id: doc._id.toString(), title: "No"}, asUser(normalUser))
|
|
1074
|
+
);
|
|
1075
|
+
|
|
1076
|
+
expect(parsed.error).toContain("Update not allowed");
|
|
1077
|
+
});
|
|
1078
|
+
|
|
1079
|
+
it("reports a postUpdate hook that throws", async () => {
|
|
1080
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Post update"});
|
|
1081
|
+
const failing: MCPRegistryEntry = {
|
|
1082
|
+
...entry,
|
|
1083
|
+
options: {
|
|
1084
|
+
...entry.options,
|
|
1085
|
+
postUpdate: () => {
|
|
1086
|
+
throw new Error("late");
|
|
1087
|
+
},
|
|
1088
|
+
},
|
|
1089
|
+
};
|
|
1090
|
+
|
|
1091
|
+
const parsed = parseResult(
|
|
1092
|
+
await handleUpdate(failing, {id: doc._id.toString(), title: "Changed"}, asUser(normalUser))
|
|
1093
|
+
);
|
|
1094
|
+
|
|
1095
|
+
expect(parsed.error).toContain("postUpdate hook failed: late");
|
|
1096
|
+
});
|
|
1097
|
+
|
|
1098
|
+
it("reports a preDelete hook that returns null", async () => {
|
|
1099
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Blocked delete"});
|
|
1100
|
+
const blocked: MCPRegistryEntry = {
|
|
1101
|
+
...entry,
|
|
1102
|
+
options: {...entry.options, preDelete: () => null},
|
|
1103
|
+
};
|
|
1104
|
+
|
|
1105
|
+
const parsed = parseResult(
|
|
1106
|
+
await handleDelete(blocked, {id: doc._id.toString()}, asUser(normalUser))
|
|
1107
|
+
);
|
|
1108
|
+
|
|
1109
|
+
expect(parsed.error).toContain("Delete not allowed");
|
|
1110
|
+
});
|
|
1111
|
+
|
|
1112
|
+
it("reports a postDelete hook that throws", async () => {
|
|
1113
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Post delete"});
|
|
1114
|
+
const failing: MCPRegistryEntry = {
|
|
1115
|
+
...entry,
|
|
1116
|
+
options: {
|
|
1117
|
+
...entry.options,
|
|
1118
|
+
postDelete: () => {
|
|
1119
|
+
throw new Error("cascade");
|
|
1120
|
+
},
|
|
1121
|
+
},
|
|
1122
|
+
};
|
|
1123
|
+
|
|
1124
|
+
const parsed = parseResult(
|
|
1125
|
+
await handleDelete(failing, {id: doc._id.toString()}, asUser(normalUser))
|
|
1126
|
+
);
|
|
1127
|
+
|
|
1128
|
+
expect(parsed.error).toContain("postDelete hook failed: cascade");
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
it("passes the update fields as the MCP request body, without the id", async () => {
|
|
1132
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Body check"});
|
|
1133
|
+
let seenBody: unknown;
|
|
1134
|
+
const withHook: MCPRegistryEntry = {
|
|
1135
|
+
...entry,
|
|
1136
|
+
options: {
|
|
1137
|
+
...entry.options,
|
|
1138
|
+
preUpdate: (body, req: express.Request) => {
|
|
1139
|
+
seenBody = (req as unknown as MCPRequest).body;
|
|
1140
|
+
return body as Record<string, unknown>;
|
|
1141
|
+
},
|
|
1142
|
+
},
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1145
|
+
await handleUpdate(
|
|
1146
|
+
withHook,
|
|
1147
|
+
{id: doc._id.toString(), title: "Updated body"},
|
|
1148
|
+
asUser(normalUser)
|
|
1149
|
+
);
|
|
1150
|
+
|
|
1151
|
+
expect(seenBody).toEqual({title: "Updated body"});
|
|
1152
|
+
});
|
|
1153
|
+
});
|
|
1154
|
+
|
|
1155
|
+
describe("excludeFields", () => {
|
|
1156
|
+
it("strips excluded fields from responses", async () => {
|
|
1157
|
+
const entryWithExcludes: MCPRegistryEntry = {
|
|
1158
|
+
...entry,
|
|
1159
|
+
config: {
|
|
1160
|
+
...entry.config,
|
|
1161
|
+
excludeFields: ["ownerId"],
|
|
1162
|
+
},
|
|
1163
|
+
};
|
|
1164
|
+
|
|
1165
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Stripped"});
|
|
1166
|
+
const result = await handleRead(
|
|
1167
|
+
entryWithExcludes,
|
|
1168
|
+
{id: doc._id.toString()},
|
|
1169
|
+
asUser(normalUser)
|
|
1170
|
+
);
|
|
1171
|
+
const parsed = parseResult(result);
|
|
1172
|
+
|
|
1173
|
+
expect(parsed.data.title).toBe("Stripped");
|
|
1174
|
+
expect(parsed.data.ownerId).toBeUndefined();
|
|
1175
|
+
});
|
|
1176
|
+
|
|
1177
|
+
it("keeps ObjectIds and Dates JSON-serializable while stripping", async () => {
|
|
1178
|
+
const entryWithExcludes: MCPRegistryEntry = {
|
|
1179
|
+
...entry,
|
|
1180
|
+
config: {...entry.config, excludeFields: ["ownerId"]},
|
|
1181
|
+
};
|
|
1182
|
+
|
|
1183
|
+
const doc = await TodoModel.create({
|
|
1184
|
+
dueDate: new Date("2026-01-02T03:04:05.000Z"),
|
|
1185
|
+
ownerId: normalUser._id,
|
|
1186
|
+
title: "Serialization",
|
|
1187
|
+
});
|
|
1188
|
+
const result = await handleRead(
|
|
1189
|
+
entryWithExcludes,
|
|
1190
|
+
{id: doc._id.toString()},
|
|
1191
|
+
asUser(normalUser)
|
|
1192
|
+
);
|
|
1193
|
+
const parsed = parseResult(result);
|
|
1194
|
+
|
|
1195
|
+
expect(parsed.data._id).toBe(doc._id.toString());
|
|
1196
|
+
expect(parsed.data.dueDate).toBe("2026-01-02T03:04:05.000Z");
|
|
1197
|
+
});
|
|
1198
|
+
|
|
1199
|
+
it("strips a bare field name nested inside an object", async () => {
|
|
1200
|
+
const entryWithExcludes: MCPRegistryEntry = {
|
|
1201
|
+
...entry,
|
|
1202
|
+
config: {...entry.config, excludeFields: ["secret"]},
|
|
1203
|
+
};
|
|
1204
|
+
|
|
1205
|
+
const doc = await TodoModel.create({
|
|
1206
|
+
metadata: {nested: {secret: "hidden", visible: "shown"}, secret: "hidden"},
|
|
1207
|
+
ownerId: normalUser._id,
|
|
1208
|
+
title: "Nested",
|
|
1209
|
+
});
|
|
1210
|
+
const result = await handleRead(
|
|
1211
|
+
entryWithExcludes,
|
|
1212
|
+
{id: doc._id.toString()},
|
|
1213
|
+
asUser(normalUser)
|
|
1214
|
+
);
|
|
1215
|
+
const parsed = parseResult(result);
|
|
1216
|
+
|
|
1217
|
+
expect(parsed.data.metadata.secret).toBeUndefined();
|
|
1218
|
+
expect(parsed.data.metadata.nested.secret).toBeUndefined();
|
|
1219
|
+
expect(parsed.data.metadata.nested.visible).toBe("shown");
|
|
1220
|
+
});
|
|
1221
|
+
|
|
1222
|
+
it("strips a dot path through an array of subdocuments", async () => {
|
|
1223
|
+
const entryWithExcludes: MCPRegistryEntry = {
|
|
1224
|
+
...entry,
|
|
1225
|
+
config: {...entry.config, excludeFields: ["metadata.items.token"]},
|
|
1226
|
+
};
|
|
1227
|
+
|
|
1228
|
+
const doc = await TodoModel.create({
|
|
1229
|
+
metadata: {
|
|
1230
|
+
items: [
|
|
1231
|
+
{name: "one", token: "t1"},
|
|
1232
|
+
{name: "two", token: "t2"},
|
|
1233
|
+
],
|
|
1234
|
+
},
|
|
1235
|
+
ownerId: normalUser._id,
|
|
1236
|
+
title: "Array paths",
|
|
1237
|
+
});
|
|
1238
|
+
const result = await handleRead(
|
|
1239
|
+
entryWithExcludes,
|
|
1240
|
+
{id: doc._id.toString()},
|
|
1241
|
+
asUser(normalUser)
|
|
1242
|
+
);
|
|
1243
|
+
const parsed = parseResult(result);
|
|
1244
|
+
|
|
1245
|
+
expect(parsed.data.metadata.items).toHaveLength(2);
|
|
1246
|
+
for (const item of parsed.data.metadata.items) {
|
|
1247
|
+
expect(item.token).toBeUndefined();
|
|
1248
|
+
expect(item.name).toBeDefined();
|
|
1249
|
+
}
|
|
1250
|
+
});
|
|
1251
|
+
|
|
1252
|
+
it("strips excluded fields from every item in a list response", async () => {
|
|
1253
|
+
const entryWithExcludes: MCPRegistryEntry = {
|
|
1254
|
+
...entry,
|
|
1255
|
+
config: {...entry.config, excludeFields: ["ownerId"]},
|
|
1256
|
+
};
|
|
1257
|
+
|
|
1258
|
+
await TodoModel.create([
|
|
1259
|
+
{ownerId: normalUser._id, title: "List 1"},
|
|
1260
|
+
{ownerId: normalUser._id, title: "List 2"},
|
|
1261
|
+
]);
|
|
1262
|
+
const result = await handleList(entryWithExcludes, {}, asUser(normalUser));
|
|
1263
|
+
const parsed = parseResult(result);
|
|
1264
|
+
|
|
1265
|
+
expect(parsed.data).toHaveLength(2);
|
|
1266
|
+
for (const item of parsed.data) {
|
|
1267
|
+
expect(item.ownerId).toBeUndefined();
|
|
1268
|
+
}
|
|
1269
|
+
});
|
|
1270
|
+
});
|
|
1271
|
+
|
|
1272
|
+
describe("lifecycle hook request", () => {
|
|
1273
|
+
it("passes an Express-shaped stub request to every lifecycle hook", async () => {
|
|
1274
|
+
const seen: Partial<Record<string, MCPRequest>> = {};
|
|
1275
|
+
const capture = (name: string, req: express.Request): void => {
|
|
1276
|
+
seen[name] = req as unknown as MCPRequest;
|
|
1277
|
+
};
|
|
1278
|
+
const entryWithHooks: MCPRegistryEntry = {
|
|
1279
|
+
...entry,
|
|
1280
|
+
options: {
|
|
1281
|
+
...entry.options,
|
|
1282
|
+
postCreate: (_data, req: express.Request) => {
|
|
1283
|
+
capture("postCreate", req);
|
|
1284
|
+
},
|
|
1285
|
+
postDelete: (req: express.Request) => {
|
|
1286
|
+
capture("postDelete", req);
|
|
1287
|
+
},
|
|
1288
|
+
postUpdate: (_doc, _body, req: express.Request) => {
|
|
1289
|
+
capture("postUpdate", req);
|
|
1290
|
+
},
|
|
1291
|
+
preCreate: (body, req: express.Request) => {
|
|
1292
|
+
capture("preCreate", req);
|
|
1293
|
+
return {
|
|
1294
|
+
...(body as Record<string, unknown>),
|
|
1295
|
+
ownerId: normalUser._id,
|
|
1296
|
+
};
|
|
1297
|
+
},
|
|
1298
|
+
preDelete: (doc, req: express.Request) => {
|
|
1299
|
+
capture("preDelete", req);
|
|
1300
|
+
return doc;
|
|
1301
|
+
},
|
|
1302
|
+
preUpdate: (body, req: express.Request) => {
|
|
1303
|
+
capture("preUpdate", req);
|
|
1304
|
+
return body;
|
|
1305
|
+
},
|
|
1306
|
+
},
|
|
1307
|
+
};
|
|
1308
|
+
|
|
1309
|
+
await handleCreate(entryWithHooks, {title: "Hooked"}, asUser(normalUser));
|
|
1310
|
+
expectStubRequest({
|
|
1311
|
+
body: {title: "Hooked"},
|
|
1312
|
+
request: seen.preCreate,
|
|
1313
|
+
user: normalUser,
|
|
1314
|
+
});
|
|
1315
|
+
expectStubRequest({
|
|
1316
|
+
body: {ownerId: normalUser._id, title: "Hooked"},
|
|
1317
|
+
request: seen.postCreate,
|
|
1318
|
+
user: normalUser,
|
|
1319
|
+
});
|
|
1320
|
+
|
|
1321
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Update hook"});
|
|
1322
|
+
await handleUpdate(
|
|
1323
|
+
entryWithHooks,
|
|
1324
|
+
{id: doc._id.toString(), title: "Updated"},
|
|
1325
|
+
asUser(normalUser)
|
|
1326
|
+
);
|
|
1327
|
+
expectStubRequest({
|
|
1328
|
+
body: {title: "Updated"},
|
|
1329
|
+
request: seen.preUpdate,
|
|
1330
|
+
user: normalUser,
|
|
1331
|
+
});
|
|
1332
|
+
expectStubRequest({
|
|
1333
|
+
body: {title: "Updated"},
|
|
1334
|
+
request: seen.postUpdate,
|
|
1335
|
+
user: normalUser,
|
|
1336
|
+
});
|
|
1337
|
+
|
|
1338
|
+
const toDelete = await TodoModel.create({ownerId: normalUser._id, title: "Delete hook"});
|
|
1339
|
+
const deleteArgs = {id: toDelete._id.toString()};
|
|
1340
|
+
await handleDelete(entryWithHooks, deleteArgs, asUser(normalUser));
|
|
1341
|
+
expectStubRequest({
|
|
1342
|
+
body: deleteArgs,
|
|
1343
|
+
request: seen.preDelete,
|
|
1344
|
+
user: normalUser,
|
|
1345
|
+
});
|
|
1346
|
+
expectStubRequest({
|
|
1347
|
+
body: deleteArgs,
|
|
1348
|
+
request: seen.postDelete,
|
|
1349
|
+
user: normalUser,
|
|
1350
|
+
});
|
|
1351
|
+
});
|
|
1352
|
+
});
|
|
1353
|
+
|
|
1354
|
+
describe("mcpResponseHandler", () => {
|
|
1355
|
+
it("uses custom response handler", async () => {
|
|
1356
|
+
const entryWithHandler: MCPRegistryEntry = {
|
|
1357
|
+
...entry,
|
|
1358
|
+
config: {
|
|
1359
|
+
...entry.config,
|
|
1360
|
+
mcpResponseHandler: async (value, method) => {
|
|
1361
|
+
if (Array.isArray(value)) {
|
|
1362
|
+
return value.map((v: {title?: string}) => ({method, summary: v.title ?? null}));
|
|
1363
|
+
}
|
|
1364
|
+
return {method, summary: (value as {title?: string}).title ?? null};
|
|
1365
|
+
},
|
|
1366
|
+
},
|
|
1367
|
+
};
|
|
1368
|
+
|
|
1369
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Custom"});
|
|
1370
|
+
const result = await handleRead(
|
|
1371
|
+
entryWithHandler,
|
|
1372
|
+
{id: doc._id.toString()},
|
|
1373
|
+
asUser(normalUser)
|
|
1374
|
+
);
|
|
1375
|
+
const parsed = parseResult(result);
|
|
1376
|
+
|
|
1377
|
+
expect(parsed.data.summary).toBe("Custom");
|
|
1378
|
+
expect(parsed.data.method).toBe("read");
|
|
1379
|
+
});
|
|
1380
|
+
|
|
1381
|
+
it("passes a stub Express-shaped request to REST responseHandler", async () => {
|
|
1382
|
+
let seenRequest: MCPRequest | undefined;
|
|
1383
|
+
const entryWithHandler: MCPRegistryEntry = {
|
|
1384
|
+
...entry,
|
|
1385
|
+
options: {
|
|
1386
|
+
...entry.options,
|
|
1387
|
+
responseHandler: async (value, method, req) => {
|
|
1388
|
+
seenRequest = req as unknown as MCPRequest;
|
|
1389
|
+
return {method, title: (value as {title?: string}).title ?? null};
|
|
1390
|
+
},
|
|
1391
|
+
},
|
|
1392
|
+
};
|
|
1393
|
+
|
|
1394
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Stubbed"});
|
|
1395
|
+
const result = await handleRead(
|
|
1396
|
+
entryWithHandler,
|
|
1397
|
+
{id: doc._id.toString()},
|
|
1398
|
+
asUser(normalUser)
|
|
1399
|
+
);
|
|
1400
|
+
const parsed = parseResult(result);
|
|
1401
|
+
|
|
1402
|
+
expect(parsed.data.title).toBe("Stubbed");
|
|
1403
|
+
expectStubRequest({
|
|
1404
|
+
body: {},
|
|
1405
|
+
request: seenRequest,
|
|
1406
|
+
user: normalUser,
|
|
1407
|
+
});
|
|
1408
|
+
});
|
|
1409
|
+
|
|
1410
|
+
it("returns a structured error when responseHandler throws", async () => {
|
|
1411
|
+
const entryWithHandler: MCPRegistryEntry = {
|
|
1412
|
+
...entry,
|
|
1413
|
+
options: {
|
|
1414
|
+
...entry.options,
|
|
1415
|
+
responseHandler: async () => {
|
|
1416
|
+
throw new Error("serializer exploded");
|
|
1417
|
+
},
|
|
1418
|
+
},
|
|
1419
|
+
};
|
|
1420
|
+
|
|
1421
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Boom"});
|
|
1422
|
+
const result = await handleRead(
|
|
1423
|
+
entryWithHandler,
|
|
1424
|
+
{id: doc._id.toString()},
|
|
1425
|
+
asUser(normalUser)
|
|
1426
|
+
);
|
|
1427
|
+
const parsed = parseResult(result);
|
|
1428
|
+
|
|
1429
|
+
expect(result.isError).toBe(true);
|
|
1430
|
+
expect(parsed.error).toContain("Response handler failed");
|
|
1431
|
+
expect(parsed.error).toContain("serializer exploded");
|
|
1432
|
+
});
|
|
1433
|
+
});
|
|
1434
|
+
|
|
1435
|
+
describe("registry and tool generation", () => {
|
|
1436
|
+
it("registerMCPModel adds to registry", () => {
|
|
1437
|
+
registerMCPModel(TodoModel, {methods: ["list"]}, entry.options);
|
|
1438
|
+
const tools = generateAllTools([entry]);
|
|
1439
|
+
|
|
1440
|
+
expect(tools.length).toBeGreaterThan(0);
|
|
1441
|
+
});
|
|
1442
|
+
|
|
1443
|
+
it("updateMCPRegistryOptions replaces permissions used by handlers", async () => {
|
|
1444
|
+
registerMCPModel(TodoModel, entry.config, entry.options);
|
|
1445
|
+
updateMCPRegistryOptions("MCPTodo", {
|
|
1446
|
+
...entry.options,
|
|
1447
|
+
permissions: {
|
|
1448
|
+
...entry.options.permissions,
|
|
1449
|
+
delete: [],
|
|
1450
|
+
},
|
|
1451
|
+
});
|
|
1452
|
+
|
|
1453
|
+
const doc = await TodoModel.create({ownerId: normalUser._id, title: "Locked"});
|
|
1454
|
+
const registered = getMCPRegistry()[0];
|
|
1455
|
+
expect(registered).toBeDefined();
|
|
1456
|
+
const result = await handleDelete(registered, {id: doc._id.toString()}, asUser(normalUser));
|
|
1457
|
+
const parsed = parseResult(result);
|
|
1458
|
+
|
|
1459
|
+
expect(parsed.error).toContain("cannot delete");
|
|
1460
|
+
expect(await TodoModel.findById(doc._id)).toBeTruthy();
|
|
1461
|
+
});
|
|
1462
|
+
|
|
1463
|
+
it("updateMCPRegistryOptions no-ops for an unknown model", () => {
|
|
1464
|
+
registerMCPModel(TodoModel, {methods: ["list"]}, entry.options);
|
|
1465
|
+
const original = getMCPRegistry()[0]?.options;
|
|
1466
|
+
updateMCPRegistryOptions("MissingModel", {
|
|
1467
|
+
...entry.options,
|
|
1468
|
+
permissions: {...entry.options.permissions, delete: []},
|
|
1469
|
+
});
|
|
1470
|
+
expect(getMCPRegistry()[0]?.options).toBe(original);
|
|
1471
|
+
});
|
|
1472
|
+
|
|
1473
|
+
it("generates correct tool names", () => {
|
|
1474
|
+
const tools = generateAllTools([entry]);
|
|
1475
|
+
const names = tools.map((t) => t.name);
|
|
1476
|
+
|
|
1477
|
+
expect(names).toContain("mcptodos_create");
|
|
1478
|
+
expect(names).toContain("mcptodos_list");
|
|
1479
|
+
expect(names).toContain("mcptodos_read");
|
|
1480
|
+
expect(names).toContain("mcptodos_update");
|
|
1481
|
+
expect(names).toContain("mcptodos_delete");
|
|
1482
|
+
});
|
|
1483
|
+
|
|
1484
|
+
it("advertises filter operators and $and/$or on the list tool", () => {
|
|
1485
|
+
const tools = generateAllTools([entry]);
|
|
1486
|
+
const listTool = tools.find((t) => t.name === "mcptodos_list");
|
|
1487
|
+
|
|
1488
|
+
expect(listTool?.inputSchema.properties?.$and).toBeDefined();
|
|
1489
|
+
expect(listTool?.inputSchema.properties?.$or).toBeDefined();
|
|
1490
|
+
expect(listTool?.description).toContain("$in");
|
|
1491
|
+
});
|
|
1492
|
+
});
|
|
1493
|
+
});
|