@terreno/admin-backend 57.0.0 → 57.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/adminApp.d.ts +11 -4
- package/dist/adminApp.js +859 -263
- package/dist/adminConfigIdentity.d.ts +16 -0
- package/dist/adminConfigIdentity.js +100 -0
- package/dist/adminTextSearch.d.ts +17 -0
- package/dist/adminTextSearch.js +117 -0
- package/dist/adminUiV2.js +1 -1
- package/dist/aggregateAdmin.d.ts +20 -0
- package/dist/aggregateAdmin.js +275 -0
- package/dist/documentStorageApp.d.ts +3 -1
- package/dist/documentStorageApp.js +20 -42
- package/dist/filterParser.d.ts +11 -0
- package/dist/filterParser.js +260 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +15 -1
- package/dist/legacy.d.ts +9 -0
- package/dist/legacy.js +61 -0
- package/dist/resolvedAdminModel.d.ts +23 -0
- package/dist/resolvedAdminModel.js +130 -0
- package/dist/routePath.d.ts +2 -0
- package/dist/routePath.js +18 -0
- package/package.json +4 -3
- package/src/adminApp.models.test.ts +894 -1
- package/src/adminApp.test.ts +35 -1
- package/src/adminApp.ts +599 -98
- package/src/adminConfigIdentity.test.ts +56 -0
- package/src/adminConfigIdentity.ts +87 -0
- package/src/adminTextSearch.test.ts +73 -0
- package/src/adminTextSearch.ts +74 -0
- package/src/adminUiV2.test.ts +1 -0
- package/src/adminUiV2.ts +1 -1
- package/src/aggregateAdmin.test.ts +220 -0
- package/src/aggregateAdmin.ts +206 -0
- package/src/documentStorageApp.test.ts +11 -0
- package/src/documentStorageApp.ts +20 -39
- package/src/filterParser.test.ts +68 -0
- package/src/filterParser.ts +229 -0
- package/src/index.ts +35 -0
- package/src/legacy.test.ts +34 -0
- package/src/legacy.ts +63 -0
- package/src/resolvedAdminModel.ts +114 -0
- package/src/routePath.ts +14 -0
|
@@ -4,7 +4,13 @@ import {
|
|
|
4
4
|
apiErrorMiddleware,
|
|
5
5
|
apiUnauthorizedMiddleware,
|
|
6
6
|
BackgroundTask,
|
|
7
|
+
createAccess,
|
|
8
|
+
modelRouter,
|
|
9
|
+
Permissions,
|
|
7
10
|
setupAuth,
|
|
11
|
+
type TerrenoApp,
|
|
12
|
+
type TerrenoPlugin,
|
|
13
|
+
terrenoStatements,
|
|
8
14
|
type UserModel as UserModelType,
|
|
9
15
|
VersionConfig,
|
|
10
16
|
} from "@terreno/api";
|
|
@@ -108,6 +114,96 @@ describe("AdminApp /admin/config", () => {
|
|
|
108
114
|
expect(foodMeta.routePath).toBe("/admin/foods");
|
|
109
115
|
expect(foodMeta.defaultSort).toBe("-created");
|
|
110
116
|
expect(foodMeta.fieldOrder).toEqual(["name", "calories", "tags"]);
|
|
117
|
+
expect(res.body.capabilities).toEqual({
|
|
118
|
+
actions: true,
|
|
119
|
+
fieldsets: true,
|
|
120
|
+
filters: true,
|
|
121
|
+
realtime: false,
|
|
122
|
+
});
|
|
123
|
+
expect(res.body.widgetIds).toEqual([]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("exposes plugin widgetIds and aggregates models from TerrenoApp registrations", async () => {
|
|
127
|
+
const plugin: TerrenoPlugin = {
|
|
128
|
+
adminContribution: () => ({
|
|
129
|
+
homeWidgets: [{displayName: "Overrides", id: "feature-flags-overrides"}],
|
|
130
|
+
models: [
|
|
131
|
+
{
|
|
132
|
+
admin: {displayName: "Flags", listFields: ["key"]},
|
|
133
|
+
model: FoodModel,
|
|
134
|
+
routePath: "/feature-flags",
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
}),
|
|
138
|
+
register() {},
|
|
139
|
+
};
|
|
140
|
+
const terrenoApp = {
|
|
141
|
+
getPlugins: () => [plugin],
|
|
142
|
+
getRegistrations: () => [
|
|
143
|
+
modelRouter("/foods", FoodModel, {
|
|
144
|
+
admin: {displayName: "Registered Foods", listFields: ["name"]},
|
|
145
|
+
permissions: {
|
|
146
|
+
create: [Permissions.IsAny],
|
|
147
|
+
delete: [Permissions.IsAny],
|
|
148
|
+
list: [Permissions.IsAny],
|
|
149
|
+
read: [Permissions.IsAny],
|
|
150
|
+
update: [Permissions.IsAny],
|
|
151
|
+
},
|
|
152
|
+
}),
|
|
153
|
+
],
|
|
154
|
+
} as unknown as TerrenoApp;
|
|
155
|
+
|
|
156
|
+
const appWithAggregation = getBaseServer();
|
|
157
|
+
setupAuth(appWithAggregation, UserModel as unknown as UserModelType);
|
|
158
|
+
addAuthRoutes(appWithAggregation, UserModel as unknown as UserModelType);
|
|
159
|
+
new AdminApp({basePath: "/admin"}).register(appWithAggregation, undefined, terrenoApp);
|
|
160
|
+
appWithAggregation.use(apiUnauthorizedMiddleware);
|
|
161
|
+
appWithAggregation.use(apiErrorMiddleware);
|
|
162
|
+
|
|
163
|
+
const agent = await authAsUser(appWithAggregation, "admin");
|
|
164
|
+
const res = await agent.get("/admin/config").expect(200);
|
|
165
|
+
|
|
166
|
+
expect(res.body.widgetIds).toEqual(["feature-flags-overrides"]);
|
|
167
|
+
expect(res.body.models.map((m: {routePath: string}) => m.routePath).sort()).toEqual([
|
|
168
|
+
"/admin/feature-flags",
|
|
169
|
+
"/admin/foods",
|
|
170
|
+
]);
|
|
171
|
+
const foods = res.body.models.find((m: {routePath: string}) => m.routePath === "/admin/foods");
|
|
172
|
+
expect(foods?.displayName).toBe("Registered Foods");
|
|
173
|
+
expect(foods?.name).toBe("Food");
|
|
174
|
+
const flags = res.body.models.find(
|
|
175
|
+
(m: {routePath: string}) => m.routePath === "/admin/feature-flags"
|
|
176
|
+
);
|
|
177
|
+
expect(flags?.name).toBe("Food-feature-flags");
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("gives each mounted path unique config names and per-path searchFields", async () => {
|
|
181
|
+
const localApp = buildApp([
|
|
182
|
+
{
|
|
183
|
+
...foodModelConfig,
|
|
184
|
+
searchFields: ["name"],
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
displayName: "Archived Foods",
|
|
188
|
+
listFields: ["name", "calories"],
|
|
189
|
+
model: FoodModel,
|
|
190
|
+
routePath: "/archived-foods",
|
|
191
|
+
searchFields: [],
|
|
192
|
+
},
|
|
193
|
+
]);
|
|
194
|
+
await FoodModel.create({calories: 100, name: "GreenApple"});
|
|
195
|
+
await FoodModel.create({calories: 2, name: "Banana"});
|
|
196
|
+
const agent = await authAsUser(localApp, "admin");
|
|
197
|
+
const config = await agent.get("/admin/config").expect(200);
|
|
198
|
+
const names = config.body.models.map((m: {name: string}) => m.name).sort();
|
|
199
|
+
expect(names).toEqual(["Food", "Food-archived-foods"]);
|
|
200
|
+
|
|
201
|
+
const byName = await agent.get("/admin/foods?q=apple").expect(200);
|
|
202
|
+
expect(byName.body.data).toHaveLength(1);
|
|
203
|
+
expect(byName.body.data[0].name).toBe("GreenApple");
|
|
204
|
+
|
|
205
|
+
const archivedIgnoresNameSearch = await agent.get("/admin/archived-foods?q=apple").expect(200);
|
|
206
|
+
expect(archivedIgnoresNameSearch.body.data).toHaveLength(2);
|
|
111
207
|
});
|
|
112
208
|
|
|
113
209
|
it("includes recordTitleField in config when set on the model", async () => {
|
|
@@ -307,11 +403,12 @@ describe("AdminApp model CRUD routes", () => {
|
|
|
307
403
|
expect([401, 403, 405]).toContain(res.status);
|
|
308
404
|
});
|
|
309
405
|
|
|
310
|
-
it("
|
|
406
|
+
it("scrubs responses even when the parent model has no hidden or exclude fields", async () => {
|
|
311
407
|
app = buildApp([foodModelConfig]);
|
|
312
408
|
const agent = await authAsUser(app, "admin");
|
|
313
409
|
await FoodModel.create({calories: 120, hidden: true, name: "Apple"});
|
|
314
410
|
const res = await agent.get("/admin/foods").expect(200);
|
|
411
|
+
expect(res.body.data[0].name).toBe("Apple");
|
|
315
412
|
expect(res.body.data[0].hidden).toBe(true);
|
|
316
413
|
});
|
|
317
414
|
|
|
@@ -420,6 +517,47 @@ describe("AdminApp /admin/version-config", () => {
|
|
|
420
517
|
await notAdminAgent.put("/admin/version-config").send({}).expect(403);
|
|
421
518
|
});
|
|
422
519
|
|
|
520
|
+
it("requires configuration:update to PUT version-config when accessControl is set", async () => {
|
|
521
|
+
const accessControl = createAccess({
|
|
522
|
+
connection: mongoose.connection,
|
|
523
|
+
sources: [
|
|
524
|
+
{
|
|
525
|
+
getGrants: async () => ({permissions: {admin: ["access"]}}),
|
|
526
|
+
name: "admin-access-only",
|
|
527
|
+
},
|
|
528
|
+
],
|
|
529
|
+
statements: terrenoStatements,
|
|
530
|
+
});
|
|
531
|
+
await accessControl.roles.seedDefaults();
|
|
532
|
+
const localApp = buildApp([], {accessControl});
|
|
533
|
+
const agent = await authAsUser(localApp, "admin");
|
|
534
|
+
await agent.get("/admin/version-config").expect(403);
|
|
535
|
+
await agent.put("/admin/version-config").send({mobileRequiredVersion: 3}).expect(403);
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
it("allows configuration:update to PUT version-config", async () => {
|
|
539
|
+
const accessControl = createAccess({
|
|
540
|
+
connection: mongoose.connection,
|
|
541
|
+
sources: [
|
|
542
|
+
{
|
|
543
|
+
getGrants: async () => ({
|
|
544
|
+
permissions: {admin: ["access"], configuration: ["read", "update"]},
|
|
545
|
+
}),
|
|
546
|
+
name: "config-admin",
|
|
547
|
+
},
|
|
548
|
+
],
|
|
549
|
+
statements: terrenoStatements,
|
|
550
|
+
});
|
|
551
|
+
await accessControl.roles.seedDefaults();
|
|
552
|
+
const localApp = buildApp([], {accessControl});
|
|
553
|
+
const agent = await authAsUser(localApp, "admin");
|
|
554
|
+
const res = await agent
|
|
555
|
+
.put("/admin/version-config")
|
|
556
|
+
.send({mobileRequiredVersion: 3})
|
|
557
|
+
.expect(200);
|
|
558
|
+
expect(res.body.mobileRequiredVersion).toBe(3);
|
|
559
|
+
});
|
|
560
|
+
|
|
423
561
|
it("returns 401 for unauthenticated users", async () => {
|
|
424
562
|
await supertest(app).get("/admin/version-config").expect(401);
|
|
425
563
|
});
|
|
@@ -620,6 +758,42 @@ describe("AdminApp admin UI v2 routes", () => {
|
|
|
620
758
|
await notAdminAgent.post("/admin/background-tasks").send({kind: "x"}).expect(403);
|
|
621
759
|
});
|
|
622
760
|
|
|
761
|
+
it("requires admin:runScripts to enqueue background tasks when accessControl is set", async () => {
|
|
762
|
+
const accessControl = createAccess({
|
|
763
|
+
connection: mongoose.connection,
|
|
764
|
+
sources: [
|
|
765
|
+
{
|
|
766
|
+
getGrants: async () => ({permissions: {admin: ["access"]}}),
|
|
767
|
+
name: "admin-access-only",
|
|
768
|
+
},
|
|
769
|
+
],
|
|
770
|
+
statements: terrenoStatements,
|
|
771
|
+
});
|
|
772
|
+
await accessControl.roles.seedDefaults();
|
|
773
|
+
const localApp = buildApp([foodModelConfig], {accessControl});
|
|
774
|
+
const agent = await authAsUser(localApp, "admin");
|
|
775
|
+
await agent.post("/admin/background-tasks").send({kind: "reindex-search"}).expect(403);
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
it("allows admin:runScripts to enqueue background tasks", async () => {
|
|
779
|
+
const accessControl = createAccess({
|
|
780
|
+
connection: mongoose.connection,
|
|
781
|
+
sources: [
|
|
782
|
+
{
|
|
783
|
+
getGrants: async () => ({
|
|
784
|
+
permissions: {admin: ["access", "runScripts"]},
|
|
785
|
+
}),
|
|
786
|
+
name: "admin-run-scripts",
|
|
787
|
+
},
|
|
788
|
+
],
|
|
789
|
+
statements: terrenoStatements,
|
|
790
|
+
});
|
|
791
|
+
await accessControl.roles.seedDefaults();
|
|
792
|
+
const localApp = buildApp([foodModelConfig], {accessControl});
|
|
793
|
+
const agent = await authAsUser(localApp, "admin");
|
|
794
|
+
await agent.post("/admin/background-tasks").send({kind: "reindex-search"}).expect(201);
|
|
795
|
+
});
|
|
796
|
+
|
|
623
797
|
it("strips readonly fields from PATCH updates", async () => {
|
|
624
798
|
app = buildApp([
|
|
625
799
|
{
|
|
@@ -791,4 +965,723 @@ describe("AdminApp per-model queryFilter", () => {
|
|
|
791
965
|
expect(res.body.data).toHaveLength(1);
|
|
792
966
|
expect(res.body.data[0].name).toBe("FilteredOnly");
|
|
793
967
|
});
|
|
968
|
+
|
|
969
|
+
it("fails closed on list and mutating CRUD when the model is missing from RBAC statements", async () => {
|
|
970
|
+
await setupDb();
|
|
971
|
+
const accessControl = createAccess({
|
|
972
|
+
connection: mongoose.connection,
|
|
973
|
+
sources: [
|
|
974
|
+
{
|
|
975
|
+
getGrants: async () => ({permissions: {admin: ["access"]}}),
|
|
976
|
+
name: "admin-access",
|
|
977
|
+
},
|
|
978
|
+
],
|
|
979
|
+
statements: terrenoStatements,
|
|
980
|
+
});
|
|
981
|
+
await accessControl.roles.seedDefaults();
|
|
982
|
+
|
|
983
|
+
const localApp = buildApp([foodModelConfig], {accessControl});
|
|
984
|
+
const agent = await authAsUser(localApp, "admin");
|
|
985
|
+
|
|
986
|
+
await agent.get("/admin/foods").expect(405);
|
|
987
|
+
await agent.get("/admin/foods/search?q=x").expect(403);
|
|
988
|
+
await agent.post("/admin/foods").send({calories: 1, name: "Nope"}).expect(405);
|
|
989
|
+
});
|
|
990
|
+
|
|
991
|
+
it("applies queryFilter to admin search so autocomplete cannot leak out-of-scope rows", async () => {
|
|
992
|
+
const localApp = buildApp([
|
|
993
|
+
{
|
|
994
|
+
...foodModelConfig,
|
|
995
|
+
queryFilter: (_user, query): Record<string, unknown> => ({
|
|
996
|
+
...(query ?? {}),
|
|
997
|
+
name: "FilteredOnly",
|
|
998
|
+
}),
|
|
999
|
+
},
|
|
1000
|
+
]);
|
|
1001
|
+
await FoodModel.create({calories: 1, name: "OtherApple"});
|
|
1002
|
+
await FoodModel.create({calories: 2, name: "FilteredOnly"});
|
|
1003
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1004
|
+
const res = await agent.get("/admin/foods/search?q=e").expect(200);
|
|
1005
|
+
expect(res.body.data).toHaveLength(1);
|
|
1006
|
+
expect(res.body.data[0].name).toBe("FilteredOnly");
|
|
1007
|
+
});
|
|
1008
|
+
|
|
1009
|
+
it("accepts Mongo operators from queryFilter without treating them as client filters", async () => {
|
|
1010
|
+
const localApp = buildApp([
|
|
1011
|
+
{
|
|
1012
|
+
...foodModelConfig,
|
|
1013
|
+
queryFilter: (): Record<string, unknown> => ({
|
|
1014
|
+
$or: [{name: "Alpha"}, {name: "Beta"}],
|
|
1015
|
+
}),
|
|
1016
|
+
},
|
|
1017
|
+
]);
|
|
1018
|
+
await FoodModel.create({calories: 1, name: "Alpha"});
|
|
1019
|
+
await FoodModel.create({calories: 2, name: "Other"});
|
|
1020
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1021
|
+
const res = await agent.get("/admin/foods").expect(200);
|
|
1022
|
+
expect(res.body.data).toHaveLength(1);
|
|
1023
|
+
expect(res.body.data[0].name).toBe("Alpha");
|
|
1024
|
+
});
|
|
1025
|
+
|
|
1026
|
+
it("applies q against searchFields derived from listFields when none are configured", async () => {
|
|
1027
|
+
const localApp = buildApp([foodModelConfig]);
|
|
1028
|
+
await FoodModel.create({calories: 1, name: "GreenApple"});
|
|
1029
|
+
await FoodModel.create({calories: 2, name: "Banana"});
|
|
1030
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1031
|
+
const res = await agent.get("/admin/foods?q=apple").expect(200);
|
|
1032
|
+
expect(res.body.data).toHaveLength(1);
|
|
1033
|
+
expect(res.body.data[0].name).toBe("GreenApple");
|
|
1034
|
+
});
|
|
1035
|
+
|
|
1036
|
+
it("applies q as a case-insensitive partial match across searchFields", async () => {
|
|
1037
|
+
const localApp = buildApp([
|
|
1038
|
+
{
|
|
1039
|
+
...foodModelConfig,
|
|
1040
|
+
searchFields: ["name"],
|
|
1041
|
+
},
|
|
1042
|
+
]);
|
|
1043
|
+
await FoodModel.create({calories: 1, name: "GreenApple"});
|
|
1044
|
+
await FoodModel.create({calories: 2, name: "Banana"});
|
|
1045
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1046
|
+
const res = await agent.get("/admin/foods?q=apple").expect(200);
|
|
1047
|
+
expect(res.body.data).toHaveLength(1);
|
|
1048
|
+
expect(res.body.data[0].name).toBe("GreenApple");
|
|
1049
|
+
});
|
|
1050
|
+
|
|
1051
|
+
it("allows default -created sort even when created is not in listDisplay", async () => {
|
|
1052
|
+
const localApp = buildApp([
|
|
1053
|
+
{
|
|
1054
|
+
displayName: "Foods",
|
|
1055
|
+
listFields: ["name"],
|
|
1056
|
+
model: FoodModel,
|
|
1057
|
+
routePath: "/foods",
|
|
1058
|
+
},
|
|
1059
|
+
]);
|
|
1060
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1061
|
+
await agent.get("/admin/foods?sort=-created").expect(200);
|
|
1062
|
+
});
|
|
1063
|
+
|
|
1064
|
+
it("strips excludeFields from admin create and update bodies", async () => {
|
|
1065
|
+
const localApp = buildApp([
|
|
1066
|
+
{
|
|
1067
|
+
...foodModelConfig,
|
|
1068
|
+
excludeFields: ["calories"],
|
|
1069
|
+
},
|
|
1070
|
+
]);
|
|
1071
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1072
|
+
const created = await agent
|
|
1073
|
+
.post("/admin/foods")
|
|
1074
|
+
.send({calories: 999, name: "Stripped"})
|
|
1075
|
+
.expect(201);
|
|
1076
|
+
expect(created.body.data.calories).toBeUndefined();
|
|
1077
|
+
const createdId = created.body.data._id as string;
|
|
1078
|
+
const stored = await FoodModel.findById(createdId).lean();
|
|
1079
|
+
expect(stored?.calories).toBeUndefined();
|
|
1080
|
+
|
|
1081
|
+
await FoodModel.updateOne({_id: createdId}, {calories: 12});
|
|
1082
|
+
const patched = await agent
|
|
1083
|
+
.patch(`/admin/foods/${createdId}`)
|
|
1084
|
+
.send({calories: 50, name: "Renamed"})
|
|
1085
|
+
.expect(200);
|
|
1086
|
+
expect(patched.body.data.name).toBe("Renamed");
|
|
1087
|
+
const after = await FoodModel.findById(createdId).lean();
|
|
1088
|
+
expect(after?.calories).toBe(12);
|
|
1089
|
+
});
|
|
1090
|
+
});
|
|
1091
|
+
|
|
1092
|
+
describe("AdminApp user elevation and scoped bulk-patch", () => {
|
|
1093
|
+
beforeEach(async () => {
|
|
1094
|
+
await setupDb();
|
|
1095
|
+
});
|
|
1096
|
+
|
|
1097
|
+
afterEach(async () => {
|
|
1098
|
+
await FoodModel.deleteMany({});
|
|
1099
|
+
await VersionConfig.deleteMany({});
|
|
1100
|
+
});
|
|
1101
|
+
|
|
1102
|
+
it("allows admin CRUD to set the User admin flag", async () => {
|
|
1103
|
+
const localApp = buildApp([
|
|
1104
|
+
{
|
|
1105
|
+
displayName: "Users",
|
|
1106
|
+
listFields: ["email", "admin"],
|
|
1107
|
+
model: UserModel,
|
|
1108
|
+
routePath: "/users",
|
|
1109
|
+
},
|
|
1110
|
+
]);
|
|
1111
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1112
|
+
const target = await UserModel.findOne({email: "notAdmin@example.com"});
|
|
1113
|
+
expect(target).toBeTruthy();
|
|
1114
|
+
|
|
1115
|
+
await agent
|
|
1116
|
+
.patch(`/admin/users/${String(target?._id)}`)
|
|
1117
|
+
.send({admin: true})
|
|
1118
|
+
.expect(200);
|
|
1119
|
+
|
|
1120
|
+
const after = await UserModel.findById(target?._id);
|
|
1121
|
+
expect(after?.admin).toBe(true);
|
|
1122
|
+
});
|
|
1123
|
+
|
|
1124
|
+
it("requires rbac:assignRoles to set User admin when accessControl is enabled", async () => {
|
|
1125
|
+
const accessControl = createAccess({
|
|
1126
|
+
connection: mongoose.connection,
|
|
1127
|
+
sources: [
|
|
1128
|
+
{
|
|
1129
|
+
getGrants: async () => ({
|
|
1130
|
+
permissions: {
|
|
1131
|
+
admin: ["access"],
|
|
1132
|
+
user: ["create", "list", "read", "update"],
|
|
1133
|
+
},
|
|
1134
|
+
}),
|
|
1135
|
+
name: "admin-without-assign",
|
|
1136
|
+
},
|
|
1137
|
+
],
|
|
1138
|
+
statements: terrenoStatements,
|
|
1139
|
+
userModel: UserModel as unknown as UserModelType,
|
|
1140
|
+
});
|
|
1141
|
+
await accessControl.roles.seedDefaults();
|
|
1142
|
+
|
|
1143
|
+
const localApp = buildApp(
|
|
1144
|
+
[
|
|
1145
|
+
{
|
|
1146
|
+
displayName: "Users",
|
|
1147
|
+
listFields: ["email", "admin"],
|
|
1148
|
+
model: UserModel,
|
|
1149
|
+
routePath: "/users",
|
|
1150
|
+
},
|
|
1151
|
+
],
|
|
1152
|
+
{accessControl}
|
|
1153
|
+
);
|
|
1154
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1155
|
+
const target = await UserModel.findOne({email: "notAdmin@example.com"});
|
|
1156
|
+
expect(target).toBeTruthy();
|
|
1157
|
+
expect(target?.admin).toBe(false);
|
|
1158
|
+
|
|
1159
|
+
await agent
|
|
1160
|
+
.patch(`/admin/users/${String(target?._id)}`)
|
|
1161
|
+
.send({admin: true})
|
|
1162
|
+
.expect(403);
|
|
1163
|
+
|
|
1164
|
+
const afterDenied = await UserModel.findById(target?._id);
|
|
1165
|
+
expect(afterDenied?.admin).toBe(false);
|
|
1166
|
+
|
|
1167
|
+
await agent
|
|
1168
|
+
.patch(`/admin/users/${String(target?._id)}`)
|
|
1169
|
+
.send({admin: false, name: "Kept"})
|
|
1170
|
+
.expect(200);
|
|
1171
|
+
const afterEcho = await UserModel.findById(target?._id);
|
|
1172
|
+
expect(afterEcho?.admin).toBe(false);
|
|
1173
|
+
expect(afterEcho?.name).toBe("Kept");
|
|
1174
|
+
|
|
1175
|
+
const created = await agent
|
|
1176
|
+
.post("/admin/users")
|
|
1177
|
+
.send({admin: false, email: "form-echo-admin@example.com"})
|
|
1178
|
+
.expect(201);
|
|
1179
|
+
expect(created.body.data.admin).toBe(false);
|
|
1180
|
+
expect(await UserModel.findOne({email: "form-echo-admin@example.com"})).toBeTruthy();
|
|
1181
|
+
|
|
1182
|
+
await UserModel.updateOne({_id: target?._id}, {admin: true});
|
|
1183
|
+
await agent
|
|
1184
|
+
.patch(`/admin/users/${String(target?._id)}`)
|
|
1185
|
+
.send({admin: "false"})
|
|
1186
|
+
.expect(403);
|
|
1187
|
+
expect((await UserModel.findById(target?._id))?.admin).toBe(true);
|
|
1188
|
+
});
|
|
1189
|
+
|
|
1190
|
+
it("does not let assignRoles grant the legacy admin flag", async () => {
|
|
1191
|
+
const accessControl = createAccess({
|
|
1192
|
+
connection: mongoose.connection,
|
|
1193
|
+
sources: [
|
|
1194
|
+
{
|
|
1195
|
+
getGrants: async () => ({
|
|
1196
|
+
permissions: {
|
|
1197
|
+
admin: ["access"],
|
|
1198
|
+
rbac: ["assignRoles"],
|
|
1199
|
+
user: ["create", "list", "read", "update"],
|
|
1200
|
+
},
|
|
1201
|
+
}),
|
|
1202
|
+
name: "assigner-without-legacy-admin",
|
|
1203
|
+
},
|
|
1204
|
+
],
|
|
1205
|
+
statements: terrenoStatements,
|
|
1206
|
+
userModel: UserModel as unknown as UserModelType,
|
|
1207
|
+
});
|
|
1208
|
+
await accessControl.roles.seedDefaults();
|
|
1209
|
+
|
|
1210
|
+
const localApp = buildApp(
|
|
1211
|
+
[
|
|
1212
|
+
{
|
|
1213
|
+
displayName: "Users",
|
|
1214
|
+
listFields: ["email", "admin"],
|
|
1215
|
+
model: UserModel,
|
|
1216
|
+
routePath: "/users",
|
|
1217
|
+
},
|
|
1218
|
+
],
|
|
1219
|
+
{accessControl}
|
|
1220
|
+
);
|
|
1221
|
+
const agent = await authAsUser(localApp, "notAdmin");
|
|
1222
|
+
const target = await UserModel.findOne({email: "notAdmin@example.com"});
|
|
1223
|
+
expect(target?.admin).toBe(false);
|
|
1224
|
+
|
|
1225
|
+
await agent
|
|
1226
|
+
.patch(`/admin/users/${String(target?._id)}`)
|
|
1227
|
+
.send({admin: true})
|
|
1228
|
+
.expect(403);
|
|
1229
|
+
expect((await UserModel.findById(target?._id))?.admin).toBe(false);
|
|
1230
|
+
});
|
|
1231
|
+
|
|
1232
|
+
it("does not let assignRoles revoke the legacy admin flag", async () => {
|
|
1233
|
+
const accessControl = createAccess({
|
|
1234
|
+
connection: mongoose.connection,
|
|
1235
|
+
sources: [
|
|
1236
|
+
{
|
|
1237
|
+
getGrants: async () => ({
|
|
1238
|
+
permissions: {
|
|
1239
|
+
admin: ["access"],
|
|
1240
|
+
rbac: ["assignRoles"],
|
|
1241
|
+
user: ["create", "list", "read", "update"],
|
|
1242
|
+
},
|
|
1243
|
+
}),
|
|
1244
|
+
name: "assigner-without-legacy-admin",
|
|
1245
|
+
},
|
|
1246
|
+
],
|
|
1247
|
+
statements: terrenoStatements,
|
|
1248
|
+
userModel: UserModel as unknown as UserModelType,
|
|
1249
|
+
});
|
|
1250
|
+
await accessControl.roles.seedDefaults();
|
|
1251
|
+
|
|
1252
|
+
const localApp = buildApp(
|
|
1253
|
+
[
|
|
1254
|
+
{
|
|
1255
|
+
displayName: "Users",
|
|
1256
|
+
listFields: ["email", "admin"],
|
|
1257
|
+
model: UserModel,
|
|
1258
|
+
routePath: "/users",
|
|
1259
|
+
},
|
|
1260
|
+
],
|
|
1261
|
+
{accessControl}
|
|
1262
|
+
);
|
|
1263
|
+
const agent = await authAsUser(localApp, "notAdmin");
|
|
1264
|
+
const target = await UserModel.findOne({email: "admin@example.com"});
|
|
1265
|
+
expect(target?.admin).toBe(true);
|
|
1266
|
+
|
|
1267
|
+
await agent
|
|
1268
|
+
.patch(`/admin/users/${String(target?._id)}`)
|
|
1269
|
+
.send({admin: false})
|
|
1270
|
+
.expect(403);
|
|
1271
|
+
expect((await UserModel.findById(target?._id))?.admin).toBe(true);
|
|
1272
|
+
});
|
|
1273
|
+
|
|
1274
|
+
it("does not let manageRoles mint the legacy admin flag", async () => {
|
|
1275
|
+
const accessControl = createAccess({
|
|
1276
|
+
connection: mongoose.connection,
|
|
1277
|
+
sources: [
|
|
1278
|
+
{
|
|
1279
|
+
getGrants: async () => ({
|
|
1280
|
+
permissions: {
|
|
1281
|
+
admin: ["access"],
|
|
1282
|
+
rbac: ["assignRoles", "manageRoles"],
|
|
1283
|
+
user: ["create", "list", "read", "update"],
|
|
1284
|
+
},
|
|
1285
|
+
}),
|
|
1286
|
+
name: "manager-without-legacy-admin",
|
|
1287
|
+
},
|
|
1288
|
+
],
|
|
1289
|
+
statements: terrenoStatements,
|
|
1290
|
+
userModel: UserModel as unknown as UserModelType,
|
|
1291
|
+
});
|
|
1292
|
+
await accessControl.roles.seedDefaults();
|
|
1293
|
+
|
|
1294
|
+
const localApp = buildApp(
|
|
1295
|
+
[
|
|
1296
|
+
{
|
|
1297
|
+
displayName: "Users",
|
|
1298
|
+
listFields: ["email", "admin"],
|
|
1299
|
+
model: UserModel,
|
|
1300
|
+
routePath: "/users",
|
|
1301
|
+
},
|
|
1302
|
+
],
|
|
1303
|
+
{accessControl}
|
|
1304
|
+
);
|
|
1305
|
+
const agent = await authAsUser(localApp, "notAdmin");
|
|
1306
|
+
const target = await UserModel.findOne({email: "notAdmin@example.com"});
|
|
1307
|
+
expect(target?.admin).toBe(false);
|
|
1308
|
+
|
|
1309
|
+
await agent
|
|
1310
|
+
.patch(`/admin/users/${String(target?._id)}`)
|
|
1311
|
+
.send({admin: true})
|
|
1312
|
+
.expect(403);
|
|
1313
|
+
expect((await UserModel.findById(target?._id))?.admin).toBe(false);
|
|
1314
|
+
|
|
1315
|
+
await agent
|
|
1316
|
+
.post("/admin/users")
|
|
1317
|
+
.send({admin: true, email: "minted-admin@example.com"})
|
|
1318
|
+
.expect(403);
|
|
1319
|
+
expect(await UserModel.findOne({email: "minted-admin@example.com"})).toBeNull();
|
|
1320
|
+
});
|
|
1321
|
+
|
|
1322
|
+
it("does not let manageRoles revoke the legacy admin flag", async () => {
|
|
1323
|
+
const accessControl = createAccess({
|
|
1324
|
+
connection: mongoose.connection,
|
|
1325
|
+
sources: [
|
|
1326
|
+
{
|
|
1327
|
+
getGrants: async () => ({
|
|
1328
|
+
permissions: {
|
|
1329
|
+
admin: ["access"],
|
|
1330
|
+
rbac: ["assignRoles", "manageRoles"],
|
|
1331
|
+
user: ["create", "list", "read", "update"],
|
|
1332
|
+
},
|
|
1333
|
+
}),
|
|
1334
|
+
name: "manager-without-legacy-admin",
|
|
1335
|
+
},
|
|
1336
|
+
],
|
|
1337
|
+
statements: terrenoStatements,
|
|
1338
|
+
userModel: UserModel as unknown as UserModelType,
|
|
1339
|
+
});
|
|
1340
|
+
await accessControl.roles.seedDefaults();
|
|
1341
|
+
|
|
1342
|
+
const localApp = buildApp(
|
|
1343
|
+
[
|
|
1344
|
+
{
|
|
1345
|
+
displayName: "Users",
|
|
1346
|
+
listFields: ["email", "admin"],
|
|
1347
|
+
model: UserModel,
|
|
1348
|
+
routePath: "/users",
|
|
1349
|
+
},
|
|
1350
|
+
],
|
|
1351
|
+
{accessControl}
|
|
1352
|
+
);
|
|
1353
|
+
const agent = await authAsUser(localApp, "notAdmin");
|
|
1354
|
+
const target = await UserModel.findOne({email: "admin@example.com"});
|
|
1355
|
+
expect(target?.admin).toBe(true);
|
|
1356
|
+
|
|
1357
|
+
await agent
|
|
1358
|
+
.patch(`/admin/users/${String(target?._id)}`)
|
|
1359
|
+
.send({admin: false})
|
|
1360
|
+
.expect(403);
|
|
1361
|
+
expect((await UserModel.findById(target?._id))?.admin).toBe(true);
|
|
1362
|
+
});
|
|
1363
|
+
|
|
1364
|
+
it("does not persist organizationIds on RBAC admin User writes", async () => {
|
|
1365
|
+
const accessControl = createAccess({
|
|
1366
|
+
connection: mongoose.connection,
|
|
1367
|
+
sources: [
|
|
1368
|
+
{
|
|
1369
|
+
getGrants: async () => ({
|
|
1370
|
+
permissions: {
|
|
1371
|
+
admin: ["access"],
|
|
1372
|
+
rbac: ["assignRoles"],
|
|
1373
|
+
user: ["create", "list", "read", "update"],
|
|
1374
|
+
},
|
|
1375
|
+
}),
|
|
1376
|
+
name: "admin-user-editor",
|
|
1377
|
+
},
|
|
1378
|
+
],
|
|
1379
|
+
statements: terrenoStatements,
|
|
1380
|
+
userModel: UserModel as unknown as UserModelType,
|
|
1381
|
+
});
|
|
1382
|
+
await accessControl.roles.seedDefaults();
|
|
1383
|
+
|
|
1384
|
+
const localApp = buildApp(
|
|
1385
|
+
[
|
|
1386
|
+
{
|
|
1387
|
+
bulkPatchAllowlist: ["name", "organizationIds"],
|
|
1388
|
+
displayName: "Users",
|
|
1389
|
+
listFields: ["email", "admin"],
|
|
1390
|
+
model: UserModel,
|
|
1391
|
+
routePath: "/users",
|
|
1392
|
+
},
|
|
1393
|
+
],
|
|
1394
|
+
{accessControl}
|
|
1395
|
+
);
|
|
1396
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1397
|
+
const target = await UserModel.findOne({email: "notAdmin@example.com"});
|
|
1398
|
+
expect(target).toBeTruthy();
|
|
1399
|
+
|
|
1400
|
+
await agent
|
|
1401
|
+
.patch(`/admin/users/${String(target?._id)}`)
|
|
1402
|
+
.send({name: "Org-stripped", organizationIds: ["other-tenant"]})
|
|
1403
|
+
.expect(200);
|
|
1404
|
+
const afterPatch = await UserModel.findById(target?._id);
|
|
1405
|
+
expect(afterPatch?.name).toBe("Org-stripped");
|
|
1406
|
+
expect(
|
|
1407
|
+
(afterPatch as {organizationIds?: string[]} | null)?.organizationIds ?? []
|
|
1408
|
+
).not.toContain("other-tenant");
|
|
1409
|
+
|
|
1410
|
+
const created = await agent
|
|
1411
|
+
.post("/admin/users")
|
|
1412
|
+
.send({email: "org-stripped@example.com", organizationIds: ["other-tenant"]})
|
|
1413
|
+
.expect(201);
|
|
1414
|
+
expect(created.body.data.organizationIds ?? []).not.toContain("other-tenant");
|
|
1415
|
+
const createdDoc = await UserModel.findOne({email: "org-stripped@example.com"});
|
|
1416
|
+
expect(
|
|
1417
|
+
(createdDoc as {organizationIds?: string[]} | null)?.organizationIds ?? []
|
|
1418
|
+
).not.toContain("other-tenant");
|
|
1419
|
+
|
|
1420
|
+
await agent
|
|
1421
|
+
.post("/admin/users/bulk-patch")
|
|
1422
|
+
.send({
|
|
1423
|
+
ids: [String(target?._id)],
|
|
1424
|
+
patch: {organizationIds: ["bulk-tenant"]},
|
|
1425
|
+
})
|
|
1426
|
+
.expect(400);
|
|
1427
|
+
expect(
|
|
1428
|
+
((await UserModel.findById(target?._id)) as {organizationIds?: string[]} | null)
|
|
1429
|
+
?.organizationIds ?? []
|
|
1430
|
+
).not.toContain("bulk-tenant");
|
|
1431
|
+
});
|
|
1432
|
+
|
|
1433
|
+
it("checks each bulk-patch target document against RBAC scopes", async () => {
|
|
1434
|
+
const foodStatements = {
|
|
1435
|
+
...terrenoStatements,
|
|
1436
|
+
food: ["create", "list", "read", "update", "delete"],
|
|
1437
|
+
} as const;
|
|
1438
|
+
const accessControl = createAccess({
|
|
1439
|
+
connection: mongoose.connection,
|
|
1440
|
+
scopes: {
|
|
1441
|
+
"food.update": {
|
|
1442
|
+
check: ({doc, user}) => {
|
|
1443
|
+
if (!doc) {
|
|
1444
|
+
return true;
|
|
1445
|
+
}
|
|
1446
|
+
const ownerId = (doc as {ownerId?: unknown}).ownerId;
|
|
1447
|
+
return String(ownerId) === String(user.id);
|
|
1448
|
+
},
|
|
1449
|
+
},
|
|
1450
|
+
},
|
|
1451
|
+
sources: [
|
|
1452
|
+
{
|
|
1453
|
+
getGrants: async () => ({
|
|
1454
|
+
permissions: {admin: ["access"], food: ["list", "read", "update"]},
|
|
1455
|
+
}),
|
|
1456
|
+
name: "scoped-food-update",
|
|
1457
|
+
},
|
|
1458
|
+
],
|
|
1459
|
+
statements: foodStatements,
|
|
1460
|
+
});
|
|
1461
|
+
await accessControl.roles.seedDefaults();
|
|
1462
|
+
|
|
1463
|
+
const localApp = buildApp([{...foodModelConfig, bulkPatchAllowlist: ["calories", "name"]}], {
|
|
1464
|
+
accessControl,
|
|
1465
|
+
});
|
|
1466
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1467
|
+
const actor = await UserModel.findOne({email: "admin@example.com"});
|
|
1468
|
+
const other = await UserModel.findOne({email: "notAdmin@example.com"});
|
|
1469
|
+
expect(actor).toBeTruthy();
|
|
1470
|
+
expect(other).toBeTruthy();
|
|
1471
|
+
|
|
1472
|
+
const owned = await FoodModel.create({
|
|
1473
|
+
calories: 1,
|
|
1474
|
+
name: "Owned",
|
|
1475
|
+
ownerId: actor?._id,
|
|
1476
|
+
});
|
|
1477
|
+
const foreign = await FoodModel.create({
|
|
1478
|
+
calories: 2,
|
|
1479
|
+
name: "Foreign",
|
|
1480
|
+
ownerId: other?._id,
|
|
1481
|
+
});
|
|
1482
|
+
|
|
1483
|
+
const res = await agent
|
|
1484
|
+
.post("/admin/foods/bulk-patch")
|
|
1485
|
+
.send({
|
|
1486
|
+
ids: [String(owned._id), String(foreign._id)],
|
|
1487
|
+
patch: {calories: 50},
|
|
1488
|
+
})
|
|
1489
|
+
.expect(200);
|
|
1490
|
+
|
|
1491
|
+
expect(res.body.updated).toBe(1);
|
|
1492
|
+
expect(res.body.failures).toEqual([{id: String(foreign._id), title: "Forbidden"}]);
|
|
1493
|
+
expect((await FoodModel.findById(owned._id).lean())?.calories).toBe(50);
|
|
1494
|
+
expect((await FoodModel.findById(foreign._id).lean())?.calories).toBe(2);
|
|
1495
|
+
});
|
|
1496
|
+
|
|
1497
|
+
it("returns assigned User roles in the admin update response", async () => {
|
|
1498
|
+
if (!UserModel.schema.path("roles")) {
|
|
1499
|
+
UserModel.schema.add({
|
|
1500
|
+
roles: {default: [], description: "RBAC role names", type: [String]},
|
|
1501
|
+
});
|
|
1502
|
+
}
|
|
1503
|
+
const accessControl = createAccess({
|
|
1504
|
+
connection: mongoose.connection,
|
|
1505
|
+
sources: [
|
|
1506
|
+
{
|
|
1507
|
+
getGrants: async () => ({
|
|
1508
|
+
permissions: {
|
|
1509
|
+
admin: ["access"],
|
|
1510
|
+
rbac: ["assignRoles", "manageRoles"],
|
|
1511
|
+
user: ["create", "list", "read", "update"],
|
|
1512
|
+
},
|
|
1513
|
+
}),
|
|
1514
|
+
name: "admin-user-role-editor",
|
|
1515
|
+
},
|
|
1516
|
+
],
|
|
1517
|
+
statements: terrenoStatements,
|
|
1518
|
+
userModel: UserModel as unknown as UserModelType,
|
|
1519
|
+
});
|
|
1520
|
+
await accessControl.roles.seedDefaults();
|
|
1521
|
+
|
|
1522
|
+
const localApp = buildApp(
|
|
1523
|
+
[
|
|
1524
|
+
{
|
|
1525
|
+
displayName: "Users",
|
|
1526
|
+
listFields: ["email", "roles"],
|
|
1527
|
+
model: UserModel,
|
|
1528
|
+
routePath: "/users",
|
|
1529
|
+
},
|
|
1530
|
+
],
|
|
1531
|
+
{accessControl}
|
|
1532
|
+
);
|
|
1533
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1534
|
+
const target = await UserModel.findOne({email: "notAdmin@example.com"});
|
|
1535
|
+
expect(target).toBeTruthy();
|
|
1536
|
+
|
|
1537
|
+
const response = await agent
|
|
1538
|
+
.patch(`/admin/users/${String(target?._id)}`)
|
|
1539
|
+
.send({roles: ["member"]})
|
|
1540
|
+
.expect(200);
|
|
1541
|
+
|
|
1542
|
+
expect(response.body.data.roles).toEqual(["member"]);
|
|
1543
|
+
});
|
|
1544
|
+
|
|
1545
|
+
it("deletes a newly created User when role assignment fails", async () => {
|
|
1546
|
+
const accessControl = createAccess({
|
|
1547
|
+
connection: mongoose.connection,
|
|
1548
|
+
sources: [
|
|
1549
|
+
{
|
|
1550
|
+
getGrants: async () => ({
|
|
1551
|
+
permissions: {
|
|
1552
|
+
admin: ["access"],
|
|
1553
|
+
rbac: ["assignRoles"],
|
|
1554
|
+
user: ["create", "list", "read", "update"],
|
|
1555
|
+
},
|
|
1556
|
+
}),
|
|
1557
|
+
name: "admin-user-create",
|
|
1558
|
+
},
|
|
1559
|
+
],
|
|
1560
|
+
statements: terrenoStatements,
|
|
1561
|
+
userModel: UserModel as unknown as UserModelType,
|
|
1562
|
+
});
|
|
1563
|
+
await accessControl.roles.seedDefaults();
|
|
1564
|
+
|
|
1565
|
+
const localApp = buildApp(
|
|
1566
|
+
[
|
|
1567
|
+
{
|
|
1568
|
+
displayName: "Users",
|
|
1569
|
+
listFields: ["email"],
|
|
1570
|
+
model: UserModel,
|
|
1571
|
+
routePath: "/users",
|
|
1572
|
+
},
|
|
1573
|
+
],
|
|
1574
|
+
{accessControl}
|
|
1575
|
+
);
|
|
1576
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1577
|
+
const email = "rollback-roles@example.com";
|
|
1578
|
+
|
|
1579
|
+
const res = await agent.post("/admin/users").send({email, roles: ["does-not-exist"]});
|
|
1580
|
+
expect(res.status).toBeGreaterThanOrEqual(400);
|
|
1581
|
+
expect(await UserModel.findOne({email})).toBeNull();
|
|
1582
|
+
});
|
|
1583
|
+
|
|
1584
|
+
it("does not persist other User fields when role assignment fails on update", async () => {
|
|
1585
|
+
if (!UserModel.schema.path("roles")) {
|
|
1586
|
+
UserModel.schema.add({
|
|
1587
|
+
roles: {default: [], description: "RBAC role names", type: [String]},
|
|
1588
|
+
});
|
|
1589
|
+
}
|
|
1590
|
+
const accessControl = createAccess({
|
|
1591
|
+
connection: mongoose.connection,
|
|
1592
|
+
sources: [
|
|
1593
|
+
{
|
|
1594
|
+
getGrants: async () => ({
|
|
1595
|
+
permissions: {
|
|
1596
|
+
admin: ["access"],
|
|
1597
|
+
rbac: ["assignRoles"],
|
|
1598
|
+
user: ["create", "list", "read", "update"],
|
|
1599
|
+
},
|
|
1600
|
+
}),
|
|
1601
|
+
name: "admin-user-update-rollback",
|
|
1602
|
+
},
|
|
1603
|
+
],
|
|
1604
|
+
statements: terrenoStatements,
|
|
1605
|
+
userModel: UserModel as unknown as UserModelType,
|
|
1606
|
+
});
|
|
1607
|
+
await accessControl.roles.seedDefaults();
|
|
1608
|
+
|
|
1609
|
+
const localApp = buildApp(
|
|
1610
|
+
[
|
|
1611
|
+
{
|
|
1612
|
+
displayName: "Users",
|
|
1613
|
+
listFields: ["email", "name", "roles"],
|
|
1614
|
+
model: UserModel,
|
|
1615
|
+
routePath: "/users",
|
|
1616
|
+
},
|
|
1617
|
+
],
|
|
1618
|
+
{accessControl}
|
|
1619
|
+
);
|
|
1620
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1621
|
+
const target = await UserModel.findOne({email: "notAdmin@example.com"});
|
|
1622
|
+
expect(target).toBeTruthy();
|
|
1623
|
+
const originalName = target?.name;
|
|
1624
|
+
|
|
1625
|
+
const res = await agent
|
|
1626
|
+
.patch(`/admin/users/${String(target?._id)}`)
|
|
1627
|
+
.send({name: "Should-not-stick", roles: ["does-not-exist"]});
|
|
1628
|
+
expect(res.status).toBeGreaterThanOrEqual(400);
|
|
1629
|
+
|
|
1630
|
+
const after = await UserModel.findById(target?._id);
|
|
1631
|
+
expect(after?.name).toBe(originalName);
|
|
1632
|
+
});
|
|
1633
|
+
|
|
1634
|
+
it("does not persist bulk-patch fields when role assignment fails", async () => {
|
|
1635
|
+
if (!UserModel.schema.path("roles")) {
|
|
1636
|
+
UserModel.schema.add({
|
|
1637
|
+
roles: {default: [], description: "RBAC role names", type: [String]},
|
|
1638
|
+
});
|
|
1639
|
+
}
|
|
1640
|
+
const accessControl = createAccess({
|
|
1641
|
+
connection: mongoose.connection,
|
|
1642
|
+
sources: [
|
|
1643
|
+
{
|
|
1644
|
+
getGrants: async () => ({
|
|
1645
|
+
permissions: {
|
|
1646
|
+
admin: ["access"],
|
|
1647
|
+
rbac: ["assignRoles"],
|
|
1648
|
+
user: ["create", "list", "read", "update"],
|
|
1649
|
+
},
|
|
1650
|
+
}),
|
|
1651
|
+
name: "admin-user-bulk-rollback",
|
|
1652
|
+
},
|
|
1653
|
+
],
|
|
1654
|
+
statements: terrenoStatements,
|
|
1655
|
+
userModel: UserModel as unknown as UserModelType,
|
|
1656
|
+
});
|
|
1657
|
+
await accessControl.roles.seedDefaults();
|
|
1658
|
+
|
|
1659
|
+
const localApp = buildApp(
|
|
1660
|
+
[
|
|
1661
|
+
{
|
|
1662
|
+
bulkPatchAllowlist: ["name", "roles"],
|
|
1663
|
+
displayName: "Users",
|
|
1664
|
+
listFields: ["email", "name", "roles"],
|
|
1665
|
+
model: UserModel,
|
|
1666
|
+
routePath: "/users",
|
|
1667
|
+
},
|
|
1668
|
+
],
|
|
1669
|
+
{accessControl}
|
|
1670
|
+
);
|
|
1671
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1672
|
+
const target = await UserModel.findOne({email: "notAdmin@example.com"});
|
|
1673
|
+
expect(target).toBeTruthy();
|
|
1674
|
+
const originalName = target?.name;
|
|
1675
|
+
|
|
1676
|
+
const res = await agent.post("/admin/users/bulk-patch").send({
|
|
1677
|
+
ids: [String(target?._id)],
|
|
1678
|
+
patch: {name: "Should-not-stick", roles: ["does-not-exist"]},
|
|
1679
|
+
});
|
|
1680
|
+
expect(res.status).toBe(200);
|
|
1681
|
+
expect(res.body.updated).toBe(0);
|
|
1682
|
+
expect(res.body.failures?.length).toBeGreaterThan(0);
|
|
1683
|
+
|
|
1684
|
+
const after = await UserModel.findById(target?._id);
|
|
1685
|
+
expect(after?.name).toBe(originalName);
|
|
1686
|
+
});
|
|
794
1687
|
});
|