storeBackend.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import { jest } from '@jest/globals';
  2. import {
  3. MemoryBackend,
  4. NeDBBackend,
  5. MongoDBBackend,
  6. wrapBackend,
  7. } from '../store/backends';
  8. import { MONGODB_URI } from '../settings';
  9. const MONGODB_DATABASE_TEST = process.env.MONGODB_DATABASE_TEST;
  10. jest.mock('nanoid', () => {
  11. let count = 0;
  12. return {
  13. customAlphabet: () =>
  14. jest.fn(() => {
  15. return 'nanoid_' + count++;
  16. }),
  17. };
  18. });
  19. let delta = 0;
  20. // Fake date
  21. jest.spyOn(global.Date, 'now').mockImplementation(() => {
  22. delta += 1;
  23. return new Date(1584183719135 + delta * 1000).valueOf();
  24. });
  25. const backends = [
  26. ['memory', MemoryBackend()],
  27. //['NeDb', NeDBBackend({ filename: null, inMemoryOnly: true })],
  28. ];
  29. if (MONGODB_DATABASE_TEST) {
  30. /*backends.push([
  31. 'MongoDB',
  32. MongoDBBackend({ uri: MONGODB_URI, database: MONGODB_DATABASE_TEST }),
  33. ]);*/
  34. }
  35. describe.each(backends)(
  36. 'Store backend <%s> tests',
  37. (backendName, rawBackend) => {
  38. let backend;
  39. beforeAll(async () => {
  40. backend = wrapBackend(rawBackend, 'siteid', 'userid');
  41. if (backendName === 'MongoDB') {
  42. const { MongoClient, ServerApiVersion } = await import('mongodb');
  43. const _client = new MongoClient(MONGODB_URI, {
  44. serverApi: ServerApiVersion.v1,
  45. });
  46. await _client.connect();
  47. await _client.db(MONGODB_DATABASE_TEST).dropDatabase();
  48. await _client.close();
  49. }
  50. });
  51. afterAll(async () => {
  52. if (backendName === 'MongoDB') {
  53. rawBackend._close();
  54. }
  55. });
  56. it('should get empty box', async () => {
  57. const box = 'boxid1';
  58. await expect(backend.list(box, {})).rejects.toThrow();
  59. // Create box
  60. await backend.createOrUpdateBox(box, { option: 1 });
  61. const res = await backend.list(box, {});
  62. expect(res).toEqual([]);
  63. });
  64. it('should add resource', async () => {
  65. const box = 'boxid2';
  66. // Create box
  67. await backend.createOrUpdateBox(box, { option: 1 });
  68. const res = await backend.save(box, null, { value: 42, test: true });
  69. expect(res).toEqual(expect.objectContaining({ test: true, value: 42 }));
  70. // Is return value ok
  71. expect(res._id).toBeDefined();
  72. expect(res._createdOn).toBeGreaterThanOrEqual(1584183661135);
  73. // Is get working
  74. const res2 = await backend.get(box, res._id);
  75. expect(res2).toEqual(res);
  76. // Is list updated
  77. const res3 = await backend.list(box);
  78. expect(res3[0]).toEqual(res);
  79. });
  80. it('should add resource with specified id', async () => {
  81. const box = 'boxId10';
  82. // Create box
  83. await backend.createOrUpdateBox(box, { option: 1 });
  84. const res = await backend.save(box, 'myid', {
  85. value: 42,
  86. test: true,
  87. _createdOn: 1,
  88. });
  89. expect(res).toEqual(expect.objectContaining({ test: true, value: 42 }));
  90. // Is return value ok
  91. expect(res._id).toBe('myid');
  92. expect(res._createdOn).toBeGreaterThanOrEqual(1584183661135);
  93. // Is get working
  94. const res2 = await backend.get(box, 'myid');
  95. expect(res2).toEqual(res);
  96. });
  97. it('should save resource with specified id', async () => {
  98. const box = 'boxId15';
  99. // Create box
  100. await backend.createOrUpdateBox(box, { option: 1 });
  101. const res = await backend.save(box, 'myid', { value: 42, test: true });
  102. expect(res).toEqual(expect.objectContaining({ test: true, value: 42 }));
  103. // Is return value ok
  104. expect(res._id).toBe('myid');
  105. expect(res._createdOn).toBeGreaterThanOrEqual(1584183661135);
  106. const resAfterSave = await backend.save(box, 'myid', {
  107. value: 45,
  108. foo: 18,
  109. });
  110. expect(resAfterSave).toEqual(
  111. expect.objectContaining({ foo: 18, value: 45 })
  112. );
  113. expect(resAfterSave).not.toEqual(expect.objectContaining({ test: true }));
  114. expect(resAfterSave._createdOn).toEqual(res._createdOn);
  115. expect(resAfterSave._updatedOn).toBeGreaterThanOrEqual(1584183661135);
  116. });
  117. it('should add tree resources', async () => {
  118. const box = 'boxid20';
  119. // Create box
  120. await backend.createOrUpdateBox(box, { option: 1 });
  121. const first = await backend.save(box, null, {
  122. value: 40,
  123. test: false,
  124. });
  125. expect(first).toEqual(
  126. expect.objectContaining({ test: false, value: 40 })
  127. );
  128. const second = await backend.save(box, null, {
  129. value: 42,
  130. test: true,
  131. });
  132. expect(second).toEqual(
  133. expect.objectContaining({ test: true, value: 42 })
  134. );
  135. const third = await backend.save(box, null, { value: 44 });
  136. expect(third).toEqual(expect.objectContaining({ value: 44 }));
  137. // Is get working
  138. const firstGet = await backend.get(box, first._id);
  139. expect(firstGet).toEqual(first);
  140. const secondGet = await backend.get(box, second._id);
  141. expect(secondGet).toEqual(second);
  142. // Is list updated
  143. const allResources = await backend.list(box, { sort: '_createdOn' });
  144. expect(allResources[0]).toEqual(first);
  145. expect(allResources[1]).toEqual(second);
  146. expect(allResources[2]).toEqual(third);
  147. expect(allResources.length).toBe(3);
  148. });
  149. it('should update resource', async () => {
  150. const box = 'boxid3';
  151. // Create box
  152. await backend.createOrUpdateBox(box, { option: 1 });
  153. const res = await backend.save(box, null, { value: 40, test: true });
  154. expect(res.value).toBe(40);
  155. const modified = await backend.update(box, res._id, { value: 42 });
  156. const afterModification = await backend.get(box, res._id);
  157. expect(afterModification).toEqual(modified);
  158. expect(afterModification.value).toBe(42);
  159. expect(afterModification._createdOn).toEqual(res._createdOn);
  160. expect(afterModification._updatedOn).toBeGreaterThanOrEqual(
  161. res._createdOn
  162. );
  163. });
  164. it('should delete resource', async () => {
  165. const box = 'boxid4';
  166. // Create box
  167. await backend.createOrUpdateBox(box, { option: 1 });
  168. const predel = await backend.delete(box, 'noid');
  169. expect(predel).toBe(0);
  170. const res = await backend.save(box, null, { value: 42, test: true });
  171. const allResources = await backend.list(box);
  172. expect(allResources.length).toBe(1);
  173. const del = await backend.delete(box, res._id);
  174. expect(del).toBe(1);
  175. const allResourcesAfterDelete = await backend.list(box);
  176. expect(allResourcesAfterDelete.length).toBe(0);
  177. const nodel = await backend.delete(box, res._id);
  178. expect(nodel).toBe(0);
  179. });
  180. it('should list resources', async () => {
  181. const box = 'boxId50';
  182. // Create box
  183. await backend.createOrUpdateBox(box, { option: 1 });
  184. const first = await backend.save(box, null, { value: 40, test: false });
  185. const second = await backend.save(box, null, { value: 44, test: true });
  186. const third = await backend.save(box, null, { value: 42 });
  187. const last = await backend.save(box, null, { value: 42 });
  188. // Is sort working
  189. const allResources = await backend.list(box, {
  190. sort: '_createdOn',
  191. });
  192. expect(allResources[0]).toEqual(first);
  193. expect(allResources[2]).toEqual(third);
  194. // Is sort working
  195. const allResourcesReverse = await backend.list(box, {
  196. sort: '_createdOn',
  197. asc: false,
  198. });
  199. expect(allResourcesReverse[3]).toEqual(first);
  200. expect(allResourcesReverse[0]).toEqual(last);
  201. const allResourcesReverse2 = await backend.list(box, {
  202. sort: 'value',
  203. asc: false,
  204. });
  205. expect(allResourcesReverse2[3]).toEqual(first);
  206. expect(allResourcesReverse2[0]).toEqual(second);
  207. // Is limit working
  208. const limitedResources = await backend.list(box, {
  209. sort: '_createdOn',
  210. limit: 1,
  211. });
  212. expect(limitedResources[0]).toEqual(first);
  213. expect(limitedResources.length).toBe(1);
  214. // Is skip working
  215. const skippedResources = await backend.list(box, {
  216. sort: '_createdOn',
  217. limit: 1,
  218. skip: 1,
  219. });
  220. expect(skippedResources[0]).toEqual(second);
  221. expect(skippedResources.length).toBe(1);
  222. // Is onlyFields working
  223. const filteredResources = await backend.list(box, {
  224. sort: '_createdOn',
  225. onlyFields: ['value'],
  226. });
  227. expect(filteredResources[0]).not.toEqual(
  228. expect.objectContaining({ test: false })
  229. );
  230. // Test queries
  231. console.log('si');
  232. const foundResources = await backend.list(box, {
  233. q: 'value > 42',
  234. });
  235. expect(foundResources.length).toBe(1);
  236. const foundResources2 = await backend.list(box, {
  237. q: 'value > 42 and test = false',
  238. });
  239. expect(foundResources2.length).toBe(0);
  240. await expect(
  241. backend.list(box, {
  242. q: 'value > 42 and test = false bla bl',
  243. })
  244. ).rejects.toThrow();
  245. });
  246. it('should check security', async () => {
  247. const box = 'boxId51';
  248. // Create box
  249. await backend.createOrUpdateBox(box, { option: 1 });
  250. const first = await backend.save(box, null, { value: 40, test: false });
  251. const second = await backend.save(box, null, { value: 44, test: true });
  252. const third = await backend.save(box, null, { value: 42 });
  253. const result = await backend.checkSecurity(box, first._id, 'nokey');
  254. // FIXME
  255. });
  256. it('should throw error', async () => {
  257. const box = 'boxId60';
  258. await expect(backend.get(box, 'noid')).rejects.toThrow();
  259. await expect(
  260. backend.update(box, 'noid', { value: 'titi' })
  261. ).rejects.toThrow();
  262. // Create box
  263. await backend.createOrUpdateBox(box, { option: 1 });
  264. await expect(backend.get(box, 'noid')).rejects.toThrow();
  265. await expect(
  266. backend.update(box, 'noid', { value: 'titi' })
  267. ).rejects.toThrow();
  268. });
  269. it('should check security', async () => {
  270. const box = 'boxId70';
  271. // Create box
  272. await backend.createOrUpdateBox(box, { security: 'private' });
  273. await expect(backend.checkSecurity(box, null)).resolves.toBe(false);
  274. await expect(backend.checkSecurity(box, null, true)).resolves.toBe(false);
  275. // Update box to be readOnly
  276. await backend.createOrUpdateBox(box, { security: 'readOnly' });
  277. await expect(backend.checkSecurity(box, null)).resolves.toBe(true);
  278. await expect(backend.checkSecurity(box, null, true)).resolves.toBe(false);
  279. // Update box to be public
  280. await backend.createOrUpdateBox(box, { security: 'public' });
  281. await expect(backend.checkSecurity(box, null)).resolves.toBe(true);
  282. await expect(backend.checkSecurity(box, null, true)).resolves.toBe(true);
  283. // update box to be default privacy
  284. await backend.createOrUpdateBox(box);
  285. await expect(backend.checkSecurity(box, null)).resolves.toBe(false);
  286. await expect(backend.checkSecurity(box, null, true)).resolves.toBe(false);
  287. // update box to be bad value privacy
  288. await backend.createOrUpdateBox(box, { security: 'nosecurity' });
  289. await expect(backend.checkSecurity(box, null)).resolves.toBe(false);
  290. await expect(backend.checkSecurity(box, null, true)).resolves.toBe(false);
  291. });
  292. }
  293. );