diff --git a/test/users.spec.js b/test/users.spec.js index 774bb03..cbf1d5d 100644 --- a/test/users.spec.js +++ b/test/users.spec.js @@ -1,12 +1,18 @@ import app from '../config/server' import supertest from 'supertest' import { expect, should } from 'chai' +import { cleanDb } from './utils' should() const request = supertest.agent(app.listen()) const context = {} describe('Users', () => { + before((done) => { + cleanDb() + done() + }) + describe('POST /users', () => { it('should reject signup when data is incomplete', (done) => { request @@ -36,4 +42,20 @@ describe('Users', () => { }) }) }) + + describe('GET /users', () => { + it('should not fetch users if token is invalid', (done) => { + request + .get('/users?token=1') + .set('Accept', 'application/json') + .expect(401, done) + }) + + it('should fetch all users', (done) => { + request + .get(`/users?token=${context.token}`) + .set('Accept', 'application/json') + .expect(200, done) + }) + }) }) diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 0000000..3ac5f12 --- /dev/null +++ b/test/utils.js @@ -0,0 +1,7 @@ +import mongoose from 'mongoose' + +export function cleanDb () { + for (const collection in mongoose.connection.collections) { + mongoose.connection.collections[collection].remove(); + } +}