|
|
@@ -1,33 +1,40 @@
|
|
|
/* eslint-disable no-unused-expressions */
|
|
|
|
|
|
const { expect } = require('chai');
|
|
|
-
|
|
|
const mongoMocker = require('mongo-mocker');
|
|
|
|
|
|
+const joi = require('joi');
|
|
|
+
|
|
|
const {
|
|
|
- insertUser,
|
|
|
- modifyUser,
|
|
|
- deleteUser,
|
|
|
- getUsers,
|
|
|
- COLLECTION_USERS
|
|
|
-} = require('server/modules/users');
|
|
|
-
|
|
|
-describe('API - Users module', () => {
|
|
|
+ makeMongoCrud
|
|
|
+} = require('server/modules/crud');
|
|
|
+
|
|
|
+describe('API - Crud module', () => {
|
|
|
let db = null;
|
|
|
+ let crud = null;
|
|
|
+
|
|
|
+ const schema = {
|
|
|
+ name: joi.string(),
|
|
|
+ email: joi.string().email()
|
|
|
+ };
|
|
|
|
|
|
const testUsers = [
|
|
|
{ name: 'John Doe', email: 'john.doe@mubaloo.com' },
|
|
|
{ name: 'Jill Doe', email: 'jill.doe@mubaloo.com' }
|
|
|
];
|
|
|
|
|
|
+ const collection = 'users';
|
|
|
+
|
|
|
before(() => {
|
|
|
db = mongoMocker('mongoDriver/path', {
|
|
|
- [COLLECTION_USERS]: testUsers
|
|
|
+ [collection]: testUsers
|
|
|
});
|
|
|
+
|
|
|
+ crud = makeMongoCrud(collection, schema);
|
|
|
});
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
- const users = await db.collection(COLLECTION_USERS);
|
|
|
+ const users = await db.collection(collection);
|
|
|
|
|
|
await Promise.all(testUsers.map(user => users.insert(user)));
|
|
|
});
|
|
|
@@ -36,9 +43,14 @@ describe('API - Users module', () => {
|
|
|
db.mock.clearAll();
|
|
|
});
|
|
|
|
|
|
- describe('insertUser', () => {
|
|
|
- it('should insert an employee into the users collection', async () => {
|
|
|
- const users = await db.collection(COLLECTION_USERS);
|
|
|
+ describe('insertDoc', () => {
|
|
|
+ it('should be defined', () => {
|
|
|
+ expect(crud).to.have.property('insertDoc');
|
|
|
+ expect(crud.insertDoc).to.be.a('function');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should insert a document into the collection', async () => {
|
|
|
+ const users = await db.collection(collection);
|
|
|
|
|
|
const testUser = {
|
|
|
name: 'Jack Doe',
|
|
|
@@ -50,7 +62,7 @@ describe('API - Users module', () => {
|
|
|
|
|
|
expect(rowsBefore).to.be.an('array').of.length(0);
|
|
|
|
|
|
- const newUserResult = await insertUser(db, testUser);
|
|
|
+ const newUserResult = await crud.insertDoc(db, testUser);
|
|
|
|
|
|
const rowsAfter = await users.find(testUser)
|
|
|
.toArray();
|
|
|
@@ -81,7 +93,7 @@ describe('API - Users module', () => {
|
|
|
let errorThrown = false;
|
|
|
|
|
|
try {
|
|
|
- await insertUser(db, user);
|
|
|
+ await crud.insertDoc(db, user);
|
|
|
} catch (err) {
|
|
|
expect(err).to.have.property('statusCode', 400);
|
|
|
|
|
|
@@ -95,15 +107,20 @@ describe('API - Users module', () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- describe('modifyUser', () => {
|
|
|
- it('should modify a specific user', async () => {
|
|
|
- const users = await db.collection(COLLECTION_USERS);
|
|
|
+ describe('modifyDoc', () => {
|
|
|
+ it('should be defined', () => {
|
|
|
+ expect(crud).to.have.property('modifyDoc');
|
|
|
+ expect(crud.modifyDoc).to.be.a('function');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should modify a specific document', async () => {
|
|
|
+ const users = await db.collection(collection);
|
|
|
|
|
|
const user = await users.findOne({ name: 'John Doe' });
|
|
|
|
|
|
const id = String(user._id);
|
|
|
|
|
|
- await modifyUser(db, id, {
|
|
|
+ await crud.modifyDoc(db, id, {
|
|
|
name: 'Jack Doe'
|
|
|
});
|
|
|
|
|
|
@@ -120,7 +137,7 @@ describe('API - Users module', () => {
|
|
|
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 users = await db.collection(collection);
|
|
|
|
|
|
const userResult = await users.findOne({ name: 'John Doe' });
|
|
|
|
|
|
@@ -129,7 +146,7 @@ describe('API - Users module', () => {
|
|
|
let errorThrown = false;
|
|
|
|
|
|
try {
|
|
|
- await modifyUser(db, id, user);
|
|
|
+ await crud.modifyDoc(db, id, user);
|
|
|
} catch (err) {
|
|
|
errorThrown = true;
|
|
|
|
|
|
@@ -143,9 +160,14 @@ describe('API - Users module', () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- describe('deleteUser', () => {
|
|
|
- it('should delete a user', async () => {
|
|
|
- const users = await db.collection(COLLECTION_USERS);
|
|
|
+ describe('deleteDoc', () => {
|
|
|
+ it('should be defined', () => {
|
|
|
+ expect(crud).to.have.property('deleteDoc');
|
|
|
+ expect(crud.deleteDoc).to.be.a('function');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should delete a document', async () => {
|
|
|
+ const users = await db.collection(collection);
|
|
|
|
|
|
const existingUser = await users.findOne({ name: 'John Doe' });
|
|
|
|
|
|
@@ -153,7 +175,7 @@ describe('API - Users module', () => {
|
|
|
|
|
|
const id = String(existingUser._id);
|
|
|
|
|
|
- await deleteUser(db, id);
|
|
|
+ await crud.deleteDoc(db, id);
|
|
|
|
|
|
const newQueryResult = await users.findOne({ name: 'John Doe' });
|
|
|
|
|
|
@@ -161,9 +183,14 @@ describe('API - Users module', () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- describe('getUsers', () => {
|
|
|
- it('should get a complete list of users (employees) from the database', async () => {
|
|
|
- const result = await getUsers(db);
|
|
|
+ describe('getDocs', () => {
|
|
|
+ it('should be defined', () => {
|
|
|
+ expect(crud).to.have.property('getDocs');
|
|
|
+ expect(crud.getDocs).to.be.a('function');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should get a complete list of documents', async () => {
|
|
|
+ const result = await crud.getDocs(db);
|
|
|
|
|
|
expect(result).to.be.an('array').of.length(2);
|
|
|
|
|
|
@@ -186,3 +213,4 @@ describe('API - Users module', () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
+
|