jxp 3.0.0 → 4.0.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 (219) hide show
  1. package/.env.sample +31 -6
  2. package/README.md +27 -13
  3. package/dist/bin/init_db.d.ts +3 -0
  4. package/dist/bin/init_db.d.ts.map +1 -0
  5. package/dist/bin/init_db.js +48 -0
  6. package/dist/bin/init_db.js.map +1 -0
  7. package/dist/bin/server.d.ts +4 -0
  8. package/dist/bin/server.d.ts.map +1 -0
  9. package/dist/bin/server.js +132 -0
  10. package/dist/bin/server.js.map +1 -0
  11. package/dist/bin/setup.d.ts +3 -0
  12. package/dist/bin/setup.d.ts.map +1 -0
  13. package/dist/bin/setup.js +185 -0
  14. package/dist/bin/setup.js.map +1 -0
  15. package/dist/globals.d.ts +14 -0
  16. package/dist/globals.d.ts.map +1 -0
  17. package/dist/globals.js +3 -0
  18. package/dist/globals.js.map +1 -0
  19. package/dist/libs/aggregate_guard.d.ts +5 -0
  20. package/dist/libs/aggregate_guard.d.ts.map +1 -0
  21. package/dist/libs/aggregate_guard.js +58 -0
  22. package/dist/libs/aggregate_guard.js.map +1 -0
  23. package/dist/libs/bulkwrite_guard.d.ts +5 -0
  24. package/dist/libs/bulkwrite_guard.d.ts.map +1 -0
  25. package/dist/libs/bulkwrite_guard.js +44 -0
  26. package/dist/libs/bulkwrite_guard.js.map +1 -0
  27. package/dist/libs/cache.d.ts +2 -0
  28. package/dist/libs/cache.d.ts.map +1 -0
  29. package/dist/libs/cache.js +94 -0
  30. package/dist/libs/cache.js.map +1 -0
  31. package/dist/libs/call_guard.d.ts +3 -0
  32. package/dist/libs/call_guard.d.ts.map +1 -0
  33. package/dist/libs/call_guard.js +17 -0
  34. package/dist/libs/call_guard.js.map +1 -0
  35. package/dist/libs/connection_string.d.ts +3 -0
  36. package/dist/libs/connection_string.d.ts.map +1 -0
  37. package/dist/libs/connection_string.js +4 -0
  38. package/dist/libs/connection_string.js.map +1 -0
  39. package/dist/libs/datamunging.d.ts +2 -0
  40. package/dist/libs/datamunging.d.ts.map +1 -0
  41. package/dist/libs/datamunging.js +320 -0
  42. package/dist/libs/datamunging.js.map +1 -0
  43. package/dist/libs/docs.d.ts +2 -0
  44. package/dist/libs/docs.d.ts.map +1 -0
  45. package/dist/libs/docs.js +307 -0
  46. package/dist/libs/docs.js.map +1 -0
  47. package/dist/libs/groups.d.ts +2 -0
  48. package/dist/libs/groups.d.ts.map +1 -0
  49. package/dist/libs/groups.js +136 -0
  50. package/dist/libs/groups.js.map +1 -0
  51. package/dist/libs/jxp.d.ts +4 -0
  52. package/dist/libs/jxp.d.ts.map +1 -0
  53. package/dist/libs/jxp.js +1157 -0
  54. package/dist/libs/jxp.js.map +1 -0
  55. package/dist/libs/link_index.d.ts +14 -0
  56. package/dist/libs/link_index.d.ts.map +1 -0
  57. package/dist/libs/link_index.js +37 -0
  58. package/dist/libs/link_index.js.map +1 -0
  59. package/dist/libs/load-config.d.ts +16 -0
  60. package/dist/libs/load-config.d.ts.map +1 -0
  61. package/dist/libs/load-config.js +155 -0
  62. package/dist/libs/load-config.js.map +1 -0
  63. package/dist/libs/login.d.ts +2 -0
  64. package/dist/libs/login.d.ts.map +1 -0
  65. package/dist/libs/login.js +244 -0
  66. package/dist/libs/login.js.map +1 -0
  67. package/dist/libs/modeldir.d.ts +5 -0
  68. package/dist/libs/modeldir.d.ts.map +1 -0
  69. package/dist/libs/modeldir.js +15 -0
  70. package/dist/libs/modeldir.js.map +1 -0
  71. package/dist/libs/query_limits.d.ts +2 -0
  72. package/dist/libs/query_limits.d.ts.map +1 -0
  73. package/dist/libs/query_limits.js +84 -0
  74. package/dist/libs/query_limits.js.map +1 -0
  75. package/dist/libs/query_manipulation.d.ts +3 -0
  76. package/dist/libs/query_manipulation.d.ts.map +1 -0
  77. package/{libs → dist/libs}/query_manipulation.js +22 -21
  78. package/dist/libs/query_manipulation.js.map +1 -0
  79. package/dist/libs/query_sanitize.d.ts +5 -0
  80. package/dist/libs/query_sanitize.d.ts.map +1 -0
  81. package/dist/libs/query_sanitize.js +109 -0
  82. package/dist/libs/query_sanitize.js.map +1 -0
  83. package/dist/libs/response_sanitize.d.ts +7 -0
  84. package/dist/libs/response_sanitize.d.ts.map +1 -0
  85. package/dist/libs/response_sanitize.js +57 -0
  86. package/dist/libs/response_sanitize.js.map +1 -0
  87. package/dist/libs/safe_error.d.ts +3 -0
  88. package/dist/libs/safe_error.d.ts.map +1 -0
  89. package/dist/libs/safe_error.js +14 -0
  90. package/dist/libs/safe_error.js.map +1 -0
  91. package/dist/libs/schema.d.ts +14 -0
  92. package/dist/libs/schema.d.ts.map +1 -0
  93. package/dist/libs/schema.js +98 -0
  94. package/dist/libs/schema.js.map +1 -0
  95. package/dist/libs/schema_description.d.ts +3 -0
  96. package/dist/libs/schema_description.d.ts.map +1 -0
  97. package/{libs → dist/libs}/schema_description.js +12 -21
  98. package/dist/libs/schema_description.js.map +1 -0
  99. package/dist/libs/security.d.ts +2 -0
  100. package/dist/libs/security.d.ts.map +1 -0
  101. package/dist/libs/security.js +428 -0
  102. package/dist/libs/security.js.map +1 -0
  103. package/dist/libs/setup.d.ts +2 -0
  104. package/dist/libs/setup.d.ts.map +1 -0
  105. package/dist/libs/setup.js +87 -0
  106. package/dist/libs/setup.js.map +1 -0
  107. package/dist/libs/startup-deprecations.d.ts +2 -0
  108. package/dist/libs/startup-deprecations.d.ts.map +1 -0
  109. package/dist/libs/startup-deprecations.js +21 -0
  110. package/dist/libs/startup-deprecations.js.map +1 -0
  111. package/dist/libs/startup.d.ts +16 -0
  112. package/dist/libs/startup.d.ts.map +1 -0
  113. package/dist/libs/startup.js +63 -0
  114. package/dist/libs/startup.js.map +1 -0
  115. package/dist/libs/ws.d.ts +2 -0
  116. package/dist/libs/ws.d.ts.map +1 -0
  117. package/{libs → dist/libs}/ws.js +91 -73
  118. package/dist/libs/ws.js.map +1 -0
  119. package/dist/models/apikey_model.d.ts +10 -0
  120. package/dist/models/apikey_model.d.ts.map +1 -0
  121. package/dist/models/apikey_model.js +14 -0
  122. package/dist/models/apikey_model.js.map +1 -0
  123. package/dist/models/link_model.d.ts +9 -0
  124. package/dist/models/link_model.d.ts.map +1 -0
  125. package/dist/models/link_model.js +20 -0
  126. package/dist/models/link_model.js.map +1 -0
  127. package/dist/models/refreshtoken_model.d.ts +11 -0
  128. package/dist/models/refreshtoken_model.d.ts.map +1 -0
  129. package/dist/models/refreshtoken_model.js +22 -0
  130. package/dist/models/refreshtoken_model.js.map +1 -0
  131. package/dist/models/test_model.d.ts +24 -0
  132. package/dist/models/test_model.d.ts.map +1 -0
  133. package/dist/models/test_model.js +44 -0
  134. package/dist/models/test_model.js.map +1 -0
  135. package/dist/models/token_model.d.ts +14 -0
  136. package/dist/models/token_model.d.ts.map +1 -0
  137. package/dist/models/token_model.js +25 -0
  138. package/dist/models/token_model.js.map +1 -0
  139. package/dist/models/user_model.d.ts +15 -0
  140. package/dist/models/user_model.d.ts.map +1 -0
  141. package/dist/models/user_model.js +38 -0
  142. package/dist/models/user_model.js.map +1 -0
  143. package/dist/models/usergroups_model.d.ts +9 -0
  144. package/dist/models/usergroups_model.d.ts.map +1 -0
  145. package/dist/models/usergroups_model.js +18 -0
  146. package/dist/models/usergroups_model.js.map +1 -0
  147. package/dist/types/jxp-config.d.ts +134 -0
  148. package/dist/types/jxp-config.d.ts.map +1 -0
  149. package/dist/types/jxp-config.js +3 -0
  150. package/dist/types/jxp-config.js.map +1 -0
  151. package/dist/types/schema-fields.d.ts +43 -0
  152. package/dist/types/schema-fields.d.ts.map +1 -0
  153. package/dist/types/schema-fields.js +3 -0
  154. package/dist/types/schema-fields.js.map +1 -0
  155. package/docs/aggregations.md +40 -21
  156. package/docs/api.md +55 -15
  157. package/docs/authentication.md +60 -37
  158. package/docs/bulk_writes.md +11 -8
  159. package/docs/caching.md +28 -11
  160. package/docs/changelog.md +237 -31
  161. package/docs/configuration.md +99 -118
  162. package/docs/hooks.md +12 -8
  163. package/docs/index.md +17 -17
  164. package/docs/installation.md +23 -18
  165. package/docs/permissions.md +33 -19
  166. package/docs/queries.md +9 -5
  167. package/docs/schemas.md +21 -10
  168. package/docs/special.md +4 -4
  169. package/docs/typescript.md +86 -0
  170. package/docs/websocket.md +99 -1
  171. package/mkdocs.yml +1 -0
  172. package/package.json +121 -69
  173. package/templates/api.pug +32 -0
  174. package/templates/app-server.js +43 -0
  175. package/templates/app.env.sample +8 -0
  176. package/templates/assets/api-console.js +133 -0
  177. package/templates/assets/docs.css +227 -0
  178. package/templates/index.pug +62 -2
  179. package/templates/layout.pug +17 -11
  180. package/templates/md.pug +1 -1
  181. package/templates/model.pug +117 -68
  182. package/templates/navbar.pug +14 -21
  183. package/templates/sidebar.pug +30 -0
  184. package/.env +0 -9
  185. package/.eslintrc.json +0 -20
  186. package/.jshintrc +0 -4
  187. package/.vscode/launch.json +0 -34
  188. package/.vscode/settings.json +0 -3
  189. package/Dockerfile +0 -7
  190. package/bin/init_db.js +0 -54
  191. package/bin/server.js +0 -131
  192. package/bin/setup.js +0 -190
  193. package/config_sample.json +0 -10
  194. package/docker-compose.yml +0 -32
  195. package/libs/cache.js +0 -92
  196. package/libs/connection_string.js +0 -24
  197. package/libs/datamunging.js +0 -335
  198. package/libs/docs.js +0 -162
  199. package/libs/groups.js +0 -133
  200. package/libs/jxp.js +0 -1208
  201. package/libs/login.js +0 -222
  202. package/libs/modeldir.js +0 -14
  203. package/libs/schema.js +0 -99
  204. package/libs/security.js +0 -410
  205. package/libs/setup.js +0 -90
  206. package/models/apikey_model.js +0 -10
  207. package/models/link_model.js +0 -17
  208. package/models/refreshtoken_model.js +0 -20
  209. package/models/test_model.js +0 -50
  210. package/models/token_model.js +0 -23
  211. package/models/user_model.js +0 -38
  212. package/models/usergroups_model.js +0 -15
  213. package/setup.sh +0 -3
  214. package/test/.eslintrc.json +0 -13
  215. package/test/auth.js +0 -190
  216. package/test/init.js +0 -92
  217. package/test/setup.test.js +0 -62
  218. package/test/test.js +0 -1527
  219. package/test/wstest.js.old +0 -124
package/test/test.js DELETED
@@ -1,1527 +0,0 @@
1
- process.env.NODE_ENV = 'test';
2
- const config = require("config");
3
- const path = require("path");
4
-
5
- var model_dir = config.model_dir || path.join(process.cwd(), "./models");
6
- const Test = require(path.join(model_dir, "test_model"));
7
-
8
- var chai = require('chai');
9
- var chaiHttp = require('chai-http');
10
- var should = chai.should();
11
-
12
- var init = require("./init");
13
-
14
- var server = require("../bin/server");
15
-
16
- chai.use(chaiHttp);
17
-
18
- const pause = ms => new Promise(res => setTimeout(res, ms));
19
-
20
- describe('Test', () => {
21
- before(async function () {
22
- await init.init();
23
- })
24
-
25
- beforeEach(async function () {
26
- await pause(0);
27
- })
28
-
29
-
30
- var apikey = null;
31
- var token = null;
32
- var refresh_token = null;
33
- var user_id = null;
34
- var objectid = null;
35
-
36
- describe("login", () => {
37
- it("it should login", (done) => {
38
- chai.request(server)
39
- .post("/login")
40
- .send({ email: init.email, password: init.password })
41
- .end((err, res) => {
42
- res.should.have.status(200);
43
- res.body.should.have.property('user_id');
44
- res.body.should.have.property('apikey');
45
- res.body.should.have.property('token');
46
- res.body.should.have.property('token_expires');
47
- res.body.should.have.property('refresh_token');
48
- res.body.should.have.property('refresh_token_expires');
49
- res.body.should.have.property('provider');
50
- res.body.provider.should.be.eql('https://api.workspaceman.nl');
51
- apikey = res.body.apikey;
52
- token = res.body.token;
53
- user_id = res.body.user_id;
54
- done();
55
- });
56
- });
57
- });
58
-
59
- describe("logout", () => {
60
- it("should logout", done => {
61
- chai.request(server)
62
- .get("/login/logout")
63
- .set("Authorization", `Bearer ${token}`)
64
- .end((err, res) => {
65
- res.should.have.status(200);
66
- done();
67
- });
68
- })
69
- })
70
-
71
- describe("logged_out", () => {
72
- it("should have an expired token", done => {
73
- chai.request(server)
74
- .get("/api/test")
75
- .set("Authorization", `Bearer ${token}`)
76
- .end((err, res) => {
77
- res.should.have.status(403);
78
- done();
79
- });
80
- })
81
- })
82
-
83
- describe("login_again", () => {
84
- it("it should login again with the same api key but a new token", (done) => {
85
- chai.request(server)
86
- .post("/login")
87
- .send({ email: init.email, password: init.password })
88
- .end((err, res) => {
89
- res.should.have.status(200);
90
- res.body.should.have.property('user_id');
91
- res.body.should.have.property('apikey');
92
- res.body.should.have.property('token');
93
- res.body.should.have.property('token_expires');
94
- res.body.apikey.should.be.eql(apikey);
95
- res.body.token.should.not.eql(token);
96
- token = res.body.token;
97
- refresh_token = res.body.refresh_token;
98
- done();
99
- });
100
- });
101
- });
102
-
103
- describe("refresh_token", () => {
104
- it("it should refresh the token", (done) => {
105
- chai.request(server)
106
- .post("/refresh")
107
- .set("Authorization", `Bearer ${refresh_token}`)
108
- .end((err, res) => {
109
- res.should.have.status(200);
110
- res.body.should.have.property('user_id');
111
- res.body.should.have.property('token');
112
- res.body.should.have.property('token_expires');
113
- res.body.should.have.property('refresh_token');
114
- res.body.should.have.property('refresh_token_expires');
115
- res.body.token.should.not.eql(token);
116
- res.body.refresh_token.should.not.eql(refresh_token);
117
- token = res.body.token;
118
- refresh_token = res.body.refresh_token;
119
- done();
120
- });
121
- });
122
- });
123
-
124
- describe("Authentication", () => {
125
- it("should authenticate with basic auth", done => {
126
- chai.request(server)
127
- .get("/api/user")
128
- .auth(init.email, init.password)
129
- .end((err, res) => {
130
- res.should.have.status(200);
131
- res.body.data.should.be.an('array');
132
- done();
133
- });
134
- });
135
- it("should authenticate with a token", done => {
136
- chai.request(server)
137
- .get("/api/user")
138
- .set("Authorization", `Bearer ${token}`)
139
- .end((err, res) => {
140
- res.should.have.status(200);
141
- res.body.data.should.be.an('array');
142
- done();
143
- });
144
- })
145
- it("should authenticate with an API key in the header", done => {
146
- chai.request(server)
147
- .get("/api/user")
148
- .set("X-API-Key", apikey)
149
- .end((err, res) => {
150
- res.should.have.status(200);
151
- res.body.data.should.be.an('array');
152
- done();
153
- });
154
- })
155
- it("should authenticate with an API key in the url", done => {
156
- chai.request(server)
157
- .get(`/api/user?apikey=${apikey}`)
158
- .end((err, res) => {
159
- res.should.have.status(200);
160
- res.body.data.should.be.an('array');
161
- done();
162
- });
163
- })
164
- it("should fail authenticate without any auth methods", done => {
165
- chai.request(server)
166
- .get(`/api/user`)
167
- .end((err, res) => {
168
- res.should.have.status(403);
169
- done();
170
- });
171
- })
172
- })
173
-
174
- describe("/GET test", () => {
175
- it("it should GET all the tests", (done) => {
176
- Test.deleteMany(() => {
177
- chai.request(server)
178
- .get("/api/test")
179
- // .auth(init.email, init.password)
180
- .end((err, res) => {
181
- res.should.have.status(200);
182
- res.body.data.should.be.an('array');
183
- res.body.data.length.should.be.eql(0);
184
- done();
185
- });
186
- });
187
- });
188
- });
189
-
190
- describe("/GET test count", () => {
191
- it("it should count all the tests", (done) => {
192
- Test.deleteMany(() => {
193
- chai.request(server)
194
- .get("/count/test")
195
- .end((err, res) => {
196
- res.should.have.status(200);
197
- res.body.count.should.be.eql(0);
198
- done();
199
- });
200
- });
201
- });
202
- });
203
-
204
- var post_id = null;
205
- describe("/POST test", () => {
206
- it("it should POST a new test", (done) => {
207
- var test = {
208
- foo: "Foo",
209
- bar: "Bar",
210
- yack: { yack: "yack", shmack: 1 },
211
- shmack: ["do", "ray", "me"],
212
- password: "password",
213
- fulltext: "In Xanadu did Kubla Khan a stately pleasure dome decree",
214
- };
215
- chai.request(server)
216
- .post("/api/test")
217
- .auth(init.email, init.password)
218
- .send(test)
219
- .end((err, res) => {
220
- res.should.have.status(200);
221
- res.body.data.should.be.an('object');
222
- res.body.data.should.have.property("_id");
223
- res.body.data.should.have.property("foo");
224
- res.body.data.should.have.property("bar");
225
- res.body.data.should.have.property("yack").which.should.be.an("object");
226
- res.body.data.should.have.property("shmack");
227
- res.body.data.shmack.should.be.an("array");
228
- res.body.data.foo.should.be.a("string");
229
- res.body.data.bar.should.be.a("string");
230
- res.body.data.foo.length.should.be.eql(3);
231
- res.body.data.foo.length.should.not.be.eql("password");
232
- post_id = res.body.data._id;
233
- done();
234
- });
235
- });
236
-
237
- it("it should POST a test with string_array", (done) => {
238
- var test = {
239
- foo: "FooArray",
240
- bar: "BarArray",
241
- string_array: ["one", "two", "three"]
242
- };
243
- chai.request(server)
244
- .post("/api/test")
245
- .auth(init.email, init.password)
246
- .send(test)
247
- .end((err, res) => {
248
- res.should.have.status(200);
249
- res.body.data.should.be.an('object');
250
- res.body.data.should.have.property("string_array");
251
- res.body.data.string_array.should.be.an("array");
252
- res.body.data.string_array.should.have.length(3);
253
- res.body.data.string_array[0].should.equal("one");
254
- res.body.data.string_array[1].should.equal("two");
255
- res.body.data.string_array[2].should.equal("three");
256
- done();
257
- });
258
- });
259
- });
260
-
261
- describe("/GET test count", () => {
262
- it("it should count all the tests", (done) => {
263
- chai.request(server)
264
- .get("/count/test")
265
- .end((err, res) => {
266
- res.should.have.status(200);
267
- res.body.count.should.be.eql(2);
268
- done();
269
- });
270
- });
271
- });
272
-
273
- describe("/PUT test", () => {
274
- it("it should PUT a new test", (done) => {
275
- var test = {
276
- foo: "Foo1",
277
- };
278
- chai.request(server)
279
- .put("/api/test/" + post_id)
280
- .send(test)
281
- .auth(init.email, init.password)
282
- .end((err, res) => {
283
- res.should.have.status(200);
284
- res.body.data.should.be.an('object');
285
- res.body.data.should.have.property("_id");
286
- res.body.data.should.have.property("foo")
287
- res.body.data.foo.should.eql("Foo1");
288
- res.body.data._updated_by_id.should.eql(user_id);
289
- done();
290
- });
291
- });
292
- });
293
-
294
- describe("Search test", () => {
295
- it("it should search all the tests", (done) => {
296
- chai.request(server)
297
- .get("/api/test?search=Xanadu")
298
- // .auth(init.email, init.password)
299
- .end((err, res) => {
300
- res.should.have.status(200);
301
- res.body.data.should.be.a('array');
302
- res.body.data.length.should.be.eql(1);
303
- res.body.data[0].score.should.be.a("number");
304
- done();
305
- });
306
- });
307
- });
308
-
309
- describe("Date filtering", () => {
310
- let dateTestId;
311
- const testDate = new Date('2024-03-20T10:00:00.000Z');
312
-
313
- before(async () => {
314
- // Create a test record with a known date
315
- const test = new Test({
316
- foo: "DateTest",
317
- bar: "DateTestBar",
318
- date_field: testDate
319
- });
320
- const saved = await test.save();
321
- dateTestId = saved._id;
322
- });
323
-
324
- it("should filter by exact date", (done) => {
325
- chai.request(server)
326
- .get(`/api/test?filter[date_field]=${testDate.toISOString()}`)
327
- .auth(init.email, init.password)
328
- .end((err, res) => {
329
- res.should.have.status(200);
330
- res.body.data.should.be.an('array');
331
- res.body.data.length.should.be.eql(1);
332
- res.body.data[0]._id.should.equal(dateTestId.toString());
333
- done();
334
- });
335
- });
336
-
337
- it("should filter by date range using array syntax", (done) => {
338
- const startDate = new Date(testDate);
339
- startDate.setDate(startDate.getDate() - 1);
340
- const endDate = new Date(testDate);
341
- endDate.setDate(endDate.getDate() + 1);
342
-
343
- chai.request(server)
344
- .get(`/api/test?filter[date_field]=$gte:${startDate.toISOString()}&filter[date_field]=$lte:${endDate.toISOString()}`)
345
- .auth(init.email, init.password)
346
- .end((err, res) => {
347
- res.should.have.status(200);
348
- res.body.data.should.be.an('array');
349
- res.body.data.length.should.be.eql(1);
350
- res.body.data[0]._id.should.equal(dateTestId.toString());
351
- done();
352
- });
353
- });
354
-
355
- it("should filter by date range using operator syntax", (done) => {
356
- const startDate = new Date(testDate);
357
- startDate.setDate(startDate.getDate() - 1);
358
- const endDate = new Date(testDate);
359
- endDate.setDate(endDate.getDate() + 1);
360
-
361
- chai.request(server)
362
- .get(`/api/test?filter[date_field]=$gte:${startDate.toISOString()}&filter[date_field]=$lte:${endDate.toISOString()}`)
363
- .auth(init.email, init.password)
364
- .end((err, res) => {
365
- res.should.have.status(200);
366
- res.body.data.should.be.an('array');
367
- res.body.data.length.should.be.eql(1);
368
- res.body.data[0]._id.should.equal(dateTestId.toString());
369
- done();
370
- });
371
- });
372
-
373
- it("should handle invalid date formats gracefully", (done) => {
374
- chai.request(server)
375
- .get('/api/test?filter[date_field]=invalid-date')
376
- .auth(init.email, init.password)
377
- .end((err, res) => {
378
- res.should.have.status(500);
379
- res.body.should.have.property('code', 'InternalServer');
380
- done();
381
- });
382
- });
383
-
384
- it("should return empty array for dates outside range", (done) => {
385
- const futureDate = new Date(testDate);
386
- futureDate.setFullYear(futureDate.getFullYear() + 1);
387
-
388
- chai.request(server)
389
- .get(`/api/test?filter[date_field]=${futureDate.toISOString()}`)
390
- .auth(init.email, init.password)
391
- .end((err, res) => {
392
- res.should.have.status(200);
393
- res.body.data.should.be.an('array');
394
- res.body.data.length.should.be.eql(0);
395
- done();
396
- });
397
- });
398
- });
399
-
400
- describe("/GET test", () => {
401
- it("it should GET a single test contained in a data object", (done) => {
402
- chai.request(server)
403
- .get("/api/test/" + post_id)
404
- .auth(init.email, init.password)
405
- .end((err, res) => {
406
- res.should.have.status(200);
407
- res.body.should.have.property("data");
408
- res.body.data.should.be.an('object');
409
- res.body.data.should.have.property("_id");
410
- res.body.data.should.have.property("foo")
411
- res.body.data.foo.should.eql("Foo1");
412
- done();
413
- });
414
- });
415
- });
416
-
417
- describe("Link test", () => {
418
- var link_id = null;
419
- it("should add a LINK item", done => {
420
- let data = {
421
- name: "name1",
422
- val: "val1"
423
- }
424
- chai.request(server)
425
- .post("/api/link")
426
- .auth(init.email, init.password)
427
- .send(data)
428
- .end((err, res) => {
429
- res.should.have.status(200);
430
- res.body.data.should.be.an('object');
431
- res.body.data.should.have.property("_id");
432
- res.body.data.should.have.property("name")
433
- res.body.data.name.should.eql("name1");
434
- link_id = res.body.data._id;
435
- done();
436
- });
437
- });
438
- it("should link a LINK item to a TEST", done => {
439
- chai.request(server)
440
- .put("/api/test/" + post_id)
441
- .auth(init.email, init.password)
442
- .send({ link_id })
443
- .end((err, res) => {
444
- res.should.have.status(200);
445
- res.body.data.should.be.an('object');
446
- res.body.data.should.have.property("link_id")
447
- res.body.data.link_id.should.eql(link_id);
448
- done();
449
- });
450
- });
451
- var other_link_id = null;
452
- it("should add another LINK item", done => {
453
- let data = {
454
- name: "name2",
455
- val: "val2"
456
- }
457
- chai.request(server)
458
- .post("/api/link")
459
- .auth(init.email, init.password)
460
- .send(data)
461
- .end((err, res) => {
462
- res.should.have.status(200);
463
- res.body.data.should.be.an('object');
464
- res.body.data.should.have.property("_id");
465
- res.body.data.should.have.property("name")
466
- res.body.data.name.should.eql("name2");
467
- other_link_id = res.body.data._id;
468
- done();
469
- });
470
- });
471
- it("should link another LINK item to a TEST", done => {
472
- chai.request(server)
473
- .put("/api/test/" + post_id)
474
- .auth(init.email, init.password)
475
- .send({ other_link_id })
476
- .end((err, res) => {
477
- res.should.have.status(200);
478
- res.body.data.should.be.an('object');
479
- res.body.data.should.have.property("other_link_id")
480
- res.body.data.other_link_id.should.eql(other_link_id);
481
- done();
482
- });
483
- });
484
- it("should non-descructively autopopulate on a single record", done => {
485
- chai.request(server)
486
- .get(`/api/test/${post_id}?populate=link`)
487
- .auth(init.email, init.password)
488
- .end((err, res) => {
489
- res.should.have.status(200);
490
- res.body.should.have.property("data");
491
- res.body.data.should.have.property("link");
492
- res.body.data.link.should.be.an('object');
493
- res.body.data.link.name.should.eql("name1");
494
- res.body.data.link.val.should.eql("val1");
495
- res.body.data.should.have.property("link_id");
496
- res.body.data.link_id.should.be.eql(link_id);
497
- done();
498
- });
499
- });
500
- it("should non-descructively autopopulate on a single record to a specific virtual", done => {
501
- chai.request(server)
502
- .get(`/api/test/${post_id}?populate=other_link`)
503
- .auth(init.email, init.password)
504
- .end((err, res) => {
505
- res.should.have.status(200);
506
- res.body.should.have.property("data");
507
- res.body.data.should.have.property("other_link")
508
- res.body.data.other_link.should.be.an('object');
509
- res.body.data.other_link.name.should.eql("name2");
510
- res.body.data.other_link.val.should.eql("val2");
511
- res.body.data.should.have.property("link_id");
512
- res.body.data.other_link_id.should.be.eql(other_link_id);
513
- done();
514
- });
515
- });
516
- it("should autopopulate on all records", done => {
517
- chai.request(server)
518
- .get(`/api/test?autopopulate=true`)
519
- .auth(init.email, init.password)
520
- .end((err, res) => {
521
- res.should.have.status(200);
522
- res.body.data[0].should.have.property("link")
523
- res.body.data[0].link.should.be.an('object');
524
- res.body.data[0].link.name.should.eql("name1");
525
- res.body.data[0].link.val.should.eql("val1");
526
- res.body.data[0].should.have.property("other_link")
527
- res.body.data[0].other_link.should.be.an('object');
528
- res.body.data[0].other_link.name.should.eql("name2");
529
- res.body.data[0].other_link.val.should.eql("val2");
530
- done();
531
- });
532
- });
533
- it("should autopopulate on a single records", done => {
534
- chai.request(server)
535
- .get(`/api/test/${post_id}?autopopulate=true`)
536
- .auth(init.email, init.password)
537
- .end((err, res) => {
538
- res.should.have.status(200);
539
- res.body.data.should.have.property("link")
540
- res.body.data.link.should.be.an('object');
541
- res.body.data.link.name.should.eql("name1");
542
- res.body.data.link.val.should.eql("val1");
543
- res.body.data.should.have.property("other_link")
544
- res.body.data.other_link.should.be.an('object');
545
- res.body.data.other_link.name.should.eql("name2");
546
- res.body.data.other_link.val.should.eql("val2");
547
- done();
548
- });
549
- });
550
- it("should non-destructively populate link on a single record", done => {
551
- chai.request(server)
552
- .get(`/api/test/${post_id}?populate=link`)
553
- .auth(init.email, init.password)
554
- .end((err, res) => {
555
- res.should.have.status(200);
556
- res.body.should.have.property("data");
557
- res.body.data.should.have.property("link")
558
- res.body.data.link.should.be.an('object');
559
- res.body.data.link.name.should.eql("name1");
560
- res.body.data.link.val.should.eql("val1");
561
- res.body.data.link_id.should.be.eql(link_id);
562
- done();
563
- });
564
- });
565
- it("should populate link_id on all records", done => {
566
- chai.request(server)
567
- .get(`/api/test?populate=link`)
568
- .auth(init.email, init.password)
569
- .end((err, res) => {
570
- res.should.have.status(200);
571
- res.body.data[0].should.have.property("link")
572
- res.body.data[0].link.should.be.an('object');
573
- res.body.data[0].link.name.should.eql("name1");
574
- res.body.data[0].link.val.should.eql("val1");
575
- res.body.data[0].link_id.should.be.eql(link_id);
576
- done();
577
- });
578
- });
579
- it("should populate just val from link_id on a single record", done => {
580
- chai.request(server)
581
- .get(`/api/test/${post_id}?populate[link]=val`)
582
- .auth(init.email, init.password)
583
- .end((err, res) => {
584
- res.should.have.status(200);
585
- res.body.should.have.property("data");
586
- res.body.data.link.should.have.property("val")
587
- res.body.data.link.should.not.have.property("name")
588
- res.body.data.link.should.be.an('object');
589
- res.body.data.link.val.should.eql("val1");
590
- done();
591
- });
592
- });
593
- it("should populate just val from link_id on all records", done => {
594
- chai.request(server)
595
- .get(`/api/test?populate[link]=val`)
596
- .auth(init.email, init.password)
597
- .end((err, res) => {
598
- res.should.have.status(200);
599
- res.body.data[0].link.should.have.property("val")
600
- res.body.data[0].link.should.not.have.property("name")
601
- res.body.data[0].link.val.should.eql("val1");
602
- done();
603
- });
604
- });
605
- it("should populate name and val from link_id on a single record", done => {
606
- chai.request(server)
607
- .get(`/api/test/${post_id}?populate[link]=val,name`)
608
- .auth(init.email, init.password)
609
- .end((err, res) => {
610
- res.should.have.status(200);
611
- res.body.should.have.property("data");
612
- res.body.data.link.should.have.property("val")
613
- res.body.data.link.should.have.property("name")
614
- res.body.data.link.should.be.an('object');
615
- res.body.data.link.val.should.eql("val1");
616
- done();
617
- });
618
- });
619
- it("should populate name and val from link_id on all records", done => {
620
- chai.request(server)
621
- .get(`/api/test?populate[link]=val,name`)
622
- .auth(init.email, init.password)
623
- .end((err, res) => {
624
- res.should.have.status(200);
625
- res.body.data[0].link.should.have.property("val")
626
- res.body.data[0].link.should.have.property("name")
627
- res.body.data[0].link.val.should.eql("val1");
628
- done();
629
- });
630
- });
631
- it("should populate link_id and other_link_id on a single record", done => {
632
- chai.request(server)
633
- .get(`/api/test/${post_id}?populate[]=link&populate[]=other_link`)
634
- .auth(init.email, init.password)
635
- .end((err, res) => {
636
- res.should.have.status(200);
637
- res.body.should.have.property("data");
638
- res.body.data.should.have.property("link")
639
- res.body.data.link.should.be.an('object');
640
- res.body.data.link.name.should.eql("name1");
641
- res.body.data.link.val.should.eql("val1");
642
- res.body.data.should.have.property("other_link");
643
- res.body.data.other_link.should.be.an('object');
644
- res.body.data.other_link.name.should.eql("name2");
645
- res.body.data.other_link.val.should.eql("val2");
646
- done();
647
- });
648
- });
649
- it("should populate link_id and other_link_id on all records", done => {
650
- chai.request(server)
651
- .get(`/api/test?populate[]=link&populate[]=other_link`)
652
- .auth(init.email, init.password)
653
- .end((err, res) => {
654
- res.should.have.status(200);
655
- res.body.data[0].link.should.have.property("val")
656
- res.body.data[0].link.should.have.property("name")
657
- res.body.data[0].link.val.should.eql("val1");
658
- res.body.data[0].other_link.should.have.property("val")
659
- res.body.data[0].other_link.should.have.property("name")
660
- res.body.data[0].other_link.val.should.eql("val2");
661
- done();
662
- });
663
- });
664
- it("should link an array of links to TEST", done => {
665
- chai.request(server)
666
- .put("/api/test/" + post_id)
667
- .auth(init.email, init.password)
668
- .send({ array_link_id: [link_id, other_link_id] })
669
- .end((err, res) => {
670
- res.should.have.status(200);
671
- res.body.data.should.be.an('object');
672
- res.body.data.should.have.property("array_link_id")
673
- res.body.data.array_link_id.should.be.an("array");
674
- res.body.data.array_link_id.should.have.length(2);
675
- done();
676
- });
677
- });
678
- it("should populate an array of links", done => {
679
- chai.request(server)
680
- .get(`/api/test?populate=array_link`)
681
- .auth(init.email, init.password)
682
- .end((err, res) => {
683
- res.should.have.status(200);
684
- res.body.data[0].should.have.property("array_link");
685
- res.body.data[0].array_link.should.be.an("array");
686
- res.body.data[0].array_link.should.have.length(2);
687
- res.body.data[0].array_link[0].should.be.an("object");
688
- res.body.data[0].array_link[0].val.should.eql("val1");
689
- done();
690
- });
691
- });
692
- describe("/POST query", () => {
693
- it("it should POST a complex query", (done) => {
694
- var query = {
695
- "$and": [
696
- {
697
- "foo": {
698
- "$regex": "foo",
699
- "$options": "i"
700
- }
701
- },
702
- {
703
- "bar": "Bar"
704
- }
705
- ]
706
- };
707
- chai.request(server)
708
- .post("/query/test")
709
- .auth(init.email, init.password)
710
- .send({ query })
711
- .end((err, res) => {
712
- res.should.have.status(200);
713
- res.body.data.should.be.an('array');
714
- res.body.data[0].should.have.property("_id");
715
- res.body.data[0].should.have.property("foo");
716
- res.body.data[0].should.have.property("bar");
717
- res.body.data[0].should.have.property("yack").which.should.be.an("object");
718
- res.body.data[0].should.have.property("shmack");
719
- res.body.data[0].shmack.should.be.an("array");
720
- res.body.data[0].foo.should.be.a("string");
721
- res.body.data[0].bar.should.be.a("string");
722
- res.body.data[0].foo.should.eql("Foo1");
723
- res.body.data[0].bar.should.eql("Bar");
724
- objectid = res.body.data[0]._id;
725
- done();
726
- });
727
- });
728
- });
729
- describe("/POST aggregate", () => {
730
- it("it should POST an aggregate query", (done) => {
731
- var query = [
732
- { $group: { _id: null, count: { $sum: 1 } } }
733
- ];
734
- chai.request(server)
735
- .post("/aggregate/test")
736
- .auth(init.email, init.password)
737
- .send({ query })
738
- .end((err, res) => {
739
- res.should.have.status(200);
740
- res.body.data.should.be.an('array');
741
- res.body.data[0].should.have.property("_id");
742
- res.body.data[0].should.have.property("count");
743
- res.body.data[0].count.should.eql(3);
744
- done();
745
- });
746
- });
747
- });
748
- describe("/POST aggregate", () => {
749
- it("it should POST an aggregate query but not embedded in query", (done) => {
750
- var query = [
751
- { $group: { _id: null, count: { $sum: 1 } } }
752
- ];
753
- chai.request(server)
754
- .post("/aggregate/test")
755
- .auth(init.email, init.password)
756
- .send(query)
757
- .end((err, res) => {
758
- res.should.have.status(200);
759
- res.body.data.should.be.an('array');
760
- res.body.data[0].should.have.property("_id");
761
- res.body.data[0].should.have.property("count");
762
- res.body.data[0].count.should.eql(3);
763
- done();
764
- });
765
- });
766
- });
767
- describe("/POST aggregate", () => {
768
- it("it should POST an aggregate query with calculated Date", (done) => {
769
- var query = [
770
- {
771
- $match: {
772
- "createdAt": {
773
- "$gte": "new Date(\"2020-01-01\")"
774
- }
775
- }
776
- },
777
- { $group: { _id: null, count: { $sum: 1 } } }
778
- ];
779
- chai.request(server)
780
- .post("/aggregate/test")
781
- .auth(init.email, init.password)
782
- .send({ query })
783
- .end((err, res) => {
784
- res.should.have.status(200);
785
- res.body.data.should.be.an('array');
786
- res.body.data[0].should.have.property("_id");
787
- res.body.data[0].should.have.property("count");
788
- res.body.data[0].count.should.eql(3);
789
- done();
790
- });
791
- });
792
- });
793
- describe("/POST aggregate", () => {
794
- it("it should POST an aggregate query with calculated relative_date", (done) => {
795
- var query = [
796
- {
797
- $match: {
798
- "createdAt": {
799
- "$gte": "relative_date(-1, \"days\")",
800
- "$lte": "relative_date(null, null, null, \"month\")",
801
- }
802
- }
803
- },
804
- { $group: { _id: null, count: { $sum: 1 } } }
805
- ];
806
- chai.request(server)
807
- .post("/aggregate/test")
808
- .auth(init.email, init.password)
809
- .send({ query })
810
- .end((err, res) => {
811
- res.should.have.status(200);
812
- res.body.data.should.be.an('array');
813
- res.body.data[0].should.have.property("_id");
814
- res.body.data[0].should.have.property("count");
815
- res.body.data[0].count.should.eql(3);
816
- done();
817
- });
818
- });
819
- });
820
- describe("/POST aggregate", () => {
821
- it("it should POST an aggregate query with calculated ObjectId", (done) => {
822
- var query = [
823
- {
824
- $match: {
825
- "_id": `ObjectId("${objectid}")`
826
- }
827
- },
828
- { $group: { _id: null, count: { $sum: 1 } } }
829
- ];
830
- chai.request(server)
831
- .post("/aggregate/test")
832
- .auth(init.email, init.password)
833
- .send({ query })
834
- .end((err, res) => {
835
- res.should.have.status(200);
836
- res.body.data.should.be.an('array');
837
- res.body.data[0].should.have.property("_id");
838
- res.body.data[0].should.have.property("count");
839
- res.body.data[0].count.should.eql(1);
840
- done();
841
- });
842
- });
843
- });
844
- describe("/POST aggregate allowDiskUse", () => {
845
- it("it should POST an aggregate query with allowDiskUse", (done) => {
846
- var query = [
847
- { $group: { _id: null, count: { $sum: 1 } } }
848
- ];
849
- chai.request(server)
850
- .post("/aggregate/test?allowDiskUse=true")
851
- .auth(init.email, init.password)
852
- .send({ query })
853
- .end((err, res) => {
854
- res.should.have.status(200);
855
- res.body.data.should.be.an('array');
856
- res.body.data[0].should.have.property("_id");
857
- res.body.data[0].should.have.property("count");
858
- res.body.data[0].count.should.eql(3);
859
- done();
860
- });
861
- });
862
- });
863
- describe("/POST bulkwrite", () => {
864
- it("it should make sure we are set up right", done => {
865
- chai.request(server)
866
- .get("/api/test")
867
- .auth(init.email, init.password)
868
- .end((err, res) => {
869
- res.should.have.status(200);
870
- res.body.data.should.be.an('array');
871
- res.body.data[0].should.have.property("foo");
872
- res.body.data[0].foo.should.eql("Foo1");
873
- res.body.should.have.property("count");
874
- res.body.count.should.eql(3);
875
- done();
876
- });
877
- })
878
- it("it should bulkwrite", (done) => {
879
- const query = [
880
- {
881
- "insertOne": {
882
- "document": {
883
- "foo": "Foo2",
884
- "bar": "Bar2",
885
- "yack": { "yack": "yack2", "shmack": 2 },
886
- }
887
- },
888
- },
889
- {
890
- "updateOne": {
891
- "filter": {
892
- "foo": "Foo1"
893
- },
894
- "update": {
895
- "$set": {
896
- "foo": "Foo bulk updated"
897
- }
898
- }
899
- }
900
- },
901
- {
902
- "updateOne": {
903
- "filter": {
904
- "foo": "Foo3"
905
- },
906
- "update": {
907
- "foo": "Foo3",
908
- "bar": "Bar3",
909
- "yack": { "yack": "yack3", "shmack": 3 },
910
- },
911
- "upsert": true
912
- },
913
- }
914
- ];
915
- chai.request(server)
916
- .post("/bulkwrite/test")
917
- .auth(init.admin_email, init.admin_password)
918
- .send(query)
919
- .end((err, res) => {
920
- res.should.have.status(200);
921
- res.body.data.should.be.an('object');
922
- res.body.data.should.have.property("ok");
923
- res.body.data.ok.should.eql(1);
924
- res.body.data.nInserted.should.eql(1);
925
- res.body.data.nUpserted.should.eql(1);
926
- res.body.data.nMatched.should.eql(1);
927
- res.body.data.nModified.should.eql(1);
928
- done();
929
- });
930
- });
931
- it("it should test bulkwrite", (done) => {
932
- chai.request(server)
933
- .get("/api/test?sort[createdAt]=1")
934
- .auth(init.email, init.password)
935
- .end((err, res) => {
936
- res.should.have.status(200);
937
- res.body.data.should.be.an('array');
938
- res.body.data[0].should.have.property("foo");
939
- res.body.data[0].foo.should.eql("Foo bulk updated");
940
- res.body.data[1].should.have.property("foo");
941
- res.body.data[2].should.have.property("foo");
942
- res.body.should.have.property("count");
943
- res.body.count.should.eql(5);
944
- done();
945
- });
946
- });
947
- });
948
- // it("should $push to an array", done => {
949
- // chai.request(server)
950
- // .patch("/api/test/" + post_id)
951
- // .auth(init.email, init.password)
952
- // .send({ $push: { shmack: "fah" } })
953
- // .end((err, res) => {
954
- // // console.log(res.body.data);
955
- // res.should.have.status(200);
956
- // res.body.data.should.be.an('object');
957
- // res.body.data.should.have.property("shmack")
958
- // res.body.data.shmack.should.be.an("array");
959
- // res.body.data.shmack.should.have.length(4);
960
- // done();
961
- // });
962
- // });
963
- });
964
-
965
- describe("Models", () => {
966
- it("it should get all the model definitions", (done) => {
967
- chai.request(server)
968
- .get("/model")
969
- // .auth(init.email, init.password)
970
- .end((err, res) => {
971
- res.should.have.status(200);
972
- res.body.should.be.a('array');
973
- done();
974
- });
975
- });
976
- });
977
-
978
- describe("Mixed Array Tests", () => {
979
- it("should save complex objects in mixed_array", (done) => {
980
- const complexData = {
981
- foo: "MixedArrayTest",
982
- bar: "MixedArrayBar",
983
- mixed_array: [
984
- {
985
- nested: {
986
- prop1: "value1",
987
- prop2: 123,
988
- prop3: true
989
- },
990
- array: [1, 2, 3],
991
- date: new Date("2024-03-20T10:00:00Z")
992
- },
993
- ["array", "of", "strings"],
994
- 42,
995
- { simple: "object" },
996
- null,
997
- new Date("2024-03-21"),
998
- [
999
- {
1000
- foo: "bar",
1001
- bar: "foo"
1002
- },
1003
- {
1004
- foo: "baz",
1005
- bar: "baz"
1006
- }
1007
- ]
1008
- ]
1009
- };
1010
-
1011
- chai.request(server)
1012
- .post("/api/test")
1013
- .auth(init.email, init.password)
1014
- .send(complexData)
1015
- .end((err, res) => {
1016
- res.should.have.status(200);
1017
- res.body.data.should.be.an('object');
1018
- res.body.data.should.have.property("mixed_array");
1019
- res.body.data.mixed_array.should.be.an("array");
1020
- res.body.data.mixed_array.should.have.length(7);
1021
-
1022
- // Verify first object with nested properties
1023
- res.body.data.mixed_array[0].should.have.property("nested");
1024
- res.body.data.mixed_array[0].nested.prop1.should.equal("value1");
1025
- res.body.data.mixed_array[0].nested.prop2.should.equal(123);
1026
- res.body.data.mixed_array[0].nested.prop3.should.equal(true);
1027
- res.body.data.mixed_array[0].array.should.deep.equal([1, 2, 3]);
1028
-
1029
- // Verify array of strings
1030
- res.body.data.mixed_array[1].should.deep.equal(["array", "of", "strings"]);
1031
-
1032
- // Verify number
1033
- res.body.data.mixed_array[2].should.equal(42);
1034
-
1035
- // Verify simple object
1036
- res.body.data.mixed_array[3].should.have.property("simple");
1037
- res.body.data.mixed_array[3].simple.should.equal("object");
1038
-
1039
- // Verify null
1040
- should.equal(res.body.data.mixed_array[4], null);
1041
-
1042
- // Verify dates are stored as ISO strings
1043
- res.body.data.mixed_array[0].date.should.be.a("string");
1044
- res.body.data.mixed_array[5].should.be.a("string");
1045
- res.body.data.mixed_array[6].should.be.a("array");
1046
- res.body.data.mixed_array[6][0].should.have.property("foo");
1047
- res.body.data.mixed_array[6][0].foo.should.equal("bar");
1048
- res.body.data.mixed_array[6][1].should.have.property("foo");
1049
- res.body.data.mixed_array[6][1].foo.should.equal("baz");
1050
- done();
1051
- });
1052
- });
1053
-
1054
- it("should retrieve and maintain complex objects in mixed_array", (done) => {
1055
- // First create a test document
1056
- const testData = {
1057
- foo: "MixedArrayRetrieveTest",
1058
- bar: "RetrieveBar",
1059
- mixed_array: [
1060
- {
1061
- deep: { nested: { object: true } },
1062
- array: [[1, 2], [3, 4]],
1063
- },
1064
- Buffer.from("Hello World").toString("base64"),
1065
- new RegExp("test").toString()
1066
- ]
1067
- };
1068
-
1069
- let testId;
1070
-
1071
- chai.request(server)
1072
- .post("/api/test")
1073
- .auth(init.email, init.password)
1074
- .send(testData)
1075
- .end((err, res) => {
1076
- res.should.have.status(200);
1077
- testId = res.body.data._id;
1078
-
1079
- // Now retrieve and verify
1080
- chai.request(server)
1081
- .get(`/api/test/${testId}`)
1082
- .auth(init.email, init.password)
1083
- .end((err, res) => {
1084
- res.should.have.status(200);
1085
- res.body.data.mixed_array[0].deep.nested.object.should.equal(true);
1086
- res.body.data.mixed_array[0].array.should.deep.equal([[1, 2], [3, 4]]);
1087
- res.body.data.mixed_array[1].should.be.a("string");
1088
- res.body.data.mixed_array[2].should.equal("/test/");
1089
- done();
1090
- });
1091
- });
1092
- });
1093
- });
1094
-
1095
- describe("Delete", () => {
1096
- it("should soft-delete an item", (done) => {
1097
- chai.request(server)
1098
- .del(`/api/test/${post_id}`)
1099
- .auth(init.email, init.password)
1100
- .end((err, res) => {
1101
- res.should.have.status(200);
1102
- res.body.status.should.equal('ok');
1103
- done();
1104
- });
1105
- });
1106
- it("should show item as deleted", (done) => {
1107
- chai.request(server)
1108
- .get(`/api/test/${post_id}`)
1109
- .auth(init.email, init.password)
1110
- .end((err, res) => {
1111
- res.should.have.status(404);
1112
- res.body.message.should.equal(`Document ${post_id} is deleted on Test`);
1113
- res.body.code.should.equal('NotFound');
1114
- done();
1115
- });
1116
- });
1117
- it("should show item", (done) => {
1118
- chai.request(server)
1119
- .get(`/api/test/${post_id}?showDeleted=1`)
1120
- .auth(init.email, init.password)
1121
- .end((err, res) => {
1122
- res.should.have.status(200);
1123
- res.body.data._deleted.should.equal(true);
1124
- res.body.data.bar.should.equal("Bar");
1125
- done();
1126
- });
1127
- });
1128
- it("should undeleted item", (done) => {
1129
- chai.request(server)
1130
- .put(`/api/test/${post_id}`)
1131
- .send({
1132
- _deleted: false
1133
- })
1134
- .auth(init.email, init.password)
1135
- .end((err, res) => {
1136
- res.should.have.status(200);
1137
- res.body.status.should.equal('ok');
1138
- done();
1139
- });
1140
- });
1141
- it("should show item", (done) => {
1142
- chai.request(server)
1143
- .get(`/api/test/${post_id}`)
1144
- .auth(init.email, init.password)
1145
- .end((err, res) => {
1146
- res.should.have.status(200);
1147
- res.body.data._deleted.should.equal(false);
1148
- res.body.data.bar.should.equal("Bar");
1149
- done();
1150
- });
1151
- });
1152
- it("should permanently delete item", (done) => {
1153
- chai.request(server)
1154
- .del(`/api/test/${post_id}?_permaDelete=1`)
1155
- .auth(init.email, init.password)
1156
- .end((err, res) => {
1157
- res.should.have.status(200);
1158
- res.body.status.should.equal('ok');
1159
- done();
1160
- });
1161
- });
1162
- it("should fail to find item", (done) => {
1163
- chai.request(server)
1164
- .get(`/api/test/${post_id}?showDeleted=1`)
1165
- .auth(init.email, init.password)
1166
- .end((err, res) => {
1167
- res.should.have.status(404);
1168
- done();
1169
- });
1170
- });
1171
- let link_id = null;
1172
- let test_with_links_id = null;
1173
- it("should add a LINK item", done => {
1174
- let data = {
1175
- name: "deltest_name",
1176
- val: "deltest_val",
1177
- bar: "Gloop"
1178
- }
1179
- chai.request(server)
1180
- .post("/api/link")
1181
- .auth(init.email, init.password)
1182
- .send(data)
1183
- .end((err, res) => {
1184
- res.should.have.status(200);
1185
- res.body.data.should.be.an('object');
1186
- res.body.data.should.have.property("_id");
1187
- res.body.data.should.have.property("name")
1188
- res.body.data.name.should.eql("deltest_name");
1189
- link_id = res.body.data._id;
1190
- done();
1191
- });
1192
- });
1193
- it("should link a LINK item to a TEST", done => {
1194
- chai.request(server)
1195
- .post("/api/test")
1196
- .auth(init.email, init.password)
1197
- .send({
1198
- link_id
1199
- })
1200
- .end((err, res) => {
1201
- res.should.have.status(200);
1202
- res.body.data.should.be.an('object');
1203
- res.body.data.should.have.property("link_id")
1204
- res.body.data.link_id.should.eql(link_id);
1205
- test_with_links_id = res.body.data._id;
1206
- done();
1207
- });
1208
- });
1209
- it("should fail because a parent item exists", (done) => {
1210
- chai.request(server)
1211
- .del(`/api/link/${link_id}`)
1212
- .auth(init.email, init.password)
1213
- .end((err, res) => {
1214
- res.should.have.status(409);
1215
- res.body.message.should.equal(`Parent link item exists in test/link_id`);
1216
- done();
1217
- });
1218
- });
1219
- it("should cascade delete", (done) => {
1220
- chai.request(server)
1221
- .del(`/api/link/${link_id}?_cascade=1`)
1222
- .auth(init.email, init.password)
1223
- .end((err, res) => {
1224
- res.should.have.status(200);
1225
- res.body.status.should.equal('ok');
1226
- done();
1227
- });
1228
- });
1229
- it("link item should no longer exist", (done) => {
1230
- chai.request(server)
1231
- .get(`/api/test/${test_with_links_id}`)
1232
- .auth(init.email, init.password)
1233
- .end((err, res) => {
1234
- res.should.have.status(404);
1235
- res.body.message.should.equal(`Document ${test_with_links_id} is deleted on Test`);
1236
- done();
1237
- });
1238
- });
1239
- it("link item should be soft-deleted", (done) => {
1240
- chai.request(server)
1241
- .get(`/api/test/${test_with_links_id}?showDeleted=1`)
1242
- .auth(init.email, init.password)
1243
- .end((err, res) => {
1244
- res.should.have.status(200);
1245
- done();
1246
- });
1247
- });
1248
- it("should add a LINK item", done => {
1249
- let data = {
1250
- name: "permdeltest_name",
1251
- val: "permdeltest_val",
1252
- bar: "Yoop"
1253
- }
1254
- chai.request(server)
1255
- .post("/api/link")
1256
- .auth(init.email, init.password)
1257
- .send(data)
1258
- .end((err, res) => {
1259
- res.should.have.status(200);
1260
- res.body.data.should.be.an('object');
1261
- res.body.data.should.have.property("_id");
1262
- res.body.data.should.have.property("name")
1263
- res.body.data.name.should.eql("permdeltest_name");
1264
- link_id = res.body.data._id;
1265
- done();
1266
- });
1267
- });
1268
- it("should link a LINK item to a TEST", done => {
1269
- chai.request(server)
1270
- .post("/api/test")
1271
- .auth(init.email, init.password)
1272
- .send({
1273
- link_id,
1274
- bar: "link1"
1275
- })
1276
- .end((err, res) => {
1277
- res.should.have.status(200);
1278
- res.body.data.should.be.an('object');
1279
- res.body.data.should.have.property("link_id")
1280
- res.body.data.link_id.should.eql(link_id);
1281
- test_with_links_id = res.body.data._id;
1282
- done();
1283
- });
1284
- });
1285
- it("should fail because a parent item exists", (done) => {
1286
- chai.request(server)
1287
- .del(`/api/link/${link_id}`)
1288
- .auth(init.email, init.password)
1289
- .end((err, res) => {
1290
- res.should.have.status(409);
1291
- res.body.message.should.equal(`Parent link item exists in test/link_id`);
1292
- done();
1293
- });
1294
- });
1295
- it("should cascade delete", (done) => {
1296
- chai.request(server)
1297
- .del(`/api/link/${link_id}?_cascade=1&_permaDelete=1`)
1298
- .auth(init.email, init.password)
1299
- .end((err, res) => {
1300
- res.should.have.status(200);
1301
- res.body.status.should.equal('ok');
1302
- done();
1303
- });
1304
- });
1305
- it("link item should be permanently deleted", (done) => {
1306
- chai.request(server)
1307
- .get(`/api/test/${test_with_links_id}?showDeleted=1`)
1308
- .auth(init.email, init.password)
1309
- .end((err, res) => {
1310
- res.should.have.status(404);
1311
- done();
1312
- });
1313
- });
1314
- });
1315
-
1316
- describe("/Filter with +", () => {
1317
- it("it should POST a user with a + in email", (done) => {
1318
- var user = {
1319
- name: "Plus User",
1320
- email: "plus+user@gmail.com"
1321
- };
1322
- chai.request(server)
1323
- .post("/api/user")
1324
- .auth(init.admin_email, init.admin_password)
1325
- .send(user)
1326
- .end((err, res) => {
1327
- res.should.have.status(200);
1328
- res.body.data.should.have.property("_id");
1329
- done();
1330
- });
1331
- });
1332
- it("it should GET a user with a + in email", (done) => {
1333
- chai.request(server)
1334
- .get("/api/user?filter[email]=plus%2Buser@gmail.com")
1335
- .auth(init.admin_email, init.admin_password)
1336
- .end((err, res) => {
1337
- res.should.have.status(200);
1338
- res.body.data[0].should.have.property("_id");
1339
- done();
1340
- });
1341
- });
1342
- });
1343
-
1344
- describe("Error Handling", () => {
1345
- it("should get an error", (done) => {
1346
- chai.request(server)
1347
- .post("/api/test")
1348
- .auth(init.email, init.password)
1349
- .send({
1350
- error: true,
1351
- bar: "Throw an error"
1352
- })
1353
- .end((err, res) => {
1354
- res.should.have.status(418);
1355
- res.body.message.should.equal(`I'm a teapot`);
1356
- done();
1357
- });
1358
- })
1359
- });
1360
-
1361
- describe("Caching", () => {
1362
- it("should give us cache stats", (done) => {
1363
- chai.request(server)
1364
- .get("/cache/stats")
1365
- .end((err, res) => {
1366
- // console.log(res.body);
1367
- res.should.have.status(200);
1368
- res.body.should.have.property("hits");
1369
- done();
1370
- });
1371
- })
1372
- it("should clear the cache stats", (done) => {
1373
- chai.request(server)
1374
- .get("/cache/clear")
1375
- .end((err, res) => {
1376
- // console.log(res.body);
1377
- res.should.have.status(200);
1378
- done();
1379
- });
1380
- });
1381
- it("should get an uncached request", (done) => {
1382
- chai.request(server)
1383
- .get("/api/test")
1384
- .auth(init.email, init.password)
1385
- .end((err, res) => {
1386
- res.should.have.status(200);
1387
- res.headers.should.have.property("jxp-cache");
1388
- res.headers["jxp-cache"].should.equal("miss");
1389
- done();
1390
- });
1391
- });
1392
- it("should get an cached request", (done) => {
1393
- chai.request(server)
1394
- .get("/api/test")
1395
- .auth(init.email, init.password)
1396
- .end((err, res) => {
1397
- res.should.have.status(200);
1398
- res.headers.should.have.property("jxp-cache");
1399
- res.headers["jxp-cache"].should.equal("hit");
1400
- done();
1401
- });
1402
- });
1403
- it("should give us cache stats", (done) => {
1404
- chai.request(server)
1405
- .get("/cache/stats")
1406
- .end((err, res) => {
1407
- res.should.have.status(200);
1408
- res.body.should.have.property("hits");
1409
- res.body.hits.should.be.greaterThan(0);
1410
- res.body.misses.should.be.greaterThan(0);
1411
- done();
1412
- });
1413
- })
1414
- });
1415
- describe("Cache invalidating", () => {
1416
- let test_id;
1417
- it("should get a test record", done => {
1418
- chai.request(server)
1419
- .get(`/api/test?populate=link&limit=1`)
1420
- .auth(init.email, init.password)
1421
- .end((err, res) => {
1422
- // console.log(res.headers)
1423
- res.should.have.status(200);
1424
- res.body.data[0].should.have.property("_id");
1425
- test_id = res.body.data[0]._id;
1426
- res.body.data[0].should.have.property("link");
1427
- should.equal(res.body.data[0].link, null);
1428
- res.headers["jxp-cache"].should.equal("miss");
1429
- done();
1430
- });
1431
- });
1432
- it("should get a cached test record", done => {
1433
- chai.request(server)
1434
- .get(`/api/test?populate=link&limit=1`)
1435
- .auth(init.email, init.password)
1436
- .end((err, res) => {
1437
- // console.log(res.headers)
1438
- res.should.have.status(200);
1439
- res.body.data[0].should.have.property("_id");
1440
- res.headers["jxp-cache"].should.equal("hit");
1441
- should.equal(res.body.data[0].link, null);
1442
- done();
1443
- });
1444
- });
1445
- let link_id;
1446
- it("should add a link record", done => {
1447
- chai.request(server)
1448
- .post(`/api/link`)
1449
- .auth(init.email, init.password)
1450
- .send({
1451
- name: "cache_test",
1452
- val: "YoYoYo"
1453
- })
1454
- .end((err, res) => {
1455
- // console.log(res.body)
1456
- res.should.have.status(200);
1457
- res.body.data.should.have.property("_id");
1458
- link_id = res.body.data._id;
1459
- done();
1460
- });
1461
- });
1462
- it("should put link_id into test record", done => {
1463
- chai.request(server)
1464
- .put(`/api/test/${test_id}`)
1465
- .auth(init.admin_email, init.admin_password)
1466
- .send({
1467
- link_id
1468
- })
1469
- .end((err, res) => {
1470
- // console.log(res.body)
1471
- res.should.have.status(200);
1472
- res.body.data.should.have.property("_id");
1473
- res.body.data.link_id.should.equal(link_id);
1474
- done();
1475
- });
1476
- });
1477
- it("should get a test record with link", done => {
1478
- chai.request(server)
1479
- .get(`/api/test?populate=link&limit=1`)
1480
- .auth(init.email, init.password)
1481
- .end((err, res) => {
1482
- // console.log(res.body)
1483
- res.should.have.status(200);
1484
- res.body.data[0].should.have.property("_id");
1485
- res.body.data[0].should.have.property("link");
1486
- res.body.data[0].link.should.not.equal(null);
1487
- res.body.data[0].link._id.should.equal(link_id);
1488
- res.body.data[0].link.name.should.equal("cache_test");
1489
- res.body.data[0].link.val.should.equal("YoYoYo");
1490
- res.headers["jxp-cache"].should.equal("miss");
1491
- done();
1492
- });
1493
- });
1494
- it("should update link record", done => {
1495
- chai.request(server)
1496
- .put(`/api/link/${link_id}`)
1497
- .auth(init.admin_email, init.admin_password)
1498
- .send({
1499
- val: "YoYoYo2"
1500
- })
1501
- .end((err, res) => {
1502
- // console.log(res.body)
1503
- res.should.have.status(200);
1504
- res.body.data.should.have.property("_id");
1505
- res.body.data.val.should.equal("YoYoYo2");
1506
- done();
1507
- });
1508
- });
1509
- it("should get a test record with updated link", done => {
1510
- chai.request(server)
1511
- .get(`/api/test?populate=link&limit=1`)
1512
- .auth(init.email, init.password)
1513
- .end((err, res) => {
1514
- // console.log(res.body.data[0].link)
1515
- res.should.have.status(200);
1516
- res.body.data[0].should.have.property("_id");
1517
- res.body.data[0].should.have.property("link");
1518
- res.body.data[0].link.should.not.equal(null);
1519
- res.body.data[0].link._id.should.equal(link_id);
1520
- res.body.data[0].link.name.should.equal("cache_test");
1521
- res.body.data[0].link.val.should.equal("YoYoYo2");
1522
- res.headers["jxp-cache"].should.equal("miss");
1523
- done();
1524
- });
1525
- });
1526
- })
1527
- });