@terreno/api 0.31.0 → 56.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +122 -2
- package/LICENSE +202 -21
- package/dist/api.d.ts +12 -3
- package/dist/api.js +110 -361
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +100 -1
- package/dist/betterAuth.d.ts +5 -0
- package/dist/betterAuthSetup.d.ts +1 -15
- package/dist/betterAuthSetup.js +29 -8
- package/dist/configurationPlugin.js +2 -2
- package/dist/docLoader.js +12 -9
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +29 -1
- package/dist/expressServer.js +5 -11
- package/dist/githubAuth.js +4 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +29 -1
- package/dist/middleware.d.ts +7 -0
- package/dist/middleware.js +16 -1
- package/dist/openApiCompat.d.ts +10 -1
- package/dist/openApiCompat.js +115 -47
- package/dist/permissions.d.ts +27 -0
- package/dist/permissions.js +42 -1
- package/dist/plugins.d.ts +15 -3
- package/dist/plugins.js +36 -6
- package/dist/realtime/changeStreamWatcher.d.ts +100 -3
- package/dist/realtime/changeStreamWatcher.js +542 -137
- package/dist/realtime/realtimeApp.d.ts +3 -1
- package/dist/realtime/realtimeApp.js +75 -27
- package/dist/realtime/sessionRevalidation.d.ts +90 -0
- package/dist/realtime/sessionRevalidation.js +438 -0
- package/dist/realtime/socketAuth.d.ts +97 -0
- package/dist/realtime/socketAuth.js +272 -0
- package/dist/realtime/socketUser.d.ts +54 -0
- package/dist/realtime/socketUser.js +75 -5
- package/dist/realtime/types.d.ts +40 -0
- package/dist/secretProviders.d.ts +23 -0
- package/dist/secretProviders.js +9 -3
- package/dist/sync/executors.d.ts +198 -0
- package/dist/sync/executors.js +670 -0
- package/dist/sync/loadHarness.js +807 -0
- package/dist/sync/models.d.ts +216 -0
- package/dist/sync/models.js +644 -0
- package/dist/sync/mutationHandler.d.ts +97 -0
- package/dist/sync/mutationHandler.js +886 -0
- package/dist/sync/registry.d.ts +73 -0
- package/dist/sync/registry.js +223 -0
- package/dist/sync/routes.d.ts +62 -0
- package/dist/sync/routes.js +936 -0
- package/dist/sync/scripts/compactTombstones.d.ts +47 -0
- package/dist/sync/scripts/compactTombstones.js +270 -0
- package/dist/sync/serialize.d.ts +20 -0
- package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
- package/dist/sync/socketHandlers.d.ts +54 -0
- package/dist/sync/socketHandlers.js +470 -0
- package/dist/sync/streams.d.ts +85 -0
- package/dist/sync/streams.js +221 -0
- package/dist/sync/syncApp.d.ts +29 -0
- package/dist/sync/syncApp.js +44 -0
- package/dist/sync/syncBatch.d.ts +71 -0
- package/dist/sync/syncBatch.js +252 -0
- package/dist/sync/syncSeqPlugin.d.ts +8 -0
- package/dist/sync/syncSeqPlugin.js +903 -0
- package/dist/sync/types.d.ts +254 -0
- package/dist/sync/types.js +9 -0
- package/dist/terrenoApp.js +90 -27
- package/dist/tests/models.d.ts +11 -27
- package/dist/tests/models.js +3 -5
- package/dist/tests/testHelper.d.ts +3 -9
- package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
- package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
- package/package.json +10 -8
- package/src/__tests__/openApiCompat.test.ts +20 -0
- package/src/actions.test.ts +12 -19
- package/src/actions.ts +2 -2
- package/src/api.coverage.test.ts +9 -7
- package/src/api.errorPaths.test.ts +1 -47
- package/src/api.test.ts +12 -0
- package/src/api.ts +98 -317
- package/src/auth.test.ts +128 -176
- package/src/auth.ts +109 -0
- package/src/betterAuth.ts +6 -0
- package/src/betterAuthSetup.test.ts +34 -0
- package/src/betterAuthSetup.ts +27 -8
- package/src/configurationPlugin.ts +2 -2
- package/src/consentApp.test.ts +12 -12
- package/src/docLoader.ts +3 -1
- package/src/errors.ts +33 -0
- package/src/expressServer.test.ts +7 -1
- package/src/expressServer.ts +5 -11
- package/src/githubAuth.ts +3 -1
- package/src/index.ts +42 -0
- package/src/middleware.test.ts +40 -1
- package/src/middleware.ts +15 -0
- package/src/models/versionConfig.ts +1 -1
- package/src/openApiBuilder.test.ts +9 -24
- package/src/openApiCompat.ts +99 -45
- package/src/openApiValidator.test.ts +0 -41
- package/src/permissions.test.ts +75 -1
- package/src/permissions.ts +49 -0
- package/src/plugins.test.ts +54 -14
- package/src/plugins.ts +47 -9
- package/src/populate.test.ts +16 -25
- package/src/realtime/changeStreamWatcher.ts +552 -114
- package/src/realtime/realtime.test.ts +157 -239
- package/src/realtime/realtimeApp.ts +80 -19
- package/src/realtime/sessionRevalidation.test.ts +423 -0
- package/src/realtime/sessionRevalidation.ts +273 -0
- package/src/realtime/socketAuth.test.ts +355 -0
- package/src/realtime/socketAuth.ts +228 -0
- package/src/realtime/socketUser.ts +69 -0
- package/src/realtime/types.ts +40 -0
- package/src/secretProviders.test.ts +7 -1
- package/src/secretProviders.ts +15 -3
- package/src/sync/ensureSyncIndexes.test.ts +144 -0
- package/src/sync/executors.test.ts +882 -0
- package/src/sync/executors.ts +703 -0
- package/src/sync/integration.test.ts +1160 -0
- package/src/sync/loadHarness.ts +618 -0
- package/src/sync/models.ts +640 -0
- package/src/sync/mutationHandler.test.ts +1252 -0
- package/src/sync/mutationHandler.ts +850 -0
- package/src/sync/registry.ts +232 -0
- package/src/sync/routes.ts +812 -0
- package/src/sync/scripts/compactTombstones.test.ts +249 -0
- package/src/sync/scripts/compactTombstones.ts +145 -0
- package/src/sync/serialize.ts +48 -0
- package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
- package/src/sync/socketHandlers.ts +320 -0
- package/src/sync/streams.ts +188 -0
- package/src/sync/sync.test.ts +582 -0
- package/src/sync/syncApp.ts +44 -0
- package/src/sync/syncBatch.ts +219 -0
- package/src/sync/syncFrontier.test.ts +494 -0
- package/src/sync/syncPhaseC.test.ts +718 -0
- package/src/sync/syncRoutes.test.ts +865 -0
- package/src/sync/syncSeqPlugin.ts +738 -0
- package/src/sync/syncSocket.test.ts +1440 -0
- package/src/sync/types.ts +270 -0
- package/src/terrenoApp.ts +37 -17
- package/src/tests/bunSetup.ts +8 -0
- package/src/tests/models.ts +6 -8
- package/tsconfig.json +4 -4
- package/tsconfig.typedoc.json +7 -1
- package/dist/__tests__/openApiCompat.test.js +0 -317
- package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
- package/dist/__tests__/versionCheckPlugin.test.js +0 -597
- package/dist/actions.openApi.test.d.ts +0 -1
- package/dist/actions.openApi.test.js +0 -253
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -1087
- package/dist/api.arrayOperations.test.d.ts +0 -1
- package/dist/api.arrayOperations.test.js +0 -878
- package/dist/api.asyncHandler.test.d.ts +0 -1
- package/dist/api.asyncHandler.test.js +0 -236
- package/dist/api.coverage.test.d.ts +0 -1
- package/dist/api.coverage.test.js +0 -192
- package/dist/api.errorPaths.test.d.ts +0 -1
- package/dist/api.errorPaths.test.js +0 -887
- package/dist/api.errors.test.d.ts +0 -1
- package/dist/api.errors.test.js +0 -185
- package/dist/api.hooks.test.d.ts +0 -1
- package/dist/api.hooks.test.js +0 -950
- package/dist/api.query.test.d.ts +0 -1
- package/dist/api.query.test.js +0 -948
- package/dist/api.test.d.ts +0 -1
- package/dist/api.test.js +0 -2820
- package/dist/auth.compile.test.d.ts +0 -1
- package/dist/auth.compile.test.js +0 -201
- package/dist/auth.coverage.test.d.ts +0 -1
- package/dist/auth.coverage.test.js +0 -202
- package/dist/auth.test.d.ts +0 -1
- package/dist/auth.test.js +0 -2286
- package/dist/betterAuth.test.d.ts +0 -1
- package/dist/betterAuth.test.js +0 -181
- package/dist/betterAuthApp.test.d.ts +0 -1
- package/dist/betterAuthApp.test.js +0 -242
- package/dist/betterAuthSetup.test.d.ts +0 -1
- package/dist/betterAuthSetup.test.js +0 -762
- package/dist/config.test.d.ts +0 -1
- package/dist/config.test.js +0 -353
- package/dist/configuration.test.d.ts +0 -1
- package/dist/configuration.test.js +0 -980
- package/dist/configurationPlugin.test.d.ts +0 -1
- package/dist/configurationPlugin.test.js +0 -827
- package/dist/consentApp.test.d.ts +0 -1
- package/dist/consentApp.test.js +0 -1411
- package/dist/docLoader.test.d.ts +0 -1
- package/dist/docLoader.test.js +0 -137
- package/dist/envConfigurationPlugin.test.d.ts +0 -1
- package/dist/envConfigurationPlugin.test.js +0 -369
- package/dist/errors.test.d.ts +0 -1
- package/dist/errors.test.js +0 -607
- package/dist/expressServer.test.d.ts +0 -1
- package/dist/expressServer.test.js +0 -1249
- package/dist/githubAuth.test.d.ts +0 -1
- package/dist/githubAuth.test.js +0 -1142
- package/dist/httpClient.test.d.ts +0 -1
- package/dist/httpClient.test.js +0 -1471
- package/dist/logger.test.d.ts +0 -1
- package/dist/logger.test.js +0 -685
- package/dist/middleware.test.d.ts +0 -1
- package/dist/middleware.test.js +0 -241
- package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
- package/dist/notifiers/googleChatNotifier.test.js +0 -298
- package/dist/notifiers/slackNotifier.test.d.ts +0 -1
- package/dist/notifiers/slackNotifier.test.js +0 -297
- package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
- package/dist/notifiers/zoomNotifier.test.js +0 -373
- package/dist/openApi.test.d.ts +0 -1
- package/dist/openApi.test.js +0 -620
- package/dist/openApiBuilder.test.d.ts +0 -1
- package/dist/openApiBuilder.test.js +0 -599
- package/dist/openApiEtag.test.d.ts +0 -1
- package/dist/openApiEtag.test.js +0 -86
- package/dist/openApiValidator.test.d.ts +0 -1
- package/dist/openApiValidator.test.js +0 -795
- package/dist/permissions.middleware.test.d.ts +0 -1
- package/dist/permissions.middleware.test.js +0 -308
- package/dist/permissions.test.d.ts +0 -1
- package/dist/permissions.test.js +0 -439
- package/dist/plugins.test.d.ts +0 -1
- package/dist/plugins.test.js +0 -902
- package/dist/populate.test.d.ts +0 -1
- package/dist/populate.test.js +0 -370
- package/dist/realtime/realtime.test.d.ts +0 -10
- package/dist/realtime/realtime.test.js +0 -4213
- package/dist/realtime/realtimeAppSetup.test.d.ts +0 -1
- package/dist/realtime/realtimeAppSetup.test.js +0 -213
- package/dist/requestContext.test.d.ts +0 -1
- package/dist/requestContext.test.js +0 -412
- package/dist/scriptRunner.test.d.ts +0 -1
- package/dist/scriptRunner.test.js +0 -386
- package/dist/secretProviders.test.d.ts +0 -1
- package/dist/secretProviders.test.js +0 -726
- package/dist/syncConsents.test.d.ts +0 -1
- package/dist/syncConsents.test.js +0 -522
- package/dist/terrenoApp.test.d.ts +0 -1
- package/dist/terrenoApp.test.js +0 -489
- package/dist/tests/bunSetup.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.js +0 -37
- package/dist/tests/testEnv.d.ts +0 -0
- package/dist/tests/testEnv.js +0 -6
- package/dist/transformers.test.d.ts +0 -1
- package/dist/transformers.test.js +0 -423
- package/dist/utils.test.d.ts +0 -1
- package/dist/utils.test.js +0 -242
- package/src/realtime/realtimeAppSetup.test.ts +0 -161
- /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
|
@@ -0,0 +1,865 @@
|
|
|
1
|
+
// noExplicitAny: test model typing
|
|
2
|
+
// biome-ignore-all lint/suspicious/noExplicitAny: test model typing
|
|
3
|
+
import {beforeAll, beforeEach, describe, expect, it} from "bun:test";
|
|
4
|
+
import type express from "express";
|
|
5
|
+
import {model, Schema, Types} from "mongoose";
|
|
6
|
+
import supertest from "supertest";
|
|
7
|
+
import type TestAgent from "supertest/lib/agent";
|
|
8
|
+
import {type ModelRouterOptions, modelRouter} from "../api";
|
|
9
|
+
import {addAuthRoutes, setupAuth} from "../auth";
|
|
10
|
+
import {APIError} from "../errors";
|
|
11
|
+
import {OwnerQueryFilter, Permissions} from "../permissions";
|
|
12
|
+
import {createdUpdatedPlugin, type IsDeleted, isDeletedPlugin} from "../plugins";
|
|
13
|
+
import {authAsUser, getBaseServer, setupDb, UserModel} from "../tests";
|
|
14
|
+
import {SyncCounter, SyncKey, SyncMutation} from "./models";
|
|
15
|
+
import {MAX_SYNC_MUTATIONS_PER_BATCH} from "./mutationHandler";
|
|
16
|
+
import {clearSyncRegistry, registerSync} from "./registry";
|
|
17
|
+
import {MAX_ENTITY_FETCH, MAX_SYNC_HTTP_MUTATIONS_PER_SECOND} from "./routes";
|
|
18
|
+
import {SyncApp} from "./syncApp";
|
|
19
|
+
import {syncPlugin} from "./syncSeqPlugin";
|
|
20
|
+
|
|
21
|
+
interface RouteStuff extends IsDeleted {
|
|
22
|
+
_id: string;
|
|
23
|
+
name: string;
|
|
24
|
+
ownerId: string;
|
|
25
|
+
created: Date;
|
|
26
|
+
_syncSeq?: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const routeStuffSchema = new Schema<RouteStuff>({
|
|
30
|
+
name: {description: "The name of the item", required: true, type: String},
|
|
31
|
+
ownerId: {description: "The user who owns this item", type: String},
|
|
32
|
+
});
|
|
33
|
+
routeStuffSchema.plugin(isDeletedPlugin);
|
|
34
|
+
routeStuffSchema.plugin(createdUpdatedPlugin);
|
|
35
|
+
routeStuffSchema.plugin(syncPlugin);
|
|
36
|
+
const RouteStuffModel = model<RouteStuff>("SyncRouteStuff", routeStuffSchema);
|
|
37
|
+
|
|
38
|
+
interface RouteProject extends IsDeleted {
|
|
39
|
+
_id: string;
|
|
40
|
+
title: string;
|
|
41
|
+
orgId: string;
|
|
42
|
+
_syncSeq?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const routeProjectSchema = new Schema<RouteProject>({
|
|
46
|
+
orgId: {description: "The organization this project belongs to", type: String},
|
|
47
|
+
title: {description: "The project title", required: true, type: String},
|
|
48
|
+
});
|
|
49
|
+
routeProjectSchema.plugin(isDeletedPlugin);
|
|
50
|
+
routeProjectSchema.plugin(createdUpdatedPlugin);
|
|
51
|
+
routeProjectSchema.plugin(syncPlugin);
|
|
52
|
+
const RouteProjectModel = model<RouteProject>("SyncRouteProject", routeProjectSchema);
|
|
53
|
+
|
|
54
|
+
interface RouteBanner extends IsDeleted {
|
|
55
|
+
_id: string;
|
|
56
|
+
name: string;
|
|
57
|
+
ownerId: string;
|
|
58
|
+
_syncSeq?: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Broadcast-scoped: sync adds no scope clause of its own, so the modelRouter `queryFilter`
|
|
62
|
+
// is the only row-level scoping (Task 9.19).
|
|
63
|
+
const routeBannerSchema = new Schema<RouteBanner>({
|
|
64
|
+
name: {description: "The banner name", required: true, type: String},
|
|
65
|
+
ownerId: {description: "The user who owns this banner", type: String},
|
|
66
|
+
});
|
|
67
|
+
routeBannerSchema.plugin(isDeletedPlugin);
|
|
68
|
+
routeBannerSchema.plugin(createdUpdatedPlugin);
|
|
69
|
+
routeBannerSchema.plugin(syncPlugin);
|
|
70
|
+
const RouteBannerModel = model<RouteBanner>("SyncRouteBanner", routeBannerSchema);
|
|
71
|
+
|
|
72
|
+
const authedOptions = {
|
|
73
|
+
permissions: {
|
|
74
|
+
create: [Permissions.IsAuthenticated],
|
|
75
|
+
delete: [Permissions.IsAuthenticated],
|
|
76
|
+
list: [Permissions.IsAuthenticated],
|
|
77
|
+
read: [Permissions.IsAuthenticated],
|
|
78
|
+
update: [Permissions.IsAuthenticated],
|
|
79
|
+
},
|
|
80
|
+
} as unknown as ModelRouterOptions<any>;
|
|
81
|
+
|
|
82
|
+
const adminOnlyOptions = {
|
|
83
|
+
permissions: {
|
|
84
|
+
create: [Permissions.IsAdmin],
|
|
85
|
+
delete: [Permissions.IsAdmin],
|
|
86
|
+
list: [Permissions.IsAdmin],
|
|
87
|
+
read: [Permissions.IsAdmin],
|
|
88
|
+
update: [Permissions.IsAdmin],
|
|
89
|
+
},
|
|
90
|
+
} as unknown as ModelRouterOptions<any>;
|
|
91
|
+
|
|
92
|
+
// The shared test database can be dropped by another test file mid-suite
|
|
93
|
+
// (configurationPlugin.test.ts drops it in an afterAll); rebuild the unique indexes the
|
|
94
|
+
// duplicate-delivery and key tests depend on.
|
|
95
|
+
beforeAll(async () => {
|
|
96
|
+
await Promise.all([
|
|
97
|
+
SyncCounter.ensureIndexes(),
|
|
98
|
+
SyncKey.ensureIndexes(),
|
|
99
|
+
SyncMutation.ensureIndexes(),
|
|
100
|
+
]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe("sync routes", () => {
|
|
104
|
+
let app: express.Application;
|
|
105
|
+
let server: TestAgent;
|
|
106
|
+
let agent: TestAgent;
|
|
107
|
+
let adminAgent: TestAgent;
|
|
108
|
+
let notAdminId: string;
|
|
109
|
+
let adminId: string;
|
|
110
|
+
|
|
111
|
+
beforeEach(async () => {
|
|
112
|
+
const [admin, notAdmin] = await setupDb();
|
|
113
|
+
notAdminId = String(notAdmin._id);
|
|
114
|
+
adminId = String(admin._id);
|
|
115
|
+
|
|
116
|
+
clearSyncRegistry();
|
|
117
|
+
registerSync({
|
|
118
|
+
config: {scope: {type: "owner"}},
|
|
119
|
+
model: RouteStuffModel as any,
|
|
120
|
+
options: authedOptions,
|
|
121
|
+
routePath: "/routeStuff",
|
|
122
|
+
});
|
|
123
|
+
registerSync({
|
|
124
|
+
config: {scope: {field: "orgId", type: "tenant"}},
|
|
125
|
+
model: RouteProjectModel as any,
|
|
126
|
+
options: authedOptions,
|
|
127
|
+
routePath: "/routeProjects",
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
await Promise.all([
|
|
131
|
+
RouteStuffModel.collection.deleteMany({}),
|
|
132
|
+
RouteProjectModel.collection.deleteMany({}),
|
|
133
|
+
SyncCounter.deleteMany({}),
|
|
134
|
+
SyncKey.deleteMany({}),
|
|
135
|
+
SyncMutation.deleteMany({}),
|
|
136
|
+
]);
|
|
137
|
+
|
|
138
|
+
app = getBaseServer();
|
|
139
|
+
setupAuth(app as any, UserModel as any);
|
|
140
|
+
addAuthRoutes(app as any, UserModel as any);
|
|
141
|
+
new SyncApp({
|
|
142
|
+
getUserScopes: () => ["org1"],
|
|
143
|
+
}).register(app);
|
|
144
|
+
|
|
145
|
+
server = supertest(app);
|
|
146
|
+
agent = await authAsUser(app, "notAdmin");
|
|
147
|
+
adminAgent = await authAsUser(app, "admin");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// C2: the snapshot endpoint is now per-STREAM. The owner stream for notAdmin is
|
|
151
|
+
// `routeStuff|owner:{notAdminId}`; the tenant stream (getUserScopes -> ["org1"]) is
|
|
152
|
+
// `routeProjects|tenant:org1`.
|
|
153
|
+
describe("GET /sync/snapshot", () => {
|
|
154
|
+
const ownerStream = (): string => `routeStuff|owner:${notAdminId}`;
|
|
155
|
+
|
|
156
|
+
it("requires authentication", async () => {
|
|
157
|
+
await server
|
|
158
|
+
.get(`/sync/snapshot?stream=${encodeURIComponent("routeStuff|owner:x")}`)
|
|
159
|
+
.expect(401);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("requires a stream parameter", async () => {
|
|
163
|
+
const res = await agent.get("/sync/snapshot").expect(400);
|
|
164
|
+
expect(res.body.title).toMatch(/stream/);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("400s for an unparseable stream key", async () => {
|
|
168
|
+
await agent.get("/sync/snapshot?stream=notAStreamKey").expect(400);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("404s for unknown collections", async () => {
|
|
172
|
+
await agent.get(`/sync/snapshot?stream=${encodeURIComponent("nope|owner:x")}`).expect(404);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("403s when the caller does not belong to the requested stream", async () => {
|
|
176
|
+
// notAdmin's own owner stream is keyed by their id; someoneElse's is not theirs.
|
|
177
|
+
await agent
|
|
178
|
+
.get(`/sync/snapshot?stream=${encodeURIComponent("routeStuff|owner:someoneElse")}`)
|
|
179
|
+
.expect(403);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("400s for invalid cursor and limit", async () => {
|
|
183
|
+
const s = encodeURIComponent(ownerStream());
|
|
184
|
+
await agent.get(`/sync/snapshot?stream=${s}&cursor=abc`).expect(400);
|
|
185
|
+
await agent.get(`/sync/snapshot?stream=${s}&limit=-2`).expect(400);
|
|
186
|
+
// Task 9.27(b): parseInt used to stop at the first non-digit, so these silently
|
|
187
|
+
// became cursor 12 / limit 1 and the client paged on from the wrong seq.
|
|
188
|
+
await agent.get(`/sync/snapshot?stream=${s}&cursor=12abc`).expect(400);
|
|
189
|
+
await agent.get(`/sync/snapshot?stream=${s}&limit=1e9`).expect(400);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("enforces the model's list permissions", async () => {
|
|
193
|
+
clearSyncRegistry();
|
|
194
|
+
registerSync({
|
|
195
|
+
config: {scope: {type: "owner"}},
|
|
196
|
+
model: RouteStuffModel as any,
|
|
197
|
+
options: adminOnlyOptions,
|
|
198
|
+
routePath: "/routeStuff",
|
|
199
|
+
});
|
|
200
|
+
// notAdmin is denied at the list-permission gate; admin's own owner stream is allowed.
|
|
201
|
+
await agent.get(`/sync/snapshot?stream=${encodeURIComponent(ownerStream())}`).expect(403);
|
|
202
|
+
const admin = await UserModel.findOne({email: "admin@example.com"});
|
|
203
|
+
await adminAgent
|
|
204
|
+
.get(
|
|
205
|
+
`/sync/snapshot?stream=${encodeURIComponent(`routeStuff|owner:${String(admin?._id)}`)}`
|
|
206
|
+
)
|
|
207
|
+
.expect(200);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("returns a full owner-scoped snapshot at cursor 0", async () => {
|
|
211
|
+
await RouteStuffModel.create({name: "mine 1", ownerId: notAdminId});
|
|
212
|
+
await RouteStuffModel.create({name: "mine 2", ownerId: notAdminId});
|
|
213
|
+
await RouteStuffModel.create({name: "theirs", ownerId: "someoneElse"});
|
|
214
|
+
|
|
215
|
+
const res = await agent
|
|
216
|
+
.get(`/sync/snapshot?stream=${encodeURIComponent(ownerStream())}`)
|
|
217
|
+
.expect(200);
|
|
218
|
+
expect(res.body.stream).toBe(ownerStream());
|
|
219
|
+
expect(res.body.entities).toHaveLength(2);
|
|
220
|
+
expect(res.body.entities.map((e: any) => e.data.name)).toEqual(["mine 1", "mine 2"]);
|
|
221
|
+
expect(res.body.entities.map((e: any) => e.seq)).toEqual([1, 2]);
|
|
222
|
+
expect(res.body.cursor).toBe(2);
|
|
223
|
+
expect(res.body.hasMore).toBe(false);
|
|
224
|
+
// C1/C7 response fields present.
|
|
225
|
+
expect(res.body.frontierSeq).toBe(2);
|
|
226
|
+
expect(typeof res.body.oldestRetainedSeq).toBe("number");
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("returns incremental changes and tombstones past a cursor", async () => {
|
|
230
|
+
const doc1 = await RouteStuffModel.create({name: "first", ownerId: notAdminId});
|
|
231
|
+
const doc2 = await RouteStuffModel.create({name: "second", ownerId: notAdminId});
|
|
232
|
+
|
|
233
|
+
const initial = await agent
|
|
234
|
+
.get(`/sync/snapshot?stream=${encodeURIComponent(ownerStream())}`)
|
|
235
|
+
.expect(200);
|
|
236
|
+
const cursor = initial.body.cursor;
|
|
237
|
+
|
|
238
|
+
doc1.name = "first updated";
|
|
239
|
+
await doc1.save();
|
|
240
|
+
doc2.deleted = true;
|
|
241
|
+
await doc2.save();
|
|
242
|
+
|
|
243
|
+
const res = await agent
|
|
244
|
+
.get(`/sync/snapshot?stream=${encodeURIComponent(ownerStream())}&cursor=${cursor}`)
|
|
245
|
+
.expect(200);
|
|
246
|
+
expect(res.body.entities).toHaveLength(2);
|
|
247
|
+
const updated = res.body.entities.find((e: any) => e.id === String(doc1._id));
|
|
248
|
+
const tombstone = res.body.entities.find((e: any) => e.id === String(doc2._id));
|
|
249
|
+
expect(updated.data.name).toBe("first updated");
|
|
250
|
+
expect(updated.deleted).toBe(false);
|
|
251
|
+
expect(tombstone.deleted).toBe(true);
|
|
252
|
+
// C7: tombstones carry no data.
|
|
253
|
+
expect(tombstone.data).toBeNull();
|
|
254
|
+
expect(tombstone.seq).toBeGreaterThan(cursor);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("paginates by seq with hasMore and a resumable cursor", async () => {
|
|
258
|
+
for (let i = 1; i <= 5; i++) {
|
|
259
|
+
await RouteStuffModel.create({name: `item ${i}`, ownerId: notAdminId});
|
|
260
|
+
}
|
|
261
|
+
const s = encodeURIComponent(ownerStream());
|
|
262
|
+
const page1 = await agent.get(`/sync/snapshot?stream=${s}&limit=2`).expect(200);
|
|
263
|
+
expect(page1.body.entities).toHaveLength(2);
|
|
264
|
+
expect(page1.body.hasMore).toBe(true);
|
|
265
|
+
|
|
266
|
+
const page2 = await agent
|
|
267
|
+
.get(`/sync/snapshot?stream=${s}&limit=2&cursor=${page1.body.cursor}`)
|
|
268
|
+
.expect(200);
|
|
269
|
+
expect(page2.body.entities).toHaveLength(2);
|
|
270
|
+
expect(page2.body.hasMore).toBe(true);
|
|
271
|
+
|
|
272
|
+
const page3 = await agent
|
|
273
|
+
.get(`/sync/snapshot?stream=${s}&limit=2&cursor=${page2.body.cursor}`)
|
|
274
|
+
.expect(200);
|
|
275
|
+
expect(page3.body.entities).toHaveLength(1);
|
|
276
|
+
expect(page3.body.hasMore).toBe(false);
|
|
277
|
+
|
|
278
|
+
const names = [...page1.body.entities, ...page2.body.entities, ...page3.body.entities].map(
|
|
279
|
+
(e: any) => e.data.name
|
|
280
|
+
);
|
|
281
|
+
expect(names).toEqual(["item 1", "item 2", "item 3", "item 4", "item 5"]);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("never advances the returned cursor beyond the stable frontier (C1)", async () => {
|
|
285
|
+
await RouteStuffModel.create({name: "committed 1", ownerId: notAdminId});
|
|
286
|
+
await RouteStuffModel.create({name: "committed 2", ownerId: notAdminId});
|
|
287
|
+
const res = await agent
|
|
288
|
+
.get(`/sync/snapshot?stream=${encodeURIComponent(ownerStream())}`)
|
|
289
|
+
.expect(200);
|
|
290
|
+
// With no in-flight writes, frontier == head and the cursor equals the frontier.
|
|
291
|
+
expect(res.body.cursor).toBeLessThanOrEqual(res.body.frontierSeq);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it("scopes tenant collections to the requested tenant stream only", async () => {
|
|
295
|
+
await RouteProjectModel.create({orgId: "org1", title: "visible"});
|
|
296
|
+
await RouteProjectModel.create({orgId: "org2", title: "hidden"});
|
|
297
|
+
|
|
298
|
+
const res = await agent
|
|
299
|
+
.get("/sync/snapshot?stream=routeProjects%7Ctenant%3Aorg1")
|
|
300
|
+
.expect(200);
|
|
301
|
+
expect(res.body.entities).toHaveLength(1);
|
|
302
|
+
expect(res.body.entities[0].data.title).toBe("visible");
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("500s for tenant collections when no getUserScopes resolver is configured", async () => {
|
|
306
|
+
const bareApp = getBaseServer();
|
|
307
|
+
setupAuth(bareApp as any, UserModel as any);
|
|
308
|
+
addAuthRoutes(bareApp as any, UserModel as any);
|
|
309
|
+
new SyncApp().register(bareApp);
|
|
310
|
+
const bareAgent = await authAsUser(bareApp, "notAdmin");
|
|
311
|
+
await bareAgent.get("/sync/snapshot?stream=routeProjects%7Ctenant%3Aorg1").expect(500);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("uses the sync responseHandler to serialize entities", async () => {
|
|
315
|
+
clearSyncRegistry();
|
|
316
|
+
registerSync({
|
|
317
|
+
config: {
|
|
318
|
+
responseHandler: (doc) => ({redactedName: `x-${(doc as any).name}`}),
|
|
319
|
+
scope: {type: "owner"},
|
|
320
|
+
},
|
|
321
|
+
model: RouteStuffModel as any,
|
|
322
|
+
options: authedOptions,
|
|
323
|
+
routePath: "/routeStuff",
|
|
324
|
+
});
|
|
325
|
+
await RouteStuffModel.create({name: "secret", ownerId: notAdminId});
|
|
326
|
+
const res = await agent
|
|
327
|
+
.get(`/sync/snapshot?stream=${encodeURIComponent(ownerStream())}`)
|
|
328
|
+
.expect(200);
|
|
329
|
+
expect(res.body.entities[0].data).toEqual({redactedName: "x-secret"});
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
describe("POST /sync/mutate", () => {
|
|
334
|
+
it("requires authentication", async () => {
|
|
335
|
+
await server
|
|
336
|
+
.post("/sync/mutate")
|
|
337
|
+
.send({collection: "routeStuff", mutationId: "hm-1", operation: "create"})
|
|
338
|
+
.expect(401);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it("returns 200 with an ack for a successful create", async () => {
|
|
342
|
+
const res = await agent
|
|
343
|
+
.post("/sync/mutate")
|
|
344
|
+
.send({
|
|
345
|
+
collection: "routeStuff",
|
|
346
|
+
data: {name: "via http", ownerId: notAdminId},
|
|
347
|
+
mutationId: "hm-create-1",
|
|
348
|
+
operation: "create",
|
|
349
|
+
})
|
|
350
|
+
.expect(200);
|
|
351
|
+
expect(res.body.ack.mutationId).toBe("hm-create-1");
|
|
352
|
+
expect(res.body.ack.seq).toBe(1);
|
|
353
|
+
const saved = await RouteStuffModel.findById(res.body.ack.id);
|
|
354
|
+
expect(saved?.name).toBe("via http");
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it("returns 200 with an ack for updates and deletes", async () => {
|
|
358
|
+
const doc = await RouteStuffModel.create({name: "http original", ownerId: notAdminId});
|
|
359
|
+
const updateRes = await agent
|
|
360
|
+
.post("/sync/mutate")
|
|
361
|
+
.send({
|
|
362
|
+
baseVersion: 1,
|
|
363
|
+
collection: "routeStuff",
|
|
364
|
+
data: {name: "http updated"},
|
|
365
|
+
id: String(doc._id),
|
|
366
|
+
mutationId: "hm-update-1",
|
|
367
|
+
operation: "update",
|
|
368
|
+
})
|
|
369
|
+
.expect(200);
|
|
370
|
+
expect(updateRes.body.ack.seq).toBe(2);
|
|
371
|
+
|
|
372
|
+
const deleteRes = await agent
|
|
373
|
+
.post("/sync/mutate")
|
|
374
|
+
.send({
|
|
375
|
+
collection: "routeStuff",
|
|
376
|
+
id: String(doc._id),
|
|
377
|
+
mutationId: "hm-delete-1",
|
|
378
|
+
operation: "delete",
|
|
379
|
+
})
|
|
380
|
+
.expect(200);
|
|
381
|
+
expect(deleteRes.body.ack.seq).toBe(3);
|
|
382
|
+
const tombstones = await RouteStuffModel.find({_id: doc._id, deleted: true});
|
|
383
|
+
expect(tombstones).toHaveLength(1);
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
// Task 9.21: a create with no tenant value used to be written into a
|
|
387
|
+
// `tenant:undefined` stream that no client can subscribe to — stored but unsyncable.
|
|
388
|
+
it("rejects a tenant-scoped create whose scope field is absent", async () => {
|
|
389
|
+
const res = await agent
|
|
390
|
+
.post("/sync/mutate")
|
|
391
|
+
.send({
|
|
392
|
+
collection: "routeProjects",
|
|
393
|
+
data: {title: "no org"},
|
|
394
|
+
mutationId: "hm-tenant-missing",
|
|
395
|
+
operation: "create",
|
|
396
|
+
})
|
|
397
|
+
.expect(422);
|
|
398
|
+
expect(res.body.nack.code).toBe("validation");
|
|
399
|
+
expect(res.body.nack.message).toMatch(/missing tenant scope field "orgId"/);
|
|
400
|
+
expect(await RouteProjectModel.countDocuments({title: "no org"})).toBe(0);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// The write-path guard must not leak into reads: a legacy row predating the scope
|
|
404
|
+
// field still has to be loadable, or the documents it was meant to protect become
|
|
405
|
+
// unreadable instead of merely unwritable.
|
|
406
|
+
it("still hydrates a tenant-scoped document that has no scope value", async () => {
|
|
407
|
+
const legacyId = new Types.ObjectId();
|
|
408
|
+
await RouteProjectModel.collection.insertOne({
|
|
409
|
+
_id: legacyId,
|
|
410
|
+
_syncSeq: 1,
|
|
411
|
+
title: "legacy row",
|
|
412
|
+
} as never);
|
|
413
|
+
|
|
414
|
+
const loaded = await RouteProjectModel.findById(legacyId);
|
|
415
|
+
expect(loaded?.title).toBe("legacy row");
|
|
416
|
+
|
|
417
|
+
// Writing it, however, is still refused until the tenant field is supplied.
|
|
418
|
+
loaded!.title = "renamed";
|
|
419
|
+
await expect(loaded!.save()).rejects.toThrow(/missing tenant scope field "orgId"/);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
// A query-write can strip the tenant field off a document that already has one. The
|
|
423
|
+
// effective new value must be read from the update, not coalesced back to the
|
|
424
|
+
// document's current value — otherwise the guard sees the old org and passes, and the
|
|
425
|
+
// write is stamped into a stream the document no longer belongs to.
|
|
426
|
+
it("rejects a query-write that clears the tenant scope field", async () => {
|
|
427
|
+
const created = await RouteProjectModel.create({orgId: "org1", title: "has an org"});
|
|
428
|
+
|
|
429
|
+
await expect(
|
|
430
|
+
RouteProjectModel.updateOne({_id: created._id}, {$set: {orgId: null}}).exec()
|
|
431
|
+
).rejects.toThrow(/missing tenant scope field "orgId"/);
|
|
432
|
+
await expect(
|
|
433
|
+
RouteProjectModel.updateOne({_id: created._id}, {$unset: {orgId: 1}}).exec()
|
|
434
|
+
).rejects.toThrow(/missing tenant scope field "orgId"/);
|
|
435
|
+
|
|
436
|
+
// The document is untouched, and an update that leaves the scope field alone or
|
|
437
|
+
// moves it to another tenant still works.
|
|
438
|
+
const untouched = await RouteProjectModel.findById(created._id);
|
|
439
|
+
expect(untouched?.orgId).toBe("org1");
|
|
440
|
+
await RouteProjectModel.updateOne({_id: created._id}, {$set: {title: "renamed"}});
|
|
441
|
+
await RouteProjectModel.updateOne({_id: created._id}, {$set: {orgId: "org2"}});
|
|
442
|
+
const moved = await RouteProjectModel.findById(created._id);
|
|
443
|
+
expect(moved?.orgId).toBe("org2");
|
|
444
|
+
expect(moved?.title).toBe("renamed");
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
it("accepts a tenant-scoped create carrying a membership scope value", async () => {
|
|
448
|
+
const res = await agent
|
|
449
|
+
.post("/sync/mutate")
|
|
450
|
+
.send({
|
|
451
|
+
collection: "routeProjects",
|
|
452
|
+
data: {orgId: "org1", title: "with org"},
|
|
453
|
+
mutationId: "hm-tenant-present",
|
|
454
|
+
operation: "create",
|
|
455
|
+
})
|
|
456
|
+
.expect(200);
|
|
457
|
+
expect(res.body.ack.mutationId).toBe("hm-tenant-present");
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
it("returns the recorded ack for a duplicate delivery without re-applying", async () => {
|
|
461
|
+
const body = {
|
|
462
|
+
collection: "routeStuff",
|
|
463
|
+
data: {name: "http once", ownerId: notAdminId},
|
|
464
|
+
mutationId: "hm-dup-1",
|
|
465
|
+
operation: "create",
|
|
466
|
+
};
|
|
467
|
+
const first = await agent.post("/sync/mutate").send(body).expect(200);
|
|
468
|
+
const second = await agent.post("/sync/mutate").send(body).expect(200);
|
|
469
|
+
expect(second.body.ack).toEqual(first.body.ack);
|
|
470
|
+
expect(await RouteStuffModel.countDocuments({name: "http once"})).toBe(1);
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
it("returns 409 with the server doc on a stale baseVersion", async () => {
|
|
474
|
+
const doc = await RouteStuffModel.create({name: "server v1", ownerId: notAdminId});
|
|
475
|
+
doc.name = "server v2";
|
|
476
|
+
await doc.save(); // seq 2
|
|
477
|
+
|
|
478
|
+
const res = await agent
|
|
479
|
+
.post("/sync/mutate")
|
|
480
|
+
.send({
|
|
481
|
+
baseVersion: 1,
|
|
482
|
+
collection: "routeStuff",
|
|
483
|
+
data: {name: "stale"},
|
|
484
|
+
id: String(doc._id),
|
|
485
|
+
mutationId: "hm-conflict-1",
|
|
486
|
+
operation: "update",
|
|
487
|
+
})
|
|
488
|
+
.expect(409);
|
|
489
|
+
expect(res.body.nack.code).toBe("conflict");
|
|
490
|
+
expect(res.body.nack.serverSeq).toBe(2);
|
|
491
|
+
expect(res.body.nack.serverDoc.name).toBe("server v2");
|
|
492
|
+
const saved = await RouteStuffModel.findById(doc._id);
|
|
493
|
+
expect(saved?.name).toBe("server v2");
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
it("returns 403 with an unauthorized nack for permission denials", async () => {
|
|
497
|
+
clearSyncRegistry();
|
|
498
|
+
registerSync({
|
|
499
|
+
config: {scope: {type: "owner"}},
|
|
500
|
+
model: RouteStuffModel as any,
|
|
501
|
+
options: adminOnlyOptions,
|
|
502
|
+
routePath: "/routeStuff",
|
|
503
|
+
});
|
|
504
|
+
// Each caller writes to their OWN owner scope (C6 rejects a foreign ownerId with a
|
|
505
|
+
// separate scope-violation nack — exercised in the C6 tests, not here).
|
|
506
|
+
const body = (mutationId: string, ownerId: string) => ({
|
|
507
|
+
collection: "routeStuff",
|
|
508
|
+
data: {name: "admin only", ownerId},
|
|
509
|
+
mutationId,
|
|
510
|
+
operation: "create",
|
|
511
|
+
});
|
|
512
|
+
const res = await agent.post("/sync/mutate").send(body("hm-perm-1", notAdminId)).expect(403);
|
|
513
|
+
expect(res.body.nack.code).toBe("unauthorized");
|
|
514
|
+
await adminAgent.post("/sync/mutate").send(body("hm-perm-2", adminId)).expect(200);
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
it("returns 422 with a validation nack for invalid mutations", async () => {
|
|
518
|
+
const missingField = await agent
|
|
519
|
+
.post("/sync/mutate")
|
|
520
|
+
.send({
|
|
521
|
+
collection: "routeStuff",
|
|
522
|
+
data: {ownerId: notAdminId},
|
|
523
|
+
mutationId: "hm-invalid-1",
|
|
524
|
+
operation: "create",
|
|
525
|
+
})
|
|
526
|
+
.expect(422);
|
|
527
|
+
expect(missingField.body.nack.code).toBe("validation");
|
|
528
|
+
|
|
529
|
+
const unknownCollection = await agent
|
|
530
|
+
.post("/sync/mutate")
|
|
531
|
+
.send({
|
|
532
|
+
collection: "nope",
|
|
533
|
+
data: {name: "x"},
|
|
534
|
+
mutationId: "hm-invalid-2",
|
|
535
|
+
operation: "create",
|
|
536
|
+
})
|
|
537
|
+
.expect(422);
|
|
538
|
+
expect(unknownCollection.body.nack.code).toBe("validation");
|
|
539
|
+
|
|
540
|
+
const missingId = await agent
|
|
541
|
+
.post("/sync/mutate")
|
|
542
|
+
.send({
|
|
543
|
+
collection: "routeStuff",
|
|
544
|
+
data: {name: "x"},
|
|
545
|
+
mutationId: "hm-invalid-3",
|
|
546
|
+
operation: "update",
|
|
547
|
+
})
|
|
548
|
+
.expect(422);
|
|
549
|
+
expect(missingId.body.nack.code).toBe("validation");
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
it("returns 500 with an error nack for unexpected failures", async () => {
|
|
553
|
+
clearSyncRegistry();
|
|
554
|
+
registerSync({
|
|
555
|
+
config: {scope: {type: "owner"}},
|
|
556
|
+
model: RouteStuffModel as any,
|
|
557
|
+
options: {
|
|
558
|
+
...authedOptions,
|
|
559
|
+
preCreate: () => {
|
|
560
|
+
throw new APIError({status: 500, title: "database exploded"});
|
|
561
|
+
},
|
|
562
|
+
} as unknown as ModelRouterOptions<any>,
|
|
563
|
+
routePath: "/routeStuff",
|
|
564
|
+
});
|
|
565
|
+
const res = await agent
|
|
566
|
+
.post("/sync/mutate")
|
|
567
|
+
.send({
|
|
568
|
+
collection: "routeStuff",
|
|
569
|
+
data: {name: "boom", ownerId: notAdminId},
|
|
570
|
+
mutationId: "hm-error-1",
|
|
571
|
+
operation: "create",
|
|
572
|
+
})
|
|
573
|
+
.expect(500);
|
|
574
|
+
expect(res.body.nack.code).toBe("error");
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
it("returns 429 with a rate_limited nack carrying retryAfterMs once the per-second budget is exceeded (FIX 1)", async () => {
|
|
578
|
+
// notAdminId is fresh per test (setupDb() runs in beforeEach), so this
|
|
579
|
+
// user's rolling window starts empty here. Fire the budget-filling
|
|
580
|
+
// requests CONCURRENTLY (not sequentially awaited) so 100 requests
|
|
581
|
+
// cannot spread across the 1-second rolling window under machine load
|
|
582
|
+
// (a sequential loop's wall-clock time is unbounded and would let the
|
|
583
|
+
// window reset mid-flood, making the final request wrongly succeed).
|
|
584
|
+
const body = (mutationId: string) => ({
|
|
585
|
+
collection: "routeStuff",
|
|
586
|
+
data: {name: "flood", ownerId: notAdminId},
|
|
587
|
+
mutationId,
|
|
588
|
+
operation: "create",
|
|
589
|
+
});
|
|
590
|
+
await Promise.all(
|
|
591
|
+
Array.from({length: MAX_SYNC_HTTP_MUTATIONS_PER_SECOND}, (_v, i) =>
|
|
592
|
+
agent
|
|
593
|
+
.post("/sync/mutate")
|
|
594
|
+
.send(body(`hm-flood-${i}`))
|
|
595
|
+
.expect(200)
|
|
596
|
+
)
|
|
597
|
+
);
|
|
598
|
+
const res = await agent.post("/sync/mutate").send(body("hm-flood-over")).expect(429);
|
|
599
|
+
expect(res.body.nack.code).toBe("rate_limited");
|
|
600
|
+
expect(typeof res.body.nack.retryAfterMs).toBe("number");
|
|
601
|
+
expect(res.body.nack.retryAfterMs).toBeGreaterThan(0);
|
|
602
|
+
});
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
describe("POST /sync/mutate/batch", () => {
|
|
606
|
+
const create = (mutationId: string, name: string) => ({
|
|
607
|
+
collection: "routeStuff",
|
|
608
|
+
data: {name, ownerId: notAdminId},
|
|
609
|
+
mutationId,
|
|
610
|
+
operation: "create",
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
it("requires authentication", async () => {
|
|
614
|
+
await server
|
|
615
|
+
.post("/sync/mutate/batch")
|
|
616
|
+
.send({mutations: [create("batch-http-noauth", "x")]})
|
|
617
|
+
.expect(401);
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
it("applies mutations strictly in order and returns one ack per mutation", async () => {
|
|
621
|
+
const mutations = Array.from({length: 5}, (_v, i) => create(`batch-http-${i}`, `item ${i}`));
|
|
622
|
+
const res = await agent.post("/sync/mutate/batch").send({mutations}).expect(200);
|
|
623
|
+
expect(res.body.results).toHaveLength(5);
|
|
624
|
+
expect(res.body.results.every((r: any) => r.type === "ack")).toBe(true);
|
|
625
|
+
const seqs = res.body.results.map((r: any) => r.ack.seq);
|
|
626
|
+
expect(seqs).toEqual([1, 2, 3, 4, 5]);
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
it("stops at the first nack: results shorter than the request", async () => {
|
|
630
|
+
const mutations = [
|
|
631
|
+
create("batch-http-halt-1", "ok 1"),
|
|
632
|
+
{
|
|
633
|
+
collection: "routeStuff",
|
|
634
|
+
data: {name: "bad"},
|
|
635
|
+
mutationId: "batch-http-halt-2",
|
|
636
|
+
operation: "update", // no id supplied -> validation nack
|
|
637
|
+
},
|
|
638
|
+
create("batch-http-halt-3", "never applied"),
|
|
639
|
+
];
|
|
640
|
+
const res = await agent.post("/sync/mutate/batch").send({mutations}).expect(200);
|
|
641
|
+
expect(res.body.results).toHaveLength(2);
|
|
642
|
+
expect(res.body.results[0].type).toBe("ack");
|
|
643
|
+
expect(res.body.results[1].type).toBe("nack");
|
|
644
|
+
expect(res.body.results[1].nack.code).toBe("validation");
|
|
645
|
+
expect(await RouteStuffModel.countDocuments({name: "never applied"})).toBe(0);
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
it("rejects an oversized batch with a 422 before processing anything", async () => {
|
|
649
|
+
const mutations = Array.from({length: MAX_SYNC_MUTATIONS_PER_BATCH + 1}, (_v, i) =>
|
|
650
|
+
create(`batch-http-oversized-${i}`, `item ${i}`)
|
|
651
|
+
);
|
|
652
|
+
const res = await agent.post("/sync/mutate/batch").send({mutations}).expect(422);
|
|
653
|
+
expect(res.body.results).toHaveLength(1);
|
|
654
|
+
expect(res.body.results[0].nack.code).toBe("validation");
|
|
655
|
+
expect(await RouteStuffModel.countDocuments({})).toBe(0);
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
it("rejects intra-batch duplicate mutationIds with a 422", async () => {
|
|
659
|
+
const mutations = [create("batch-http-dup", "a"), create("batch-http-dup", "b")];
|
|
660
|
+
const res = await agent.post("/sync/mutate/batch").send({mutations}).expect(422);
|
|
661
|
+
expect(res.body.results[0].nack.code).toBe("validation");
|
|
662
|
+
expect(await RouteStuffModel.countDocuments({})).toBe(0);
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
it("a whole-batch duplicate resend is idempotent", async () => {
|
|
666
|
+
const mutations = [create("batch-http-idem-1", "once"), create("batch-http-idem-2", "twice")];
|
|
667
|
+
const first = await agent.post("/sync/mutate/batch").send({mutations}).expect(200);
|
|
668
|
+
const second = await agent.post("/sync/mutate/batch").send({mutations}).expect(200);
|
|
669
|
+
expect(second.body).toEqual(first.body);
|
|
670
|
+
expect(await RouteStuffModel.countDocuments({name: "once"})).toBe(1);
|
|
671
|
+
expect(await RouteStuffModel.countDocuments({name: "twice"})).toBe(1);
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
it("returns 429 with a rate_limited nack carrying retryAfterMs once the shared per-second budget is exceeded (FIX 1)", async () => {
|
|
675
|
+
// The batch route counts each mutation in the batch (not the batch
|
|
676
|
+
// itself) against the same per-user window as POST /sync/mutate. Two
|
|
677
|
+
// batches: the first (at the batch-size cap) nearly exhausts the
|
|
678
|
+
// per-second budget, the second (small) tips it over — a single batch
|
|
679
|
+
// over MAX_SYNC_HTTP_MUTATIONS_PER_SECOND would also exceed
|
|
680
|
+
// MAX_SYNC_MUTATIONS_PER_BATCH and get rejected as oversized first.
|
|
681
|
+
// A cheap validation nack (unknown collection, mirroring the sibling
|
|
682
|
+
// sync:mutateBatch rate-limit test) — no DB writes — keeps the first
|
|
683
|
+
// batch's wall-clock time well under the 1-second window even on a
|
|
684
|
+
// loaded machine, so it can't spuriously reset before the second
|
|
685
|
+
// request lands (100 real document creates would risk exactly that).
|
|
686
|
+
// The batch route always returns 200 with a `results` array (the
|
|
687
|
+
// client inspects each entry's type) — stop-on-first-nack means only
|
|
688
|
+
// the first mutation's validation nack comes back, but the FULL
|
|
689
|
+
// batch length (100) still counts against the rate-limit window
|
|
690
|
+
// (consumed before any mutation is applied).
|
|
691
|
+
const firstBatch = Array.from({length: MAX_SYNC_MUTATIONS_PER_BATCH}, (_v, i) => ({
|
|
692
|
+
collection: "nope",
|
|
693
|
+
mutationId: `batch-http-flood-a-${i}`,
|
|
694
|
+
operation: "create",
|
|
695
|
+
}));
|
|
696
|
+
const firstRes = await agent
|
|
697
|
+
.post("/sync/mutate/batch")
|
|
698
|
+
.send({mutations: firstBatch})
|
|
699
|
+
.expect(200);
|
|
700
|
+
expect(firstRes.body.results[0].nack.code).toBe("validation");
|
|
701
|
+
const secondBatch = [create("batch-http-flood-b-0", "over budget")];
|
|
702
|
+
const res = await agent.post("/sync/mutate/batch").send({mutations: secondBatch}).expect(429);
|
|
703
|
+
expect(res.body.results).toHaveLength(1);
|
|
704
|
+
expect(res.body.results[0].nack.code).toBe("rate_limited");
|
|
705
|
+
expect(typeof res.body.results[0].nack.retryAfterMs).toBe("number");
|
|
706
|
+
expect(res.body.results[0].nack.retryAfterMs).toBeGreaterThan(0);
|
|
707
|
+
expect(await RouteStuffModel.countDocuments({name: "over budget"})).toBe(0);
|
|
708
|
+
});
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
// Task 9.27(b): bad caller input on the repair-fetch endpoint used to come back as a 500
|
|
712
|
+
// (CastError) or, worse, a truncated 200 the client read as "those entities are gone".
|
|
713
|
+
describe("GET /sync/entities input validation (Task 9.27)", () => {
|
|
714
|
+
it("400s (not 500s) for ids that cannot be cast to the model's _id type", async () => {
|
|
715
|
+
const doc = await RouteStuffModel.create({name: "real", ownerId: notAdminId});
|
|
716
|
+
const res = await agent
|
|
717
|
+
.get(`/sync/entities?collection=routeStuff&ids=${doc._id},not-an-object-id`)
|
|
718
|
+
.expect(400);
|
|
719
|
+
expect(res.body.title).toMatch(/not-an-object-id/);
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
it("400s instead of silently truncating an over-cap id list", async () => {
|
|
723
|
+
const doc = await RouteStuffModel.create({name: "real", ownerId: notAdminId});
|
|
724
|
+
const ids = [
|
|
725
|
+
String(doc._id),
|
|
726
|
+
...Array.from({length: MAX_ENTITY_FETCH}, () => new Types.ObjectId().toString()),
|
|
727
|
+
];
|
|
728
|
+
const res = await agent
|
|
729
|
+
.get(`/sync/entities?collection=routeStuff&ids=${ids.join(",")}`)
|
|
730
|
+
.expect(400);
|
|
731
|
+
expect(res.body.title).toMatch(new RegExp(String(MAX_ENTITY_FETCH)));
|
|
732
|
+
|
|
733
|
+
// The cap itself still serves a full page.
|
|
734
|
+
const atCap = await agent
|
|
735
|
+
.get(`/sync/entities?collection=routeStuff&ids=${ids.slice(0, MAX_ENTITY_FETCH).join(",")}`)
|
|
736
|
+
.expect(200);
|
|
737
|
+
expect(atCap.body.entities.map((e: any) => e.id)).toEqual([String(doc._id)]);
|
|
738
|
+
});
|
|
739
|
+
});
|
|
740
|
+
|
|
741
|
+
describe("GET /sync/key", () => {
|
|
742
|
+
it("requires authentication", async () => {
|
|
743
|
+
await server.get("/sync/key").expect(401);
|
|
744
|
+
});
|
|
745
|
+
|
|
746
|
+
it("returns stable 32-byte key material per user", async () => {
|
|
747
|
+
const first = await agent.get("/sync/key").expect(200);
|
|
748
|
+
const second = await agent.get("/sync/key").expect(200);
|
|
749
|
+
expect(first.body.keyMaterial).toBe(second.body.keyMaterial);
|
|
750
|
+
expect(Buffer.from(first.body.keyMaterial, "base64")).toHaveLength(32);
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
it("gives different users different material", async () => {
|
|
754
|
+
const notAdminKey = await agent.get("/sync/key").expect(200);
|
|
755
|
+
const adminKey = await adminAgent.get("/sync/key").expect(200);
|
|
756
|
+
expect(notAdminKey.body.keyMaterial).not.toBe(adminKey.body.keyMaterial);
|
|
757
|
+
});
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
// Task 9.19: a broadcast-scoped collection contributes no scope clause of its own, so
|
|
761
|
+
// `queryFilter` is the ONLY thing standing between one user's rows and another's. These
|
|
762
|
+
// tests pin sync's read paths to the same visible row set the REST list endpoint serves.
|
|
763
|
+
describe("modelRouter queryFilter parity (Task 9.19)", () => {
|
|
764
|
+
const bannerStream = "routeBanners|all";
|
|
765
|
+
|
|
766
|
+
/** Register the banner model for sync + REST with the given queryFilter and re-auth. */
|
|
767
|
+
const setupBanners = async (
|
|
768
|
+
queryFilter: ModelRouterOptions<any>["queryFilter"]
|
|
769
|
+
): Promise<{bannerAgent: TestAgent}> => {
|
|
770
|
+
const bannerOptions = {...authedOptions, queryFilter} as ModelRouterOptions<any>;
|
|
771
|
+
clearSyncRegistry();
|
|
772
|
+
registerSync({
|
|
773
|
+
config: {scope: {type: "broadcast"}},
|
|
774
|
+
model: RouteBannerModel as any,
|
|
775
|
+
options: bannerOptions,
|
|
776
|
+
routePath: "/routeBanners",
|
|
777
|
+
});
|
|
778
|
+
const bannerApp = getBaseServer();
|
|
779
|
+
setupAuth(bannerApp as any, UserModel as any);
|
|
780
|
+
addAuthRoutes(bannerApp as any, UserModel as any);
|
|
781
|
+
bannerApp.use("/routeBanners", modelRouter(RouteBannerModel as any, bannerOptions));
|
|
782
|
+
new SyncApp().register(bannerApp);
|
|
783
|
+
return {bannerAgent: await authAsUser(bannerApp, "notAdmin")};
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
beforeEach(async () => {
|
|
787
|
+
await RouteBannerModel.collection.deleteMany({});
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
it("serves the same rows through the snapshot as the REST list endpoint", async () => {
|
|
791
|
+
const {bannerAgent} = await setupBanners(OwnerQueryFilter);
|
|
792
|
+
const mine = await RouteBannerModel.create({name: "mine", ownerId: notAdminId});
|
|
793
|
+
await RouteBannerModel.create({name: "theirs", ownerId: "someoneElse"});
|
|
794
|
+
|
|
795
|
+
const rest = await bannerAgent.get("/routeBanners").expect(200);
|
|
796
|
+
const restIds = rest.body.data.map((d: any) => String(d._id));
|
|
797
|
+
expect(restIds).toEqual([String(mine._id)]);
|
|
798
|
+
|
|
799
|
+
const snapshot = await bannerAgent
|
|
800
|
+
.get(`/sync/snapshot?stream=${encodeURIComponent(bannerStream)}`)
|
|
801
|
+
.expect(200);
|
|
802
|
+
expect(snapshot.body.entities.map((e: any) => e.id)).toEqual(restIds);
|
|
803
|
+
});
|
|
804
|
+
|
|
805
|
+
it("filters GET /sync/entities by the queryFilter", async () => {
|
|
806
|
+
const {bannerAgent} = await setupBanners(OwnerQueryFilter);
|
|
807
|
+
const mine = await RouteBannerModel.create({name: "mine", ownerId: notAdminId});
|
|
808
|
+
const theirs = await RouteBannerModel.create({name: "theirs", ownerId: "someoneElse"});
|
|
809
|
+
|
|
810
|
+
const res = await bannerAgent
|
|
811
|
+
.get(`/sync/entities?collection=routeBanners&ids=${mine._id},${theirs._id}`)
|
|
812
|
+
.expect(200);
|
|
813
|
+
expect(res.body.entities).toHaveLength(1);
|
|
814
|
+
expect(res.body.entities[0].id).toBe(String(mine._id));
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
it("serves a terminal empty page when the queryFilter denies the caller", async () => {
|
|
818
|
+
const {bannerAgent} = await setupBanners(() => null);
|
|
819
|
+
const banner = await RouteBannerModel.create({name: "hidden", ownerId: notAdminId});
|
|
820
|
+
|
|
821
|
+
const snapshot = await bannerAgent
|
|
822
|
+
.get(`/sync/snapshot?stream=${encodeURIComponent(bannerStream)}`)
|
|
823
|
+
.expect(200);
|
|
824
|
+
expect(snapshot.body.entities).toEqual([]);
|
|
825
|
+
expect(snapshot.body.hasMore).toBe(false);
|
|
826
|
+
|
|
827
|
+
const entities = await bannerAgent
|
|
828
|
+
.get(`/sync/entities?collection=routeBanners&ids=${banner._id}`)
|
|
829
|
+
.expect(200);
|
|
830
|
+
expect(entities.body.entities).toEqual([]);
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
it("denies the read (rather than serving everything) when the queryFilter throws", async () => {
|
|
834
|
+
const {bannerAgent} = await setupBanners(() => {
|
|
835
|
+
throw new Error("queryFilter exploded");
|
|
836
|
+
});
|
|
837
|
+
await RouteBannerModel.create({name: "hidden", ownerId: notAdminId});
|
|
838
|
+
|
|
839
|
+
const snapshot = await bannerAgent
|
|
840
|
+
.get(`/sync/snapshot?stream=${encodeURIComponent(bannerStream)}`)
|
|
841
|
+
.expect(200);
|
|
842
|
+
expect(snapshot.body.entities).toEqual([]);
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
it("composes a snapshotFilter with the scope clause instead of dropping it", async () => {
|
|
846
|
+
clearSyncRegistry();
|
|
847
|
+
registerSync({
|
|
848
|
+
config: {
|
|
849
|
+
scope: {type: "owner"},
|
|
850
|
+
snapshotFilter: () => ({name: "keep"}),
|
|
851
|
+
},
|
|
852
|
+
model: RouteStuffModel as any,
|
|
853
|
+
options: authedOptions,
|
|
854
|
+
routePath: "/routeStuff",
|
|
855
|
+
});
|
|
856
|
+
await RouteStuffModel.create({name: "keep", ownerId: notAdminId});
|
|
857
|
+
await RouteStuffModel.create({name: "drop", ownerId: notAdminId});
|
|
858
|
+
|
|
859
|
+
const res = await agent
|
|
860
|
+
.get(`/sync/snapshot?stream=${encodeURIComponent(`routeStuff|owner:${notAdminId}`)}`)
|
|
861
|
+
.expect(200);
|
|
862
|
+
expect(res.body.entities.map((e: any) => e.data.name)).toEqual(["keep"]);
|
|
863
|
+
});
|
|
864
|
+
});
|
|
865
|
+
});
|