users.spec.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. errorThrown = true;
  64. }
  65. if (!errorThrown) {
  66. throw new Error('Did not throw an error');
  67. }
  68. });
  69. });
  70. });
  71. describe('modifyUser', () => {
  72. it('should modify a specific user', async () => {
  73. const users = await db.collection(COLLECTION_USERS);
  74. const user = await users.findOne({ name: 'John Doe' });
  75. const id = String(user._id);
  76. await modifyUser(db, id, {
  77. name: 'Jack Doe'
  78. });
  79. const modifiedUser = await users.findOne({ _id: user._id });
  80. expect(modifiedUser).to.have.property('name', 'Jack Doe');
  81. expect(modifiedUser).to.have.property('email', 'john.doe@mubaloo.com');
  82. });
  83. const badUsers = [
  84. { email: 'notanemail' }
  85. ];
  86. badUsers.forEach((user, index) => {
  87. it(`should throw an error if information is invalid (${index + 1}/${badUsers.length})`, async () => {
  88. const users = await db.collection(COLLECTION_USERS);
  89. const userResult = await users.findOne({ name: 'John Doe' });
  90. const id = String(userResult._id);
  91. let errorThrown = false;
  92. try {
  93. await modifyUser(db, id, user);
  94. } catch (err) {
  95. errorThrown = true;
  96. }
  97. if (!errorThrown) {
  98. throw new Error('Did not throw an error');
  99. }
  100. });
  101. });
  102. });
  103. describe('deleteUser', () => {
  104. it('should delete a user', async () => {
  105. const users = await db.collection(COLLECTION_USERS);
  106. const existingUser = await users.findOne({ name: 'John Doe' });
  107. expect(existingUser).to.be.an('object');
  108. const id = String(existingUser._id);
  109. await deleteUser(db, id);
  110. const newQueryResult = await users.findOne({ name: 'John Doe' });
  111. expect(newQueryResult).to.be.undefined;
  112. });
  113. });
  114. describe('getUsers', () => {
  115. it('should get a complete list of users (employees) from the database', async () => {
  116. const result = await getUsers(db);
  117. expect(result).to.be.an('array').of.length(2);
  118. expect(result[0]).to.deep.include({
  119. name: 'John Doe',
  120. email: 'john.doe@mubaloo.com'
  121. });
  122. expect(result[1]).to.deep.include({
  123. name: 'Jill Doe',
  124. email: 'jill.doe@mubaloo.com'
  125. });
  126. expect(result[0]).to.have.property('id');
  127. expect(result[1]).to.have.property('id');
  128. expect(result[0].id).to.be.a('string');
  129. expect(result[1].id).to.be.a('string');
  130. });
  131. });
  132. });