@terreno/admin-backend 57.2.0 → 57.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ai/guidelines/core.md +12 -4
- package/.claude/CLAUDE.local.md +17 -2
- package/.cursor/rules/00-root.mdc +17 -2
- package/.github/copilot-instructions.md +17 -2
- package/AGENTS.md +17 -2
- package/README.md +3 -1
- package/bunfig.toml +9 -1
- package/dist/adminApp.d.ts +20 -2
- package/dist/adminApp.js +480 -474
- package/dist/adminConfigIdentity.d.ts +1 -0
- package/dist/adminConfigIdentity.js +1 -0
- package/dist/adminMigrations.d.ts +10 -0
- package/dist/adminMigrations.js +205 -0
- package/dist/adminScriptJob.d.ts +32 -0
- package/dist/adminScriptJob.js +360 -0
- package/dist/adminTextSearch.d.ts +1 -0
- package/dist/adminTextSearch.js +1 -0
- package/dist/adminUiV2.d.ts +2 -0
- package/dist/aggregateAdmin.js +6 -1
- package/dist/documentStorageApp.js +1 -4
- package/dist/enrichAdminListFilters.d.ts +7 -0
- package/dist/enrichAdminListFilters.js +37 -0
- package/dist/filterParser.js +139 -12
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -1
- package/dist/legacy.js +4 -2
- package/dist/resolvedAdminModel.js +2 -1
- package/package.json +8 -8
- package/src/adminApp.migrations.test.ts +232 -0
- package/src/adminApp.models.test.ts +647 -5
- package/src/adminApp.syncMeta.test.ts +237 -0
- package/src/adminApp.test.ts +92 -0
- package/src/adminApp.ts +354 -240
- package/src/adminConfigIdentity.ts +1 -0
- package/src/adminHttpGoldens.test.ts +337 -0
- package/src/adminMigrations.ts +157 -0
- package/src/adminScriptJob.test.ts +297 -0
- package/src/adminScriptJob.ts +271 -0
- package/src/adminSyncMutationAuthMismatch.test.ts +650 -0
- package/src/adminTextSearch.test.ts +28 -0
- package/src/adminTextSearch.ts +1 -0
- package/src/adminUiV2.test.ts +71 -1
- package/src/adminUiV2.ts +2 -0
- package/src/aggregateAdmin.test.ts +125 -0
- package/src/aggregateAdmin.ts +7 -4
- package/src/documentStorageApp.ts +1 -3
- package/src/enrichAdminListFilters.test.ts +44 -0
- package/src/enrichAdminListFilters.ts +29 -0
- package/src/filterParser.test.ts +95 -0
- package/src/filterParser.ts +156 -10
- package/src/index.ts +2 -0
- package/src/legacy.ts +5 -1
- package/src/resolvedAdminModel.ts +2 -1
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
import {afterEach, beforeEach, describe, expect, it} from "bun:test";
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, spyOn} from "bun:test";
|
|
2
2
|
import {
|
|
3
3
|
ADMIN_MODEL_ACCESS,
|
|
4
|
+
AuditApp,
|
|
4
5
|
addAuthRoutes,
|
|
5
6
|
apiErrorMiddleware,
|
|
6
7
|
apiUnauthorizedMiddleware,
|
|
7
8
|
BackgroundTask,
|
|
8
9
|
createAccess,
|
|
9
10
|
findOneOrNoneFor,
|
|
11
|
+
flushAuditRecorderForTests,
|
|
12
|
+
Membership,
|
|
10
13
|
modelRouter,
|
|
14
|
+
Organization,
|
|
15
|
+
orgScopedPlugin,
|
|
11
16
|
Permissions,
|
|
17
|
+
resetAuditRecorderForTests,
|
|
12
18
|
setupAuth,
|
|
13
19
|
type TerrenoApp,
|
|
14
20
|
type TerrenoPlugin,
|
|
@@ -70,6 +76,24 @@ const enumArraySchema = new mongoose.Schema({
|
|
|
70
76
|
const EnumArrayModel =
|
|
71
77
|
mongoose.models.AdminEnumArray ?? mongoose.model("AdminEnumArray", enumArraySchema);
|
|
72
78
|
|
|
79
|
+
const orgScopedFoodSchema = new mongoose.Schema(
|
|
80
|
+
{
|
|
81
|
+
calories: {description: "Calories", type: Number},
|
|
82
|
+
name: {description: "Food name", type: String},
|
|
83
|
+
},
|
|
84
|
+
{strict: "throw"}
|
|
85
|
+
);
|
|
86
|
+
orgScopedFoodSchema.plugin(orgScopedPlugin);
|
|
87
|
+
const OrgScopedFoodModel =
|
|
88
|
+
mongoose.models.AdminOrgScopedFood ?? mongoose.model("AdminOrgScopedFood", orgScopedFoodSchema);
|
|
89
|
+
|
|
90
|
+
const priorityTodoSchema = new mongoose.Schema({
|
|
91
|
+
priority: {enum: ["low", "high"], type: String},
|
|
92
|
+
title: {required: true, type: String},
|
|
93
|
+
});
|
|
94
|
+
const PriorityTodoModel =
|
|
95
|
+
mongoose.models.AdminPriorityTodo ?? mongoose.model("AdminPriorityTodo", priorityTodoSchema);
|
|
96
|
+
|
|
73
97
|
describe("getArrayEmbeddedSchemaType", () => {
|
|
74
98
|
it("supports the legacy Mongoose 8 caster property", () => {
|
|
75
99
|
const embeddedSchemaType = new mongoose.Schema({value: String}).path("value");
|
|
@@ -282,6 +306,23 @@ describe("AdminApp /admin/config", () => {
|
|
|
282
306
|
expect(res.body.models[0].fields.values.itemEnum).toEqual(["alpha", "beta"]);
|
|
283
307
|
});
|
|
284
308
|
|
|
309
|
+
it("accepts enum array values on admin create", async () => {
|
|
310
|
+
const enumApp = buildApp([
|
|
311
|
+
{
|
|
312
|
+
displayName: "Enum arrays",
|
|
313
|
+
listFields: ["values"],
|
|
314
|
+
model: EnumArrayModel,
|
|
315
|
+
routePath: "/enum-arrays",
|
|
316
|
+
},
|
|
317
|
+
]);
|
|
318
|
+
const enumAgent = await authAsUser(enumApp, "admin");
|
|
319
|
+
const res = await enumAgent
|
|
320
|
+
.post("/admin/enum-arrays")
|
|
321
|
+
.send({values: ["alpha", "beta"]})
|
|
322
|
+
.expect(201);
|
|
323
|
+
expect(res.body.data.values).toEqual(["alpha", "beta"]);
|
|
324
|
+
});
|
|
325
|
+
|
|
285
326
|
it("extracts itemType and itemRef for arrays of ObjectId references", async () => {
|
|
286
327
|
const res = await adminAgent.get("/admin/config").expect(200);
|
|
287
328
|
const [foodMeta] = res.body.models;
|
|
@@ -320,11 +361,9 @@ describe("AdminApp /admin/config", () => {
|
|
|
320
361
|
expect(res.body.models[0].defaultSort).toBe("name");
|
|
321
362
|
});
|
|
322
363
|
|
|
323
|
-
it("
|
|
364
|
+
it("does not inject version config into the custom screen navigation", async () => {
|
|
324
365
|
const res = await adminAgent.get("/admin/config").expect(200);
|
|
325
|
-
expect(res.body.customScreens).toEqual([
|
|
326
|
-
{displayName: "Version Config", name: "version-config"},
|
|
327
|
-
]);
|
|
366
|
+
expect(res.body.customScreens).toEqual([]);
|
|
328
367
|
});
|
|
329
368
|
|
|
330
369
|
it("normalizes home.slots so recentActivity is last in sidebar", async () => {
|
|
@@ -779,6 +818,7 @@ describe("AdminApp model CRUD routes", () => {
|
|
|
779
818
|
|
|
780
819
|
afterEach(async () => {
|
|
781
820
|
await FoodModel.deleteMany({});
|
|
821
|
+
await PriorityTodoModel.deleteMany({});
|
|
782
822
|
});
|
|
783
823
|
|
|
784
824
|
it("creates documents via POST and strips hidden fields from response", async () => {
|
|
@@ -803,6 +843,249 @@ describe("AdminApp model CRUD routes", () => {
|
|
|
803
843
|
}
|
|
804
844
|
});
|
|
805
845
|
|
|
846
|
+
it("applies declared multi-choice filters through the admin list route", async () => {
|
|
847
|
+
app = buildApp([
|
|
848
|
+
{
|
|
849
|
+
...foodModelConfig,
|
|
850
|
+
filters: [
|
|
851
|
+
{
|
|
852
|
+
choices: [
|
|
853
|
+
{label: "Apple", value: "Apple"},
|
|
854
|
+
{label: "Banana", value: "Banana"},
|
|
855
|
+
{label: "Carrot", value: "Carrot"},
|
|
856
|
+
],
|
|
857
|
+
field: "name",
|
|
858
|
+
kind: "choice",
|
|
859
|
+
},
|
|
860
|
+
],
|
|
861
|
+
},
|
|
862
|
+
]);
|
|
863
|
+
const agent = await authAsUser(app, "admin");
|
|
864
|
+
await FoodModel.create({calories: 120, name: "Apple"});
|
|
865
|
+
await FoodModel.create({calories: 95, name: "Banana"});
|
|
866
|
+
await FoodModel.create({calories: 40, name: "Carrot"});
|
|
867
|
+
|
|
868
|
+
const query = new URLSearchParams([
|
|
869
|
+
["name[$in][0]", "Apple"],
|
|
870
|
+
["name[$in][1]", "Banana"],
|
|
871
|
+
]).toString();
|
|
872
|
+
const res = await agent.get(`/admin/foods?${query}`).expect(200);
|
|
873
|
+
|
|
874
|
+
expect(res.body.data.map((item: {name: string}) => item.name).sort()).toEqual([
|
|
875
|
+
"Apple",
|
|
876
|
+
"Banana",
|
|
877
|
+
]);
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
it("filters optional priority to high only and excludes unset priority", async () => {
|
|
881
|
+
app = buildApp([
|
|
882
|
+
{
|
|
883
|
+
displayName: "Priority todos",
|
|
884
|
+
filters: [
|
|
885
|
+
{
|
|
886
|
+
choices: [
|
|
887
|
+
{label: "High", value: "high"},
|
|
888
|
+
{label: "Low", value: "low"},
|
|
889
|
+
],
|
|
890
|
+
field: "priority",
|
|
891
|
+
kind: "choice",
|
|
892
|
+
label: "Priority",
|
|
893
|
+
},
|
|
894
|
+
],
|
|
895
|
+
listFields: ["title", "priority"],
|
|
896
|
+
model: PriorityTodoModel,
|
|
897
|
+
routePath: "/priority-todos",
|
|
898
|
+
},
|
|
899
|
+
]);
|
|
900
|
+
const agent = await authAsUser(app, "admin");
|
|
901
|
+
await PriorityTodoModel.create({priority: "high", title: "high one"});
|
|
902
|
+
await PriorityTodoModel.create({priority: "low", title: "low one"});
|
|
903
|
+
await PriorityTodoModel.create({title: "unset"});
|
|
904
|
+
|
|
905
|
+
const scalarRes = await agent.get("/admin/priority-todos?priority=high").expect(200);
|
|
906
|
+
expect(scalarRes.body.data.map((item: {title: string}) => item.title)).toEqual(["high one"]);
|
|
907
|
+
|
|
908
|
+
const inRes = await agent
|
|
909
|
+
.get(
|
|
910
|
+
`/admin/priority-todos?${new URLSearchParams([["priority[$in][0]", "high"]]).toString()}`
|
|
911
|
+
)
|
|
912
|
+
.expect(200);
|
|
913
|
+
expect(inRes.body.data.map((item: {title: string}) => item.title)).toEqual(["high one"]);
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
it("filters optional priority empty sentinel to missing or null values", async () => {
|
|
917
|
+
app = buildApp([
|
|
918
|
+
{
|
|
919
|
+
displayName: "Priority todos",
|
|
920
|
+
filters: [
|
|
921
|
+
{
|
|
922
|
+
choices: [
|
|
923
|
+
{label: "High", value: "high"},
|
|
924
|
+
{label: "Low", value: "low"},
|
|
925
|
+
],
|
|
926
|
+
field: "priority",
|
|
927
|
+
kind: "choice",
|
|
928
|
+
label: "Priority",
|
|
929
|
+
},
|
|
930
|
+
],
|
|
931
|
+
listFields: ["title", "priority"],
|
|
932
|
+
model: PriorityTodoModel,
|
|
933
|
+
routePath: "/priority-todos",
|
|
934
|
+
},
|
|
935
|
+
]);
|
|
936
|
+
const agent = await authAsUser(app, "admin");
|
|
937
|
+
await PriorityTodoModel.create({priority: "high", title: "high one"});
|
|
938
|
+
await PriorityTodoModel.create({priority: "low", title: "low one"});
|
|
939
|
+
await PriorityTodoModel.create({title: "unset"});
|
|
940
|
+
await PriorityTodoModel.create({priority: null, title: "null priority"});
|
|
941
|
+
|
|
942
|
+
const query = new URLSearchParams([["priority[$in][0]", "__empty__"]]).toString();
|
|
943
|
+
const res = await agent.get(`/admin/priority-todos?${query}`).expect(200);
|
|
944
|
+
expect(res.body.data.map((item: {title: string}) => item.title).sort()).toEqual([
|
|
945
|
+
"null priority",
|
|
946
|
+
"unset",
|
|
947
|
+
]);
|
|
948
|
+
});
|
|
949
|
+
|
|
950
|
+
it("combines empty sentinel with concrete priority values", async () => {
|
|
951
|
+
app = buildApp([
|
|
952
|
+
{
|
|
953
|
+
displayName: "Priority todos",
|
|
954
|
+
filters: [
|
|
955
|
+
{
|
|
956
|
+
choices: [
|
|
957
|
+
{label: "High", value: "high"},
|
|
958
|
+
{label: "Low", value: "low"},
|
|
959
|
+
],
|
|
960
|
+
field: "priority",
|
|
961
|
+
kind: "choice",
|
|
962
|
+
label: "Priority",
|
|
963
|
+
},
|
|
964
|
+
],
|
|
965
|
+
listFields: ["title", "priority"],
|
|
966
|
+
model: PriorityTodoModel,
|
|
967
|
+
routePath: "/priority-todos",
|
|
968
|
+
},
|
|
969
|
+
]);
|
|
970
|
+
const agent = await authAsUser(app, "admin");
|
|
971
|
+
await PriorityTodoModel.create({priority: "high", title: "high one"});
|
|
972
|
+
await PriorityTodoModel.create({priority: "low", title: "low one"});
|
|
973
|
+
await PriorityTodoModel.create({title: "unset"});
|
|
974
|
+
|
|
975
|
+
const query = new URLSearchParams([
|
|
976
|
+
["priority[$in][0]", "high"],
|
|
977
|
+
["priority[$in][1]", "__empty__"],
|
|
978
|
+
]).toString();
|
|
979
|
+
const res = await agent.get(`/admin/priority-todos?${query}`).expect(200);
|
|
980
|
+
expect(res.body.data.map((item: {title: string}) => item.title).sort()).toEqual([
|
|
981
|
+
"high one",
|
|
982
|
+
"unset",
|
|
983
|
+
]);
|
|
984
|
+
});
|
|
985
|
+
|
|
986
|
+
it("filters empty priority with search without leaving the wire sentinel on the query", async () => {
|
|
987
|
+
app = buildApp([
|
|
988
|
+
{
|
|
989
|
+
displayName: "Priority todos",
|
|
990
|
+
filters: [
|
|
991
|
+
{
|
|
992
|
+
choices: [
|
|
993
|
+
{label: "High", value: "high"},
|
|
994
|
+
{label: "Low", value: "low"},
|
|
995
|
+
],
|
|
996
|
+
field: "priority",
|
|
997
|
+
kind: "choice",
|
|
998
|
+
label: "Priority",
|
|
999
|
+
},
|
|
1000
|
+
],
|
|
1001
|
+
listFields: ["title", "priority"],
|
|
1002
|
+
model: PriorityTodoModel,
|
|
1003
|
+
routePath: "/priority-todos",
|
|
1004
|
+
searchFields: ["title"],
|
|
1005
|
+
},
|
|
1006
|
+
]);
|
|
1007
|
+
const agent = await authAsUser(app, "admin");
|
|
1008
|
+
await PriorityTodoModel.create({priority: "high", title: "alpha task"});
|
|
1009
|
+
await PriorityTodoModel.create({priority: "low", title: "beta task"});
|
|
1010
|
+
await PriorityTodoModel.create({title: "gamma task"});
|
|
1011
|
+
await PriorityTodoModel.create({title: "delta task"});
|
|
1012
|
+
await PriorityTodoModel.create({priority: null, title: "epsilon task"});
|
|
1013
|
+
|
|
1014
|
+
const query = new URLSearchParams([
|
|
1015
|
+
["priority[$in][0]", "__empty__"],
|
|
1016
|
+
["q", "task"],
|
|
1017
|
+
]).toString();
|
|
1018
|
+
const res = await agent.get(`/admin/priority-todos?${query}`).expect(200);
|
|
1019
|
+
expect(res.body.data.map((item: {title: string}) => item.title).sort()).toEqual([
|
|
1020
|
+
"delta task",
|
|
1021
|
+
"epsilon task",
|
|
1022
|
+
"gamma task",
|
|
1023
|
+
]);
|
|
1024
|
+
});
|
|
1025
|
+
|
|
1026
|
+
it("combines empty and concrete priority filters with search", async () => {
|
|
1027
|
+
app = buildApp([
|
|
1028
|
+
{
|
|
1029
|
+
displayName: "Priority todos",
|
|
1030
|
+
filters: [
|
|
1031
|
+
{
|
|
1032
|
+
choices: [
|
|
1033
|
+
{label: "High", value: "high"},
|
|
1034
|
+
{label: "Low", value: "low"},
|
|
1035
|
+
],
|
|
1036
|
+
field: "priority",
|
|
1037
|
+
kind: "choice",
|
|
1038
|
+
label: "Priority",
|
|
1039
|
+
},
|
|
1040
|
+
],
|
|
1041
|
+
listFields: ["title", "priority"],
|
|
1042
|
+
model: PriorityTodoModel,
|
|
1043
|
+
routePath: "/priority-todos",
|
|
1044
|
+
searchFields: ["title"],
|
|
1045
|
+
},
|
|
1046
|
+
]);
|
|
1047
|
+
const agent = await authAsUser(app, "admin");
|
|
1048
|
+
await PriorityTodoModel.create({priority: "high", title: "alpha task"});
|
|
1049
|
+
await PriorityTodoModel.create({priority: "low", title: "beta task"});
|
|
1050
|
+
await PriorityTodoModel.create({title: "gamma task"});
|
|
1051
|
+
await PriorityTodoModel.create({priority: "high", title: "high hidden"});
|
|
1052
|
+
|
|
1053
|
+
const query = new URLSearchParams([
|
|
1054
|
+
["priority[$in][0]", "high"],
|
|
1055
|
+
["priority[$in][1]", "__empty__"],
|
|
1056
|
+
["q", "task"],
|
|
1057
|
+
]).toString();
|
|
1058
|
+
const res = await agent.get(`/admin/priority-todos?${query}`).expect(200);
|
|
1059
|
+
expect(res.body.data.map((item: {title: string}) => item.title).sort()).toEqual([
|
|
1060
|
+
"alpha task",
|
|
1061
|
+
"gamma task",
|
|
1062
|
+
]);
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
it("exposes allowEmpty on optional choice filters in admin config", async () => {
|
|
1066
|
+
app = buildApp([
|
|
1067
|
+
{
|
|
1068
|
+
displayName: "Priority todos",
|
|
1069
|
+
filters: [
|
|
1070
|
+
{
|
|
1071
|
+
choices: [{label: "High", value: "high"}],
|
|
1072
|
+
field: "priority",
|
|
1073
|
+
kind: "choice",
|
|
1074
|
+
},
|
|
1075
|
+
],
|
|
1076
|
+
listFields: ["title", "priority"],
|
|
1077
|
+
model: PriorityTodoModel,
|
|
1078
|
+
routePath: "/priority-todos",
|
|
1079
|
+
},
|
|
1080
|
+
]);
|
|
1081
|
+
const agent = await authAsUser(app, "admin");
|
|
1082
|
+
const res = await agent.get("/admin/config").expect(200);
|
|
1083
|
+
const model = res.body.models.find((entry: {routePath: string}) =>
|
|
1084
|
+
entry.routePath.endsWith("/priority-todos")
|
|
1085
|
+
);
|
|
1086
|
+
expect(model.filters[0].allowEmpty).toBe(true);
|
|
1087
|
+
});
|
|
1088
|
+
|
|
806
1089
|
it("reads a document via GET /:id", async () => {
|
|
807
1090
|
const food = await FoodModel.create({calories: 120, name: "Apple"});
|
|
808
1091
|
const res = await adminAgent.get(`/admin/foods/${food._id}`).expect(200);
|
|
@@ -1352,6 +1635,180 @@ describe("AdminApp onAdminAudit is best-effort", () => {
|
|
|
1352
1635
|
});
|
|
1353
1636
|
});
|
|
1354
1637
|
|
|
1638
|
+
const deleteAuditEventModel = (): void => {
|
|
1639
|
+
if (mongoose.connection.models.AuditEvent) {
|
|
1640
|
+
mongoose.connection.deleteModel("AuditEvent");
|
|
1641
|
+
}
|
|
1642
|
+
};
|
|
1643
|
+
|
|
1644
|
+
const buildAppWithAuditPlugin = (adminOverrides?: Partial<AdminOptions>): express.Application => {
|
|
1645
|
+
const app = getBaseServer();
|
|
1646
|
+
setupAuth(app, UserModel as unknown as UserModelType);
|
|
1647
|
+
addAuthRoutes(app, UserModel as unknown as UserModelType);
|
|
1648
|
+
new AuditApp().register(app);
|
|
1649
|
+
const admin = new AdminApp({
|
|
1650
|
+
basePath: "/admin",
|
|
1651
|
+
models: [foodModelConfig],
|
|
1652
|
+
...adminOverrides,
|
|
1653
|
+
});
|
|
1654
|
+
admin.register(app);
|
|
1655
|
+
app.use(apiUnauthorizedMiddleware);
|
|
1656
|
+
app.use(apiErrorMiddleware);
|
|
1657
|
+
return app;
|
|
1658
|
+
};
|
|
1659
|
+
|
|
1660
|
+
describe("AdminApp AuditEvent auto-write", () => {
|
|
1661
|
+
beforeEach(async () => {
|
|
1662
|
+
deleteAuditEventModel();
|
|
1663
|
+
resetAuditRecorderForTests();
|
|
1664
|
+
await setupDb();
|
|
1665
|
+
await mongoose.connection.collection("auditevents").deleteMany({});
|
|
1666
|
+
});
|
|
1667
|
+
|
|
1668
|
+
afterEach(async () => {
|
|
1669
|
+
await FoodModel.deleteMany({});
|
|
1670
|
+
await mongoose.connection.collection("auditevents").deleteMany({});
|
|
1671
|
+
resetAuditRecorderForTests();
|
|
1672
|
+
deleteAuditEventModel();
|
|
1673
|
+
});
|
|
1674
|
+
|
|
1675
|
+
it("persists source admin AuditEvent on POST without onAdminAudit", async () => {
|
|
1676
|
+
const localApp = buildAppWithAuditPlugin();
|
|
1677
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1678
|
+
await agent.post("/admin/foods").send({calories: 5, name: "AuditedFood"}).expect(201);
|
|
1679
|
+
await flushAuditRecorderForTests();
|
|
1680
|
+
const events = await mongoose.connection.collection("auditevents").find({}).toArray();
|
|
1681
|
+
assert.equal(events.length, 1);
|
|
1682
|
+
assert.equal(events[0]?.source, "admin");
|
|
1683
|
+
assert.equal(events[0]?.verb, "created");
|
|
1684
|
+
assert.equal(events[0]?.modelName, "Food");
|
|
1685
|
+
assert.equal(events[0]?.recordLabel, "AuditedFood");
|
|
1686
|
+
});
|
|
1687
|
+
|
|
1688
|
+
it("omits hiddenFields from admin AuditEvent diffs", async () => {
|
|
1689
|
+
const hiddenApp = getBaseServer();
|
|
1690
|
+
setupAuth(hiddenApp, UserModel as unknown as UserModelType);
|
|
1691
|
+
addAuthRoutes(hiddenApp, UserModel as unknown as UserModelType);
|
|
1692
|
+
new AuditApp().register(hiddenApp);
|
|
1693
|
+
new AdminApp({
|
|
1694
|
+
basePath: "/admin",
|
|
1695
|
+
models: [{...foodModelConfig, hiddenFields: ["calories"]}],
|
|
1696
|
+
}).register(hiddenApp);
|
|
1697
|
+
hiddenApp.use(apiUnauthorizedMiddleware);
|
|
1698
|
+
hiddenApp.use(apiErrorMiddleware);
|
|
1699
|
+
const agent = await authAsUser(hiddenApp, "admin");
|
|
1700
|
+
const food = await FoodModel.create({calories: 42, name: "HiddenCals"});
|
|
1701
|
+
await agent.delete(`/admin/foods/${food._id}`).expect(204);
|
|
1702
|
+
await flushAuditRecorderForTests();
|
|
1703
|
+
const events = await mongoose.connection.collection("auditevents").find({}).toArray();
|
|
1704
|
+
assert.equal(events.length, 1);
|
|
1705
|
+
const before = events[0]?.before as Record<string, unknown> | undefined;
|
|
1706
|
+
assert.equal(before?.name, "HiddenCals");
|
|
1707
|
+
assert.isUndefined(before?.calories);
|
|
1708
|
+
});
|
|
1709
|
+
|
|
1710
|
+
it("runs onAdminAudit and persists AuditEvent when both are configured", async () => {
|
|
1711
|
+
const extra: AdminAuditEvent[] = [];
|
|
1712
|
+
const localApp = buildAppWithAuditPlugin({
|
|
1713
|
+
onAdminAudit: async (event): Promise<void> => {
|
|
1714
|
+
extra.push(event);
|
|
1715
|
+
},
|
|
1716
|
+
});
|
|
1717
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1718
|
+
await agent.post("/admin/foods").send({calories: 8, name: "BothSinks"}).expect(201);
|
|
1719
|
+
await flushAuditRecorderForTests();
|
|
1720
|
+
assert.equal(extra.length, 1);
|
|
1721
|
+
assert.equal(extra[0]?.verb, "created");
|
|
1722
|
+
const events = await mongoose.connection.collection("auditevents").find({}).toArray();
|
|
1723
|
+
assert.equal(events.length, 1);
|
|
1724
|
+
assert.equal(events[0]?.source, "admin");
|
|
1725
|
+
});
|
|
1726
|
+
|
|
1727
|
+
it("returns 201 on POST when AuditEvent persistence throws", async () => {
|
|
1728
|
+
const localApp = buildAppWithAuditPlugin();
|
|
1729
|
+
const AuditEvent = mongoose.connection.models.AuditEvent;
|
|
1730
|
+
const createSpy = spyOn(AuditEvent, "create").mockImplementation(() => {
|
|
1731
|
+
throw new Error("recorder boom");
|
|
1732
|
+
});
|
|
1733
|
+
const agent = await authAsUser(localApp, "admin");
|
|
1734
|
+
const res = await agent
|
|
1735
|
+
.post("/admin/foods")
|
|
1736
|
+
.send({calories: 5, name: "StillCreated"})
|
|
1737
|
+
.expect(201);
|
|
1738
|
+
await flushAuditRecorderForTests();
|
|
1739
|
+
createSpy.mockRestore();
|
|
1740
|
+
const stored = await FoodModel.findById(res.body.data._id).lean();
|
|
1741
|
+
assert.equal(stored?.name, "StillCreated");
|
|
1742
|
+
});
|
|
1743
|
+
|
|
1744
|
+
it("returns 405 for admin create on AuditEvent when AuditApp is a plugin", async () => {
|
|
1745
|
+
const auditPlugin = new AuditApp();
|
|
1746
|
+
const terrenoApp = {
|
|
1747
|
+
getPlugins: () => [auditPlugin],
|
|
1748
|
+
getRegistrations: () => [],
|
|
1749
|
+
} as unknown as TerrenoApp;
|
|
1750
|
+
const app = getBaseServer();
|
|
1751
|
+
setupAuth(app, UserModel as unknown as UserModelType);
|
|
1752
|
+
addAuthRoutes(app, UserModel as unknown as UserModelType);
|
|
1753
|
+
auditPlugin.register(app);
|
|
1754
|
+
new AdminApp({basePath: "/admin", models: [foodModelConfig]}).register(
|
|
1755
|
+
app,
|
|
1756
|
+
undefined,
|
|
1757
|
+
terrenoApp
|
|
1758
|
+
);
|
|
1759
|
+
app.use(apiUnauthorizedMiddleware);
|
|
1760
|
+
app.use(apiErrorMiddleware);
|
|
1761
|
+
const agent = await authAsUser(app, "admin");
|
|
1762
|
+
const config = await agent.get("/admin/config").expect(200);
|
|
1763
|
+
const auditMeta = (
|
|
1764
|
+
config.body.models as Array<{name: string; permissions: object; routePath: string}>
|
|
1765
|
+
).find((model) => model.name === "AuditEvent" || model.routePath.includes("audit-events"));
|
|
1766
|
+
assert.ok(
|
|
1767
|
+
auditMeta,
|
|
1768
|
+
JSON.stringify(config.body.models.map((model: {name: string}) => model.name))
|
|
1769
|
+
);
|
|
1770
|
+
assert.deepEqual(auditMeta.permissions, {create: false, delete: false, update: false});
|
|
1771
|
+
const create = await agent.post("/admin/audit-events").send({
|
|
1772
|
+
modelName: "Todo",
|
|
1773
|
+
operation: "create",
|
|
1774
|
+
source: "admin",
|
|
1775
|
+
verb: "created",
|
|
1776
|
+
});
|
|
1777
|
+
assert.equal(create.status, 405, JSON.stringify(create.body));
|
|
1778
|
+
});
|
|
1779
|
+
|
|
1780
|
+
it("lists AuditEvent without an organization even when organizations is enabled", async () => {
|
|
1781
|
+
const auditPlugin = new AuditApp();
|
|
1782
|
+
const terrenoApp = {
|
|
1783
|
+
getPlugins: () => [auditPlugin],
|
|
1784
|
+
getRegistrations: () => [],
|
|
1785
|
+
} as unknown as TerrenoApp;
|
|
1786
|
+
const app = getBaseServer();
|
|
1787
|
+
setupAuth(app, UserModel as unknown as UserModelType);
|
|
1788
|
+
addAuthRoutes(app, UserModel as unknown as UserModelType);
|
|
1789
|
+
auditPlugin.register(app);
|
|
1790
|
+
new AdminApp({
|
|
1791
|
+
basePath: "/admin",
|
|
1792
|
+
models: [foodModelConfig],
|
|
1793
|
+
organizations: true,
|
|
1794
|
+
}).register(app, undefined, terrenoApp);
|
|
1795
|
+
app.use(apiUnauthorizedMiddleware);
|
|
1796
|
+
app.use(apiErrorMiddleware);
|
|
1797
|
+
const agent = await authAsUser(app, "admin");
|
|
1798
|
+
const config = await agent.get("/admin/config").expect(200);
|
|
1799
|
+
const auditMeta = (
|
|
1800
|
+
config.body.models as Array<{name: string; organizationScoped?: boolean; routePath: string}>
|
|
1801
|
+
).find((model) => model.name === "AuditEvent" || model.routePath.includes("audit-events"));
|
|
1802
|
+
assert.ok(
|
|
1803
|
+
auditMeta,
|
|
1804
|
+
JSON.stringify(config.body.models.map((model: {name: string}) => model.name))
|
|
1805
|
+
);
|
|
1806
|
+
assert.strictEqual(auditMeta?.organizationScoped, false);
|
|
1807
|
+
const list = await agent.get("/admin/audit-events").expect(200);
|
|
1808
|
+
assert.isArray(list.body.data);
|
|
1809
|
+
});
|
|
1810
|
+
});
|
|
1811
|
+
|
|
1355
1812
|
describe("AdminApp per-model queryFilter", () => {
|
|
1356
1813
|
beforeEach(async () => {
|
|
1357
1814
|
await setupDb();
|
|
@@ -1516,6 +1973,191 @@ describe("AdminApp per-model queryFilter", () => {
|
|
|
1516
1973
|
});
|
|
1517
1974
|
});
|
|
1518
1975
|
|
|
1976
|
+
describe("AdminApp organization scoping", () => {
|
|
1977
|
+
beforeEach(async () => {
|
|
1978
|
+
const [admin] = await setupDb();
|
|
1979
|
+
admin.roles = ["operator"];
|
|
1980
|
+
await admin.save();
|
|
1981
|
+
await Promise.all([
|
|
1982
|
+
Membership.deleteMany({}),
|
|
1983
|
+
Organization.deleteMany({}),
|
|
1984
|
+
OrgScopedFoodModel.deleteMany({}),
|
|
1985
|
+
]);
|
|
1986
|
+
});
|
|
1987
|
+
|
|
1988
|
+
it("requires org context and scopes list/create when organizations is enabled", async () => {
|
|
1989
|
+
const accessControl = createAccess({
|
|
1990
|
+
connection: mongoose.connection,
|
|
1991
|
+
organizations: true,
|
|
1992
|
+
sources: [
|
|
1993
|
+
{
|
|
1994
|
+
getGrants: async () => ({
|
|
1995
|
+
permissions: {
|
|
1996
|
+
admin: ["access"],
|
|
1997
|
+
adminOrgScopedFood: ["read", "write"],
|
|
1998
|
+
},
|
|
1999
|
+
}),
|
|
2000
|
+
name: "org-scoped-food-admin",
|
|
2001
|
+
},
|
|
2002
|
+
],
|
|
2003
|
+
statements: {
|
|
2004
|
+
...terrenoStatements,
|
|
2005
|
+
adminOrgScopedFood: ADMIN_MODEL_ACCESS,
|
|
2006
|
+
},
|
|
2007
|
+
userModel: UserModel as unknown as UserModelType,
|
|
2008
|
+
});
|
|
2009
|
+
await accessControl.roles.seedDefaults();
|
|
2010
|
+
const localApp = buildApp(
|
|
2011
|
+
[
|
|
2012
|
+
{
|
|
2013
|
+
displayName: "Org foods",
|
|
2014
|
+
listFields: ["name", "calories"],
|
|
2015
|
+
model: OrgScopedFoodModel,
|
|
2016
|
+
routePath: "/org-foods",
|
|
2017
|
+
},
|
|
2018
|
+
],
|
|
2019
|
+
{accessControl, organizations: true}
|
|
2020
|
+
);
|
|
2021
|
+
const agent = await authAsUser(localApp, "admin");
|
|
2022
|
+
const admin = await UserModel.findOne({email: "admin@example.com"}).orFail();
|
|
2023
|
+
const [firstOrg, secondOrg] = await Organization.create([
|
|
2024
|
+
{name: "First Org", ownerId: admin._id},
|
|
2025
|
+
{name: "Second Org", ownerId: admin._id},
|
|
2026
|
+
]);
|
|
2027
|
+
await OrgScopedFoodModel.create([
|
|
2028
|
+
{calories: 1, name: "First", organizationId: firstOrg._id},
|
|
2029
|
+
{calories: 2, name: "Second", organizationId: secondOrg._id},
|
|
2030
|
+
]);
|
|
2031
|
+
|
|
2032
|
+
await agent.get("/admin/org-foods").expect(400);
|
|
2033
|
+
const list = await agent
|
|
2034
|
+
.get("/admin/org-foods")
|
|
2035
|
+
.set("X-Organization-Id", String(firstOrg._id))
|
|
2036
|
+
.expect(200);
|
|
2037
|
+
expect(list.body.data).toHaveLength(1);
|
|
2038
|
+
expect(list.body.data[0].name).toBe("First");
|
|
2039
|
+
await agent.get("/admin/org-foods/search?q=Second").expect(400);
|
|
2040
|
+
const search = await agent
|
|
2041
|
+
.get("/admin/org-foods/search?q=Second")
|
|
2042
|
+
.set("X-Organization-Id", String(firstOrg._id))
|
|
2043
|
+
.expect(200);
|
|
2044
|
+
expect(search.body.data).toHaveLength(0);
|
|
2045
|
+
const second = await OrgScopedFoodModel.findOne({name: "Second"}).orFail();
|
|
2046
|
+
await agent
|
|
2047
|
+
.get(`/admin/org-foods/${second._id}`)
|
|
2048
|
+
.set("X-Organization-Id", String(firstOrg._id))
|
|
2049
|
+
.expect(403);
|
|
2050
|
+
await agent
|
|
2051
|
+
.patch(`/admin/org-foods/${second._id}`)
|
|
2052
|
+
.set("X-Organization-Id", String(firstOrg._id))
|
|
2053
|
+
.send({calories: 99})
|
|
2054
|
+
.expect(403);
|
|
2055
|
+
await agent
|
|
2056
|
+
.delete(`/admin/org-foods/${second._id}`)
|
|
2057
|
+
.set("X-Organization-Id", String(firstOrg._id))
|
|
2058
|
+
.expect(403);
|
|
2059
|
+
await agent
|
|
2060
|
+
.post("/admin/org-foods/bulk-patch")
|
|
2061
|
+
.send({ids: [second._id], patch: {calories: 99}})
|
|
2062
|
+
.expect(400);
|
|
2063
|
+
const bulk = await agent
|
|
2064
|
+
.post("/admin/org-foods/bulk-patch")
|
|
2065
|
+
.set("X-Organization-Id", String(firstOrg._id))
|
|
2066
|
+
.send({ids: [second._id], patch: {calories: 99}})
|
|
2067
|
+
.expect(200);
|
|
2068
|
+
expect(bulk.body.updated).toBe(0);
|
|
2069
|
+
expect(bulk.body.failures).toHaveLength(1);
|
|
2070
|
+
|
|
2071
|
+
const created = await agent
|
|
2072
|
+
.post("/admin/org-foods")
|
|
2073
|
+
.set("X-Organization-Id", String(firstOrg._id))
|
|
2074
|
+
.send({calories: 3, name: "Created", organizationId: secondOrg._id})
|
|
2075
|
+
.expect(201);
|
|
2076
|
+
expect(String(created.body.data.organizationId)).toBe(String(firstOrg._id));
|
|
2077
|
+
const stored = await OrgScopedFoodModel.findById(created.body.data._id).lean();
|
|
2078
|
+
expect(String(stored?.organizationId)).toBe(String(firstOrg._id));
|
|
2079
|
+
});
|
|
2080
|
+
|
|
2081
|
+
it("infers a single org-admin membership and denies another organization", async () => {
|
|
2082
|
+
const admin = await UserModel.findOne({email: "admin@example.com"}).orFail();
|
|
2083
|
+
admin.roles = [];
|
|
2084
|
+
await admin.save();
|
|
2085
|
+
const accessControl = createAccess({
|
|
2086
|
+
connection: mongoose.connection,
|
|
2087
|
+
organizations: true,
|
|
2088
|
+
sources: [
|
|
2089
|
+
{
|
|
2090
|
+
getGrants: async () => ({
|
|
2091
|
+
permissions: {
|
|
2092
|
+
admin: ["access"],
|
|
2093
|
+
adminOrgScopedFood: ["read", "write"],
|
|
2094
|
+
},
|
|
2095
|
+
}),
|
|
2096
|
+
name: "org-scoped-food-admin",
|
|
2097
|
+
},
|
|
2098
|
+
],
|
|
2099
|
+
statements: {
|
|
2100
|
+
...terrenoStatements,
|
|
2101
|
+
adminOrgScopedFood: ADMIN_MODEL_ACCESS,
|
|
2102
|
+
},
|
|
2103
|
+
userModel: UserModel as unknown as UserModelType,
|
|
2104
|
+
});
|
|
2105
|
+
await accessControl.roles.seedDefaults();
|
|
2106
|
+
const localApp = buildApp(
|
|
2107
|
+
[
|
|
2108
|
+
{
|
|
2109
|
+
displayName: "Org foods",
|
|
2110
|
+
listFields: ["name", "calories"],
|
|
2111
|
+
model: OrgScopedFoodModel,
|
|
2112
|
+
routePath: "/org-foods",
|
|
2113
|
+
},
|
|
2114
|
+
],
|
|
2115
|
+
{accessControl, organizations: true}
|
|
2116
|
+
);
|
|
2117
|
+
const [firstOrg, secondOrg] = await Organization.create([
|
|
2118
|
+
{name: "Admin Org", ownerId: admin._id},
|
|
2119
|
+
{name: "Other Org", ownerId: admin._id},
|
|
2120
|
+
]);
|
|
2121
|
+
await Membership.create({
|
|
2122
|
+
organizationId: firstOrg._id,
|
|
2123
|
+
roleName: "org-admin",
|
|
2124
|
+
userId: admin._id,
|
|
2125
|
+
});
|
|
2126
|
+
await OrgScopedFoodModel.create([
|
|
2127
|
+
{calories: 1, name: "Admin row", organizationId: firstOrg._id},
|
|
2128
|
+
{calories: 2, name: "Other row", organizationId: secondOrg._id},
|
|
2129
|
+
]);
|
|
2130
|
+
const agent = await authAsUser(localApp, "admin");
|
|
2131
|
+
|
|
2132
|
+
const inferred = await agent.get("/admin/org-foods").expect(200);
|
|
2133
|
+
expect(inferred.body.data).toHaveLength(1);
|
|
2134
|
+
expect(inferred.body.data[0].name).toBe("Admin row");
|
|
2135
|
+
await agent.get("/admin/org-foods").set("X-Organization-Id", String(secondOrg._id)).expect(403);
|
|
2136
|
+
});
|
|
2137
|
+
|
|
2138
|
+
it("leaves org-scoped models unscoped when organizations is omitted", async () => {
|
|
2139
|
+
const localApp = buildApp([
|
|
2140
|
+
{
|
|
2141
|
+
displayName: "Org foods",
|
|
2142
|
+
listFields: ["name", "calories"],
|
|
2143
|
+
model: OrgScopedFoodModel,
|
|
2144
|
+
routePath: "/org-foods",
|
|
2145
|
+
},
|
|
2146
|
+
]);
|
|
2147
|
+
const agent = await authAsUser(localApp, "admin");
|
|
2148
|
+
const admin = await UserModel.findOne({email: "admin@example.com"}).orFail();
|
|
2149
|
+
const organization = await Organization.create({name: "Optional", ownerId: admin._id});
|
|
2150
|
+
await OrgScopedFoodModel.create({
|
|
2151
|
+
calories: 1,
|
|
2152
|
+
name: "Visible",
|
|
2153
|
+
organizationId: organization._id,
|
|
2154
|
+
});
|
|
2155
|
+
|
|
2156
|
+
const list = await agent.get("/admin/org-foods").expect(200);
|
|
2157
|
+
expect(list.body.data).toHaveLength(1);
|
|
2158
|
+
});
|
|
2159
|
+
});
|
|
2160
|
+
|
|
1519
2161
|
describe("AdminApp user elevation and scoped bulk-patch", () => {
|
|
1520
2162
|
beforeEach(async () => {
|
|
1521
2163
|
await setupDb();
|