@terreno/admin-backend 0.20.2 → 0.22.1
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/adminUiV2.d.ts +96 -0
- package/dist/adminUiV2.js +189 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +15 -0
- package/package.json +5 -4
- package/src/adminApp.models.test.ts +285 -3
- package/src/adminApp.test.ts +96 -1
- package/src/adminApp.ts +598 -22
- package/src/adminUiV2.test.ts +28 -0
- package/src/adminUiV2.ts +188 -0
- package/src/documentStorageApp.test.ts +1 -1
- package/src/index.ts +9 -1
- package/src/tests/bunSetup.ts +9 -0
- package/tsconfig.json +1 -1
- package/dist/adminApp.models.test.d.ts +0 -1
- package/dist/adminApp.models.test.js +0 -969
- package/dist/adminApp.test.d.ts +0 -1
- package/dist/adminApp.test.js +0 -564
- package/dist/documentStorageApp.test.d.ts +0 -1
- package/dist/documentStorageApp.test.js +0 -874
|
@@ -1,969 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
-
if (k2 === undefined) k2 = k;
|
|
15
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
16
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
17
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
18
|
-
}
|
|
19
|
-
Object.defineProperty(o, k2, desc);
|
|
20
|
-
}) : (function(o, m, k, k2) {
|
|
21
|
-
if (k2 === undefined) k2 = k;
|
|
22
|
-
o[k2] = m[k];
|
|
23
|
-
}));
|
|
24
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
25
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
26
|
-
}) : function(o, v) {
|
|
27
|
-
o["default"] = v;
|
|
28
|
-
});
|
|
29
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
30
|
-
var ownKeys = function(o) {
|
|
31
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
32
|
-
var ar = [];
|
|
33
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
34
|
-
return ar;
|
|
35
|
-
};
|
|
36
|
-
return ownKeys(o);
|
|
37
|
-
};
|
|
38
|
-
return function (mod) {
|
|
39
|
-
if (mod && mod.__esModule) return mod;
|
|
40
|
-
var result = {};
|
|
41
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
42
|
-
__setModuleDefault(result, mod);
|
|
43
|
-
return result;
|
|
44
|
-
};
|
|
45
|
-
})();
|
|
46
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
47
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
48
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
49
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
50
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
51
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
52
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
56
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
57
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
58
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
59
|
-
function step(op) {
|
|
60
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
61
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
62
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
63
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
64
|
-
switch (op[0]) {
|
|
65
|
-
case 0: case 1: t = op; break;
|
|
66
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
67
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
68
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
69
|
-
default:
|
|
70
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
71
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
72
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
73
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
74
|
-
if (t[2]) _.ops.pop();
|
|
75
|
-
_.trys.pop(); continue;
|
|
76
|
-
}
|
|
77
|
-
op = body.call(thisArg, _);
|
|
78
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
79
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
83
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
84
|
-
if (!m) return o;
|
|
85
|
-
var i = m.call(o), r, ar = [], e;
|
|
86
|
-
try {
|
|
87
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
88
|
-
}
|
|
89
|
-
catch (error) { e = { error: error }; }
|
|
90
|
-
finally {
|
|
91
|
-
try {
|
|
92
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
93
|
-
}
|
|
94
|
-
finally { if (e) throw e.error; }
|
|
95
|
-
}
|
|
96
|
-
return ar;
|
|
97
|
-
};
|
|
98
|
-
var __values = (this && this.__values) || function(o) {
|
|
99
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
100
|
-
if (m) return m.call(o);
|
|
101
|
-
if (o && typeof o.length === "number") return {
|
|
102
|
-
next: function () {
|
|
103
|
-
if (o && i >= o.length) o = void 0;
|
|
104
|
-
return { value: o && o[i++], done: !o };
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
108
|
-
};
|
|
109
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
110
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
111
|
-
};
|
|
112
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
113
|
-
var bun_test_1 = require("bun:test");
|
|
114
|
-
var api_1 = require("@terreno/api");
|
|
115
|
-
var tests_1 = require("@terreno/api/src/tests");
|
|
116
|
-
var supertest_1 = __importDefault(require("supertest"));
|
|
117
|
-
var adminApp_1 = require("./adminApp");
|
|
118
|
-
var buildApp = function (models) {
|
|
119
|
-
if (models === void 0) { models = []; }
|
|
120
|
-
var app = (0, tests_1.getBaseServer)();
|
|
121
|
-
(0, api_1.setupAuth)(app, tests_1.UserModel);
|
|
122
|
-
(0, api_1.addAuthRoutes)(app, tests_1.UserModel);
|
|
123
|
-
var admin = new adminApp_1.AdminApp({
|
|
124
|
-
basePath: "/admin",
|
|
125
|
-
models: models,
|
|
126
|
-
});
|
|
127
|
-
admin.register(app);
|
|
128
|
-
app.use(api_1.apiUnauthorizedMiddleware);
|
|
129
|
-
app.use(api_1.apiErrorMiddleware);
|
|
130
|
-
return app;
|
|
131
|
-
};
|
|
132
|
-
var foodModelConfig = {
|
|
133
|
-
displayName: "Foods",
|
|
134
|
-
listFields: ["name", "calories"],
|
|
135
|
-
model: tests_1.FoodModel,
|
|
136
|
-
routePath: "/foods",
|
|
137
|
-
};
|
|
138
|
-
(0, bun_test_1.describe)("AdminApp /admin/config", function () {
|
|
139
|
-
var app;
|
|
140
|
-
var adminAgent;
|
|
141
|
-
var notAdminAgent;
|
|
142
|
-
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
143
|
-
return __generator(this, function (_a) {
|
|
144
|
-
switch (_a.label) {
|
|
145
|
-
case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
|
|
146
|
-
case 1:
|
|
147
|
-
_a.sent();
|
|
148
|
-
app = buildApp([
|
|
149
|
-
__assign(__assign({}, foodModelConfig), { fieldOrder: ["name", "calories", "tags"], fieldOverrides: { name: { widget: "markdown" } }, hiddenFields: ["hidden"] }),
|
|
150
|
-
]);
|
|
151
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
152
|
-
case 2:
|
|
153
|
-
adminAgent = _a.sent();
|
|
154
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
|
|
155
|
-
case 3:
|
|
156
|
-
notAdminAgent = _a.sent();
|
|
157
|
-
return [2 /*return*/];
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
}); });
|
|
161
|
-
(0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
162
|
-
return __generator(this, function (_a) {
|
|
163
|
-
switch (_a.label) {
|
|
164
|
-
case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
|
|
165
|
-
case 1:
|
|
166
|
-
_a.sent();
|
|
167
|
-
return [4 /*yield*/, api_1.VersionConfig.deleteMany({})];
|
|
168
|
-
case 2:
|
|
169
|
-
_a.sent();
|
|
170
|
-
return [2 /*return*/];
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
}); });
|
|
174
|
-
(0, bun_test_1.it)("returns metadata for configured models to admins", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
175
|
-
var res, _a, foodMeta;
|
|
176
|
-
return __generator(this, function (_b) {
|
|
177
|
-
switch (_b.label) {
|
|
178
|
-
case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
|
|
179
|
-
case 1:
|
|
180
|
-
res = _b.sent();
|
|
181
|
-
(0, bun_test_1.expect)(res.body.models).toHaveLength(1);
|
|
182
|
-
_a = __read(res.body.models, 1), foodMeta = _a[0];
|
|
183
|
-
(0, bun_test_1.expect)(foodMeta.name).toBe("Food");
|
|
184
|
-
(0, bun_test_1.expect)(foodMeta.displayName).toBe("Foods");
|
|
185
|
-
(0, bun_test_1.expect)(foodMeta.listFields).toEqual(["name", "calories"]);
|
|
186
|
-
(0, bun_test_1.expect)(foodMeta.routePath).toBe("/admin/foods");
|
|
187
|
-
(0, bun_test_1.expect)(foodMeta.defaultSort).toBe("-created");
|
|
188
|
-
(0, bun_test_1.expect)(foodMeta.fieldOrder).toEqual(["name", "calories", "tags"]);
|
|
189
|
-
return [2 /*return*/];
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
}); });
|
|
193
|
-
(0, bun_test_1.it)("applies fieldOverrides to generated config", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
194
|
-
var res, _a, foodMeta;
|
|
195
|
-
return __generator(this, function (_b) {
|
|
196
|
-
switch (_b.label) {
|
|
197
|
-
case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
|
|
198
|
-
case 1:
|
|
199
|
-
res = _b.sent();
|
|
200
|
-
_a = __read(res.body.models, 1), foodMeta = _a[0];
|
|
201
|
-
(0, bun_test_1.expect)(foodMeta.fields.name.widget).toBe("markdown");
|
|
202
|
-
return [2 /*return*/];
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
}); });
|
|
206
|
-
(0, bun_test_1.it)("removes hidden fields from config and listFields", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
207
|
-
var agent, res, _a, foodMeta;
|
|
208
|
-
return __generator(this, function (_b) {
|
|
209
|
-
switch (_b.label) {
|
|
210
|
-
case 0:
|
|
211
|
-
app = buildApp([
|
|
212
|
-
__assign(__assign({}, foodModelConfig), { hiddenFields: ["hidden"], listFields: ["name", "hidden", "calories"] }),
|
|
213
|
-
]);
|
|
214
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
215
|
-
case 1:
|
|
216
|
-
agent = _b.sent();
|
|
217
|
-
return [4 /*yield*/, agent.get("/admin/config").expect(200)];
|
|
218
|
-
case 2:
|
|
219
|
-
res = _b.sent();
|
|
220
|
-
_a = __read(res.body.models, 1), foodMeta = _a[0];
|
|
221
|
-
(0, bun_test_1.expect)(foodMeta.fields.hidden).toBeUndefined();
|
|
222
|
-
(0, bun_test_1.expect)(foodMeta.listFields).toEqual(["name", "calories"]);
|
|
223
|
-
return [2 /*return*/];
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
}); });
|
|
227
|
-
(0, bun_test_1.it)("extracts item sub-field metadata for array fields", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
228
|
-
var res, _a, foodMeta;
|
|
229
|
-
return __generator(this, function (_b) {
|
|
230
|
-
switch (_b.label) {
|
|
231
|
-
case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
|
|
232
|
-
case 1:
|
|
233
|
-
res = _b.sent();
|
|
234
|
-
_a = __read(res.body.models, 1), foodMeta = _a[0];
|
|
235
|
-
// likesIds is an array of subdocuments with likes + userId sub-fields
|
|
236
|
-
(0, bun_test_1.expect)(foodMeta.fields.likesIds.type).toBe("array");
|
|
237
|
-
(0, bun_test_1.expect)(foodMeta.fields.likesIds.items).toBeDefined();
|
|
238
|
-
(0, bun_test_1.expect)(foodMeta.fields.likesIds.items.userId).toBeDefined();
|
|
239
|
-
(0, bun_test_1.expect)(foodMeta.fields.likesIds.items.likes).toBeDefined();
|
|
240
|
-
return [2 /*return*/];
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
}); });
|
|
244
|
-
(0, bun_test_1.it)("extracts ref information for ObjectId fields", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
245
|
-
var res, _a, foodMeta;
|
|
246
|
-
return __generator(this, function (_b) {
|
|
247
|
-
switch (_b.label) {
|
|
248
|
-
case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
|
|
249
|
-
case 1:
|
|
250
|
-
res = _b.sent();
|
|
251
|
-
_a = __read(res.body.models, 1), foodMeta = _a[0];
|
|
252
|
-
(0, bun_test_1.expect)(foodMeta.fields.ownerId.ref).toBe("User");
|
|
253
|
-
return [2 /*return*/];
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
}); });
|
|
257
|
-
(0, bun_test_1.it)("extracts ref information for array of ObjectId references", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
258
|
-
var res, _a, foodMeta;
|
|
259
|
-
return __generator(this, function (_b) {
|
|
260
|
-
switch (_b.label) {
|
|
261
|
-
case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
|
|
262
|
-
case 1:
|
|
263
|
-
res = _b.sent();
|
|
264
|
-
_a = __read(res.body.models, 1), foodMeta = _a[0];
|
|
265
|
-
(0, bun_test_1.expect)(foodMeta.fields.eatenBy.ref).toBe("User");
|
|
266
|
-
return [2 /*return*/];
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
}); });
|
|
270
|
-
(0, bun_test_1.it)("extracts itemType for primitive string arrays", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
271
|
-
var res, _a, foodMeta;
|
|
272
|
-
return __generator(this, function (_b) {
|
|
273
|
-
switch (_b.label) {
|
|
274
|
-
case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
|
|
275
|
-
case 1:
|
|
276
|
-
res = _b.sent();
|
|
277
|
-
_a = __read(res.body.models, 1), foodMeta = _a[0];
|
|
278
|
-
// tags is [String] — should expose itemType so the frontend renders a primitive list
|
|
279
|
-
(0, bun_test_1.expect)(foodMeta.fields.tags.type).toBe("array");
|
|
280
|
-
(0, bun_test_1.expect)(foodMeta.fields.tags.itemType).toBe("string");
|
|
281
|
-
(0, bun_test_1.expect)(foodMeta.fields.tags.items).toBeUndefined();
|
|
282
|
-
return [2 /*return*/];
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
}); });
|
|
286
|
-
(0, bun_test_1.it)("extracts itemType and itemRef for arrays of ObjectId references", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
287
|
-
var res, _a, foodMeta;
|
|
288
|
-
return __generator(this, function (_b) {
|
|
289
|
-
switch (_b.label) {
|
|
290
|
-
case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
|
|
291
|
-
case 1:
|
|
292
|
-
res = _b.sent();
|
|
293
|
-
_a = __read(res.body.models, 1), foodMeta = _a[0];
|
|
294
|
-
// eatenBy is [{type: ObjectId, ref: "User"}] — should expose both itemType and itemRef
|
|
295
|
-
(0, bun_test_1.expect)(foodMeta.fields.eatenBy.type).toBe("array");
|
|
296
|
-
(0, bun_test_1.expect)(foodMeta.fields.eatenBy.itemType).toBe("objectid");
|
|
297
|
-
(0, bun_test_1.expect)(foodMeta.fields.eatenBy.itemRef).toBe("User");
|
|
298
|
-
return [2 /*return*/];
|
|
299
|
-
}
|
|
300
|
-
});
|
|
301
|
-
}); });
|
|
302
|
-
(0, bun_test_1.it)("returns 403 for non-admin users", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
303
|
-
var res;
|
|
304
|
-
return __generator(this, function (_a) {
|
|
305
|
-
switch (_a.label) {
|
|
306
|
-
case 0: return [4 /*yield*/, notAdminAgent.get("/admin/config").expect(403)];
|
|
307
|
-
case 1:
|
|
308
|
-
res = _a.sent();
|
|
309
|
-
(0, bun_test_1.expect)(res.body.title).toInclude("Admin access required");
|
|
310
|
-
return [2 /*return*/];
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
}); });
|
|
314
|
-
(0, bun_test_1.it)("returns 401 for unauthenticated users", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
315
|
-
return __generator(this, function (_a) {
|
|
316
|
-
switch (_a.label) {
|
|
317
|
-
case 0: return [4 /*yield*/, (0, supertest_1.default)(app).get("/admin/config").expect(401)];
|
|
318
|
-
case 1:
|
|
319
|
-
_a.sent();
|
|
320
|
-
return [2 /*return*/];
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
}); });
|
|
324
|
-
(0, bun_test_1.it)("defaults basePath to /admin when not provided", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
325
|
-
var altApp, agent, res;
|
|
326
|
-
return __generator(this, function (_a) {
|
|
327
|
-
switch (_a.label) {
|
|
328
|
-
case 0:
|
|
329
|
-
altApp = (0, tests_1.getBaseServer)();
|
|
330
|
-
(0, api_1.setupAuth)(altApp, tests_1.UserModel);
|
|
331
|
-
(0, api_1.addAuthRoutes)(altApp, tests_1.UserModel);
|
|
332
|
-
new adminApp_1.AdminApp({ models: [foodModelConfig] }).register(altApp);
|
|
333
|
-
altApp.use(api_1.apiUnauthorizedMiddleware);
|
|
334
|
-
altApp.use(api_1.apiErrorMiddleware);
|
|
335
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(altApp, "admin")];
|
|
336
|
-
case 1:
|
|
337
|
-
agent = _a.sent();
|
|
338
|
-
return [4 /*yield*/, agent.get("/admin/config").expect(200)];
|
|
339
|
-
case 2:
|
|
340
|
-
res = _a.sent();
|
|
341
|
-
(0, bun_test_1.expect)(res.body.models[0].routePath).toBe("/admin/foods");
|
|
342
|
-
return [2 /*return*/];
|
|
343
|
-
}
|
|
344
|
-
});
|
|
345
|
-
}); });
|
|
346
|
-
(0, bun_test_1.it)("uses a custom defaultSort when provided", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
347
|
-
var agent, res;
|
|
348
|
-
return __generator(this, function (_a) {
|
|
349
|
-
switch (_a.label) {
|
|
350
|
-
case 0:
|
|
351
|
-
app = buildApp([__assign(__assign({}, foodModelConfig), { defaultSort: "name" })]);
|
|
352
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
353
|
-
case 1:
|
|
354
|
-
agent = _a.sent();
|
|
355
|
-
return [4 /*yield*/, agent.get("/admin/config").expect(200)];
|
|
356
|
-
case 2:
|
|
357
|
-
res = _a.sent();
|
|
358
|
-
(0, bun_test_1.expect)(res.body.models[0].defaultSort).toBe("name");
|
|
359
|
-
return [2 /*return*/];
|
|
360
|
-
}
|
|
361
|
-
});
|
|
362
|
-
}); });
|
|
363
|
-
(0, bun_test_1.it)("includes version-config custom screen", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
364
|
-
var res;
|
|
365
|
-
return __generator(this, function (_a) {
|
|
366
|
-
switch (_a.label) {
|
|
367
|
-
case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
|
|
368
|
-
case 1:
|
|
369
|
-
res = _a.sent();
|
|
370
|
-
(0, bun_test_1.expect)(res.body.customScreens).toEqual([
|
|
371
|
-
{ displayName: "Version Config", name: "version-config" },
|
|
372
|
-
]);
|
|
373
|
-
return [2 /*return*/];
|
|
374
|
-
}
|
|
375
|
-
});
|
|
376
|
-
}); });
|
|
377
|
-
});
|
|
378
|
-
(0, bun_test_1.describe)("AdminApp model CRUD routes", function () {
|
|
379
|
-
var app;
|
|
380
|
-
var adminAgent;
|
|
381
|
-
var notAdminAgent;
|
|
382
|
-
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
383
|
-
return __generator(this, function (_a) {
|
|
384
|
-
switch (_a.label) {
|
|
385
|
-
case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
|
|
386
|
-
case 1:
|
|
387
|
-
_a.sent();
|
|
388
|
-
app = buildApp([__assign(__assign({}, foodModelConfig), { hiddenFields: ["hidden"] })]);
|
|
389
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
390
|
-
case 2:
|
|
391
|
-
adminAgent = _a.sent();
|
|
392
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
|
|
393
|
-
case 3:
|
|
394
|
-
notAdminAgent = _a.sent();
|
|
395
|
-
return [2 /*return*/];
|
|
396
|
-
}
|
|
397
|
-
});
|
|
398
|
-
}); });
|
|
399
|
-
(0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
400
|
-
return __generator(this, function (_a) {
|
|
401
|
-
switch (_a.label) {
|
|
402
|
-
case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
|
|
403
|
-
case 1:
|
|
404
|
-
_a.sent();
|
|
405
|
-
return [2 /*return*/];
|
|
406
|
-
}
|
|
407
|
-
});
|
|
408
|
-
}); });
|
|
409
|
-
(0, bun_test_1.it)("creates documents via POST and strips hidden fields from response", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
410
|
-
var res;
|
|
411
|
-
return __generator(this, function (_a) {
|
|
412
|
-
switch (_a.label) {
|
|
413
|
-
case 0: return [4 /*yield*/, adminAgent
|
|
414
|
-
.post("/admin/foods")
|
|
415
|
-
.send({ calories: 120, hidden: true, name: "Apple" })
|
|
416
|
-
.expect(201)];
|
|
417
|
-
case 1:
|
|
418
|
-
res = _a.sent();
|
|
419
|
-
(0, bun_test_1.expect)(res.body.data.name).toBe("Apple");
|
|
420
|
-
(0, bun_test_1.expect)(res.body.data.calories).toBe(120);
|
|
421
|
-
(0, bun_test_1.expect)(res.body.data.hidden).toBeUndefined();
|
|
422
|
-
return [2 /*return*/];
|
|
423
|
-
}
|
|
424
|
-
});
|
|
425
|
-
}); });
|
|
426
|
-
(0, bun_test_1.it)("lists documents via GET and strips hidden fields from results", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
427
|
-
var res, _a, _b, item;
|
|
428
|
-
var e_1, _c;
|
|
429
|
-
return __generator(this, function (_d) {
|
|
430
|
-
switch (_d.label) {
|
|
431
|
-
case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, hidden: true, name: "Apple" })];
|
|
432
|
-
case 1:
|
|
433
|
-
_d.sent();
|
|
434
|
-
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 95, hidden: false, name: "Banana" })];
|
|
435
|
-
case 2:
|
|
436
|
-
_d.sent();
|
|
437
|
-
return [4 /*yield*/, adminAgent.get("/admin/foods").expect(200)];
|
|
438
|
-
case 3:
|
|
439
|
-
res = _d.sent();
|
|
440
|
-
(0, bun_test_1.expect)(res.body.data).toHaveLength(2);
|
|
441
|
-
try {
|
|
442
|
-
for (_a = __values(res.body.data), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
443
|
-
item = _b.value;
|
|
444
|
-
(0, bun_test_1.expect)(item.hidden).toBeUndefined();
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
448
|
-
finally {
|
|
449
|
-
try {
|
|
450
|
-
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
451
|
-
}
|
|
452
|
-
finally { if (e_1) throw e_1.error; }
|
|
453
|
-
}
|
|
454
|
-
return [2 /*return*/];
|
|
455
|
-
}
|
|
456
|
-
});
|
|
457
|
-
}); });
|
|
458
|
-
(0, bun_test_1.it)("reads a document via GET /:id", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
459
|
-
var food, res;
|
|
460
|
-
return __generator(this, function (_a) {
|
|
461
|
-
switch (_a.label) {
|
|
462
|
-
case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, name: "Apple" })];
|
|
463
|
-
case 1:
|
|
464
|
-
food = _a.sent();
|
|
465
|
-
return [4 /*yield*/, adminAgent.get("/admin/foods/".concat(food._id)).expect(200)];
|
|
466
|
-
case 2:
|
|
467
|
-
res = _a.sent();
|
|
468
|
-
(0, bun_test_1.expect)(res.body.data.name).toBe("Apple");
|
|
469
|
-
return [2 /*return*/];
|
|
470
|
-
}
|
|
471
|
-
});
|
|
472
|
-
}); });
|
|
473
|
-
(0, bun_test_1.it)("updates a document via PATCH /:id", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
474
|
-
var food, res;
|
|
475
|
-
return __generator(this, function (_a) {
|
|
476
|
-
switch (_a.label) {
|
|
477
|
-
case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, name: "Apple" })];
|
|
478
|
-
case 1:
|
|
479
|
-
food = _a.sent();
|
|
480
|
-
return [4 /*yield*/, adminAgent
|
|
481
|
-
.patch("/admin/foods/".concat(food._id))
|
|
482
|
-
.send({ calories: 150 })
|
|
483
|
-
.expect(200)];
|
|
484
|
-
case 2:
|
|
485
|
-
res = _a.sent();
|
|
486
|
-
(0, bun_test_1.expect)(res.body.data.calories).toBe(150);
|
|
487
|
-
return [2 /*return*/];
|
|
488
|
-
}
|
|
489
|
-
});
|
|
490
|
-
}); });
|
|
491
|
-
(0, bun_test_1.it)("deletes a document via DELETE /:id", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
492
|
-
var food, _a;
|
|
493
|
-
return __generator(this, function (_b) {
|
|
494
|
-
switch (_b.label) {
|
|
495
|
-
case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, name: "Apple" })];
|
|
496
|
-
case 1:
|
|
497
|
-
food = _b.sent();
|
|
498
|
-
return [4 /*yield*/, adminAgent.delete("/admin/foods/".concat(food._id)).expect(204)];
|
|
499
|
-
case 2:
|
|
500
|
-
_b.sent();
|
|
501
|
-
_a = bun_test_1.expect;
|
|
502
|
-
return [4 /*yield*/, tests_1.FoodModel.findById(food._id)];
|
|
503
|
-
case 3:
|
|
504
|
-
_a.apply(void 0, [_b.sent()]).toBeNull();
|
|
505
|
-
return [2 /*return*/];
|
|
506
|
-
}
|
|
507
|
-
});
|
|
508
|
-
}); });
|
|
509
|
-
(0, bun_test_1.it)("rejects non-admin users from model list", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
510
|
-
var res;
|
|
511
|
-
return __generator(this, function (_a) {
|
|
512
|
-
switch (_a.label) {
|
|
513
|
-
case 0: return [4 /*yield*/, notAdminAgent.get("/admin/foods")];
|
|
514
|
-
case 1:
|
|
515
|
-
res = _a.sent();
|
|
516
|
-
// modelRouter with IsAdmin permission blocks non-admin users
|
|
517
|
-
(0, bun_test_1.expect)([401, 403, 405]).toContain(res.status);
|
|
518
|
-
return [2 /*return*/];
|
|
519
|
-
}
|
|
520
|
-
});
|
|
521
|
-
}); });
|
|
522
|
-
(0, bun_test_1.it)("supports models without hiddenFields (no responseHandler installed)", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
523
|
-
var agent, res;
|
|
524
|
-
return __generator(this, function (_a) {
|
|
525
|
-
switch (_a.label) {
|
|
526
|
-
case 0:
|
|
527
|
-
app = buildApp([foodModelConfig]);
|
|
528
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
529
|
-
case 1:
|
|
530
|
-
agent = _a.sent();
|
|
531
|
-
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, hidden: true, name: "Apple" })];
|
|
532
|
-
case 2:
|
|
533
|
-
_a.sent();
|
|
534
|
-
return [4 /*yield*/, agent.get("/admin/foods").expect(200)];
|
|
535
|
-
case 3:
|
|
536
|
-
res = _a.sent();
|
|
537
|
-
(0, bun_test_1.expect)(res.body.data[0].hidden).toBe(true);
|
|
538
|
-
return [2 /*return*/];
|
|
539
|
-
}
|
|
540
|
-
});
|
|
541
|
-
}); });
|
|
542
|
-
(0, bun_test_1.it)("recursively removes hidden fields from array item values", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
543
|
-
var agent, res, _a, _b, doc;
|
|
544
|
-
var e_2, _c;
|
|
545
|
-
return __generator(this, function (_d) {
|
|
546
|
-
switch (_d.label) {
|
|
547
|
-
case 0: return [4 /*yield*/, tests_1.RequiredModel.deleteMany({})];
|
|
548
|
-
case 1:
|
|
549
|
-
_d.sent();
|
|
550
|
-
app = buildApp([
|
|
551
|
-
{
|
|
552
|
-
displayName: "Required",
|
|
553
|
-
hiddenFields: ["about"],
|
|
554
|
-
listFields: ["name"],
|
|
555
|
-
model: tests_1.RequiredModel,
|
|
556
|
-
routePath: "/required",
|
|
557
|
-
},
|
|
558
|
-
]);
|
|
559
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
560
|
-
case 2:
|
|
561
|
-
agent = _d.sent();
|
|
562
|
-
return [4 /*yield*/, tests_1.RequiredModel.create({ about: "secret", name: "first" })];
|
|
563
|
-
case 3:
|
|
564
|
-
_d.sent();
|
|
565
|
-
return [4 /*yield*/, tests_1.RequiredModel.create({ about: "secret2", name: "second" })];
|
|
566
|
-
case 4:
|
|
567
|
-
_d.sent();
|
|
568
|
-
return [4 /*yield*/, agent.get("/admin/required").expect(200)];
|
|
569
|
-
case 5:
|
|
570
|
-
res = _d.sent();
|
|
571
|
-
(0, bun_test_1.expect)(res.body.data).toHaveLength(2);
|
|
572
|
-
try {
|
|
573
|
-
for (_a = __values(res.body.data), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
574
|
-
doc = _b.value;
|
|
575
|
-
(0, bun_test_1.expect)(doc.about).toBeUndefined();
|
|
576
|
-
(0, bun_test_1.expect)(doc.name).toBeDefined();
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
580
|
-
finally {
|
|
581
|
-
try {
|
|
582
|
-
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
583
|
-
}
|
|
584
|
-
finally { if (e_2) throw e_2.error; }
|
|
585
|
-
}
|
|
586
|
-
return [4 /*yield*/, tests_1.RequiredModel.deleteMany({})];
|
|
587
|
-
case 6:
|
|
588
|
-
_d.sent();
|
|
589
|
-
return [2 /*return*/];
|
|
590
|
-
}
|
|
591
|
-
});
|
|
592
|
-
}); });
|
|
593
|
-
});
|
|
594
|
-
(0, bun_test_1.describe)("AdminApp /admin/version-config", function () {
|
|
595
|
-
var app;
|
|
596
|
-
var adminAgent;
|
|
597
|
-
var notAdminAgent;
|
|
598
|
-
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
599
|
-
return __generator(this, function (_a) {
|
|
600
|
-
switch (_a.label) {
|
|
601
|
-
case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
|
|
602
|
-
case 1:
|
|
603
|
-
_a.sent();
|
|
604
|
-
return [4 /*yield*/, api_1.VersionConfig.deleteMany({})];
|
|
605
|
-
case 2:
|
|
606
|
-
_a.sent();
|
|
607
|
-
app = buildApp([]);
|
|
608
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
609
|
-
case 3:
|
|
610
|
-
adminAgent = _a.sent();
|
|
611
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
|
|
612
|
-
case 4:
|
|
613
|
-
notAdminAgent = _a.sent();
|
|
614
|
-
return [2 /*return*/];
|
|
615
|
-
}
|
|
616
|
-
});
|
|
617
|
-
}); });
|
|
618
|
-
(0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
619
|
-
return __generator(this, function (_a) {
|
|
620
|
-
switch (_a.label) {
|
|
621
|
-
case 0: return [4 /*yield*/, api_1.VersionConfig.deleteMany({})];
|
|
622
|
-
case 1:
|
|
623
|
-
_a.sent();
|
|
624
|
-
return [2 /*return*/];
|
|
625
|
-
}
|
|
626
|
-
});
|
|
627
|
-
}); });
|
|
628
|
-
(0, bun_test_1.it)("returns default values when no config exists", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
629
|
-
var res;
|
|
630
|
-
return __generator(this, function (_a) {
|
|
631
|
-
switch (_a.label) {
|
|
632
|
-
case 0: return [4 /*yield*/, adminAgent.get("/admin/version-config").expect(200)];
|
|
633
|
-
case 1:
|
|
634
|
-
res = _a.sent();
|
|
635
|
-
(0, bun_test_1.expect)(res.body.mobileRequiredVersion).toBe(0);
|
|
636
|
-
(0, bun_test_1.expect)(res.body.webWarningVersion).toBe(0);
|
|
637
|
-
(0, bun_test_1.expect)(res.body.requiredMessage).toInclude("update");
|
|
638
|
-
return [2 /*return*/];
|
|
639
|
-
}
|
|
640
|
-
});
|
|
641
|
-
}); });
|
|
642
|
-
(0, bun_test_1.it)("returns the stored config when present", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
643
|
-
var res;
|
|
644
|
-
return __generator(this, function (_a) {
|
|
645
|
-
switch (_a.label) {
|
|
646
|
-
case 0: return [4 /*yield*/, api_1.VersionConfig.create({
|
|
647
|
-
_singleton: "config",
|
|
648
|
-
mobileRequiredVersion: 5,
|
|
649
|
-
mobileWarningVersion: 3,
|
|
650
|
-
updateUrl: "https://example.com/update",
|
|
651
|
-
})];
|
|
652
|
-
case 1:
|
|
653
|
-
_a.sent();
|
|
654
|
-
return [4 /*yield*/, adminAgent.get("/admin/version-config").expect(200)];
|
|
655
|
-
case 2:
|
|
656
|
-
res = _a.sent();
|
|
657
|
-
(0, bun_test_1.expect)(res.body.mobileRequiredVersion).toBe(5);
|
|
658
|
-
(0, bun_test_1.expect)(res.body.mobileWarningVersion).toBe(3);
|
|
659
|
-
(0, bun_test_1.expect)(res.body.updateUrl).toBe("https://example.com/update");
|
|
660
|
-
return [2 /*return*/];
|
|
661
|
-
}
|
|
662
|
-
});
|
|
663
|
-
}); });
|
|
664
|
-
(0, bun_test_1.it)("creates or updates the config via PUT", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
665
|
-
var res, stored;
|
|
666
|
-
return __generator(this, function (_a) {
|
|
667
|
-
switch (_a.label) {
|
|
668
|
-
case 0: return [4 /*yield*/, adminAgent
|
|
669
|
-
.put("/admin/version-config")
|
|
670
|
-
.send({
|
|
671
|
-
mobileRequiredVersion: 10,
|
|
672
|
-
mobileWarningVersion: 8,
|
|
673
|
-
updateUrl: "https://example.com/upgrade",
|
|
674
|
-
})
|
|
675
|
-
.expect(200)];
|
|
676
|
-
case 1:
|
|
677
|
-
res = _a.sent();
|
|
678
|
-
(0, bun_test_1.expect)(res.body.mobileRequiredVersion).toBe(10);
|
|
679
|
-
(0, bun_test_1.expect)(res.body.updateUrl).toBe("https://example.com/upgrade");
|
|
680
|
-
return [4 /*yield*/, api_1.VersionConfig.findOne({ _singleton: "config" })];
|
|
681
|
-
case 2:
|
|
682
|
-
stored = _a.sent();
|
|
683
|
-
(0, bun_test_1.expect)(stored === null || stored === void 0 ? void 0 : stored.mobileRequiredVersion).toBe(10);
|
|
684
|
-
return [2 /*return*/];
|
|
685
|
-
}
|
|
686
|
-
});
|
|
687
|
-
}); });
|
|
688
|
-
(0, bun_test_1.it)("unsets fields passed as null via PUT", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
689
|
-
var res;
|
|
690
|
-
return __generator(this, function (_a) {
|
|
691
|
-
switch (_a.label) {
|
|
692
|
-
case 0: return [4 /*yield*/, adminAgent
|
|
693
|
-
.put("/admin/version-config")
|
|
694
|
-
.send({ updateUrl: "https://example.com/upgrade" })
|
|
695
|
-
.expect(200)];
|
|
696
|
-
case 1:
|
|
697
|
-
_a.sent();
|
|
698
|
-
return [4 /*yield*/, adminAgent.put("/admin/version-config").send({ updateUrl: null }).expect(200)];
|
|
699
|
-
case 2:
|
|
700
|
-
res = _a.sent();
|
|
701
|
-
(0, bun_test_1.expect)(res.body.updateUrl).toBeUndefined();
|
|
702
|
-
return [2 /*return*/];
|
|
703
|
-
}
|
|
704
|
-
});
|
|
705
|
-
}); });
|
|
706
|
-
(0, bun_test_1.it)("ignores unknown fields via PUT", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
707
|
-
var res;
|
|
708
|
-
return __generator(this, function (_a) {
|
|
709
|
-
switch (_a.label) {
|
|
710
|
-
case 0: return [4 /*yield*/, adminAgent
|
|
711
|
-
.put("/admin/version-config")
|
|
712
|
-
.send({ bogusField: "nope", mobileRequiredVersion: 2 })
|
|
713
|
-
.expect(200)];
|
|
714
|
-
case 1:
|
|
715
|
-
res = _a.sent();
|
|
716
|
-
(0, bun_test_1.expect)(res.body.mobileRequiredVersion).toBe(2);
|
|
717
|
-
(0, bun_test_1.expect)(res.body.bogusField).toBeUndefined();
|
|
718
|
-
return [2 /*return*/];
|
|
719
|
-
}
|
|
720
|
-
});
|
|
721
|
-
}); });
|
|
722
|
-
(0, bun_test_1.it)("returns 403 for non-admins (GET)", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
723
|
-
return __generator(this, function (_a) {
|
|
724
|
-
switch (_a.label) {
|
|
725
|
-
case 0: return [4 /*yield*/, notAdminAgent.get("/admin/version-config").expect(403)];
|
|
726
|
-
case 1:
|
|
727
|
-
_a.sent();
|
|
728
|
-
return [2 /*return*/];
|
|
729
|
-
}
|
|
730
|
-
});
|
|
731
|
-
}); });
|
|
732
|
-
(0, bun_test_1.it)("returns 403 for non-admins (PUT)", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
733
|
-
return __generator(this, function (_a) {
|
|
734
|
-
switch (_a.label) {
|
|
735
|
-
case 0: return [4 /*yield*/, notAdminAgent.put("/admin/version-config").send({}).expect(403)];
|
|
736
|
-
case 1:
|
|
737
|
-
_a.sent();
|
|
738
|
-
return [2 /*return*/];
|
|
739
|
-
}
|
|
740
|
-
});
|
|
741
|
-
}); });
|
|
742
|
-
(0, bun_test_1.it)("returns 401 for unauthenticated users", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
743
|
-
return __generator(this, function (_a) {
|
|
744
|
-
switch (_a.label) {
|
|
745
|
-
case 0: return [4 /*yield*/, (0, supertest_1.default)(app).get("/admin/version-config").expect(401)];
|
|
746
|
-
case 1:
|
|
747
|
-
_a.sent();
|
|
748
|
-
return [2 /*return*/];
|
|
749
|
-
}
|
|
750
|
-
});
|
|
751
|
-
}); });
|
|
752
|
-
});
|
|
753
|
-
(0, bun_test_1.describe)("AdminApp search route", function () {
|
|
754
|
-
var app;
|
|
755
|
-
var adminAgent;
|
|
756
|
-
var notAdminAgent;
|
|
757
|
-
(0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
758
|
-
return __generator(this, function (_a) {
|
|
759
|
-
switch (_a.label) {
|
|
760
|
-
case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
|
|
761
|
-
case 1:
|
|
762
|
-
_a.sent();
|
|
763
|
-
app = buildApp([foodModelConfig]);
|
|
764
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
765
|
-
case 2:
|
|
766
|
-
adminAgent = _a.sent();
|
|
767
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
|
|
768
|
-
case 3:
|
|
769
|
-
notAdminAgent = _a.sent();
|
|
770
|
-
return [2 /*return*/];
|
|
771
|
-
}
|
|
772
|
-
});
|
|
773
|
-
}); });
|
|
774
|
-
(0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
775
|
-
return __generator(this, function (_a) {
|
|
776
|
-
switch (_a.label) {
|
|
777
|
-
case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
|
|
778
|
-
case 1:
|
|
779
|
-
_a.sent();
|
|
780
|
-
return [2 /*return*/];
|
|
781
|
-
}
|
|
782
|
-
});
|
|
783
|
-
}); });
|
|
784
|
-
(0, bun_test_1.it)("returns empty results when q is empty", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
785
|
-
var res;
|
|
786
|
-
return __generator(this, function (_a) {
|
|
787
|
-
switch (_a.label) {
|
|
788
|
-
case 0: return [4 /*yield*/, adminAgent.get("/admin/foods/search").expect(200)];
|
|
789
|
-
case 1:
|
|
790
|
-
res = _a.sent();
|
|
791
|
-
(0, bun_test_1.expect)(res.body.data).toEqual([]);
|
|
792
|
-
return [2 /*return*/];
|
|
793
|
-
}
|
|
794
|
-
});
|
|
795
|
-
}); });
|
|
796
|
-
(0, bun_test_1.it)("returns results matching searchable string fields", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
797
|
-
var res;
|
|
798
|
-
return __generator(this, function (_a) {
|
|
799
|
-
switch (_a.label) {
|
|
800
|
-
case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, name: "Apple" })];
|
|
801
|
-
case 1:
|
|
802
|
-
_a.sent();
|
|
803
|
-
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 95, name: "Banana" })];
|
|
804
|
-
case 2:
|
|
805
|
-
_a.sent();
|
|
806
|
-
return [4 /*yield*/, adminAgent.get("/admin/foods/search?q=App").expect(200)];
|
|
807
|
-
case 3:
|
|
808
|
-
res = _a.sent();
|
|
809
|
-
(0, bun_test_1.expect)(res.body.data).toHaveLength(1);
|
|
810
|
-
(0, bun_test_1.expect)(res.body.data[0].name).toBe("Apple");
|
|
811
|
-
return [2 /*return*/];
|
|
812
|
-
}
|
|
813
|
-
});
|
|
814
|
-
}); });
|
|
815
|
-
(0, bun_test_1.it)("escapes regex special characters in search query", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
816
|
-
var res;
|
|
817
|
-
return __generator(this, function (_a) {
|
|
818
|
-
switch (_a.label) {
|
|
819
|
-
case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 100, name: "Rice.Bowl" })];
|
|
820
|
-
case 1:
|
|
821
|
-
_a.sent();
|
|
822
|
-
return [4 /*yield*/, adminAgent.get("/admin/foods/search?q=Rice.Bowl").expect(200)];
|
|
823
|
-
case 2:
|
|
824
|
-
res = _a.sent();
|
|
825
|
-
(0, bun_test_1.expect)(res.body.data).toHaveLength(1);
|
|
826
|
-
(0, bun_test_1.expect)(res.body.data[0].name).toBe("Rice.Bowl");
|
|
827
|
-
return [2 /*return*/];
|
|
828
|
-
}
|
|
829
|
-
});
|
|
830
|
-
}); });
|
|
831
|
-
(0, bun_test_1.it)("filters by explicit fields when provided", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
832
|
-
var res;
|
|
833
|
-
return __generator(this, function (_a) {
|
|
834
|
-
switch (_a.label) {
|
|
835
|
-
case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 100, name: "Apple" })];
|
|
836
|
-
case 1:
|
|
837
|
-
_a.sent();
|
|
838
|
-
return [4 /*yield*/, adminAgent.get("/admin/foods/search?q=Apple&fields=calories").expect(200)];
|
|
839
|
-
case 2:
|
|
840
|
-
res = _a.sent();
|
|
841
|
-
// calories isn't a searchable string field so the result should be empty
|
|
842
|
-
(0, bun_test_1.expect)(res.body.data).toEqual([]);
|
|
843
|
-
return [2 /*return*/];
|
|
844
|
-
}
|
|
845
|
-
});
|
|
846
|
-
}); });
|
|
847
|
-
(0, bun_test_1.it)("exercises ObjectId query branch when query looks like an ObjectId", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
848
|
-
var user, res;
|
|
849
|
-
return __generator(this, function (_a) {
|
|
850
|
-
switch (_a.label) {
|
|
851
|
-
case 0: return [4 /*yield*/, tests_1.UserModel.findOne({ email: "admin@example.com" })];
|
|
852
|
-
case 1:
|
|
853
|
-
user = _a.sent();
|
|
854
|
-
return [4 /*yield*/, tests_1.FoodModel.create({ calories: 100, name: "Kale", ownerId: user === null || user === void 0 ? void 0 : user._id })];
|
|
855
|
-
case 2:
|
|
856
|
-
_a.sent();
|
|
857
|
-
return [4 /*yield*/, adminAgent.get("/admin/foods/search?q=".concat(user === null || user === void 0 ? void 0 : user._id)).expect(200)];
|
|
858
|
-
case 3:
|
|
859
|
-
res = _a.sent();
|
|
860
|
-
(0, bun_test_1.expect)(Array.isArray(res.body.data)).toBe(true);
|
|
861
|
-
return [2 /*return*/];
|
|
862
|
-
}
|
|
863
|
-
});
|
|
864
|
-
}); });
|
|
865
|
-
(0, bun_test_1.it)("returns 403 for non-admins", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
866
|
-
return __generator(this, function (_a) {
|
|
867
|
-
switch (_a.label) {
|
|
868
|
-
case 0: return [4 /*yield*/, notAdminAgent.get("/admin/foods/search?q=x").expect(403)];
|
|
869
|
-
case 1:
|
|
870
|
-
_a.sent();
|
|
871
|
-
return [2 /*return*/];
|
|
872
|
-
}
|
|
873
|
-
});
|
|
874
|
-
}); });
|
|
875
|
-
(0, bun_test_1.it)("returns 401 for unauthenticated users", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
876
|
-
return __generator(this, function (_a) {
|
|
877
|
-
switch (_a.label) {
|
|
878
|
-
case 0: return [4 /*yield*/, (0, supertest_1.default)(app).get("/admin/foods/search?q=x").expect(401)];
|
|
879
|
-
case 1:
|
|
880
|
-
_a.sent();
|
|
881
|
-
return [2 /*return*/];
|
|
882
|
-
}
|
|
883
|
-
});
|
|
884
|
-
}); });
|
|
885
|
-
});
|
|
886
|
-
(0, bun_test_1.describe)("AdminApp with scripts that use context", function () {
|
|
887
|
-
var app;
|
|
888
|
-
var adminAgent;
|
|
889
|
-
(0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
890
|
-
var BackgroundTask;
|
|
891
|
-
return __generator(this, function (_a) {
|
|
892
|
-
switch (_a.label) {
|
|
893
|
-
case 0: return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require("@terreno/api")); })];
|
|
894
|
-
case 1:
|
|
895
|
-
BackgroundTask = (_a.sent()).BackgroundTask;
|
|
896
|
-
return [4 /*yield*/, BackgroundTask.deleteMany({})];
|
|
897
|
-
case 2:
|
|
898
|
-
_a.sent();
|
|
899
|
-
return [2 /*return*/];
|
|
900
|
-
}
|
|
901
|
-
});
|
|
902
|
-
}); });
|
|
903
|
-
(0, bun_test_1.it)("reports progress, logs, and checks cancellation via ctx", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
904
|
-
var BackgroundTask, appWithScript, admin, res, task;
|
|
905
|
-
var _a;
|
|
906
|
-
return __generator(this, function (_b) {
|
|
907
|
-
switch (_b.label) {
|
|
908
|
-
case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
|
|
909
|
-
case 1:
|
|
910
|
-
_b.sent();
|
|
911
|
-
return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require("@terreno/api")); })];
|
|
912
|
-
case 2:
|
|
913
|
-
BackgroundTask = (_b.sent()).BackgroundTask;
|
|
914
|
-
appWithScript = (0, tests_1.getBaseServer)();
|
|
915
|
-
(0, api_1.setupAuth)(appWithScript, tests_1.UserModel);
|
|
916
|
-
(0, api_1.addAuthRoutes)(appWithScript, tests_1.UserModel);
|
|
917
|
-
admin = new adminApp_1.AdminApp({
|
|
918
|
-
basePath: "/admin",
|
|
919
|
-
models: [],
|
|
920
|
-
scripts: [
|
|
921
|
-
{
|
|
922
|
-
description: "Exercises ctx",
|
|
923
|
-
name: "ctx-script",
|
|
924
|
-
runner: function (_wetRun, ctx) { return __awaiter(void 0, void 0, void 0, function () {
|
|
925
|
-
return __generator(this, function (_a) {
|
|
926
|
-
switch (_a.label) {
|
|
927
|
-
case 0: return [4 /*yield*/, (ctx === null || ctx === void 0 ? void 0 : ctx.addLog("info", "hello"))];
|
|
928
|
-
case 1:
|
|
929
|
-
_a.sent();
|
|
930
|
-
return [4 /*yield*/, (ctx === null || ctx === void 0 ? void 0 : ctx.updateProgress(50, "Halfway", "Processing"))];
|
|
931
|
-
case 2:
|
|
932
|
-
_a.sent();
|
|
933
|
-
return [4 /*yield*/, (ctx === null || ctx === void 0 ? void 0 : ctx.checkCancellation())];
|
|
934
|
-
case 3:
|
|
935
|
-
_a.sent();
|
|
936
|
-
return [2 /*return*/, { results: ["ok"], success: true }];
|
|
937
|
-
}
|
|
938
|
-
});
|
|
939
|
-
}); },
|
|
940
|
-
},
|
|
941
|
-
],
|
|
942
|
-
});
|
|
943
|
-
admin.register(appWithScript);
|
|
944
|
-
appWithScript.use(api_1.apiUnauthorizedMiddleware);
|
|
945
|
-
appWithScript.use(api_1.apiErrorMiddleware);
|
|
946
|
-
app = appWithScript;
|
|
947
|
-
return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
|
|
948
|
-
case 3:
|
|
949
|
-
adminAgent = _b.sent();
|
|
950
|
-
return [4 /*yield*/, adminAgent.post("/admin/scripts/ctx-script/run").expect(201)];
|
|
951
|
-
case 4:
|
|
952
|
-
res = _b.sent();
|
|
953
|
-
// Wait for script completion
|
|
954
|
-
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 150); })];
|
|
955
|
-
case 5:
|
|
956
|
-
// Wait for script completion
|
|
957
|
-
_b.sent();
|
|
958
|
-
return [4 /*yield*/, BackgroundTask.findById(res.body.taskId)];
|
|
959
|
-
case 6:
|
|
960
|
-
task = _b.sent();
|
|
961
|
-
(0, bun_test_1.expect)(task === null || task === void 0 ? void 0 : task.status).toBe("completed");
|
|
962
|
-
// addLog("info", "hello") should have persisted
|
|
963
|
-
(0, bun_test_1.expect)(task === null || task === void 0 ? void 0 : task.logs.some(function (l) { return l.message === "hello"; })).toBe(true);
|
|
964
|
-
(0, bun_test_1.expect)((_a = task === null || task === void 0 ? void 0 : task.progress) === null || _a === void 0 ? void 0 : _a.percentage).toBe(100);
|
|
965
|
-
return [2 /*return*/];
|
|
966
|
-
}
|
|
967
|
-
});
|
|
968
|
-
}); });
|
|
969
|
-
});
|