users.spec.js 5.4 KB

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