@terreno/api 0.30.0 → 56.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (226) hide show
  1. package/dist/api.d.ts +10 -0
  2. package/dist/api.js +110 -361
  3. package/dist/auth.d.ts +26 -0
  4. package/dist/auth.js +100 -1
  5. package/dist/betterAuth.d.ts +5 -0
  6. package/dist/betterAuthSetup.js +23 -4
  7. package/dist/docLoader.js +12 -9
  8. package/dist/errors.d.ts +13 -0
  9. package/dist/errors.js +29 -1
  10. package/dist/index.d.ts +17 -1
  11. package/dist/index.js +29 -1
  12. package/dist/middleware.d.ts +7 -0
  13. package/dist/middleware.js +16 -1
  14. package/dist/openApiCompat.d.ts +10 -1
  15. package/dist/openApiCompat.js +115 -47
  16. package/dist/permissions.d.ts +27 -0
  17. package/dist/permissions.js +42 -1
  18. package/dist/plugins.d.ts +13 -0
  19. package/dist/plugins.js +35 -1
  20. package/dist/realtime/changeStreamWatcher.d.ts +100 -3
  21. package/dist/realtime/changeStreamWatcher.js +542 -137
  22. package/dist/realtime/realtimeApp.d.ts +2 -0
  23. package/dist/realtime/realtimeApp.js +73 -21
  24. package/dist/realtime/sessionRevalidation.d.ts +90 -0
  25. package/dist/realtime/sessionRevalidation.js +438 -0
  26. package/dist/realtime/socketAuth.d.ts +97 -0
  27. package/dist/realtime/socketAuth.js +272 -0
  28. package/dist/realtime/socketUser.d.ts +54 -0
  29. package/dist/realtime/socketUser.js +75 -5
  30. package/dist/realtime/types.d.ts +40 -0
  31. package/dist/secretProviders.d.ts +23 -0
  32. package/dist/secretProviders.js +9 -3
  33. package/dist/sync/executors.d.ts +198 -0
  34. package/dist/sync/executors.js +670 -0
  35. package/dist/sync/loadHarness.js +807 -0
  36. package/dist/sync/models.d.ts +216 -0
  37. package/dist/sync/models.js +644 -0
  38. package/dist/sync/mutationHandler.d.ts +97 -0
  39. package/dist/sync/mutationHandler.js +886 -0
  40. package/dist/sync/registry.d.ts +73 -0
  41. package/dist/sync/registry.js +223 -0
  42. package/dist/sync/routes.d.ts +62 -0
  43. package/dist/sync/routes.js +936 -0
  44. package/dist/sync/scripts/compactTombstones.d.ts +47 -0
  45. package/dist/sync/scripts/compactTombstones.js +270 -0
  46. package/dist/sync/serialize.d.ts +20 -0
  47. package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
  48. package/dist/sync/socketHandlers.d.ts +54 -0
  49. package/dist/sync/socketHandlers.js +470 -0
  50. package/dist/sync/streams.d.ts +85 -0
  51. package/dist/sync/streams.js +221 -0
  52. package/dist/sync/syncApp.d.ts +29 -0
  53. package/dist/sync/syncApp.js +44 -0
  54. package/dist/sync/syncBatch.d.ts +71 -0
  55. package/dist/sync/syncBatch.js +252 -0
  56. package/dist/sync/syncSeqPlugin.d.ts +8 -0
  57. package/dist/sync/syncSeqPlugin.js +903 -0
  58. package/dist/sync/types.d.ts +254 -0
  59. package/dist/sync/types.js +9 -0
  60. package/dist/terrenoApp.js +90 -27
  61. package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
  62. package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
  63. package/package.json +7 -6
  64. package/src/__tests__/openApiCompat.test.ts +20 -0
  65. package/src/actions.openApi.test.ts +7 -10
  66. package/src/api.test.ts +12 -0
  67. package/src/api.ts +87 -295
  68. package/src/auth.test.ts +128 -176
  69. package/src/auth.ts +109 -0
  70. package/src/betterAuth.ts +6 -0
  71. package/src/betterAuthSetup.test.ts +34 -0
  72. package/src/betterAuthSetup.ts +20 -1
  73. package/src/docLoader.ts +3 -1
  74. package/src/errors.ts +33 -0
  75. package/src/expressServer.test.ts +7 -1
  76. package/src/index.ts +42 -0
  77. package/src/middleware.test.ts +40 -1
  78. package/src/middleware.ts +15 -0
  79. package/src/openApiCompat.ts +99 -45
  80. package/src/permissions.test.ts +75 -1
  81. package/src/permissions.ts +49 -0
  82. package/src/plugins.test.ts +54 -0
  83. package/src/plugins.ts +43 -0
  84. package/src/realtime/changeStreamWatcher.ts +552 -114
  85. package/src/realtime/realtime.test.ts +157 -0
  86. package/src/realtime/realtimeApp.ts +75 -11
  87. package/src/realtime/sessionRevalidation.test.ts +423 -0
  88. package/src/realtime/sessionRevalidation.ts +273 -0
  89. package/src/realtime/socketAuth.test.ts +355 -0
  90. package/src/realtime/socketAuth.ts +228 -0
  91. package/src/realtime/socketUser.ts +69 -0
  92. package/src/realtime/types.ts +40 -0
  93. package/src/secretProviders.test.ts +7 -1
  94. package/src/secretProviders.ts +15 -3
  95. package/src/sync/ensureSyncIndexes.test.ts +144 -0
  96. package/src/sync/executors.test.ts +882 -0
  97. package/src/sync/executors.ts +703 -0
  98. package/src/sync/integration.test.ts +1160 -0
  99. package/src/sync/loadHarness.ts +618 -0
  100. package/src/sync/models.ts +640 -0
  101. package/src/sync/mutationHandler.test.ts +1252 -0
  102. package/src/sync/mutationHandler.ts +850 -0
  103. package/src/sync/registry.ts +232 -0
  104. package/src/sync/routes.ts +812 -0
  105. package/src/sync/scripts/compactTombstones.test.ts +249 -0
  106. package/src/sync/scripts/compactTombstones.ts +145 -0
  107. package/src/sync/serialize.ts +48 -0
  108. package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
  109. package/src/sync/socketHandlers.ts +320 -0
  110. package/src/sync/streams.ts +188 -0
  111. package/src/sync/sync.test.ts +582 -0
  112. package/src/sync/syncApp.ts +44 -0
  113. package/src/sync/syncBatch.ts +219 -0
  114. package/src/sync/syncFrontier.test.ts +494 -0
  115. package/src/sync/syncPhaseC.test.ts +718 -0
  116. package/src/sync/syncRoutes.test.ts +865 -0
  117. package/src/sync/syncSeqPlugin.ts +738 -0
  118. package/src/sync/syncSocket.test.ts +1440 -0
  119. package/src/sync/types.ts +270 -0
  120. package/src/terrenoApp.ts +37 -17
  121. package/src/tests/bunSetup.ts +8 -0
  122. package/tsconfig.json +4 -4
  123. package/tsconfig.typedoc.json +7 -1
  124. package/dist/__tests__/openApiCompat.test.js +0 -317
  125. package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
  126. package/dist/__tests__/versionCheckPlugin.test.js +0 -597
  127. package/dist/actions.openApi.test.d.ts +0 -1
  128. package/dist/actions.openApi.test.js +0 -255
  129. package/dist/actions.test.d.ts +0 -1
  130. package/dist/actions.test.js +0 -1089
  131. package/dist/api.arrayOperations.test.d.ts +0 -1
  132. package/dist/api.arrayOperations.test.js +0 -878
  133. package/dist/api.asyncHandler.test.d.ts +0 -1
  134. package/dist/api.asyncHandler.test.js +0 -236
  135. package/dist/api.coverage.test.d.ts +0 -1
  136. package/dist/api.coverage.test.js +0 -194
  137. package/dist/api.errorPaths.test.d.ts +0 -1
  138. package/dist/api.errorPaths.test.js +0 -801
  139. package/dist/api.errors.test.d.ts +0 -1
  140. package/dist/api.errors.test.js +0 -185
  141. package/dist/api.hooks.test.d.ts +0 -1
  142. package/dist/api.hooks.test.js +0 -950
  143. package/dist/api.query.test.d.ts +0 -1
  144. package/dist/api.query.test.js +0 -948
  145. package/dist/api.test.d.ts +0 -1
  146. package/dist/api.test.js +0 -2820
  147. package/dist/auth.compile.test.d.ts +0 -1
  148. package/dist/auth.compile.test.js +0 -201
  149. package/dist/auth.coverage.test.d.ts +0 -1
  150. package/dist/auth.coverage.test.js +0 -202
  151. package/dist/auth.test.d.ts +0 -1
  152. package/dist/auth.test.js +0 -2286
  153. package/dist/betterAuth.test.d.ts +0 -1
  154. package/dist/betterAuth.test.js +0 -181
  155. package/dist/betterAuthApp.test.d.ts +0 -1
  156. package/dist/betterAuthApp.test.js +0 -242
  157. package/dist/betterAuthSetup.test.d.ts +0 -1
  158. package/dist/betterAuthSetup.test.js +0 -762
  159. package/dist/config.test.d.ts +0 -1
  160. package/dist/config.test.js +0 -353
  161. package/dist/configuration.test.d.ts +0 -1
  162. package/dist/configuration.test.js +0 -980
  163. package/dist/configurationPlugin.test.d.ts +0 -1
  164. package/dist/configurationPlugin.test.js +0 -827
  165. package/dist/consentApp.test.d.ts +0 -1
  166. package/dist/consentApp.test.js +0 -1415
  167. package/dist/docLoader.test.d.ts +0 -1
  168. package/dist/docLoader.test.js +0 -137
  169. package/dist/envConfigurationPlugin.test.d.ts +0 -1
  170. package/dist/envConfigurationPlugin.test.js +0 -369
  171. package/dist/errors.test.d.ts +0 -1
  172. package/dist/errors.test.js +0 -607
  173. package/dist/expressServer.test.d.ts +0 -1
  174. package/dist/expressServer.test.js +0 -1249
  175. package/dist/githubAuth.test.d.ts +0 -1
  176. package/dist/githubAuth.test.js +0 -1142
  177. package/dist/httpClient.test.d.ts +0 -1
  178. package/dist/httpClient.test.js +0 -1471
  179. package/dist/logger.test.d.ts +0 -1
  180. package/dist/logger.test.js +0 -685
  181. package/dist/middleware.test.d.ts +0 -1
  182. package/dist/middleware.test.js +0 -241
  183. package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
  184. package/dist/notifiers/googleChatNotifier.test.js +0 -298
  185. package/dist/notifiers/slackNotifier.test.d.ts +0 -1
  186. package/dist/notifiers/slackNotifier.test.js +0 -297
  187. package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
  188. package/dist/notifiers/zoomNotifier.test.js +0 -373
  189. package/dist/openApi.test.d.ts +0 -1
  190. package/dist/openApi.test.js +0 -620
  191. package/dist/openApiBuilder.test.d.ts +0 -1
  192. package/dist/openApiBuilder.test.js +0 -601
  193. package/dist/openApiEtag.test.d.ts +0 -1
  194. package/dist/openApiEtag.test.js +0 -86
  195. package/dist/openApiValidator.test.d.ts +0 -1
  196. package/dist/openApiValidator.test.js +0 -761
  197. package/dist/permissions.middleware.test.d.ts +0 -1
  198. package/dist/permissions.middleware.test.js +0 -308
  199. package/dist/permissions.test.d.ts +0 -1
  200. package/dist/permissions.test.js +0 -439
  201. package/dist/plugins.test.d.ts +0 -1
  202. package/dist/plugins.test.js +0 -890
  203. package/dist/populate.test.d.ts +0 -1
  204. package/dist/populate.test.js +0 -373
  205. package/dist/realtime/realtime.test.d.ts +0 -10
  206. package/dist/realtime/realtime.test.js +0 -3982
  207. package/dist/requestContext.test.d.ts +0 -1
  208. package/dist/requestContext.test.js +0 -412
  209. package/dist/scriptRunner.test.d.ts +0 -1
  210. package/dist/scriptRunner.test.js +0 -386
  211. package/dist/secretProviders.test.d.ts +0 -1
  212. package/dist/secretProviders.test.js +0 -726
  213. package/dist/syncConsents.test.d.ts +0 -1
  214. package/dist/syncConsents.test.js +0 -522
  215. package/dist/terrenoApp.test.d.ts +0 -1
  216. package/dist/terrenoApp.test.js +0 -489
  217. package/dist/tests/bunSetup.d.ts +0 -1
  218. package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
  219. package/dist/tests/fixtures/compileAuthEntry.js +0 -37
  220. package/dist/tests/testEnv.d.ts +0 -0
  221. package/dist/tests/testEnv.js +0 -6
  222. package/dist/transformers.test.d.ts +0 -1
  223. package/dist/transformers.test.js +0 -423
  224. package/dist/utils.test.d.ts +0 -1
  225. package/dist/utils.test.js +0 -242
  226. /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
@@ -1,948 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
- return new (P || (P = Promise))(function (resolve, reject) {
38
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
- step((generator = generator.apply(thisArg, _arguments || [])).next());
42
- });
43
- };
44
- var __generator = (this && this.__generator) || function (thisArg, body) {
45
- 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);
46
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
47
- function verb(n) { return function (v) { return step([n, v]); }; }
48
- function step(op) {
49
- if (f) throw new TypeError("Generator is already executing.");
50
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
51
- 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;
52
- if (y = 0, t) op = [op[0] & 2, t.value];
53
- switch (op[0]) {
54
- case 0: case 1: t = op; break;
55
- case 4: _.label++; return { value: op[1], done: false };
56
- case 5: _.label++; y = op[1]; op = [0]; continue;
57
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
58
- default:
59
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
60
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
61
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
62
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
63
- if (t[2]) _.ops.pop();
64
- _.trys.pop(); continue;
65
- }
66
- op = body.call(thisArg, _);
67
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
68
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
69
- }
70
- };
71
- var __read = (this && this.__read) || function (o, n) {
72
- var m = typeof Symbol === "function" && o[Symbol.iterator];
73
- if (!m) return o;
74
- var i = m.call(o), r, ar = [], e;
75
- try {
76
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
77
- }
78
- catch (error) { e = { error: error }; }
79
- finally {
80
- try {
81
- if (r && !r.done && (m = i["return"])) m.call(i);
82
- }
83
- finally { if (e) throw e.error; }
84
- }
85
- return ar;
86
- };
87
- var __importDefault = (this && this.__importDefault) || function (mod) {
88
- return (mod && mod.__esModule) ? mod : { "default": mod };
89
- };
90
- Object.defineProperty(exports, "__esModule", { value: true });
91
- // noExplicitAny: test mock typing
92
- // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
93
- var bun_test_1 = require("bun:test");
94
- var Sentry = __importStar(require("@sentry/bun"));
95
- var qs_1 = __importDefault(require("qs"));
96
- var supertest_1 = __importDefault(require("supertest"));
97
- var api_1 = require("./api");
98
- var auth_1 = require("./auth");
99
- var expressServer_1 = require("./expressServer");
100
- var permissions_1 = require("./permissions");
101
- var tests_1 = require("./tests");
102
- (0, bun_test_1.describe)("query and list methods", function () {
103
- var server;
104
- var app;
105
- var notAdmin;
106
- var admin;
107
- var adminOther;
108
- var agent;
109
- var spinach;
110
- var apple;
111
- var carrots;
112
- var pizza;
113
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
114
- var results;
115
- var _a, _b;
116
- return __generator(this, function (_c) {
117
- switch (_c.label) {
118
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
119
- case 1:
120
- _a = __read.apply(void 0, [_c.sent(), 3]), admin = _a[0], notAdmin = _a[1], adminOther = _a[2];
121
- return [4 /*yield*/, Promise.all([
122
- tests_1.FoodModel.create({
123
- calories: 1,
124
- created: new Date("2021-12-03T00:00:20.000Z"),
125
- eatenBy: [admin._id],
126
- hidden: false,
127
- lastEatenWith: {
128
- dressing: new Date("2021-12-03T19:00:30.000Z"),
129
- },
130
- name: "Spinach",
131
- ownerId: notAdmin._id,
132
- source: {
133
- dateAdded: "2023-12-13T12:30:00.000Z",
134
- href: "https://www.google.com",
135
- name: "Brand",
136
- },
137
- }),
138
- tests_1.FoodModel.create({
139
- calories: 100,
140
- created: new Date("2021-12-03T00:00:30.000Z"),
141
- hidden: true,
142
- name: "Apple",
143
- ownerId: admin._id,
144
- tags: ["healthy"],
145
- }),
146
- tests_1.FoodModel.create({
147
- calories: 100,
148
- created: new Date("2021-12-03T00:00:00.000Z"),
149
- eatenBy: [admin._id, notAdmin._id],
150
- hidden: false,
151
- name: "Carrots",
152
- ownerId: admin._id,
153
- source: {
154
- name: "USDA",
155
- },
156
- tags: ["healthy", "cheap"],
157
- }),
158
- tests_1.FoodModel.create({
159
- calories: 400,
160
- created: new Date("2021-12-03T00:00:10.000Z"),
161
- eatenBy: [adminOther._id],
162
- hidden: false,
163
- name: "Pizza",
164
- ownerId: admin._id,
165
- tags: ["cheap"],
166
- }),
167
- ])];
168
- case 2:
169
- results = (_c.sent());
170
- _b = __read(results, 4), spinach = _b[0], apple = _b[1], carrots = _b[2], pizza = _b[3];
171
- app = (0, tests_1.getBaseServer)();
172
- (0, auth_1.setupAuth)(app, tests_1.UserModel);
173
- (0, auth_1.addAuthRoutes)(app, tests_1.UserModel);
174
- app.use(expressServer_1.logRequests);
175
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
176
- allowAnonymous: true,
177
- defaultLimit: 2,
178
- defaultQueryParams: { hidden: false },
179
- maxLimit: 3,
180
- permissions: {
181
- create: [permissions_1.Permissions.IsAuthenticated],
182
- delete: [permissions_1.Permissions.IsAdmin],
183
- list: [permissions_1.Permissions.IsAny],
184
- read: [permissions_1.Permissions.IsAny],
185
- update: [permissions_1.Permissions.IsOwner],
186
- },
187
- populatePaths: [{ path: "ownerId" }],
188
- queryFields: [
189
- "hidden",
190
- "name",
191
- "calories",
192
- "created",
193
- "created_gte",
194
- "created_lte",
195
- "source.name",
196
- "tags",
197
- "eatenBy",
198
- ],
199
- sort: { created: "descending" },
200
- }));
201
- server = (0, supertest_1.default)(app);
202
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
203
- case 3:
204
- agent = _c.sent();
205
- return [2 /*return*/];
206
- }
207
- });
208
- }); });
209
- (0, bun_test_1.it)("read default", function () { return __awaiter(void 0, void 0, void 0, function () {
210
- var res;
211
- return __generator(this, function (_a) {
212
- switch (_a.label) {
213
- case 0: return [4 /*yield*/, agent.get("/food/".concat(spinach._id)).expect(200)];
214
- case 1:
215
- res = _a.sent();
216
- (0, bun_test_1.expect)(res.body.data._id).toBe(spinach._id.toString());
217
- (0, bun_test_1.expect)(res.body.data.ownerId._id).toBe(notAdmin.id);
218
- (0, bun_test_1.expect)(res.body.data.lastEatenWith).toEqual({
219
- dressing: "2021-12-03T19:00:30.000Z",
220
- });
221
- return [2 /*return*/];
222
- }
223
- });
224
- }); });
225
- (0, bun_test_1.it)("list default", function () { return __awaiter(void 0, void 0, void 0, function () {
226
- var res;
227
- return __generator(this, function (_a) {
228
- switch (_a.label) {
229
- case 0: return [4 /*yield*/, agent.get("/food").expect(200)];
230
- case 1:
231
- res = _a.sent();
232
- (0, bun_test_1.expect)(res.body.data).toHaveLength(2);
233
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(spinach.id);
234
- (0, bun_test_1.expect)(res.body.data[0].ownerId._id).toBe(notAdmin.id);
235
- (0, bun_test_1.expect)(res.body.data[1].id).toBe(pizza.id);
236
- (0, bun_test_1.expect)(res.body.data[1].ownerId._id).toBe(admin.id);
237
- (0, bun_test_1.expect)(res.body.data[0].lastEatenWith).toEqual({
238
- dressing: "2021-12-03T19:00:30.000Z",
239
- });
240
- (0, bun_test_1.expect)(res.body.data[1].lastEatenWith).toEqual(undefined);
241
- (0, bun_test_1.expect)(res.body.more).toBe(true);
242
- (0, bun_test_1.expect)(res.body.total).toBe(3);
243
- return [2 /*return*/];
244
- }
245
- });
246
- }); });
247
- (0, bun_test_1.it)("list limit", function () { return __awaiter(void 0, void 0, void 0, function () {
248
- var res;
249
- return __generator(this, function (_a) {
250
- switch (_a.label) {
251
- case 0: return [4 /*yield*/, agent.get("/food?limit=1").expect(200)];
252
- case 1:
253
- res = _a.sent();
254
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
255
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(spinach.id);
256
- (0, bun_test_1.expect)(res.body.data[0].ownerId._id).toBe(notAdmin.id);
257
- (0, bun_test_1.expect)(res.body.more).toBe(true);
258
- (0, bun_test_1.expect)(res.body.total).toBe(3);
259
- return [2 /*return*/];
260
- }
261
- });
262
- }); });
263
- (0, bun_test_1.it)("list limit over", function () { return __awaiter(void 0, void 0, void 0, function () {
264
- var res;
265
- return __generator(this, function (_a) {
266
- switch (_a.label) {
267
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({
268
- calories: 400,
269
- created: new Date("2021-12-02T00:00:10.000Z"),
270
- hidden: false,
271
- name: "Pizza",
272
- ownerId: admin._id,
273
- })];
274
- case 1:
275
- _a.sent();
276
- return [4 /*yield*/, agent.get("/food?limit=4").expect(200)];
277
- case 2:
278
- res = _a.sent();
279
- (0, bun_test_1.expect)(res.body.data).toHaveLength(3);
280
- (0, bun_test_1.expect)(res.body.more).toBe(true);
281
- (0, bun_test_1.expect)(res.body.total).toBe(4);
282
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(spinach.id);
283
- (0, bun_test_1.expect)(res.body.data[1].id).toBe(pizza.id);
284
- (0, bun_test_1.expect)(res.body.data[2].id).toBe(carrots.id);
285
- (0, bun_test_1.expect)(Sentry.captureMessage).toHaveBeenCalledWith('More than 3 results returned for foods without pagination, data may be silently truncated. req.query: {"limit":"4"}');
286
- return [2 /*return*/];
287
- }
288
- });
289
- }); });
290
- (0, bun_test_1.it)("list page", function () { return __awaiter(void 0, void 0, void 0, function () {
291
- var res;
292
- return __generator(this, function (_a) {
293
- switch (_a.label) {
294
- case 0: return [4 /*yield*/, agent.get("/food?limit=1&page=2").expect(200)];
295
- case 1:
296
- res = _a.sent();
297
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
298
- (0, bun_test_1.expect)(res.body.more).toBe(true);
299
- (0, bun_test_1.expect)(res.body.total).toBe(3);
300
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(pizza.id);
301
- return [2 /*return*/];
302
- }
303
- });
304
- }); });
305
- (0, bun_test_1.it)("list page 0 ", function () { return __awaiter(void 0, void 0, void 0, function () {
306
- var res;
307
- return __generator(this, function (_a) {
308
- switch (_a.label) {
309
- case 0: return [4 /*yield*/, agent.get("/food?limit=1&page=0").expect(400)];
310
- case 1:
311
- res = _a.sent();
312
- (0, bun_test_1.expect)(res.body.title).toBe("Invalid page");
313
- (0, bun_test_1.expect)(res.body.detail).toBe("Invalid page: 0");
314
- (0, bun_test_1.expect)(res.body.source).toEqual({ parameter: "page" });
315
- return [2 /*return*/];
316
- }
317
- });
318
- }); });
319
- (0, bun_test_1.it)("list page with garbage ", function () { return __awaiter(void 0, void 0, void 0, function () {
320
- var res;
321
- return __generator(this, function (_a) {
322
- switch (_a.label) {
323
- case 0: return [4 /*yield*/, agent.get("/food?limit=1&page=abc").expect(400)];
324
- case 1:
325
- res = _a.sent();
326
- (0, bun_test_1.expect)(res.body.title).toBe("Invalid page");
327
- (0, bun_test_1.expect)(res.body.detail).toBe("Invalid page: abc");
328
- (0, bun_test_1.expect)(res.body.source).toEqual({ parameter: "page" });
329
- return [2 /*return*/];
330
- }
331
- });
332
- }); });
333
- (0, bun_test_1.it)("list page over", function () { return __awaiter(void 0, void 0, void 0, function () {
334
- var res;
335
- return __generator(this, function (_a) {
336
- switch (_a.label) {
337
- case 0: return [4 /*yield*/, agent.get("/food?limit=1&page=5").expect(200)];
338
- case 1:
339
- res = _a.sent();
340
- (0, bun_test_1.expect)(res.body.data).toHaveLength(0);
341
- (0, bun_test_1.expect)(res.body.more).toBe(false);
342
- (0, bun_test_1.expect)(res.body.total).toBe(3);
343
- return [2 /*return*/];
344
- }
345
- });
346
- }); });
347
- (0, bun_test_1.it)("list query params", function () { return __awaiter(void 0, void 0, void 0, function () {
348
- var res;
349
- return __generator(this, function (_a) {
350
- switch (_a.label) {
351
- case 0: return [4 /*yield*/, agent.get("/food?hidden=true").expect(200)];
352
- case 1:
353
- res = _a.sent();
354
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
355
- (0, bun_test_1.expect)(res.body.more).toBe(false);
356
- (0, bun_test_1.expect)(res.body.total).toBe(1);
357
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(apple.id);
358
- return [2 /*return*/];
359
- }
360
- });
361
- }); });
362
- (0, bun_test_1.it)("list applies created_gte and created_lte as a Date range", function () { return __awaiter(void 0, void 0, void 0, function () {
363
- var res, names;
364
- return __generator(this, function (_a) {
365
- switch (_a.label) {
366
- case 0: return [4 /*yield*/, agent
367
- .get("/food")
368
- .query({
369
- created_gte: "2021-12-03T00:00:05.000Z",
370
- created_lte: "2021-12-03T00:00:25.000Z",
371
- limit: 10,
372
- })
373
- .expect(200)];
374
- case 1:
375
- res = _a.sent();
376
- names = res.body.data.map(function (d) { return d.name; }).sort();
377
- (0, bun_test_1.expect)(names).toEqual(["Pizza", "Spinach"]);
378
- return [2 /*return*/];
379
- }
380
- });
381
- }); });
382
- (0, bun_test_1.it)("list returns 400 when created_gte is not a valid date", function () { return __awaiter(void 0, void 0, void 0, function () {
383
- var res;
384
- return __generator(this, function (_a) {
385
- switch (_a.label) {
386
- case 0: return [4 /*yield*/, agent.get("/food").query({ created_gte: "not-a-date", limit: 10 }).expect(400)];
387
- case 1:
388
- res = _a.sent();
389
- (0, bun_test_1.expect)(res.body.title).toBe("Invalid date query parameter");
390
- (0, bun_test_1.expect)(res.body.detail).toBe("Invalid date for query parameter created_gte");
391
- (0, bun_test_1.expect)(res.body.source).toEqual({ parameter: "created_gte" });
392
- return [2 /*return*/];
393
- }
394
- });
395
- }); });
396
- (0, bun_test_1.it)("list returns 400 when created_lte is not a valid date", function () { return __awaiter(void 0, void 0, void 0, function () {
397
- var res;
398
- return __generator(this, function (_a) {
399
- switch (_a.label) {
400
- case 0: return [4 /*yield*/, agent
401
- .get("/food")
402
- .query({ created_lte: "also-not-a-date", limit: 10 })
403
- .expect(400)];
404
- case 1:
405
- res = _a.sent();
406
- (0, bun_test_1.expect)(res.body.title).toBe("Invalid date query parameter");
407
- (0, bun_test_1.expect)(res.body.detail).toBe("Invalid date for query parameter created_lte");
408
- (0, bun_test_1.expect)(res.body.source).toEqual({ parameter: "created_lte" });
409
- return [2 /*return*/];
410
- }
411
- });
412
- }); });
413
- (0, bun_test_1.it)("applies created_gte alone as a lower Date bound", function () { return __awaiter(void 0, void 0, void 0, function () {
414
- var res, names;
415
- return __generator(this, function (_a) {
416
- switch (_a.label) {
417
- case 0: return [4 /*yield*/, agent
418
- .get("/food")
419
- .query({ created_gte: "2021-12-03T00:00:15.000Z", limit: 10 })
420
- .expect(200)];
421
- case 1:
422
- res = _a.sent();
423
- names = res.body.data.map(function (d) { return d.name; }).sort();
424
- (0, bun_test_1.expect)(names).toEqual(["Spinach"]);
425
- return [2 /*return*/];
426
- }
427
- });
428
- }); });
429
- (0, bun_test_1.it)("applies created_lte alone as an upper Date bound", function () { return __awaiter(void 0, void 0, void 0, function () {
430
- var res, names;
431
- return __generator(this, function (_a) {
432
- switch (_a.label) {
433
- case 0: return [4 /*yield*/, agent
434
- .get("/food")
435
- .query({ created_lte: "2021-12-03T00:00:05.000Z", limit: 10 })
436
- .expect(200)];
437
- case 1:
438
- res = _a.sent();
439
- names = res.body.data.map(function (d) { return d.name; }).sort();
440
- (0, bun_test_1.expect)(names).toEqual(["Carrots"]);
441
- return [2 /*return*/];
442
- }
443
- });
444
- }); });
445
- (0, bun_test_1.it)("drops a scalar direct field value when a date range is also provided", function () { return __awaiter(void 0, void 0, void 0, function () {
446
- var res, names;
447
- return __generator(this, function (_a) {
448
- switch (_a.label) {
449
- case 0: return [4 /*yield*/, agent
450
- .get("/food")
451
- .query({
452
- created: "2021-12-03T00:00:20.000Z",
453
- created_gte: "2021-12-03T00:00:05.000Z",
454
- created_lte: "2021-12-03T00:00:25.000Z",
455
- limit: 10,
456
- })
457
- .expect(200)];
458
- case 1:
459
- res = _a.sent();
460
- names = res.body.data.map(function (d) { return d.name; }).sort();
461
- (0, bun_test_1.expect)(names).toEqual(["Pizza", "Spinach"]);
462
- return [2 /*return*/];
463
- }
464
- });
465
- }); });
466
- (0, bun_test_1.it)("merges date-range bounds into an existing object query for the field", function () { return __awaiter(void 0, void 0, void 0, function () {
467
- var res, names;
468
- return __generator(this, function (_a) {
469
- switch (_a.label) {
470
- case 0: return [4 /*yield*/, agent
471
- .get("/food?".concat(qs_1.default.stringify({
472
- created: { $lte: "2021-12-03T00:00:25.000Z" },
473
- created_gte: "2021-12-03T00:00:05.000Z",
474
- limit: 10,
475
- })))
476
- .expect(200)];
477
- case 1:
478
- res = _a.sent();
479
- names = res.body.data.map(function (d) { return d.name; }).sort();
480
- (0, bun_test_1.expect)(names).toEqual(["Pizza", "Spinach"]);
481
- return [2 /*return*/];
482
- }
483
- });
484
- }); });
485
- (0, bun_test_1.it)("list query params not in list", function () { return __awaiter(void 0, void 0, void 0, function () {
486
- var res;
487
- return __generator(this, function (_a) {
488
- switch (_a.label) {
489
- case 0: return [4 /*yield*/, agent.get("/food?ownerId=".concat(admin._id)).expect(400)];
490
- case 1:
491
- res = _a.sent();
492
- (0, bun_test_1.expect)(res.body.title).toBe("Query parameter not allowed");
493
- (0, bun_test_1.expect)(res.body.detail).toBe("ownerId is not allowed as a query param.");
494
- return [2 /*return*/];
495
- }
496
- });
497
- }); });
498
- (0, bun_test_1.it)("list query by nested param", function () { return __awaiter(void 0, void 0, void 0, function () {
499
- var res;
500
- return __generator(this, function (_a) {
501
- switch (_a.label) {
502
- case 0: return [4 /*yield*/, agent.get("/food?source.name=USDA").expect(200)];
503
- case 1:
504
- res = _a.sent();
505
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
506
- (0, bun_test_1.expect)(res.body.total).toBe(1);
507
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(carrots.id);
508
- return [2 /*return*/];
509
- }
510
- });
511
- }); });
512
- (0, bun_test_1.it)("query by date", function () { return __awaiter(void 0, void 0, void 0, function () {
513
- var authRes, token, res, createdDates;
514
- return __generator(this, function (_a) {
515
- switch (_a.label) {
516
- case 0: return [4 /*yield*/, server
517
- .post("/auth/login")
518
- .send({ email: "admin@example.com", password: "securePassword" })
519
- .expect(200)];
520
- case 1:
521
- authRes = _a.sent();
522
- token = authRes.body.data.token;
523
- return [4 /*yield*/, server
524
- .get("/food?limit=3&".concat(qs_1.default.stringify({
525
- created: {
526
- $gte: "2021-12-03T00:00:00.000Z",
527
- $lte: "2021-12-03T00:00:20.000Z",
528
- },
529
- })))
530
- .set("authorization", "Bearer ".concat(token))
531
- .expect(200)];
532
- case 2:
533
- res = _a.sent();
534
- (0, bun_test_1.expect)(res.body.data.map(function (d) { return d.created; })).toEqual(bun_test_1.expect.arrayContaining([
535
- "2021-12-03T00:00:20.000Z",
536
- "2021-12-03T00:00:10.000Z",
537
- "2021-12-03T00:00:00.000Z",
538
- ]));
539
- (0, bun_test_1.expect)(res.body.data.map(function (d) { return d.created; })).toHaveLength(3);
540
- return [4 /*yield*/, server
541
- .get("/food?limit=3&".concat(qs_1.default.stringify({
542
- created: {
543
- $gte: "2021-12-03T00:00:00.000Z",
544
- $lt: "2021-12-03T00:00:20.000Z",
545
- },
546
- })))
547
- .set("authorization", "Bearer ".concat(token))
548
- .expect(200)];
549
- case 3:
550
- res = _a.sent();
551
- (0, bun_test_1.expect)(res.body.data.map(function (d) { return d.created; })).toEqual(bun_test_1.expect.arrayContaining(["2021-12-03T00:00:10.000Z", "2021-12-03T00:00:00.000Z"]));
552
- (0, bun_test_1.expect)(res.body.data.map(function (d) { return d.created; })).toHaveLength(2);
553
- return [4 /*yield*/, server
554
- .get("/food?limit=3&".concat(qs_1.default.stringify({
555
- created: {
556
- $gt: "2021-12-03T00:00:00.000Z",
557
- $lt: "2021-12-03T00:00:20.000Z",
558
- },
559
- })))
560
- .set("authorization", "Bearer ".concat(token))
561
- .expect(200)];
562
- case 4:
563
- res = _a.sent();
564
- createdDates = res.body.data.map(function (d) { return d.created; });
565
- (0, bun_test_1.expect)(createdDates).toEqual(bun_test_1.expect.arrayContaining(["2021-12-03T00:00:10.000Z"]));
566
- (0, bun_test_1.expect)(createdDates).toHaveLength(1);
567
- return [2 /*return*/];
568
- }
569
- });
570
- }); });
571
- (0, bun_test_1.it)("query with a space", function () { return __awaiter(void 0, void 0, void 0, function () {
572
- var greenBeans, res;
573
- return __generator(this, function (_a) {
574
- switch (_a.label) {
575
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({
576
- calories: 102,
577
- created: Date.now() - 10,
578
- name: "Green Beans",
579
- ownerId: admin === null || admin === void 0 ? void 0 : admin._id,
580
- })];
581
- case 1:
582
- greenBeans = _a.sent();
583
- return [4 /*yield*/, agent.get("/food?".concat(qs_1.default.stringify({ name: "Green Beans" }))).expect(200)];
584
- case 2:
585
- res = _a.sent();
586
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
587
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(greenBeans === null || greenBeans === void 0 ? void 0 : greenBeans.id);
588
- (0, bun_test_1.expect)(res.body.data[0].name).toBe("Green Beans");
589
- return [2 /*return*/];
590
- }
591
- });
592
- }); });
593
- (0, bun_test_1.it)("query with a regex", function () { return __awaiter(void 0, void 0, void 0, function () {
594
- var greenBeans, res;
595
- return __generator(this, function (_a) {
596
- switch (_a.label) {
597
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({
598
- calories: 102,
599
- created: Date.now() - 10,
600
- name: "Green Beans",
601
- ownerId: admin === null || admin === void 0 ? void 0 : admin._id,
602
- })];
603
- case 1:
604
- greenBeans = _a.sent();
605
- return [4 /*yield*/, agent.get("/food?".concat(qs_1.default.stringify({ name: { $regex: "Green" } }))).expect(200)];
606
- case 2:
607
- res = _a.sent();
608
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
609
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(greenBeans === null || greenBeans === void 0 ? void 0 : greenBeans.id);
610
- (0, bun_test_1.expect)(res.body.data[0].name).toBe("Green Beans");
611
- return [4 /*yield*/, agent.get("/food?".concat(qs_1.default.stringify({ name: { $regex: "green" } }))).expect(200)];
612
- case 3:
613
- res = _a.sent();
614
- (0, bun_test_1.expect)(res.body.data).toHaveLength(0);
615
- return [4 /*yield*/, agent
616
- .get("/food?".concat(qs_1.default.stringify({ name: { $options: "i", $regex: "green" } })))
617
- .expect(200)];
618
- case 4:
619
- res = _a.sent();
620
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
621
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(greenBeans === null || greenBeans === void 0 ? void 0 : greenBeans.id);
622
- return [2 /*return*/];
623
- }
624
- });
625
- }); });
626
- (0, bun_test_1.it)("query with an $in operator", function () { return __awaiter(void 0, void 0, void 0, function () {
627
- var res, names1, names2;
628
- return __generator(this, function (_a) {
629
- switch (_a.label) {
630
- case 0: return [4 /*yield*/, server
631
- .get("/food?".concat(qs_1.default.stringify({
632
- name: {
633
- $in: ["Apple", "Spinach"],
634
- },
635
- })))
636
- .expect(200)];
637
- case 1:
638
- res = _a.sent();
639
- names1 = res.body.data.map(function (d) { return d.name; });
640
- (0, bun_test_1.expect)(names1).toEqual(bun_test_1.expect.arrayContaining(["Spinach"]));
641
- (0, bun_test_1.expect)(names1).toHaveLength(1);
642
- return [4 /*yield*/, server
643
- .get("/food?".concat(qs_1.default.stringify({
644
- name: {
645
- $in: ["Carrots", "Spinach"],
646
- },
647
- })))
648
- .expect(200)];
649
- case 2:
650
- res = _a.sent();
651
- names2 = res.body.data.map(function (d) { return d.name; });
652
- (0, bun_test_1.expect)(names2).toEqual(bun_test_1.expect.arrayContaining(["Spinach", "Carrots"]));
653
- (0, bun_test_1.expect)(names2).toHaveLength(2);
654
- return [2 /*return*/];
655
- }
656
- });
657
- }); });
658
- (0, bun_test_1.it)("query with an $in for _ids in nested object", function () { return __awaiter(void 0, void 0, void 0, function () {
659
- var res, names3;
660
- return __generator(this, function (_a) {
661
- switch (_a.label) {
662
- case 0: return [4 /*yield*/, server
663
- .get("/food?".concat(qs_1.default.stringify({
664
- eatenBy: {
665
- $in: [notAdmin._id.toString(), adminOther._id.toString()],
666
- },
667
- })))
668
- .expect(200)];
669
- case 1:
670
- res = _a.sent();
671
- (0, bun_test_1.expect)(res.body.more).toBe(false);
672
- (0, bun_test_1.expect)(res.body.total).toBe(2);
673
- (0, bun_test_1.expect)(res.body.data).toHaveLength(2);
674
- names3 = res.body.data.map(function (d) { return d.name; });
675
- (0, bun_test_1.expect)(names3).toEqual(bun_test_1.expect.arrayContaining(["Carrots", "Pizza"]));
676
- (0, bun_test_1.expect)(names3).toHaveLength(2);
677
- return [2 /*return*/];
678
- }
679
- });
680
- }); });
681
- (0, bun_test_1.it)("query $and operator on same field", function () { return __awaiter(void 0, void 0, void 0, function () {
682
- var res;
683
- return __generator(this, function (_a) {
684
- switch (_a.label) {
685
- case 0: return [4 /*yield*/, agent
686
- .get("/food?".concat(qs_1.default.stringify({ $and: [{ tags: "healthy" }, { tags: "cheap" }] })))
687
- .expect(200)];
688
- case 1:
689
- res = _a.sent();
690
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
691
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(carrots === null || carrots === void 0 ? void 0 : carrots._id.toString());
692
- return [2 /*return*/];
693
- }
694
- });
695
- }); });
696
- (0, bun_test_1.it)("query $and operator on same field, nested objects", function () { return __awaiter(void 0, void 0, void 0, function () {
697
- var res;
698
- return __generator(this, function (_a) {
699
- switch (_a.label) {
700
- case 0: return [4 /*yield*/, agent
701
- .get("/food?".concat(qs_1.default.stringify({
702
- $and: [{ eatenBy: admin.id }, { eatenBy: notAdmin.id }],
703
- })))
704
- .expect(200)];
705
- case 1:
706
- res = _a.sent();
707
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
708
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(carrots === null || carrots === void 0 ? void 0 : carrots._id.toString());
709
- return [2 /*return*/];
710
- }
711
- });
712
- }); });
713
- (0, bun_test_1.it)("query $or operator on same field", function () { return __awaiter(void 0, void 0, void 0, function () {
714
- var res, ids1;
715
- return __generator(this, function (_a) {
716
- switch (_a.label) {
717
- case 0: return [4 /*yield*/, agent
718
- .get("/food?".concat(qs_1.default.stringify({ $or: [{ name: "Carrots" }, { name: "Pizza" }] })))
719
- .expect(200)];
720
- case 1:
721
- res = _a.sent();
722
- (0, bun_test_1.expect)(res.body.data).toHaveLength(2);
723
- ids1 = res.body.data.map(function (d) { return d.id; });
724
- (0, bun_test_1.expect)(ids1).toEqual(bun_test_1.expect.arrayContaining([carrots === null || carrots === void 0 ? void 0 : carrots._id.toString(), pizza === null || pizza === void 0 ? void 0 : pizza._id.toString()]));
725
- (0, bun_test_1.expect)(ids1).toHaveLength(2);
726
- return [2 /*return*/];
727
- }
728
- });
729
- }); });
730
- (0, bun_test_1.it)("query $and operator on same field, nested objects for $or", function () { return __awaiter(void 0, void 0, void 0, function () {
731
- var res, ids2;
732
- return __generator(this, function (_a) {
733
- switch (_a.label) {
734
- case 0: return [4 /*yield*/, agent
735
- .get("/food?".concat(qs_1.default.stringify({
736
- $or: [{ eatenBy: admin.id }, { eatenBy: notAdmin.id }],
737
- limit: 3,
738
- })))
739
- .expect(200)];
740
- case 1:
741
- res = _a.sent();
742
- (0, bun_test_1.expect)(res.body.data).toHaveLength(2);
743
- ids2 = res.body.data.map(function (d) { return d.id; });
744
- (0, bun_test_1.expect)(ids2).toEqual(bun_test_1.expect.arrayContaining([carrots === null || carrots === void 0 ? void 0 : carrots._id.toString(), spinach === null || spinach === void 0 ? void 0 : spinach._id.toString()]));
745
- (0, bun_test_1.expect)(ids2).toHaveLength(2);
746
- return [2 /*return*/];
747
- }
748
- });
749
- }); });
750
- (0, bun_test_1.it)("query $and and $or are rejected if field is not in queryFields", function () { return __awaiter(void 0, void 0, void 0, function () {
751
- var res;
752
- return __generator(this, function (_a) {
753
- switch (_a.label) {
754
- case 0: return [4 /*yield*/, agent
755
- .get("/food?".concat(qs_1.default.stringify({ $and: [{ ownerId: "healthy" }, { tags: "cheap" }] })))
756
- .expect(400)];
757
- case 1:
758
- res = _a.sent();
759
- (0, bun_test_1.expect)(res.body.title).toBe("Query parameter not allowed");
760
- (0, bun_test_1.expect)(res.body.detail).toBe("ownerId is not allowed as a query param.");
761
- return [4 /*yield*/, agent
762
- .get("/food?".concat(qs_1.default.stringify({ $and: [{ tags: "cheap" }, { ownerId: "healthy" }] })))
763
- .expect(400)];
764
- case 2:
765
- res = _a.sent();
766
- (0, bun_test_1.expect)(res.body.title).toBe("Query parameter not allowed");
767
- (0, bun_test_1.expect)(res.body.detail).toBe("ownerId is not allowed as a query param.");
768
- return [4 /*yield*/, agent
769
- .get("/food?".concat(qs_1.default.stringify({ $or: [{ tags: "cheap" }, { ownerId: "healthy" }] })))
770
- .expect(400)];
771
- case 3:
772
- res = _a.sent();
773
- (0, bun_test_1.expect)(res.body.title).toBe("Query parameter not allowed");
774
- (0, bun_test_1.expect)(res.body.detail).toBe("ownerId is not allowed as a query param.");
775
- return [2 /*return*/];
776
- }
777
- });
778
- }); });
779
- (0, bun_test_1.it)("query with a number", function () { return __awaiter(void 0, void 0, void 0, function () {
780
- var res;
781
- return __generator(this, function (_a) {
782
- switch (_a.label) {
783
- case 0: return [4 /*yield*/, agent.get("/food?calories=100").expect(200)];
784
- case 1:
785
- res = _a.sent();
786
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
787
- (0, bun_test_1.expect)(res.body.data[0].id).toBe(carrots === null || carrots === void 0 ? void 0 : carrots._id.toString());
788
- return [2 /*return*/];
789
- }
790
- });
791
- }); });
792
- (0, bun_test_1.it)("update", function () { return __awaiter(void 0, void 0, void 0, function () {
793
- var res;
794
- return __generator(this, function (_a) {
795
- switch (_a.label) {
796
- case 0: return [4 /*yield*/, agent.patch("/food/".concat(spinach._id)).send({ name: "Kale" }).expect(200)];
797
- case 1:
798
- res = _a.sent();
799
- (0, bun_test_1.expect)(res.body.data.name).toBe("Kale");
800
- (0, bun_test_1.expect)(res.body.data.calories).toBe(1);
801
- (0, bun_test_1.expect)(res.body.data.hidden).toBe(false);
802
- return [4 /*yield*/, agent
803
- .patch("/food/".concat(spinach._id))
804
- .send({ lastEatenWith: { dressing: "2023-12-03T00:00:20.000Z" } })
805
- .expect(200)];
806
- case 2:
807
- res = _a.sent();
808
- (0, bun_test_1.expect)(res.body.data.name).toBe("Kale");
809
- (0, bun_test_1.expect)(res.body.data.calories).toBe(1);
810
- (0, bun_test_1.expect)(res.body.data.hidden).toBe(false);
811
- (0, bun_test_1.expect)(res.body.data.lastEatenWith).toEqual({
812
- dressing: "2023-12-03T00:00:20.000Z",
813
- });
814
- return [4 /*yield*/, agent
815
- .patch("/food/".concat(spinach._id))
816
- .send({
817
- lastEatenWith: {
818
- cucumber: "2023-12-04T12:00:20.000Z",
819
- dressing: "2023-12-03T00:00:20.000Z",
820
- },
821
- })
822
- .expect(200)];
823
- case 3:
824
- res = _a.sent();
825
- (0, bun_test_1.expect)(res.body.data.lastEatenWith).toEqual({
826
- cucumber: "2023-12-04T12:00:20.000Z",
827
- dressing: "2023-12-03T00:00:20.000Z",
828
- });
829
- return [2 /*return*/];
830
- }
831
- });
832
- }); });
833
- (0, bun_test_1.it)("update using dot notation", function () { return __awaiter(void 0, void 0, void 0, function () {
834
- var res, dbSpinach;
835
- return __generator(this, function (_a) {
836
- switch (_a.label) {
837
- case 0: return [4 /*yield*/, agent
838
- .patch("/food/".concat(spinach._id))
839
- .send({ "source.href": "https://food.com" })
840
- .expect(200)];
841
- case 1:
842
- res = _a.sent();
843
- (0, bun_test_1.expect)(res.body.data.source.href).toBe("https://food.com");
844
- (0, bun_test_1.expect)(res.body.data.source.name).toBe("Brand");
845
- (0, bun_test_1.expect)(res.body.data.source.dateAdded).toBe("2023-12-13T12:30:00.000Z");
846
- return [4 /*yield*/, tests_1.FoodModel.findById(spinach._id)];
847
- case 2:
848
- dbSpinach = _a.sent();
849
- (0, bun_test_1.expect)(dbSpinach === null || dbSpinach === void 0 ? void 0 : dbSpinach.source.href).toBe("https://food.com");
850
- (0, bun_test_1.expect)(dbSpinach === null || dbSpinach === void 0 ? void 0 : dbSpinach.source.name).toBe("Brand");
851
- (0, bun_test_1.expect)(dbSpinach === null || dbSpinach === void 0 ? void 0 : dbSpinach.source.dateAdded).toBe("2023-12-13T12:30:00.000Z");
852
- return [2 /*return*/];
853
- }
854
- });
855
- }); });
856
- });
857
- (0, bun_test_1.describe)("special query params", function () {
858
- var server;
859
- var app;
860
- var admin;
861
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
862
- var _a;
863
- return __generator(this, function (_b) {
864
- switch (_b.label) {
865
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
866
- case 1:
867
- _a = __read.apply(void 0, [_b.sent(), 1]), admin = _a[0];
868
- return [4 /*yield*/, tests_1.FoodModel.create({
869
- calories: 1,
870
- created: new Date("2021-12-03T00:00:20.000Z"),
871
- hidden: false,
872
- name: "Spinach",
873
- ownerId: admin._id,
874
- })];
875
- case 2:
876
- _b.sent();
877
- app = (0, tests_1.getBaseServer)();
878
- (0, auth_1.setupAuth)(app, tests_1.UserModel);
879
- (0, auth_1.addAuthRoutes)(app, tests_1.UserModel);
880
- return [2 /*return*/];
881
- }
882
- });
883
- }); });
884
- (0, bun_test_1.it)("period query param is stripped from query", function () { return __awaiter(void 0, void 0, void 0, function () {
885
- var res;
886
- return __generator(this, function (_a) {
887
- switch (_a.label) {
888
- case 0:
889
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
890
- allowAnonymous: true,
891
- permissions: {
892
- create: [permissions_1.Permissions.IsAny],
893
- delete: [permissions_1.Permissions.IsAny],
894
- list: [permissions_1.Permissions.IsAny],
895
- read: [permissions_1.Permissions.IsAny],
896
- update: [permissions_1.Permissions.IsAny],
897
- },
898
- queryFields: ["name", "period"],
899
- queryFilter: function (_user, query) {
900
- if (query === null || query === void 0 ? void 0 : query.period) {
901
- return query;
902
- }
903
- return query !== null && query !== void 0 ? query : {};
904
- },
905
- }));
906
- server = (0, supertest_1.default)(app);
907
- return [4 /*yield*/, server.get("/food?period=weekly").expect(200)];
908
- case 1:
909
- res = _a.sent();
910
- (0, bun_test_1.expect)(res.body.data).toBeDefined();
911
- return [2 /*return*/];
912
- }
913
- });
914
- }); });
915
- (0, bun_test_1.it)("query with false value", function () { return __awaiter(void 0, void 0, void 0, function () {
916
- var res;
917
- return __generator(this, function (_a) {
918
- switch (_a.label) {
919
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({
920
- calories: 50,
921
- created: new Date("2021-12-04T00:00:20.000Z"),
922
- hidden: true,
923
- name: "HiddenFood",
924
- ownerId: admin._id,
925
- })];
926
- case 1:
927
- _a.sent();
928
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
929
- allowAnonymous: true,
930
- permissions: {
931
- create: [permissions_1.Permissions.IsAny],
932
- delete: [permissions_1.Permissions.IsAny],
933
- list: [permissions_1.Permissions.IsAny],
934
- read: [permissions_1.Permissions.IsAny],
935
- update: [permissions_1.Permissions.IsAny],
936
- },
937
- queryFields: ["name", "hidden"],
938
- }));
939
- server = (0, supertest_1.default)(app);
940
- return [4 /*yield*/, server.get("/food?hidden=false").expect(200)];
941
- case 2:
942
- res = _a.sent();
943
- (0, bun_test_1.expect)(res.body.data.every(function (f) { return f.hidden === false; })).toBe(true);
944
- return [2 /*return*/];
945
- }
946
- });
947
- }); });
948
- });