crud.spec.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* eslint-disable no-unused-expressions */
  2. const { expect } = require('chai');
  3. const mongoMocker = require('mongo-mocker');
  4. const joi = require('joi');
  5. const {
  6. makeMongoCrud
  7. } = require('server/modules/crud');
  8. describe('API - Crud module', () => {
  9. let db = null;
  10. let crud = null;
  11. const schema = {
  12. name: joi.string(),
  13. email: joi.string().email()
  14. };
  15. const testUsers = [
  16. { name: 'John Doe', email: 'john.doe@mubaloo.com' },
  17. { name: 'Jill Doe', email: 'jill.doe@mubaloo.com' }
  18. ];
  19. const collection = 'users';
  20. before(() => {
  21. db = mongoMocker('mongoDriver/path', {
  22. [collection]: testUsers
  23. });
  24. crud = makeMongoCrud(collection, schema);
  25. });
  26. beforeEach(async () => {
  27. const users = await db.collection(collection);
  28. await Promise.all(testUsers.map(user => users.insert(user)));
  29. });
  30. afterEach(() => {
  31. db.mock.clearAll();
  32. });
  33. describe('insertDoc', () => {
  34. it('should be defined', () => {
  35. expect(crud).to.have.property('insertDoc');
  36. expect(crud.insertDoc).to.be.a('function');
  37. });
  38. it('should insert a document into the collection', async () => {
  39. const users = await db.collection(collection);
  40. const testUser = {
  41. name: 'Jack Doe',
  42. email: 'jack.doe@mubaloo.com'
  43. };
  44. const rowsBefore = await users.find(testUser)
  45. .toArray();
  46. expect(rowsBefore).to.be.an('array').of.length(0);
  47. const newUserResult = await crud.insertDoc(db, testUser);
  48. const rowsAfter = await users.find(testUser)
  49. .toArray();
  50. expect(rowsAfter).to.be.an('array').of.length(1);
  51. expect(rowsAfter[0]).to.have.property('name', 'Jack Doe');
  52. expect(rowsAfter[0]).to.have.property('email', 'jack.doe@mubaloo.com');
  53. expect(rowsAfter[0]).to.have.property('_id');
  54. expect(rowsAfter[0]._id).to.be.an('object');
  55. expect(newUserResult).to.be.an('object');
  56. expect(newUserResult).to.have.property('name', 'Jack Doe');
  57. expect(newUserResult).to.have.property('email', 'jack.doe@mubaloo.com');
  58. });
  59. const badUsers = [
  60. {},
  61. { name: 'Some name' },
  62. { email: 'some.email@mubaloo.com' },
  63. { name: 'Some name', email: 'notanemail' }
  64. ];
  65. badUsers.forEach((user, index) => {
  66. it(`should throw an error if information is missing or invalid (${index + 1}/${badUsers.length})`, async () => {
  67. let errorThrown = false;
  68. try {
  69. await crud.insertDoc(db, user);
  70. } catch (err) {
  71. expect(err).to.have.property('statusCode', 400);
  72. errorThrown = true;
  73. }
  74. if (!errorThrown) {
  75. throw new Error('Did not throw an error');
  76. }
  77. });
  78. });
  79. });
  80. describe('modifyDoc', () => {
  81. it('should be defined', () => {
  82. expect(crud).to.have.property('modifyDoc');
  83. expect(crud.modifyDoc).to.be.a('function');
  84. });
  85. it('should modify a specific document', async () => {
  86. const users = await db.collection(collection);
  87. const user = await users.findOne({ name: 'John Doe' });
  88. const id = String(user._id);
  89. await crud.modifyDoc(db, id, {
  90. name: 'Jack Doe'
  91. });
  92. const modifiedUser = await users.findOne({ _id: user._id });
  93. expect(modifiedUser).to.have.property('name', 'Jack Doe');
  94. expect(modifiedUser).to.have.property('email', 'john.doe@mubaloo.com');
  95. });
  96. const badUsers = [
  97. { email: 'notanemail' }
  98. ];
  99. badUsers.forEach((user, index) => {
  100. it(`should throw an error if information is invalid (${index + 1}/${badUsers.length})`, async () => {
  101. const users = await db.collection(collection);
  102. const userResult = await users.findOne({ name: 'John Doe' });
  103. const id = String(userResult._id);
  104. let errorThrown = false;
  105. try {
  106. await crud.modifyDoc(db, id, user);
  107. } catch (err) {
  108. errorThrown = true;
  109. expect(err).to.have.property('statusCode', 400);
  110. }
  111. if (!errorThrown) {
  112. throw new Error('Did not throw an error');
  113. }
  114. });
  115. });
  116. });
  117. describe('deleteDoc', () => {
  118. it('should be defined', () => {
  119. expect(crud).to.have.property('deleteDoc');
  120. expect(crud.deleteDoc).to.be.a('function');
  121. });
  122. it('should delete a document', async () => {
  123. const users = await db.collection(collection);
  124. const existingUser = await users.findOne({ name: 'John Doe' });
  125. expect(existingUser).to.be.an('object');
  126. const id = String(existingUser._id);
  127. await crud.deleteDoc(db, id);
  128. const newQueryResult = await users.findOne({ name: 'John Doe' });
  129. expect(newQueryResult).to.be.undefined;
  130. });
  131. });
  132. describe('getDocs', () => {
  133. it('should be defined', () => {
  134. expect(crud).to.have.property('getDocs');
  135. expect(crud.getDocs).to.be.a('function');
  136. });
  137. it('should get a complete list of documents', async () => {
  138. const result = await crud.getDocs(db);
  139. expect(result).to.be.an('array').of.length(2);
  140. expect(result[0]).to.deep.include({
  141. name: 'John Doe',
  142. email: 'john.doe@mubaloo.com'
  143. });
  144. expect(result[1]).to.deep.include({
  145. name: 'Jill Doe',
  146. email: 'jill.doe@mubaloo.com'
  147. });
  148. expect(result[0]).to.have.property('id');
  149. expect(result[1]).to.have.property('id');
  150. expect(result[0].id).to.be.a('string');
  151. expect(result[1].id).to.be.a('string');
  152. });
  153. });
  154. });