|
|
@@ -0,0 +1,184 @@
|
|
|
+/* eslint-disable no-unused-expressions */
|
|
|
+
|
|
|
+const { expect } = require('chai');
|
|
|
+
|
|
|
+const mongoMocker = require('mongo-mocker');
|
|
|
+
|
|
|
+const {
|
|
|
+ insertUser,
|
|
|
+ modifyUser,
|
|
|
+ deleteUser,
|
|
|
+ getUsers,
|
|
|
+ COLLECTION_USERS
|
|
|
+} = require('server/modules/users');
|
|
|
+
|
|
|
+describe('API - Users module', () => {
|
|
|
+ let db = null;
|
|
|
+
|
|
|
+ const testUsers = [
|
|
|
+ { name: 'John Doe', email: 'john.doe@mubaloo.com' },
|
|
|
+ { name: 'Jill Doe', email: 'jill.doe@mubaloo.com' }
|
|
|
+ ];
|
|
|
+
|
|
|
+ before(() => {
|
|
|
+ db = mongoMocker('mongoDriver/path', {
|
|
|
+ [COLLECTION_USERS]: testUsers
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ beforeEach(async () => {
|
|
|
+ const users = await db.collection(COLLECTION_USERS);
|
|
|
+
|
|
|
+ await Promise.all(testUsers.map(user => users.insert(user)));
|
|
|
+ });
|
|
|
+
|
|
|
+ afterEach(() => {
|
|
|
+ db.mock.clearAll();
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('insertUser', () => {
|
|
|
+ it('should insert an employee into the users collection', async () => {
|
|
|
+ const users = await db.collection(COLLECTION_USERS);
|
|
|
+
|
|
|
+ const testUser = {
|
|
|
+ name: 'Jack Doe',
|
|
|
+ email: 'jack.doe@mubaloo.com'
|
|
|
+ };
|
|
|
+
|
|
|
+ const rowsBefore = await users.find(testUser)
|
|
|
+ .toArray();
|
|
|
+
|
|
|
+ expect(rowsBefore).to.be.an('array').of.length(0);
|
|
|
+
|
|
|
+ const newUserResult = await insertUser(db, testUser);
|
|
|
+
|
|
|
+ const rowsAfter = await users.find(testUser)
|
|
|
+ .toArray();
|
|
|
+
|
|
|
+ expect(rowsAfter).to.be.an('array').of.length(1);
|
|
|
+
|
|
|
+ expect(rowsAfter[0]).to.have.property('name', 'Jack Doe');
|
|
|
+ expect(rowsAfter[0]).to.have.property('email', 'jack.doe@mubaloo.com');
|
|
|
+ expect(rowsAfter[0]).to.have.property('_id');
|
|
|
+ expect(rowsAfter[0]._id).to.be.an('object');
|
|
|
+
|
|
|
+ expect(newUserResult).to.be.an('object');
|
|
|
+
|
|
|
+ expect(newUserResult).to.have.property('name', 'Jack Doe');
|
|
|
+ expect(newUserResult).to.have.property('email', 'jack.doe@mubaloo.com');
|
|
|
+ });
|
|
|
+
|
|
|
+ const badUsers = [
|
|
|
+ {},
|
|
|
+ { name: 'Some name' },
|
|
|
+ { email: 'some.email@mubaloo.com' },
|
|
|
+ { name: 'Some name', email: 'notanemail' }
|
|
|
+ ];
|
|
|
+
|
|
|
+ badUsers.forEach((user, index) => {
|
|
|
+ it(`should throw an error if information is missing or invalid (${index + 1}/${badUsers.length})`, async () => {
|
|
|
+
|
|
|
+ let errorThrown = false;
|
|
|
+
|
|
|
+ try {
|
|
|
+ await insertUser(db, user);
|
|
|
+ } catch (err) {
|
|
|
+ errorThrown = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!errorThrown) {
|
|
|
+ throw new Error('Did not throw an error');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('modifyUser', () => {
|
|
|
+ it('should modify a specific user', async () => {
|
|
|
+ const users = await db.collection(COLLECTION_USERS);
|
|
|
+
|
|
|
+ const user = await users.findOne({ name: 'John Doe' });
|
|
|
+
|
|
|
+ const id = String(user._id);
|
|
|
+
|
|
|
+ await modifyUser(db, id, {
|
|
|
+ name: 'Jack Doe'
|
|
|
+ });
|
|
|
+
|
|
|
+ const modifiedUser = await users.findOne({ _id: user._id });
|
|
|
+
|
|
|
+ expect(modifiedUser).to.have.property('name', 'Jack Doe');
|
|
|
+ expect(modifiedUser).to.have.property('email', 'john.doe@mubaloo.com');
|
|
|
+ });
|
|
|
+
|
|
|
+ const badUsers = [
|
|
|
+ { email: 'notanemail' }
|
|
|
+ ];
|
|
|
+
|
|
|
+ badUsers.forEach((user, index) => {
|
|
|
+ it(`should throw an error if information is invalid (${index + 1}/${badUsers.length})`, async () => {
|
|
|
+
|
|
|
+ const users = await db.collection(COLLECTION_USERS);
|
|
|
+
|
|
|
+ const userResult = await users.findOne({ name: 'John Doe' });
|
|
|
+
|
|
|
+ const id = String(userResult._id);
|
|
|
+
|
|
|
+ let errorThrown = false;
|
|
|
+
|
|
|
+ try {
|
|
|
+ await modifyUser(db, id, user);
|
|
|
+ } catch (err) {
|
|
|
+ errorThrown = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!errorThrown) {
|
|
|
+ throw new Error('Did not throw an error');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('deleteUser', () => {
|
|
|
+ it('should delete a user', async () => {
|
|
|
+ const users = await db.collection(COLLECTION_USERS);
|
|
|
+
|
|
|
+ const existingUser = await users.findOne({ name: 'John Doe' });
|
|
|
+
|
|
|
+ expect(existingUser).to.be.an('object');
|
|
|
+
|
|
|
+ const id = String(existingUser._id);
|
|
|
+
|
|
|
+ await deleteUser(db, id);
|
|
|
+
|
|
|
+ const newQueryResult = await users.findOne({ name: 'John Doe' });
|
|
|
+
|
|
|
+ expect(newQueryResult).to.be.undefined;
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('getUsers', () => {
|
|
|
+ it('should get a complete list of users (employees) from the database', async () => {
|
|
|
+ const result = await getUsers(db);
|
|
|
+
|
|
|
+ expect(result).to.be.an('array').of.length(2);
|
|
|
+
|
|
|
+ expect(result[0]).to.deep.include({
|
|
|
+ name: 'John Doe',
|
|
|
+ email: 'john.doe@mubaloo.com'
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(result[1]).to.deep.include({
|
|
|
+ name: 'Jill Doe',
|
|
|
+ email: 'jill.doe@mubaloo.com'
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(result[0]).to.have.property('id');
|
|
|
+ expect(result[1]).to.have.property('id');
|
|
|
+
|
|
|
+ expect(result[0].id).to.be.a('string');
|
|
|
+ expect(result[1].id).to.be.a('string');
|
|
|
+ });
|
|
|
+ });
|
|
|
+});
|
|
|
+
|