@terreno/api 57.2.0 → 57.3.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/README.md +17 -7
- package/dist/actions.js +19 -7
- package/dist/api.d.ts +4 -0
- package/dist/api.js +12 -24
- package/dist/auth.d.ts +23 -10
- package/dist/auth.js +97 -32
- package/dist/authRecovery.d.ts +5 -0
- package/dist/authRecovery.js +350 -0
- package/dist/authTokens.d.ts +8 -0
- package/dist/authTokens.js +162 -0
- package/dist/betterAuth.d.ts +33 -0
- package/dist/betterAuthApp.d.ts +4 -0
- package/dist/betterAuthApp.js +26 -8
- package/dist/betterAuthSetup.d.ts +44 -2
- package/dist/betterAuthSetup.js +288 -18
- package/dist/collectionRegistry.d.ts +38 -0
- package/dist/collectionRegistry.js +72 -0
- package/dist/configurationApp.d.ts +11 -2
- package/dist/configurationApp.js +2 -4
- package/dist/docLoader.js +35 -6
- package/dist/expressServer.d.ts +26 -3
- package/dist/expressServer.js +79 -11
- package/dist/index.d.ts +16 -0
- package/dist/index.js +38 -2
- package/dist/mcp/auth.d.ts +1 -0
- package/dist/mcp/auth.js +47 -25
- package/dist/mcp/handlers.d.ts +1 -1
- package/dist/mcp/handlers.js +67 -233
- package/dist/mcp/query.d.ts +2 -2
- package/dist/mcp/registry.d.ts +1 -5
- package/dist/mcp/registry.js +31 -20
- package/dist/mcp/schemaGenerator.d.ts +2 -7
- package/dist/mcp/schemaGenerator.js +41 -116
- package/dist/mcp/server.d.ts +1 -0
- package/dist/mcp/server.js +1 -0
- package/dist/mcp/serviceTokens.d.ts +16 -0
- package/dist/mcp/serviceTokens.js +345 -0
- package/dist/mcp/types.d.ts +2 -2
- package/dist/models/mcpServiceToken.d.ts +3 -0
- package/dist/models/mcpServiceToken.js +208 -0
- package/dist/openApiBuilder.d.ts +5 -4
- package/dist/openApiBuilder.js +61 -8
- package/dist/openApiCompat.js +1 -1
- package/dist/openApiValidator.js +0 -2
- package/dist/plugins.d.ts +14 -1
- package/dist/plugins.js +17 -6
- package/dist/populate.d.ts +2 -2
- package/dist/populate.js +47 -42
- package/dist/rateLimit/applyTrustProxy.d.ts +3 -0
- package/dist/rateLimit/applyTrustProxy.js +17 -0
- package/dist/rateLimit/createStore.d.ts +2 -0
- package/dist/rateLimit/createStore.js +28 -0
- package/dist/rateLimit/memoryStore.d.ts +2 -0
- package/dist/rateLimit/memoryStore.js +108 -0
- package/dist/rateLimit/middleware.d.ts +11 -0
- package/dist/rateLimit/middleware.js +110 -0
- package/dist/rateLimit/mongoStore.d.ts +3 -0
- package/dist/rateLimit/mongoStore.js +122 -0
- package/dist/rateLimit/policies.d.ts +14 -0
- package/dist/rateLimit/policies.js +151 -0
- package/dist/rateLimit/redisStore.d.ts +3 -0
- package/dist/rateLimit/redisStore.js +155 -0
- package/dist/rateLimit/types.d.ts +54 -0
- package/dist/rateLimit/types.js +9 -0
- package/dist/rbac/access.js +5 -1
- package/dist/rbac/assertAllowed.d.ts +9 -0
- package/dist/rbac/assertAllowed.js +182 -0
- package/dist/rbac/modelRouterAccess.d.ts +1 -0
- package/dist/rbac/modelRouterAccess.js +8 -4
- package/dist/rbac/permissionUtils.js +3 -2
- package/dist/rbac/resolve.js +5 -4
- package/dist/rbac/routes.js +15 -6
- package/dist/realtime/deprecation.d.ts +12 -0
- package/dist/realtime/deprecation.js +32 -0
- package/dist/realtime/realtimeApp.d.ts +1 -1
- package/dist/realtime/realtimeApp.js +10 -11
- package/dist/realtime/registry.d.ts +0 -31
- package/dist/realtime/registry.js +44 -31
- package/dist/realtime/socketAuth.d.ts +5 -5
- package/dist/realtime/socketAuth.js +11 -1
- package/dist/realtime/socketUser.d.ts +5 -1
- package/dist/realtime/types.d.ts +14 -4
- package/dist/schemaMetadata.d.ts +56 -0
- package/dist/schemaMetadata.js +513 -0
- package/dist/seedRunner.d.ts +88 -0
- package/dist/seedRunner.js +627 -0
- package/dist/sync/executors.d.ts +12 -2
- package/dist/sync/executors.js +8 -10
- package/dist/sync/loadHarness.js +28 -19
- package/dist/sync/models.js +26 -24
- package/dist/sync/mutationHandler.d.ts +9 -0
- package/dist/sync/mutationHandler.js +11 -6
- package/dist/sync/registrationSideEffects.d.ts +35 -0
- package/dist/sync/registrationSideEffects.js +151 -0
- package/dist/sync/registry.d.ts +2 -52
- package/dist/sync/registry.js +38 -187
- package/dist/sync/routes.js +3 -2
- package/dist/sync/scripts/compactTombstones.js +0 -2
- package/dist/sync/syncApp.d.ts +3 -3
- package/dist/sync/syncApp.js +4 -4
- package/dist/sync/syncBatch.js +3 -2
- package/dist/sync/syncSeqPlugin.d.ts +1 -1
- package/dist/sync/syncSeqPlugin.js +20 -16
- package/dist/terrenoApp.d.ts +19 -5
- package/dist/terrenoApp.js +66 -18
- package/dist/tests/models.d.ts +2 -0
- package/dist/tests/models.js +6 -0
- package/dist/types/authToken.d.ts +33 -0
- package/dist/types/authToken.js +2 -0
- package/dist/types/mcpServiceToken.d.ts +41 -0
- package/dist/types/mcpServiceToken.js +2 -0
- package/dist/webhooks/headerValue.d.ts +2 -0
- package/dist/webhooks/headerValue.js +14 -0
- package/dist/webhooks/idempotency/memoryStore.d.ts +10 -0
- package/dist/webhooks/idempotency/memoryStore.js +65 -0
- package/dist/webhooks/idempotency/mongoStore.d.ts +4 -0
- package/dist/webhooks/idempotency/mongoStore.js +150 -0
- package/dist/webhooks/idempotency/webhookReceipt.d.ts +62 -0
- package/dist/webhooks/idempotency/webhookReceipt.js +39 -0
- package/dist/webhooks/rawBody.d.ts +14 -0
- package/dist/webhooks/rawBody.js +22 -0
- package/dist/webhooks/timingSafeEqual.d.ts +4 -0
- package/dist/webhooks/timingSafeEqual.js +17 -0
- package/dist/webhooks/verifiers/hmac.d.ts +15 -0
- package/dist/webhooks/verifiers/hmac.js +49 -0
- package/dist/webhooks/verifiers/sendgrid.d.ts +10 -0
- package/dist/webhooks/verifiers/sendgrid.js +56 -0
- package/dist/webhooks/verifiers/stripe.d.ts +9 -0
- package/dist/webhooks/verifiers/stripe.js +99 -0
- package/dist/webhooks/verifiers/twilio.d.ts +9 -0
- package/dist/webhooks/verifiers/twilio.js +112 -0
- package/dist/webhooks/webhooksApp.d.ts +33 -0
- package/dist/webhooks/webhooksApp.js +195 -0
- package/package.json +4 -5
- package/src/__snapshots__/openApi.test.ts.snap +270 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +30 -12
- package/src/__snapshots__/schemaMetadata.test.ts.snap +197 -0
- package/src/actions.ts +20 -13
- package/src/adminModelRouter.test.ts +133 -0
- package/src/api.arrayOperations.test.ts +43 -31
- package/src/api.hooks.test.ts +38 -29
- package/src/api.query.test.ts +31 -31
- package/src/api.test.ts +95 -96
- package/src/api.ts +16 -27
- package/src/auth.test.ts +107 -51
- package/src/auth.ts +98 -29
- package/src/authRecovery.test.ts +281 -0
- package/src/authRecovery.ts +279 -0
- package/src/authTokens.test.ts +94 -0
- package/src/authTokens.ts +118 -0
- package/src/authVerification.test.ts +293 -0
- package/src/betterAuth.ts +24 -0
- package/src/betterAuthApp.test.ts +1 -0
- package/src/betterAuthApp.ts +27 -9
- package/src/betterAuthSetup.test.ts +568 -38
- package/src/betterAuthSetup.ts +260 -5
- package/src/collectionRegistry.test.ts +165 -0
- package/src/collectionRegistry.ts +115 -0
- package/src/configurationApp.ts +13 -7
- package/src/docLoader.test.ts +45 -1
- package/src/docLoader.ts +37 -6
- package/src/express.d.ts +1 -0
- package/src/expressServer.test.ts +130 -66
- package/src/expressServer.ts +75 -19
- package/src/githubAuth.test.ts +60 -55
- package/src/index.ts +58 -0
- package/src/mcp/auth.test.ts +47 -0
- package/src/mcp/auth.ts +32 -11
- package/src/mcp/handlers.ts +45 -172
- package/src/mcp/integration.test.ts +59 -9
- package/src/mcp/query.ts +2 -4
- package/src/mcp/registry.ts +29 -20
- package/src/mcp/schemaGenerator.test.ts +8 -0
- package/src/mcp/schemaGenerator.ts +49 -131
- package/src/mcp/server.ts +2 -0
- package/src/mcp/serviceTokens.test.ts +330 -0
- package/src/mcp/serviceTokens.ts +307 -0
- package/src/mcp/toolGenerator.test.ts +1 -1
- package/src/mcp/types.ts +2 -6
- package/src/models/mcpServiceToken.test.ts +129 -0
- package/src/models/mcpServiceToken.ts +158 -0
- package/src/openApi.test.ts +80 -64
- package/src/openApiBuilder.test.ts +95 -7
- package/src/openApiBuilder.ts +46 -9
- package/src/openApiCompat.ts +1 -1
- package/src/openApiValidator.test.ts +49 -54
- package/src/openApiValidator.ts +1 -3
- package/src/plugins.test.ts +14 -0
- package/src/plugins.ts +34 -9
- package/src/populate.test.ts +34 -0
- package/src/populate.ts +40 -29
- package/src/rateLimit/applyTrustProxy.ts +17 -0
- package/src/rateLimit/createStore.ts +25 -0
- package/src/rateLimit/memoryStore.ts +40 -0
- package/src/rateLimit/middleware.ts +79 -0
- package/src/rateLimit/mongoStore.test.ts +80 -0
- package/src/rateLimit/mongoStore.ts +76 -0
- package/src/rateLimit/policies.ts +130 -0
- package/src/rateLimit/rateLimit.test.ts +328 -0
- package/src/rateLimit/redisStore.ioredis.test.ts +29 -0
- package/src/rateLimit/redisStore.test.ts +114 -0
- package/src/rateLimit/redisStore.ts +85 -0
- package/src/rateLimit/types.ts +64 -0
- package/src/rbac/access.ts +5 -1
- package/src/rbac/assertAllowed.test.ts +325 -0
- package/src/rbac/assertAllowed.ts +157 -0
- package/src/rbac/backfillAdmins.test.ts +124 -0
- package/src/rbac/fieldViews.test.ts +89 -0
- package/src/rbac/modelRouterAccess.test.ts +126 -0
- package/src/rbac/modelRouterAccess.ts +5 -2
- package/src/rbac/permissionUtils.ts +3 -2
- package/src/rbac/realtimeAccess.test.ts +109 -1
- package/src/rbac/resolve.ts +5 -4
- package/src/rbac/roleManager.test.ts +169 -0
- package/src/rbac/routes.test.ts +233 -0
- package/src/rbac/routes.ts +16 -6
- package/src/realtime/deprecation.test.ts +60 -0
- package/src/realtime/deprecation.ts +32 -0
- package/src/realtime/realtime.test.ts +8 -4
- package/src/realtime/realtimeApp.ts +10 -11
- package/src/realtime/realtimeAppSetup.test.ts +32 -5
- package/src/realtime/registry.ts +48 -42
- package/src/realtime/sessionRevalidation.test.ts +57 -44
- package/src/realtime/socketAuth.test.ts +75 -23
- package/src/realtime/socketAuth.ts +17 -6
- package/src/realtime/socketUser.ts +5 -3
- package/src/realtime/types.ts +14 -4
- package/src/schemaMetadata.test.ts +405 -0
- package/src/schemaMetadata.ts +593 -0
- package/src/scrubAdminFields.test.ts +105 -0
- package/src/seedRunner.test.ts +591 -0
- package/src/seedRunner.ts +464 -0
- package/src/sync/docMatchesMutationData.test.ts +16 -0
- package/src/sync/ensureSyncIndexes.test.ts +54 -15
- package/src/sync/executors.test.ts +40 -0
- package/src/sync/executors.ts +24 -12
- package/src/sync/integration.test.ts +15 -7
- package/src/sync/loadHarness.ts +28 -19
- package/src/sync/models.ts +32 -18
- package/src/sync/mutationHandler.test.ts +16 -16
- package/src/sync/mutationHandler.ts +9 -7
- package/src/sync/registrationSideEffects.test.ts +126 -0
- package/src/sync/registrationSideEffects.ts +131 -0
- package/src/sync/registry.ts +45 -171
- package/src/sync/routes.ts +3 -2
- package/src/sync/scripts/compactTombstones.ts +0 -2
- package/src/sync/sessionRevalidationIntegration.test.ts +12 -11
- package/src/sync/sync.test.ts +25 -23
- package/src/sync/syncApp.ts +4 -4
- package/src/sync/syncBatch.ts +3 -2
- package/src/sync/syncFrontier.test.ts +17 -15
- package/src/sync/syncPhaseC.test.ts +54 -43
- package/src/sync/syncRoutes.test.ts +51 -36
- package/src/sync/syncSeqPlugin.ts +66 -44
- package/src/sync/syncSocket.test.ts +112 -81
- package/src/terrenoApp.mcpServiceTokens.test.ts +173 -0
- package/src/terrenoApp.ts +87 -15
- package/src/tests/models.ts +14 -1
- package/src/types/authToken.ts +43 -0
- package/src/types/mcpServiceToken.ts +54 -0
- package/src/webhooks/headerValue.test.ts +22 -0
- package/src/webhooks/headerValue.ts +12 -0
- package/src/webhooks/idempotency/memoryStore.ts +34 -0
- package/src/webhooks/idempotency/mongoStore.test.ts +274 -0
- package/src/webhooks/idempotency/mongoStore.ts +83 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +70 -0
- package/src/webhooks/rawBody.test.ts +74 -0
- package/src/webhooks/rawBody.ts +23 -0
- package/src/webhooks/timingSafeEqual.ts +16 -0
- package/src/webhooks/verifiers/hmac.test.ts +110 -0
- package/src/webhooks/verifiers/hmac.ts +54 -0
- package/src/webhooks/verifiers/providerVerifiers.test.ts +388 -0
- package/src/webhooks/verifiers/sendgrid.ts +59 -0
- package/src/webhooks/verifiers/stripe.ts +67 -0
- package/src/webhooks/verifiers/twilio.ts +69 -0
- package/src/webhooks/webhooksApp.test.ts +221 -0
- package/src/webhooks/webhooksApp.ts +144 -0
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// noExplicitAny: test mocks use dynamic shapes for sockets, io, and documents
|
|
2
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mocks use dynamic shapes for sockets, io, and documents
|
|
3
1
|
/**
|
|
4
2
|
* Tests for the sync socket layer (Tasks 2.3–2.5):
|
|
5
3
|
* - socketHandlers.ts (sync:subscribe / sync:unsubscribe / sync:mutate, caps, rate limit)
|
|
@@ -11,7 +9,8 @@
|
|
|
11
9
|
*/
|
|
12
10
|
|
|
13
11
|
import {afterEach, beforeAll, beforeEach, describe, expect, it} from "bun:test";
|
|
14
|
-
import mongoose, {model, Schema} from "mongoose";
|
|
12
|
+
import mongoose, {type Model, model, Schema} from "mongoose";
|
|
13
|
+
import type {Server} from "socket.io";
|
|
15
14
|
|
|
16
15
|
import type {ModelRouterOptions} from "../api";
|
|
17
16
|
import {createdUpdatedPlugin, type IsDeleted, isDeletedPlugin} from "../plugins";
|
|
@@ -108,7 +107,7 @@ const permissiveOptions = {
|
|
|
108
107
|
read: [() => true],
|
|
109
108
|
update: [() => true],
|
|
110
109
|
},
|
|
111
|
-
} as unknown as ModelRouterOptions<
|
|
110
|
+
} as unknown as ModelRouterOptions<unknown>;
|
|
112
111
|
|
|
113
112
|
const adminOnlyOptions = {
|
|
114
113
|
permissions: {
|
|
@@ -118,7 +117,7 @@ const adminOnlyOptions = {
|
|
|
118
117
|
read: [(_m: string, user?: {admin?: boolean}) => user?.admin === true],
|
|
119
118
|
update: [(_m: string, user?: {admin?: boolean}) => user?.admin === true],
|
|
120
119
|
},
|
|
121
|
-
} as unknown as ModelRouterOptions<
|
|
120
|
+
} as unknown as ModelRouterOptions<unknown>;
|
|
122
121
|
|
|
123
122
|
const ownerReadOptions = {
|
|
124
123
|
permissions: {
|
|
@@ -131,31 +130,32 @@ const ownerReadOptions = {
|
|
|
131
130
|
],
|
|
132
131
|
update: [() => true],
|
|
133
132
|
},
|
|
134
|
-
} as unknown as ModelRouterOptions<
|
|
133
|
+
} as unknown as ModelRouterOptions<unknown>;
|
|
135
134
|
|
|
136
135
|
/**
|
|
137
136
|
* A minimal fake Mongoose model satisfying registerSync's schema/collection checks, for
|
|
138
137
|
* tests that need many registrations (each real registration requires a distinct
|
|
139
138
|
* compiled model).
|
|
140
139
|
*/
|
|
141
|
-
const makeFakeSyncModel = (name: string):
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
modelName: name,
|
|
147
|
-
schema: {
|
|
148
|
-
path: (p: string) => {
|
|
149
|
-
if (p === "deleted") {
|
|
150
|
-
return {instance: "Boolean"};
|
|
151
|
-
}
|
|
152
|
-
if (p === "_syncSeq") {
|
|
153
|
-
return {instance: "Number"};
|
|
154
|
-
}
|
|
155
|
-
return undefined;
|
|
140
|
+
const makeFakeSyncModel = (name: string): Model<unknown> =>
|
|
141
|
+
({
|
|
142
|
+
collection: {
|
|
143
|
+
collectionName: name.toLowerCase(),
|
|
144
|
+
createIndex: async () => {},
|
|
156
145
|
},
|
|
157
|
-
|
|
158
|
-
|
|
146
|
+
modelName: name,
|
|
147
|
+
schema: {
|
|
148
|
+
path: (p: string) => {
|
|
149
|
+
if (p === "deleted") {
|
|
150
|
+
return {instance: "Boolean"};
|
|
151
|
+
}
|
|
152
|
+
if (p === "_syncSeq") {
|
|
153
|
+
return {instance: "Number"};
|
|
154
|
+
}
|
|
155
|
+
return undefined;
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
}) as unknown as Model<unknown>;
|
|
159
159
|
|
|
160
160
|
// The shared test database can be dropped by another test file mid-suite
|
|
161
161
|
// (configurationPlugin.test.ts drops it in an afterAll); rebuild the unique indexes the
|
|
@@ -168,19 +168,19 @@ const registerAll = (): void => {
|
|
|
168
168
|
clearSyncRegistry();
|
|
169
169
|
registerSync({
|
|
170
170
|
config: {scope: {type: "owner"}},
|
|
171
|
-
model: SockStuffModel as
|
|
171
|
+
model: SockStuffModel as unknown as Model<unknown>,
|
|
172
172
|
options: permissiveOptions,
|
|
173
173
|
routePath: "/sockStuff",
|
|
174
174
|
});
|
|
175
175
|
registerSync({
|
|
176
176
|
config: {scope: {field: "orgId", type: "tenant"}},
|
|
177
|
-
model: SockProjectModel as
|
|
177
|
+
model: SockProjectModel as unknown as Model<unknown>,
|
|
178
178
|
options: permissiveOptions,
|
|
179
179
|
routePath: "/sockProjects",
|
|
180
180
|
});
|
|
181
181
|
registerSync({
|
|
182
182
|
config: {scope: {type: "broadcast"}},
|
|
183
|
-
model: SockNewsModel as
|
|
183
|
+
model: SockNewsModel as unknown as Model<unknown>,
|
|
184
184
|
options: permissiveOptions,
|
|
185
185
|
routePath: "/sockNews",
|
|
186
186
|
});
|
|
@@ -190,17 +190,19 @@ const registerAll = (): void => {
|
|
|
190
190
|
// Mock socket (realtime.test.ts conventions)
|
|
191
191
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
192
192
|
|
|
193
|
+
type SyncSocketListener = (...args: never[]) => void | Promise<void>;
|
|
194
|
+
|
|
193
195
|
interface MockSocket extends SyncSocketLike {
|
|
194
196
|
rooms: Set<string>;
|
|
195
197
|
emitted: {event: string; payload: unknown}[];
|
|
196
|
-
listeners: Map<string,
|
|
197
|
-
trigger: (event: string, ...args:
|
|
198
|
+
listeners: Map<string, SyncSocketListener>;
|
|
199
|
+
trigger: (event: string, ...args: unknown[]) => Promise<void>;
|
|
198
200
|
}
|
|
199
201
|
|
|
200
202
|
const createMockSocket = (decodedToken?: {id?: string; admin?: boolean}): MockSocket => {
|
|
201
203
|
const rooms = new Set<string>();
|
|
202
204
|
const emitted: {event: string; payload: unknown}[] = [];
|
|
203
|
-
const listeners = new Map<string,
|
|
205
|
+
const listeners = new Map<string, SyncSocketListener>();
|
|
204
206
|
|
|
205
207
|
const socket: MockSocket = {
|
|
206
208
|
decodedToken,
|
|
@@ -223,7 +225,7 @@ const createMockSocket = (decodedToken?: {id?: string; admin?: boolean}): MockSo
|
|
|
223
225
|
trigger: async (event, ...args) => {
|
|
224
226
|
const handler = listeners.get(event);
|
|
225
227
|
if (handler) {
|
|
226
|
-
await handler(...args);
|
|
228
|
+
await (handler as (...handlerArgs: unknown[]) => void | Promise<void>)(...args);
|
|
227
229
|
}
|
|
228
230
|
},
|
|
229
231
|
};
|
|
@@ -232,7 +234,9 @@ const createMockSocket = (decodedToken?: {id?: string; admin?: boolean}): MockSo
|
|
|
232
234
|
};
|
|
233
235
|
|
|
234
236
|
const syncErrors = (socket: MockSocket): Array<{collection: string; message: string}> =>
|
|
235
|
-
socket.emitted
|
|
237
|
+
socket.emitted
|
|
238
|
+
.filter((e) => e.event === "sync:error")
|
|
239
|
+
.map((e) => e.payload as {collection: string; message: string});
|
|
236
240
|
|
|
237
241
|
const install = (socket: MockSocket, options: SyncAppOptions = {}): void => {
|
|
238
242
|
installSyncSocketHandlers(null, socket, options);
|
|
@@ -271,7 +275,7 @@ describe("installSyncSocketHandlers — subscribe/unsubscribe", () => {
|
|
|
271
275
|
const socket = createMockSocket({id: "user1"});
|
|
272
276
|
install(socket);
|
|
273
277
|
// The payload has no place for a user id — assert the room is keyed by the token id.
|
|
274
|
-
await socket.trigger("sync:subscribe", {collections: ["sockStuff"], userId: "victim"}
|
|
278
|
+
await socket.trigger("sync:subscribe", {collections: ["sockStuff"], userId: "victim"});
|
|
275
279
|
expect(socket.rooms.has("sync:sockStuff|owner:user1")).toBe(true);
|
|
276
280
|
expect(Array.from(socket.rooms).some((r) => r.includes("victim"))).toBe(false);
|
|
277
281
|
});
|
|
@@ -375,7 +379,7 @@ describe("installSyncSocketHandlers — subscribe/unsubscribe", () => {
|
|
|
375
379
|
clearSyncRegistry();
|
|
376
380
|
registerSync({
|
|
377
381
|
config: {scope: {type: "owner"}},
|
|
378
|
-
model: SockStuffModel as
|
|
382
|
+
model: SockStuffModel as unknown as Model<unknown>,
|
|
379
383
|
options: adminOnlyOptions,
|
|
380
384
|
routePath: "/sockStuff",
|
|
381
385
|
});
|
|
@@ -390,7 +394,7 @@ describe("installSyncSocketHandlers — subscribe/unsubscribe", () => {
|
|
|
390
394
|
clearSyncRegistry();
|
|
391
395
|
registerSync({
|
|
392
396
|
config: {scope: {type: "owner"}},
|
|
393
|
-
model: SockStuffModel as
|
|
397
|
+
model: SockStuffModel as unknown as Model<unknown>,
|
|
394
398
|
options: adminOnlyOptions,
|
|
395
399
|
routePath: "/sockStuff",
|
|
396
400
|
});
|
|
@@ -481,7 +485,9 @@ describe("installSyncSocketHandlers — subscribe/unsubscribe", () => {
|
|
|
481
485
|
// Re-subscribing after unsubscribe emits a fresh sync:subscribed.
|
|
482
486
|
await socket.trigger("sync:subscribe", {collections: ["sockProjects"]});
|
|
483
487
|
const subscribedEvents = socket.emitted.filter(
|
|
484
|
-
(e) =>
|
|
488
|
+
(e) =>
|
|
489
|
+
e.event === "sync:subscribed" &&
|
|
490
|
+
(e.payload as {collection?: string}).collection === "sockProjects"
|
|
485
491
|
);
|
|
486
492
|
expect(subscribedEvents).toHaveLength(2);
|
|
487
493
|
});
|
|
@@ -593,7 +599,7 @@ describe("installSyncSocketHandlers — sync:mutate", () => {
|
|
|
593
599
|
const nack = lastNack(socket);
|
|
594
600
|
expect(nack?.code).toBe("conflict");
|
|
595
601
|
expect(nack?.serverSeq).toBe(2);
|
|
596
|
-
expect((nack?.serverDoc as
|
|
602
|
+
expect((nack?.serverDoc as {name?: string} | undefined)?.name).toBe("v2");
|
|
597
603
|
});
|
|
598
604
|
|
|
599
605
|
it("duplicate mutationId over the socket returns the recorded outcome without re-applying", async () => {
|
|
@@ -701,10 +707,13 @@ describe("installSyncSocketHandlers — sync:mutateBatch", () => {
|
|
|
701
707
|
socket: MockSocket,
|
|
702
708
|
mutations: unknown[]
|
|
703
709
|
): Promise<{results: Array<{type: string; ack?: SyncAck; nack?: SyncNack}>}> => {
|
|
704
|
-
let response:
|
|
710
|
+
let response: {results: Array<{type: string; ack?: SyncAck; nack?: SyncNack}>} | undefined;
|
|
705
711
|
await socket.trigger("sync:mutateBatch", {mutations}, (res: unknown) => {
|
|
706
|
-
response = res;
|
|
712
|
+
response = res as {results: Array<{type: string; ack?: SyncAck; nack?: SyncNack}>};
|
|
707
713
|
});
|
|
714
|
+
if (!response) {
|
|
715
|
+
throw new Error("sync:mutateBatch did not invoke the ack callback");
|
|
716
|
+
}
|
|
708
717
|
return response;
|
|
709
718
|
};
|
|
710
719
|
|
|
@@ -896,10 +905,29 @@ describe("SyncAppOptions context", () => {
|
|
|
896
905
|
// sync:delta emission — mock io helpers
|
|
897
906
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
898
907
|
|
|
899
|
-
|
|
900
|
-
|
|
908
|
+
interface TrackedEmission {
|
|
909
|
+
event: string;
|
|
910
|
+
payload: unknown;
|
|
911
|
+
room: string;
|
|
912
|
+
socketId: string;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
interface TrackedSocket {
|
|
916
|
+
decodedToken?: {id?: string; admin?: boolean};
|
|
917
|
+
emit: (event: string, payload: unknown) => void;
|
|
918
|
+
id: string;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
/** A Socket.io server stand-in that records every emission it receives. */
|
|
922
|
+
type TrackedIo = Server & {
|
|
923
|
+
addSocketToRoom: (room: string, decodedToken?: {id?: string; admin?: boolean}) => void;
|
|
924
|
+
emissions: TrackedEmission[];
|
|
925
|
+
};
|
|
926
|
+
|
|
927
|
+
const makeTrackedIo = (): TrackedIo => {
|
|
928
|
+
const emissions: TrackedEmission[] = [];
|
|
901
929
|
const rooms = new Map<string, Set<string>>();
|
|
902
|
-
const sockets = new Map<string,
|
|
930
|
+
const sockets = new Map<string, TrackedSocket>();
|
|
903
931
|
|
|
904
932
|
const addSocketToRoom = (
|
|
905
933
|
room: string,
|
|
@@ -931,15 +959,18 @@ const makeTrackedIo = (): any => {
|
|
|
931
959
|
emissions.push({event, payload, room, socketId: "broadcast"});
|
|
932
960
|
},
|
|
933
961
|
}),
|
|
934
|
-
};
|
|
962
|
+
} as unknown as TrackedIo;
|
|
935
963
|
};
|
|
936
964
|
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
}
|
|
965
|
+
type WatchedChange = Parameters<typeof emitSyncDeltaForChange>[0]["change"];
|
|
966
|
+
|
|
967
|
+
const makeChange = (overrides: Record<string, unknown>): WatchedChange =>
|
|
968
|
+
({
|
|
969
|
+
documentKey: {_id: "doc-1"},
|
|
970
|
+
ns: {coll: "sockstuffs"},
|
|
971
|
+
operationType: "insert",
|
|
972
|
+
...overrides,
|
|
973
|
+
}) as unknown as WatchedChange;
|
|
943
974
|
|
|
944
975
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
945
976
|
// emitSyncDeltaForChange — unit tests with synthetic changes
|
|
@@ -962,7 +993,7 @@ describe("emitSyncDeltaForChange", () => {
|
|
|
962
993
|
clearSyncRegistry();
|
|
963
994
|
registerSync({
|
|
964
995
|
config: {scope: {type: "owner"}},
|
|
965
|
-
model: SockStuffModel as
|
|
996
|
+
model: SockStuffModel as unknown as Model<unknown>,
|
|
966
997
|
options: ownerReadOptions,
|
|
967
998
|
routePath: "/sockStuff",
|
|
968
999
|
});
|
|
@@ -985,15 +1016,15 @@ describe("emitSyncDeltaForChange", () => {
|
|
|
985
1016
|
logDebug: () => {},
|
|
986
1017
|
});
|
|
987
1018
|
|
|
988
|
-
const deltas = io.emissions.filter((e
|
|
1019
|
+
const deltas = io.emissions.filter((e) => e.event === "sync:delta");
|
|
989
1020
|
expect(deltas).toHaveLength(1);
|
|
990
1021
|
const delta = deltas[0].payload as SyncDelta;
|
|
991
1022
|
expect(delta.method).toBe("create");
|
|
992
1023
|
expect(delta.seq).toBe(7);
|
|
993
1024
|
expect(delta.stream).toBe("sockStuff|owner:user1");
|
|
994
1025
|
expect(delta.collection).toBe("sockStuff");
|
|
995
|
-
expect((delta.data as
|
|
996
|
-
expect((delta.data as
|
|
1026
|
+
expect((delta.data as Record<string, unknown>).name).toBe("hello");
|
|
1027
|
+
expect((delta.data as Record<string, unknown>).id).toBe("doc-1");
|
|
997
1028
|
});
|
|
998
1029
|
|
|
999
1030
|
it("emits a delta when the REST responseHandler serializes a BSON post-image", async () => {
|
|
@@ -1040,7 +1071,7 @@ describe("emitSyncDeltaForChange", () => {
|
|
|
1040
1071
|
logDebug: () => {},
|
|
1041
1072
|
});
|
|
1042
1073
|
|
|
1043
|
-
const deltas = io.emissions.filter((e
|
|
1074
|
+
const deltas = io.emissions.filter((e) => e.event === "sync:delta");
|
|
1044
1075
|
expect(deltas).toHaveLength(1);
|
|
1045
1076
|
const receiver = io.sockets.sockets.get(deltas[0].socketId);
|
|
1046
1077
|
expect(receiver.decodedToken.id).toBe("user1");
|
|
@@ -1063,7 +1094,7 @@ describe("emitSyncDeltaForChange", () => {
|
|
|
1063
1094
|
logDebug: () => {},
|
|
1064
1095
|
});
|
|
1065
1096
|
|
|
1066
|
-
const deltas = io.emissions.filter((e
|
|
1097
|
+
const deltas = io.emissions.filter((e) => e.event === "sync:delta");
|
|
1067
1098
|
expect(deltas).toHaveLength(1);
|
|
1068
1099
|
const delta = deltas[0].payload as SyncDelta;
|
|
1069
1100
|
expect(delta.method).toBe("delete");
|
|
@@ -1107,20 +1138,20 @@ describe("emitSyncDeltaForChange", () => {
|
|
|
1107
1138
|
logDebug: () => {},
|
|
1108
1139
|
});
|
|
1109
1140
|
|
|
1110
|
-
const deltas = io.emissions.filter((e
|
|
1141
|
+
const deltas = io.emissions.filter((e) => e.event === "sync:delta");
|
|
1111
1142
|
expect(deltas).toHaveLength(2);
|
|
1112
1143
|
|
|
1113
|
-
const tombstone = deltas.find((e
|
|
1144
|
+
const tombstone = deltas.find((e) => e.room === oldRoom)?.payload as SyncDelta;
|
|
1114
1145
|
expect(tombstone.method).toBe("delete");
|
|
1115
1146
|
expect(tombstone.deleted).toBe(true);
|
|
1116
1147
|
expect(tombstone.stream).toBe("sockStuff|owner:user1");
|
|
1117
1148
|
expect(tombstone.seq).toBe(9);
|
|
1118
1149
|
expect(tombstone.data).toBeUndefined();
|
|
1119
1150
|
|
|
1120
|
-
const create = deltas.find((e
|
|
1151
|
+
const create = deltas.find((e) => e.room === newRoom)?.payload as SyncDelta;
|
|
1121
1152
|
expect(create.method).toBe("create");
|
|
1122
1153
|
expect(create.stream).toBe("sockStuff|owner:user2");
|
|
1123
|
-
expect((create.data as
|
|
1154
|
+
expect((create.data as Record<string, unknown>).name).toBe("moved");
|
|
1124
1155
|
await SyncScopeMove.deleteMany({});
|
|
1125
1156
|
});
|
|
1126
1157
|
|
|
@@ -1161,8 +1192,8 @@ describe("emitSyncDeltaForChange", () => {
|
|
|
1161
1192
|
logDebug: () => {},
|
|
1162
1193
|
});
|
|
1163
1194
|
|
|
1164
|
-
const deltas = io.emissions.filter((e
|
|
1165
|
-
const tombstone = deltas.find((e
|
|
1195
|
+
const deltas = io.emissions.filter((e) => e.event === "sync:delta");
|
|
1196
|
+
const tombstone = deltas.find((e) => e.room === oldRoom)?.payload as SyncDelta;
|
|
1166
1197
|
expect(tombstone).toBeDefined();
|
|
1167
1198
|
expect(tombstone.method).toBe("delete");
|
|
1168
1199
|
expect(tombstone.stream).toBe("sockStuff|owner:user1");
|
|
@@ -1193,7 +1224,7 @@ describe("emitSyncDeltaForChange", () => {
|
|
|
1193
1224
|
logDebug: () => {},
|
|
1194
1225
|
});
|
|
1195
1226
|
|
|
1196
|
-
const deltas = io.emissions.filter((e
|
|
1227
|
+
const deltas = io.emissions.filter((e) => e.event === "sync:delta");
|
|
1197
1228
|
expect(deltas).toHaveLength(1);
|
|
1198
1229
|
expect((deltas[0].payload as SyncDelta).method).toBe("update");
|
|
1199
1230
|
});
|
|
@@ -1211,7 +1242,7 @@ describe("emitSyncDeltaForChange", () => {
|
|
|
1211
1242
|
logDebug: () => {},
|
|
1212
1243
|
});
|
|
1213
1244
|
|
|
1214
|
-
expect(io.emissions.filter((e
|
|
1245
|
+
expect(io.emissions.filter((e) => e.event === "sync:delta")).toHaveLength(0);
|
|
1215
1246
|
});
|
|
1216
1247
|
|
|
1217
1248
|
it("applies the sync responseHandler to delta data", async () => {
|
|
@@ -1221,7 +1252,7 @@ describe("emitSyncDeltaForChange", () => {
|
|
|
1221
1252
|
responseHandler: (doc: Record<string, unknown>) => ({onlyName: doc.name}),
|
|
1222
1253
|
scope: {type: "owner"},
|
|
1223
1254
|
},
|
|
1224
|
-
model: SockStuffModel as
|
|
1255
|
+
model: SockStuffModel as unknown as Model<unknown>,
|
|
1225
1256
|
options: permissiveOptions,
|
|
1226
1257
|
routePath: "/sockStuff",
|
|
1227
1258
|
});
|
|
@@ -1239,7 +1270,7 @@ describe("emitSyncDeltaForChange", () => {
|
|
|
1239
1270
|
logDebug: () => {},
|
|
1240
1271
|
});
|
|
1241
1272
|
|
|
1242
|
-
const delta = io.emissions.find((e
|
|
1273
|
+
const delta = io.emissions.find((e) => e.event === "sync:delta")?.payload as SyncDelta;
|
|
1243
1274
|
expect(delta.data).toEqual({onlyName: "shaped"});
|
|
1244
1275
|
});
|
|
1245
1276
|
});
|
|
@@ -1263,20 +1294,20 @@ const hasReplicaSet = async (): Promise<boolean> => {
|
|
|
1263
1294
|
* Start the watcher and give the change-stream cursor a moment to open so writes made
|
|
1264
1295
|
* immediately afterwards are not missed (change streams only deliver post-open events).
|
|
1265
1296
|
*/
|
|
1266
|
-
const startWatcherAndSettle = async (io:
|
|
1297
|
+
const startWatcherAndSettle = async (io: TrackedIo): Promise<void> => {
|
|
1267
1298
|
startChangeStreamWatcher(io, {}, true);
|
|
1268
1299
|
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
1269
1300
|
};
|
|
1270
1301
|
|
|
1271
1302
|
const waitForDelta = async (
|
|
1272
|
-
io:
|
|
1273
|
-
predicate: (delta: SyncDelta, emission:
|
|
1303
|
+
io: TrackedIo,
|
|
1304
|
+
predicate: (delta: SyncDelta, emission: TrackedEmission) => boolean,
|
|
1274
1305
|
timeoutMs = 5000
|
|
1275
1306
|
): Promise<void> => {
|
|
1276
1307
|
const start = Date.now();
|
|
1277
1308
|
while (Date.now() - start < timeoutMs) {
|
|
1278
1309
|
const match = io.emissions.some(
|
|
1279
|
-
(e
|
|
1310
|
+
(e) => e.event === "sync:delta" && predicate(e.payload as SyncDelta, e)
|
|
1280
1311
|
);
|
|
1281
1312
|
if (match) {
|
|
1282
1313
|
return;
|
|
@@ -1330,8 +1361,8 @@ describe("sync:delta — change stream integration", () => {
|
|
|
1330
1361
|
await waitForDelta(io, (d) => d.id === docId && d.method === "delete");
|
|
1331
1362
|
|
|
1332
1363
|
const deltas = io.emissions
|
|
1333
|
-
.filter((e
|
|
1334
|
-
.map((e
|
|
1364
|
+
.filter((e) => e.event === "sync:delta")
|
|
1365
|
+
.map((e) => e.payload as SyncDelta);
|
|
1335
1366
|
|
|
1336
1367
|
const creates = deltas.filter((d) => d.method === "create");
|
|
1337
1368
|
const updates = deltas.filter((d) => d.method === "update");
|
|
@@ -1348,7 +1379,7 @@ describe("sync:delta — change stream integration", () => {
|
|
|
1348
1379
|
expect(delta.collection).toBe("sockStuff");
|
|
1349
1380
|
}
|
|
1350
1381
|
expect(deletes[0].deleted).toBe(true);
|
|
1351
|
-
expect((deletes[0].data as
|
|
1382
|
+
expect((deletes[0].data as Record<string, unknown>).name).toBe("cs-updated");
|
|
1352
1383
|
});
|
|
1353
1384
|
|
|
1354
1385
|
it("a socket subscribed to tenant org1 never receives org2's delta", async () => {
|
|
@@ -1366,8 +1397,8 @@ describe("sync:delta — change stream integration", () => {
|
|
|
1366
1397
|
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
1367
1398
|
|
|
1368
1399
|
const deltas = io.emissions
|
|
1369
|
-
.filter((e
|
|
1370
|
-
.map((e
|
|
1400
|
+
.filter((e) => e.event === "sync:delta")
|
|
1401
|
+
.map((e) => e.payload as SyncDelta);
|
|
1371
1402
|
expect(deltas.some((d) => d.id === String(org1Doc._id))).toBe(true);
|
|
1372
1403
|
expect(deltas.some((d) => d.id === String(org2Doc._id))).toBe(false);
|
|
1373
1404
|
expect(deltas.every((d) => d.stream === "sockProjects|tenant:org1")).toBe(true);
|
|
@@ -1393,10 +1424,10 @@ describe("sync:delta — change stream integration", () => {
|
|
|
1393
1424
|
await waitForDelta(io, (d, e) => d.id === docId && e.room === oldRoom);
|
|
1394
1425
|
|
|
1395
1426
|
const deltas = io.emissions.filter(
|
|
1396
|
-
(e
|
|
1427
|
+
(e) => e.event === "sync:delta" && (e.payload as SyncDelta).id === docId
|
|
1397
1428
|
);
|
|
1398
|
-
const tombstones = deltas.filter((e
|
|
1399
|
-
const creates = deltas.filter((e
|
|
1429
|
+
const tombstones = deltas.filter((e) => e.room === oldRoom);
|
|
1430
|
+
const creates = deltas.filter((e) => e.room === newRoom);
|
|
1400
1431
|
expect(tombstones).toHaveLength(1);
|
|
1401
1432
|
expect(creates).toHaveLength(1);
|
|
1402
1433
|
|
|
@@ -1409,7 +1440,7 @@ describe("sync:delta — change stream integration", () => {
|
|
|
1409
1440
|
const create = creates[0].payload as SyncDelta;
|
|
1410
1441
|
expect(create.method).toBe("create");
|
|
1411
1442
|
expect(create.stream).toBe("sockProjects|tenant:org2");
|
|
1412
|
-
expect((create.data as
|
|
1443
|
+
expect((create.data as Record<string, unknown>).orgId).toBe("org2");
|
|
1413
1444
|
});
|
|
1414
1445
|
|
|
1415
1446
|
it("a model with both realtime and sync configs emits both event types", async () => {
|
|
@@ -1437,10 +1468,10 @@ describe("sync:delta — change stream integration", () => {
|
|
|
1437
1468
|
await waitForDelta(io, (d) => d.id === docId);
|
|
1438
1469
|
|
|
1439
1470
|
const legacy = io.emissions.filter(
|
|
1440
|
-
(e
|
|
1471
|
+
(e) => e.event === "sync" && e.payload?.id === docId && e.payload?.method === "create"
|
|
1441
1472
|
);
|
|
1442
1473
|
const deltas = io.emissions.filter(
|
|
1443
|
-
(e
|
|
1474
|
+
(e) => e.event === "sync:delta" && (e.payload as SyncDelta).id === docId
|
|
1444
1475
|
);
|
|
1445
1476
|
expect(legacy.length).toBeGreaterThanOrEqual(1);
|
|
1446
1477
|
expect(deltas).toHaveLength(1);
|
|
@@ -1461,8 +1492,8 @@ describe("sync:delta — change stream integration", () => {
|
|
|
1461
1492
|
const doc = await SockStuffModel.create({name: "sync only", ownerId: "only-user"});
|
|
1462
1493
|
await waitForDelta(io, (d) => d.id === String(doc._id));
|
|
1463
1494
|
|
|
1464
|
-
const deltas = io.emissions.filter((e
|
|
1495
|
+
const deltas = io.emissions.filter((e) => e.event === "sync:delta");
|
|
1465
1496
|
expect(deltas).toHaveLength(1);
|
|
1466
|
-
expect(io.emissions.filter((e
|
|
1497
|
+
expect(io.emissions.filter((e) => e.event === "sync")).toHaveLength(0);
|
|
1467
1498
|
});
|
|
1468
1499
|
});
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import {beforeEach, describe, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
3
|
+
import mongoose, {model, Schema} from "mongoose";
|
|
4
|
+
import supertest from "supertest";
|
|
5
|
+
|
|
6
|
+
import {modelRouter} from "./api";
|
|
7
|
+
import type {UserModel as UserModelType} from "./auth";
|
|
8
|
+
import {clearMCPRegistry} from "./mcp/registry";
|
|
9
|
+
import {McpServiceToken} from "./models/mcpServiceToken";
|
|
10
|
+
import {Permissions} from "./permissions";
|
|
11
|
+
import {createdUpdatedPlugin} from "./plugins";
|
|
12
|
+
import {TerrenoApp} from "./terrenoApp";
|
|
13
|
+
import {authAsUser, setupDb, UserModel} from "./tests";
|
|
14
|
+
|
|
15
|
+
const typedUserModel = UserModel as unknown as UserModelType;
|
|
16
|
+
|
|
17
|
+
const PUBLIC_MCP_URL = "https://api.example.com";
|
|
18
|
+
|
|
19
|
+
const toolsCallBody = {
|
|
20
|
+
id: 3,
|
|
21
|
+
jsonrpc: "2.0",
|
|
22
|
+
method: "tools/call",
|
|
23
|
+
params: {arguments: {}, name: "tokenmcpnotes_list"},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const getNoteModel = (): mongoose.Model<{title?: string}> => {
|
|
27
|
+
if (mongoose.models.TokenMcpNote) {
|
|
28
|
+
return mongoose.models.TokenMcpNote as mongoose.Model<{title?: string}>;
|
|
29
|
+
}
|
|
30
|
+
const schema = new Schema({title: {description: "Note title", type: String}});
|
|
31
|
+
schema.plugin(createdUpdatedPlugin);
|
|
32
|
+
return model("TokenMcpNote", schema);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const TokenMcpNote = getNoteModel();
|
|
36
|
+
|
|
37
|
+
const noteRouter = () => {
|
|
38
|
+
return modelRouter("/token-mcp-notes", TokenMcpNote, {
|
|
39
|
+
mcp: {methods: ["list"], toolPrefix: "tokenmcpnotes"},
|
|
40
|
+
permissions: {
|
|
41
|
+
create: [],
|
|
42
|
+
delete: [],
|
|
43
|
+
list: [Permissions.IsAuthenticated],
|
|
44
|
+
read: [Permissions.IsAuthenticated],
|
|
45
|
+
update: [],
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
describe("TerrenoApp mcpServiceTokens", () => {
|
|
51
|
+
let notAdminId: mongoose.Types.ObjectId;
|
|
52
|
+
|
|
53
|
+
beforeEach(async () => {
|
|
54
|
+
const users = await setupDb();
|
|
55
|
+
notAdminId = users[1]._id as mongoose.Types.ObjectId;
|
|
56
|
+
await McpServiceToken.deleteMany({});
|
|
57
|
+
await TokenMcpNote.deleteMany({});
|
|
58
|
+
clearMCPRegistry();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("does not serve token routes or accept mcp_ on /mcp when the flag is off", async () => {
|
|
62
|
+
const app = new TerrenoApp({
|
|
63
|
+
skipListen: true,
|
|
64
|
+
userModel: typedUserModel,
|
|
65
|
+
})
|
|
66
|
+
.register(noteRouter())
|
|
67
|
+
.build();
|
|
68
|
+
|
|
69
|
+
const missingRoutes = await supertest(app).get("/mcp/service-tokens");
|
|
70
|
+
assert.equal(missingRoutes.status, 404);
|
|
71
|
+
|
|
72
|
+
const issued = await McpServiceToken.issueFor({_id: notAdminId}, {name: "Ignored"});
|
|
73
|
+
const mcpCall = await supertest(app)
|
|
74
|
+
.post("/mcp")
|
|
75
|
+
.set("accept", "application/json, text/event-stream")
|
|
76
|
+
.set("Authorization", `Bearer ${issued.token}`)
|
|
77
|
+
.send(toolsCallBody);
|
|
78
|
+
|
|
79
|
+
assert.include(mcpCall.text, "Permission denied: authentication required");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("serves token routes and accepts mcp_ on /mcp when the flag is true", async () => {
|
|
83
|
+
const app = new TerrenoApp({
|
|
84
|
+
mcpServiceTokens: true,
|
|
85
|
+
skipListen: true,
|
|
86
|
+
userModel: typedUserModel,
|
|
87
|
+
})
|
|
88
|
+
.register(noteRouter())
|
|
89
|
+
.build();
|
|
90
|
+
|
|
91
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
92
|
+
const created = await agent.post("/mcp/service-tokens").send({name: "Perplexity"});
|
|
93
|
+
|
|
94
|
+
assert.equal(created.status, 200);
|
|
95
|
+
assert.match(created.body.data.token, /^mcp_[0-9a-f]{64}$/);
|
|
96
|
+
|
|
97
|
+
await TokenMcpNote.create({title: "Visible note"});
|
|
98
|
+
|
|
99
|
+
const mcpCall = await supertest(app)
|
|
100
|
+
.post("/mcp")
|
|
101
|
+
.set("accept", "application/json, text/event-stream")
|
|
102
|
+
.set("Authorization", `Bearer ${created.body.data.token}`)
|
|
103
|
+
.send(toolsCallBody);
|
|
104
|
+
|
|
105
|
+
assert.notInclude(mcpCall.text, "Permission denied");
|
|
106
|
+
assert.include(mcpCall.text, "Visible note");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("uses publicMcpUrl from the object form of the flag", async () => {
|
|
110
|
+
const app = new TerrenoApp({
|
|
111
|
+
mcpServiceTokens: {enabled: true, publicMcpUrl: PUBLIC_MCP_URL},
|
|
112
|
+
skipListen: true,
|
|
113
|
+
userModel: typedUserModel,
|
|
114
|
+
}).build();
|
|
115
|
+
|
|
116
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
117
|
+
const created = await agent.post("/mcp/service-tokens").send({name: "Named"});
|
|
118
|
+
|
|
119
|
+
assert.equal(created.status, 200);
|
|
120
|
+
assert.equal(created.body.data.mcpUrl, `${PUBLIC_MCP_URL}/mcp`);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("documents the token routes in /openapi.json when enabled", async () => {
|
|
124
|
+
const app = new TerrenoApp({
|
|
125
|
+
mcpServiceTokens: true,
|
|
126
|
+
skipListen: true,
|
|
127
|
+
userModel: typedUserModel,
|
|
128
|
+
}).build();
|
|
129
|
+
|
|
130
|
+
const res = await supertest(app).get("/openapi.json");
|
|
131
|
+
assert.equal(res.body.paths["/mcp/service-tokens"].post.operationId, "createMcpServiceToken");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("lets a minted service token call tools as the owner while GET /mcp stays 405", async () => {
|
|
135
|
+
await TokenMcpNote.create({title: "Visible note"});
|
|
136
|
+
const app = new TerrenoApp({
|
|
137
|
+
mcpServiceTokens: true,
|
|
138
|
+
skipListen: true,
|
|
139
|
+
userModel: typedUserModel,
|
|
140
|
+
})
|
|
141
|
+
.register(noteRouter())
|
|
142
|
+
.build();
|
|
143
|
+
|
|
144
|
+
const agent = await authAsUser(app, "notAdmin");
|
|
145
|
+
const created = await agent.post("/mcp/service-tokens").send({name: "Perplexity probe"});
|
|
146
|
+
const mcpToken = created.body.data.token as string;
|
|
147
|
+
|
|
148
|
+
const jwtCall = await agent
|
|
149
|
+
.post("/mcp")
|
|
150
|
+
.set("accept", "application/json, text/event-stream")
|
|
151
|
+
.send(toolsCallBody);
|
|
152
|
+
const tokenCall = await supertest(app)
|
|
153
|
+
.post("/mcp")
|
|
154
|
+
.set("Authorization", `Bearer ${mcpToken}`)
|
|
155
|
+
.set("accept", "application/json, text/event-stream")
|
|
156
|
+
.send(toolsCallBody);
|
|
157
|
+
const bogusCall = await supertest(app)
|
|
158
|
+
.post("/mcp")
|
|
159
|
+
.set("Authorization", "Bearer mcp_deadbeef")
|
|
160
|
+
.set("accept", "application/json, text/event-stream")
|
|
161
|
+
.send(toolsCallBody);
|
|
162
|
+
const probe = await supertest(app)
|
|
163
|
+
.get("/mcp")
|
|
164
|
+
.set("Authorization", `Bearer ${mcpToken}`)
|
|
165
|
+
.set("accept", "application/json, text/event-stream");
|
|
166
|
+
|
|
167
|
+
assert.include(jwtCall.text, "Visible note");
|
|
168
|
+
assert.include(tokenCall.text, "Visible note");
|
|
169
|
+
assert.include(bogusCall.text, "Permission denied: authentication required");
|
|
170
|
+
assert.equal(probe.status, 405);
|
|
171
|
+
assert.equal(probe.body.error.message, "Method not allowed.");
|
|
172
|
+
});
|
|
173
|
+
});
|