@terreno/admin-backend 0.20.2 → 0.21.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 +5 -0
- package/.github/copilot-instructions.md +1 -1
- package/AGENTS.md +5 -1
- package/bunfig.toml +1 -1
- package/dist/adminApp.d.ts +74 -1
- package/dist/adminApp.js +620 -83
- package/dist/adminApp.models.test.js +537 -8
- package/dist/adminApp.test.js +182 -0
- package/dist/adminUiV2.d.ts +96 -0
- package/dist/adminUiV2.js +189 -0
- package/dist/adminUiV2.test.d.ts +1 -0
- package/dist/adminUiV2.test.js +28 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +15 -0
- package/dist/tests/testEnv.d.ts +0 -0
- package/dist/tests/testEnv.js +6 -0
- package/package.json +5 -4
- package/src/adminApp.models.test.ts +284 -2
- package/src/adminApp.test.ts +95 -0
- package/src/adminApp.ts +598 -22
- package/src/adminUiV2.test.ts +28 -0
- package/src/adminUiV2.ts +188 -0
- package/src/index.ts +9 -1
- package/src/tests/testEnv.ts +5 -0
|
@@ -115,15 +115,12 @@ var api_1 = require("@terreno/api");
|
|
|
115
115
|
var tests_1 = require("@terreno/api/src/tests");
|
|
116
116
|
var supertest_1 = __importDefault(require("supertest"));
|
|
117
117
|
var adminApp_1 = require("./adminApp");
|
|
118
|
-
var buildApp = function (models) {
|
|
118
|
+
var buildApp = function (models, adminOverrides) {
|
|
119
119
|
if (models === void 0) { models = []; }
|
|
120
120
|
var app = (0, tests_1.getBaseServer)();
|
|
121
121
|
(0, api_1.setupAuth)(app, tests_1.UserModel);
|
|
122
122
|
(0, api_1.addAuthRoutes)(app, tests_1.UserModel);
|
|
123
|
-
var admin = new adminApp_1.AdminApp({
|
|
124
|
-
basePath: "/admin",
|
|
125
|
-
models: models,
|
|
126
|
-
});
|
|
123
|
+
var admin = new adminApp_1.AdminApp(__assign({ basePath: "/admin", models: models }, adminOverrides));
|
|
127
124
|
admin.register(app);
|
|
128
125
|
app.use(api_1.apiUnauthorizedMiddleware);
|
|
129
126
|
app.use(api_1.apiErrorMiddleware);
|
|
@@ -173,11 +170,15 @@ var foodModelConfig = {
|
|
|
173
170
|
}); });
|
|
174
171
|
(0, bun_test_1.it)("returns metadata for configured models to admins", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
175
172
|
var res, _a, foodMeta;
|
|
176
|
-
|
|
177
|
-
|
|
173
|
+
var _b, _c, _d;
|
|
174
|
+
return __generator(this, function (_e) {
|
|
175
|
+
switch (_e.label) {
|
|
178
176
|
case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
|
|
179
177
|
case 1:
|
|
180
|
-
res =
|
|
178
|
+
res = _e.sent();
|
|
179
|
+
(0, bun_test_1.expect)(res.body.schemaVersion).toBe(2);
|
|
180
|
+
(0, bun_test_1.expect)((_b = res.body.home) === null || _b === void 0 ? void 0 : _b.title).toBe("Administration");
|
|
181
|
+
(0, bun_test_1.expect)((_d = (_c = res.body.home) === null || _c === void 0 ? void 0 : _c.slots) === null || _d === void 0 ? void 0 : _d.main).toEqual(["modelsGrid"]);
|
|
181
182
|
(0, bun_test_1.expect)(res.body.models).toHaveLength(1);
|
|
182
183
|
_a = __read(res.body.models, 1), foodMeta = _a[0];
|
|
183
184
|
(0, bun_test_1.expect)(foodMeta.name).toBe("Food");
|
|
@@ -190,6 +191,23 @@ var foodModelConfig = {
|
|
|
190
191
|
}
|
|
191
192
|
});
|
|
192
193
|
}); });
|
|
194
|
+
(0, bun_test_1.it)("includes recordTitleField in config when set on the model", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
195
|
+
var appWithTitle, agent, res;
|
|
196
|
+
return __generator(this, function (_a) {
|
|
197
|
+
switch (_a.label) {
|
|
198
|
+
case 0:
|
|
199
|
+
appWithTitle = buildApp([__assign(__assign({}, foodModelConfig), { recordTitleField: "name" })]);
|
|
200
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(appWithTitle, "admin")];
|
|
201
|
+
case 1:
|
|
202
|
+
agent = _a.sent();
|
|
203
|
+
return [4 /*yield*/, agent.get("/admin/config").expect(200)];
|
|
204
|
+
case 2:
|
|
205
|
+
res = _a.sent();
|
|
206
|
+
(0, bun_test_1.expect)(res.body.models[0].recordTitleField).toBe("name");
|
|
207
|
+
return [2 /*return*/];
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}); });
|
|
193
211
|
(0, bun_test_1.it)("applies fieldOverrides to generated config", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
194
212
|
var res, _a, foodMeta;
|
|
195
213
|
return __generator(this, function (_b) {
|
|
@@ -374,6 +392,29 @@ var foodModelConfig = {
|
|
|
374
392
|
}
|
|
375
393
|
});
|
|
376
394
|
}); });
|
|
395
|
+
(0, bun_test_1.it)("normalizes home.slots so recentActivity is last in sidebar", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
396
|
+
var agent, res;
|
|
397
|
+
return __generator(this, function (_a) {
|
|
398
|
+
switch (_a.label) {
|
|
399
|
+
case 0:
|
|
400
|
+
app = buildApp([foodModelConfig], {
|
|
401
|
+
home: {
|
|
402
|
+
slots: { sidebar: ["recentActivity", "versionConfig"] },
|
|
403
|
+
title: "Ops",
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
407
|
+
case 1:
|
|
408
|
+
agent = _a.sent();
|
|
409
|
+
return [4 /*yield*/, agent.get("/admin/config").expect(200)];
|
|
410
|
+
case 2:
|
|
411
|
+
res = _a.sent();
|
|
412
|
+
(0, bun_test_1.expect)(res.body.home.title).toBe("Ops");
|
|
413
|
+
(0, bun_test_1.expect)(res.body.home.slots.sidebar).toEqual(["versionConfig", "recentActivity"]);
|
|
414
|
+
return [2 /*return*/];
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
}); });
|
|
377
418
|
});
|
|
378
419
|
(0, bun_test_1.describe)("AdminApp model CRUD routes", function () {
|
|
379
420
|
var app;
|
|
@@ -967,3 +1008,491 @@ var foodModelConfig = {
|
|
|
967
1008
|
});
|
|
968
1009
|
}); });
|
|
969
1010
|
});
|
|
1011
|
+
(0, bun_test_1.describe)("AdminApp admin UI v2 routes", function () {
|
|
1012
|
+
var app;
|
|
1013
|
+
var adminAgent;
|
|
1014
|
+
var notAdminAgent;
|
|
1015
|
+
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1016
|
+
return __generator(this, function (_a) {
|
|
1017
|
+
switch (_a.label) {
|
|
1018
|
+
case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
|
|
1019
|
+
case 1:
|
|
1020
|
+
_a.sent();
|
|
1021
|
+
app = buildApp([__assign(__assign({}, foodModelConfig), { hiddenFields: ["hidden"] })]);
|
|
1022
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
1023
|
+
case 2:
|
|
1024
|
+
adminAgent = _a.sent();
|
|
1025
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
|
|
1026
|
+
case 3:
|
|
1027
|
+
notAdminAgent = _a.sent();
|
|
1028
|
+
return [2 /*return*/];
|
|
1029
|
+
}
|
|
1030
|
+
});
|
|
1031
|
+
}); });
|
|
1032
|
+
(0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1033
|
+
return __generator(this, function (_a) {
|
|
1034
|
+
switch (_a.label) {
|
|
1035
|
+
case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
|
|
1036
|
+
case 1:
|
|
1037
|
+
_a.sent();
|
|
1038
|
+
return [4 /*yield*/, api_1.BackgroundTask.deleteMany({})];
|
|
1039
|
+
case 2:
|
|
1040
|
+
_a.sent();
|
|
1041
|
+
return [2 /*return*/];
|
|
1042
|
+
}
|
|
1043
|
+
});
|
|
1044
|
+
}); });
|
|
1045
|
+
(0, bun_test_1.it)("rejects bulk-patch with an empty ids array", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1046
|
+
var res;
|
|
1047
|
+
return __generator(this, function (_a) {
|
|
1048
|
+
switch (_a.label) {
|
|
1049
|
+
case 0: return [4 /*yield*/, adminAgent
|
|
1050
|
+
.post("/admin/foods/bulk-patch")
|
|
1051
|
+
.send({ ids: [], patch: { calories: 1 } })
|
|
1052
|
+
.expect(400)];
|
|
1053
|
+
case 1:
|
|
1054
|
+
res = _a.sent();
|
|
1055
|
+
(0, bun_test_1.expect)(res.body.title).toInclude("at least one");
|
|
1056
|
+
return [2 /*return*/];
|
|
1057
|
+
}
|
|
1058
|
+
});
|
|
1059
|
+
}); });
|
|
1060
|
+
(0, bun_test_1.it)("bulk-patches allowlisted fields for many ids", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1061
|
+
var a, b, res, updated, updated_1, updated_1_1, row;
|
|
1062
|
+
var e_3, _a;
|
|
1063
|
+
return __generator(this, function (_b) {
|
|
1064
|
+
switch (_b.label) {
|
|
1065
|
+
case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "A" })];
|
|
1066
|
+
case 1:
|
|
1067
|
+
a = _b.sent();
|
|
1068
|
+
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 2, name: "B" })];
|
|
1069
|
+
case 2:
|
|
1070
|
+
b = _b.sent();
|
|
1071
|
+
return [4 /*yield*/, adminAgent
|
|
1072
|
+
.post("/admin/foods/bulk-patch")
|
|
1073
|
+
.send({
|
|
1074
|
+
ids: [String(a._id), String(b._id)],
|
|
1075
|
+
patch: { calories: 50 },
|
|
1076
|
+
})
|
|
1077
|
+
.expect(200)];
|
|
1078
|
+
case 3:
|
|
1079
|
+
res = _b.sent();
|
|
1080
|
+
(0, bun_test_1.expect)(res.body.updated).toBe(2);
|
|
1081
|
+
(0, bun_test_1.expect)(res.body.failures).toBeUndefined();
|
|
1082
|
+
return [4 /*yield*/, tests_1.FoodModel.find({ _id: { $in: [a._id, b._id] } })
|
|
1083
|
+
.lean()
|
|
1084
|
+
.exec()];
|
|
1085
|
+
case 4:
|
|
1086
|
+
updated = _b.sent();
|
|
1087
|
+
try {
|
|
1088
|
+
for (updated_1 = __values(updated), updated_1_1 = updated_1.next(); !updated_1_1.done; updated_1_1 = updated_1.next()) {
|
|
1089
|
+
row = updated_1_1.value;
|
|
1090
|
+
(0, bun_test_1.expect)(row.calories).toBe(50);
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
1094
|
+
finally {
|
|
1095
|
+
try {
|
|
1096
|
+
if (updated_1_1 && !updated_1_1.done && (_a = updated_1.return)) _a.call(updated_1);
|
|
1097
|
+
}
|
|
1098
|
+
finally { if (e_3) throw e_3.error; }
|
|
1099
|
+
}
|
|
1100
|
+
return [2 /*return*/];
|
|
1101
|
+
}
|
|
1102
|
+
});
|
|
1103
|
+
}); });
|
|
1104
|
+
(0, bun_test_1.it)("rejects bulk-patch with more than 1000 ids", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1105
|
+
var ids, res;
|
|
1106
|
+
return __generator(this, function (_a) {
|
|
1107
|
+
switch (_a.label) {
|
|
1108
|
+
case 0:
|
|
1109
|
+
ids = Array.from({ length: 1001 }, function (_, index) { return index.toString(16).padStart(24, "0"); });
|
|
1110
|
+
return [4 /*yield*/, adminAgent
|
|
1111
|
+
.post("/admin/foods/bulk-patch")
|
|
1112
|
+
.send({ ids: ids, patch: { calories: 1 } })
|
|
1113
|
+
.expect(400)];
|
|
1114
|
+
case 1:
|
|
1115
|
+
res = _a.sent();
|
|
1116
|
+
(0, bun_test_1.expect)(res.body.title).toInclude("1000");
|
|
1117
|
+
return [2 /*return*/];
|
|
1118
|
+
}
|
|
1119
|
+
});
|
|
1120
|
+
}); });
|
|
1121
|
+
(0, bun_test_1.it)("rejects bulk-patch keys outside the allowlist", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1122
|
+
var a, res;
|
|
1123
|
+
return __generator(this, function (_a) {
|
|
1124
|
+
switch (_a.label) {
|
|
1125
|
+
case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "A" })];
|
|
1126
|
+
case 1:
|
|
1127
|
+
a = _a.sent();
|
|
1128
|
+
return [4 /*yield*/, adminAgent
|
|
1129
|
+
.post("/admin/foods/bulk-patch")
|
|
1130
|
+
.send({
|
|
1131
|
+
ids: [String(a._id)],
|
|
1132
|
+
patch: { thisFieldIsNotAllowlisted: true },
|
|
1133
|
+
})
|
|
1134
|
+
.expect(400)];
|
|
1135
|
+
case 2:
|
|
1136
|
+
res = _a.sent();
|
|
1137
|
+
(0, bun_test_1.expect)(res.body.title).toInclude("allowlisted");
|
|
1138
|
+
return [2 /*return*/];
|
|
1139
|
+
}
|
|
1140
|
+
});
|
|
1141
|
+
}); });
|
|
1142
|
+
(0, bun_test_1.it)("enqueues a background task and returns taskId", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1143
|
+
var res, task;
|
|
1144
|
+
return __generator(this, function (_a) {
|
|
1145
|
+
switch (_a.label) {
|
|
1146
|
+
case 0: return [4 /*yield*/, adminAgent
|
|
1147
|
+
.post("/admin/background-tasks")
|
|
1148
|
+
.send({ kind: "reindex-search", metadata: { scope: "foods" } })
|
|
1149
|
+
.expect(201)];
|
|
1150
|
+
case 1:
|
|
1151
|
+
res = _a.sent();
|
|
1152
|
+
(0, bun_test_1.expect)(typeof res.body.taskId).toBe("string");
|
|
1153
|
+
return [4 /*yield*/, api_1.BackgroundTask.findById(res.body.taskId)];
|
|
1154
|
+
case 2:
|
|
1155
|
+
task = _a.sent();
|
|
1156
|
+
(0, bun_test_1.expect)(task === null || task === void 0 ? void 0 : task.taskType).toBe("reindex-search");
|
|
1157
|
+
(0, bun_test_1.expect)(task === null || task === void 0 ? void 0 : task.status).toBe("pending");
|
|
1158
|
+
return [2 /*return*/];
|
|
1159
|
+
}
|
|
1160
|
+
});
|
|
1161
|
+
}); });
|
|
1162
|
+
(0, bun_test_1.it)("returns 403 for background-tasks when not admin", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1163
|
+
return __generator(this, function (_a) {
|
|
1164
|
+
switch (_a.label) {
|
|
1165
|
+
case 0: return [4 /*yield*/, notAdminAgent.post("/admin/background-tasks").send({ kind: "x" }).expect(403)];
|
|
1166
|
+
case 1:
|
|
1167
|
+
_a.sent();
|
|
1168
|
+
return [2 /*return*/];
|
|
1169
|
+
}
|
|
1170
|
+
});
|
|
1171
|
+
}); });
|
|
1172
|
+
(0, bun_test_1.it)("strips readonly fields from PATCH updates", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1173
|
+
var agent, food, reRead;
|
|
1174
|
+
return __generator(this, function (_a) {
|
|
1175
|
+
switch (_a.label) {
|
|
1176
|
+
case 0:
|
|
1177
|
+
app = buildApp([
|
|
1178
|
+
__assign(__assign({}, foodModelConfig), { readonlyFields: ["name"] }),
|
|
1179
|
+
]);
|
|
1180
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
1181
|
+
case 1:
|
|
1182
|
+
agent = _a.sent();
|
|
1183
|
+
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 10, name: "KeepMe" })];
|
|
1184
|
+
case 2:
|
|
1185
|
+
food = _a.sent();
|
|
1186
|
+
return [4 /*yield*/, agent.patch("/admin/foods/".concat(food._id)).send({ calories: 99, name: "Changed" }).expect(200)];
|
|
1187
|
+
case 3:
|
|
1188
|
+
_a.sent();
|
|
1189
|
+
return [4 /*yield*/, tests_1.FoodModel.findById(food._id).lean()];
|
|
1190
|
+
case 4:
|
|
1191
|
+
reRead = _a.sent();
|
|
1192
|
+
(0, bun_test_1.expect)(reRead === null || reRead === void 0 ? void 0 : reRead.name).toBe("KeepMe");
|
|
1193
|
+
(0, bun_test_1.expect)(reRead === null || reRead === void 0 ? void 0 : reRead.calories).toBe(99);
|
|
1194
|
+
return [2 /*return*/];
|
|
1195
|
+
}
|
|
1196
|
+
});
|
|
1197
|
+
}); });
|
|
1198
|
+
(0, bun_test_1.it)("strips readonly fields from POST create", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1199
|
+
var agent, res, stored;
|
|
1200
|
+
return __generator(this, function (_a) {
|
|
1201
|
+
switch (_a.label) {
|
|
1202
|
+
case 0:
|
|
1203
|
+
app = buildApp([
|
|
1204
|
+
__assign(__assign({}, foodModelConfig), { readonlyFields: ["name"] }),
|
|
1205
|
+
]);
|
|
1206
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
1207
|
+
case 1:
|
|
1208
|
+
agent = _a.sent();
|
|
1209
|
+
return [4 /*yield*/, agent
|
|
1210
|
+
.post("/admin/foods")
|
|
1211
|
+
.send({ calories: 5, name: "IgnoredName" })
|
|
1212
|
+
.expect(201)];
|
|
1213
|
+
case 2:
|
|
1214
|
+
res = _a.sent();
|
|
1215
|
+
(0, bun_test_1.expect)(res.body.data.name).toBeUndefined();
|
|
1216
|
+
(0, bun_test_1.expect)(res.body.data.calories).toBe(5);
|
|
1217
|
+
return [4 /*yield*/, tests_1.FoodModel.findById(res.body.data._id).lean()];
|
|
1218
|
+
case 3:
|
|
1219
|
+
stored = _a.sent();
|
|
1220
|
+
(0, bun_test_1.expect)(stored === null || stored === void 0 ? void 0 : stored.name).toBeUndefined();
|
|
1221
|
+
(0, bun_test_1.expect)(stored === null || stored === void 0 ? void 0 : stored.calories).toBe(5);
|
|
1222
|
+
return [2 /*return*/];
|
|
1223
|
+
}
|
|
1224
|
+
});
|
|
1225
|
+
}); });
|
|
1226
|
+
(0, bun_test_1.it)("disables DELETE when permissions.delete is false", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1227
|
+
var agent, food, res;
|
|
1228
|
+
return __generator(this, function (_a) {
|
|
1229
|
+
switch (_a.label) {
|
|
1230
|
+
case 0:
|
|
1231
|
+
app = buildApp([
|
|
1232
|
+
__assign(__assign({}, foodModelConfig), { permissions: { delete: false } }),
|
|
1233
|
+
]);
|
|
1234
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
1235
|
+
case 1:
|
|
1236
|
+
agent = _a.sent();
|
|
1237
|
+
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "Nope" })];
|
|
1238
|
+
case 2:
|
|
1239
|
+
food = _a.sent();
|
|
1240
|
+
return [4 /*yield*/, agent.delete("/admin/foods/".concat(food._id))];
|
|
1241
|
+
case 3:
|
|
1242
|
+
res = _a.sent();
|
|
1243
|
+
(0, bun_test_1.expect)([403, 405]).toContain(res.status);
|
|
1244
|
+
return [2 /*return*/];
|
|
1245
|
+
}
|
|
1246
|
+
});
|
|
1247
|
+
}); });
|
|
1248
|
+
});
|
|
1249
|
+
(0, bun_test_1.describe)("AdminApp onAdminAudit", function () {
|
|
1250
|
+
var app;
|
|
1251
|
+
var adminAgent;
|
|
1252
|
+
var auditEvents = [];
|
|
1253
|
+
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1254
|
+
return __generator(this, function (_a) {
|
|
1255
|
+
switch (_a.label) {
|
|
1256
|
+
case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
|
|
1257
|
+
case 1:
|
|
1258
|
+
_a.sent();
|
|
1259
|
+
auditEvents.length = 0;
|
|
1260
|
+
app = buildApp([__assign(__assign({}, foodModelConfig), { hiddenFields: ["hidden"] })], {
|
|
1261
|
+
onAdminAudit: function (event) { return __awaiter(void 0, void 0, void 0, function () {
|
|
1262
|
+
return __generator(this, function (_a) {
|
|
1263
|
+
auditEvents.push(event);
|
|
1264
|
+
return [2 /*return*/];
|
|
1265
|
+
});
|
|
1266
|
+
}); },
|
|
1267
|
+
});
|
|
1268
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
1269
|
+
case 2:
|
|
1270
|
+
adminAgent = _a.sent();
|
|
1271
|
+
return [2 /*return*/];
|
|
1272
|
+
}
|
|
1273
|
+
});
|
|
1274
|
+
}); });
|
|
1275
|
+
(0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1276
|
+
return __generator(this, function (_a) {
|
|
1277
|
+
switch (_a.label) {
|
|
1278
|
+
case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
|
|
1279
|
+
case 1:
|
|
1280
|
+
_a.sent();
|
|
1281
|
+
return [2 /*return*/];
|
|
1282
|
+
}
|
|
1283
|
+
});
|
|
1284
|
+
}); });
|
|
1285
|
+
(0, bun_test_1.it)("invokes onAdminAudit with verb created after POST", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1286
|
+
return __generator(this, function (_a) {
|
|
1287
|
+
switch (_a.label) {
|
|
1288
|
+
case 0: return [4 /*yield*/, adminAgent.post("/admin/foods").send({ calories: 5, name: "Audited" }).expect(201)];
|
|
1289
|
+
case 1:
|
|
1290
|
+
_a.sent();
|
|
1291
|
+
(0, bun_test_1.expect)(auditEvents.some(function (e) { return e.verb === "created" && e.modelName === "Food"; })).toBe(true);
|
|
1292
|
+
return [2 /*return*/];
|
|
1293
|
+
}
|
|
1294
|
+
});
|
|
1295
|
+
}); });
|
|
1296
|
+
(0, bun_test_1.it)("invokes onAdminAudit with verb updated after PATCH", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1297
|
+
var food;
|
|
1298
|
+
return __generator(this, function (_a) {
|
|
1299
|
+
switch (_a.label) {
|
|
1300
|
+
case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 10, name: "PatchMe" })];
|
|
1301
|
+
case 1:
|
|
1302
|
+
food = _a.sent();
|
|
1303
|
+
return [4 /*yield*/, adminAgent.patch("/admin/foods/".concat(food._id)).send({ calories: 42 }).expect(200)];
|
|
1304
|
+
case 2:
|
|
1305
|
+
_a.sent();
|
|
1306
|
+
(0, bun_test_1.expect)(auditEvents.some(function (e) { return e.verb === "updated" && e.modelName === "Food"; })).toBe(true);
|
|
1307
|
+
return [2 /*return*/];
|
|
1308
|
+
}
|
|
1309
|
+
});
|
|
1310
|
+
}); });
|
|
1311
|
+
(0, bun_test_1.it)("invokes onAdminAudit with verb deleted after DELETE", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1312
|
+
var food;
|
|
1313
|
+
return __generator(this, function (_a) {
|
|
1314
|
+
switch (_a.label) {
|
|
1315
|
+
case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "DeleteMe" })];
|
|
1316
|
+
case 1:
|
|
1317
|
+
food = _a.sent();
|
|
1318
|
+
return [4 /*yield*/, adminAgent.delete("/admin/foods/".concat(food._id)).expect(204)];
|
|
1319
|
+
case 2:
|
|
1320
|
+
_a.sent();
|
|
1321
|
+
(0, bun_test_1.expect)(auditEvents.some(function (e) { return e.verb === "deleted" && e.modelName === "Food"; })).toBe(true);
|
|
1322
|
+
return [2 /*return*/];
|
|
1323
|
+
}
|
|
1324
|
+
});
|
|
1325
|
+
}); });
|
|
1326
|
+
});
|
|
1327
|
+
(0, bun_test_1.describe)("AdminApp onAdminAudit is best-effort", function () {
|
|
1328
|
+
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1329
|
+
return __generator(this, function (_a) {
|
|
1330
|
+
switch (_a.label) {
|
|
1331
|
+
case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
|
|
1332
|
+
case 1:
|
|
1333
|
+
_a.sent();
|
|
1334
|
+
return [2 /*return*/];
|
|
1335
|
+
}
|
|
1336
|
+
});
|
|
1337
|
+
}); });
|
|
1338
|
+
(0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1339
|
+
return __generator(this, function (_a) {
|
|
1340
|
+
switch (_a.label) {
|
|
1341
|
+
case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
|
|
1342
|
+
case 1:
|
|
1343
|
+
_a.sent();
|
|
1344
|
+
return [2 /*return*/];
|
|
1345
|
+
}
|
|
1346
|
+
});
|
|
1347
|
+
}); });
|
|
1348
|
+
var throwingAudit = {
|
|
1349
|
+
onAdminAudit: function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1350
|
+
return __generator(this, function (_a) {
|
|
1351
|
+
throw new Error("audit sink failure");
|
|
1352
|
+
});
|
|
1353
|
+
}); },
|
|
1354
|
+
};
|
|
1355
|
+
(0, bun_test_1.it)("returns 201 on POST when onAdminAudit throws", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1356
|
+
var localApp, agent, res, stored;
|
|
1357
|
+
return __generator(this, function (_a) {
|
|
1358
|
+
switch (_a.label) {
|
|
1359
|
+
case 0:
|
|
1360
|
+
localApp = buildApp([foodModelConfig], throwingAudit);
|
|
1361
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(localApp, "admin")];
|
|
1362
|
+
case 1:
|
|
1363
|
+
agent = _a.sent();
|
|
1364
|
+
return [4 /*yield*/, agent
|
|
1365
|
+
.post("/admin/foods")
|
|
1366
|
+
.send({ calories: 5, name: "StillCreated" })
|
|
1367
|
+
.expect(201)];
|
|
1368
|
+
case 2:
|
|
1369
|
+
res = _a.sent();
|
|
1370
|
+
return [4 /*yield*/, tests_1.FoodModel.findById(res.body.data._id).lean()];
|
|
1371
|
+
case 3:
|
|
1372
|
+
stored = _a.sent();
|
|
1373
|
+
(0, bun_test_1.expect)(stored === null || stored === void 0 ? void 0 : stored.name).toBe("StillCreated");
|
|
1374
|
+
return [2 /*return*/];
|
|
1375
|
+
}
|
|
1376
|
+
});
|
|
1377
|
+
}); });
|
|
1378
|
+
(0, bun_test_1.it)("returns 200 on PATCH when onAdminAudit throws", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1379
|
+
var localApp, agent, food, res, stored;
|
|
1380
|
+
return __generator(this, function (_a) {
|
|
1381
|
+
switch (_a.label) {
|
|
1382
|
+
case 0:
|
|
1383
|
+
localApp = buildApp([foodModelConfig], throwingAudit);
|
|
1384
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(localApp, "admin")];
|
|
1385
|
+
case 1:
|
|
1386
|
+
agent = _a.sent();
|
|
1387
|
+
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 10, name: "PatchMe" })];
|
|
1388
|
+
case 2:
|
|
1389
|
+
food = _a.sent();
|
|
1390
|
+
return [4 /*yield*/, agent.patch("/admin/foods/".concat(food._id)).send({ calories: 99 }).expect(200)];
|
|
1391
|
+
case 3:
|
|
1392
|
+
res = _a.sent();
|
|
1393
|
+
(0, bun_test_1.expect)(res.body.data.calories).toBe(99);
|
|
1394
|
+
return [4 /*yield*/, tests_1.FoodModel.findById(food._id).lean()];
|
|
1395
|
+
case 4:
|
|
1396
|
+
stored = _a.sent();
|
|
1397
|
+
(0, bun_test_1.expect)(stored === null || stored === void 0 ? void 0 : stored.calories).toBe(99);
|
|
1398
|
+
return [2 /*return*/];
|
|
1399
|
+
}
|
|
1400
|
+
});
|
|
1401
|
+
}); });
|
|
1402
|
+
(0, bun_test_1.it)("returns 204 on DELETE when onAdminAudit throws", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1403
|
+
var localApp, agent, food, _a;
|
|
1404
|
+
return __generator(this, function (_b) {
|
|
1405
|
+
switch (_b.label) {
|
|
1406
|
+
case 0:
|
|
1407
|
+
localApp = buildApp([foodModelConfig], throwingAudit);
|
|
1408
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(localApp, "admin")];
|
|
1409
|
+
case 1:
|
|
1410
|
+
agent = _b.sent();
|
|
1411
|
+
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "DeleteMe" })];
|
|
1412
|
+
case 2:
|
|
1413
|
+
food = _b.sent();
|
|
1414
|
+
return [4 /*yield*/, agent.delete("/admin/foods/".concat(food._id)).expect(204)];
|
|
1415
|
+
case 3:
|
|
1416
|
+
_b.sent();
|
|
1417
|
+
_a = bun_test_1.expect;
|
|
1418
|
+
return [4 /*yield*/, tests_1.FoodModel.findById(food._id)];
|
|
1419
|
+
case 4:
|
|
1420
|
+
_a.apply(void 0, [_b.sent()]).toBeNull();
|
|
1421
|
+
return [2 /*return*/];
|
|
1422
|
+
}
|
|
1423
|
+
});
|
|
1424
|
+
}); });
|
|
1425
|
+
});
|
|
1426
|
+
(0, bun_test_1.describe)("AdminApp per-model queryFilter", function () {
|
|
1427
|
+
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1428
|
+
return __generator(this, function (_a) {
|
|
1429
|
+
switch (_a.label) {
|
|
1430
|
+
case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
|
|
1431
|
+
case 1:
|
|
1432
|
+
_a.sent();
|
|
1433
|
+
return [2 /*return*/];
|
|
1434
|
+
}
|
|
1435
|
+
});
|
|
1436
|
+
}); });
|
|
1437
|
+
(0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1438
|
+
return __generator(this, function (_a) {
|
|
1439
|
+
switch (_a.label) {
|
|
1440
|
+
case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
|
|
1441
|
+
case 1:
|
|
1442
|
+
_a.sent();
|
|
1443
|
+
return [4 /*yield*/, api_1.VersionConfig.deleteMany({})];
|
|
1444
|
+
case 2:
|
|
1445
|
+
_a.sent();
|
|
1446
|
+
return [2 /*return*/];
|
|
1447
|
+
}
|
|
1448
|
+
});
|
|
1449
|
+
}); });
|
|
1450
|
+
(0, bun_test_1.it)("returns an empty list when queryFilter returns null", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1451
|
+
var localApp, agent, res;
|
|
1452
|
+
return __generator(this, function (_a) {
|
|
1453
|
+
switch (_a.label) {
|
|
1454
|
+
case 0:
|
|
1455
|
+
localApp = buildApp([
|
|
1456
|
+
__assign(__assign({}, foodModelConfig), { queryFilter: function () { return null; } }),
|
|
1457
|
+
]);
|
|
1458
|
+
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "HiddenRow" })];
|
|
1459
|
+
case 1:
|
|
1460
|
+
_a.sent();
|
|
1461
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(localApp, "admin")];
|
|
1462
|
+
case 2:
|
|
1463
|
+
agent = _a.sent();
|
|
1464
|
+
return [4 /*yield*/, agent.get("/admin/foods").expect(200)];
|
|
1465
|
+
case 3:
|
|
1466
|
+
res = _a.sent();
|
|
1467
|
+
(0, bun_test_1.expect)(res.body.data).toEqual([]);
|
|
1468
|
+
return [2 /*return*/];
|
|
1469
|
+
}
|
|
1470
|
+
});
|
|
1471
|
+
}); });
|
|
1472
|
+
(0, bun_test_1.it)("merges queryFilter constraints into list queries for that model only", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
1473
|
+
var localApp, agent, res;
|
|
1474
|
+
return __generator(this, function (_a) {
|
|
1475
|
+
switch (_a.label) {
|
|
1476
|
+
case 0:
|
|
1477
|
+
localApp = buildApp([
|
|
1478
|
+
__assign(__assign({}, foodModelConfig), { queryFilter: function (_user, query) { return (__assign(__assign({}, (query !== null && query !== void 0 ? query : {})), { name: "FilteredOnly" })); } }),
|
|
1479
|
+
]);
|
|
1480
|
+
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "Other" })];
|
|
1481
|
+
case 1:
|
|
1482
|
+
_a.sent();
|
|
1483
|
+
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 2, name: "FilteredOnly" })];
|
|
1484
|
+
case 2:
|
|
1485
|
+
_a.sent();
|
|
1486
|
+
return [4 /*yield*/, (0, tests_1.authAsUser)(localApp, "admin")];
|
|
1487
|
+
case 3:
|
|
1488
|
+
agent = _a.sent();
|
|
1489
|
+
return [4 /*yield*/, agent.get("/admin/foods").expect(200)];
|
|
1490
|
+
case 4:
|
|
1491
|
+
res = _a.sent();
|
|
1492
|
+
(0, bun_test_1.expect)(res.body.data).toHaveLength(1);
|
|
1493
|
+
(0, bun_test_1.expect)(res.body.data[0].name).toBe("FilteredOnly");
|
|
1494
|
+
return [2 /*return*/];
|
|
1495
|
+
}
|
|
1496
|
+
});
|
|
1497
|
+
}); });
|
|
1498
|
+
});
|