|
| 1 | +// Instantiate all models |
| 2 | +var mongoose = require('mongoose'); |
| 3 | +require('../../../server/db/models'); |
| 4 | +var User = mongoose.model('User'); |
| 5 | + |
| 6 | +var expect = require('chai').expect; |
| 7 | + |
| 8 | +var dbURI = 'mongodb://localhost:27017/testingDB'; |
| 9 | +var clearDB = require('mocha-mongoose')(dbURI); |
| 10 | + |
| 11 | +var supertest = require('supertest'); |
| 12 | +var app = require('../../../server/app'); |
| 13 | + |
| 14 | +describe('Members Route', function () { |
| 15 | + |
| 16 | + beforeEach('Establish DB connection', function (done) { |
| 17 | + if (mongoose.connection.db) return done(); |
| 18 | + mongoose.connect(dbURI, done); |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach('Clear test database', function (done) { |
| 22 | + clearDB(done); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('Unauthenticated request', function () { |
| 26 | + |
| 27 | + var guestAgent; |
| 28 | + |
| 29 | + beforeEach('Create guest agent', function () { |
| 30 | + guestAgent = supertest.agent(app); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should get a 401 response', function (done) { |
| 34 | + guestAgent.get('/api/members/secret-stash') |
| 35 | + .expect(401) |
| 36 | + .end(done); |
| 37 | + }); |
| 38 | + |
| 39 | + }); |
| 40 | + |
| 41 | + describe('Authenticated request', function () { |
| 42 | + |
| 43 | + var loggedInAgent; |
| 44 | + |
| 45 | + var userInfo = { |
| 46 | + |
| 47 | + password: 'shoopdawoop' |
| 48 | + }; |
| 49 | + |
| 50 | + beforeEach('Create a user', function (done) { |
| 51 | + User.create(userInfo, done); |
| 52 | + }); |
| 53 | + |
| 54 | + beforeEach('Create loggedIn user agent and authenticate', function (done) { |
| 55 | + loggedInAgent = supertest.agent(app); |
| 56 | + loggedInAgent.post('/login').send(userInfo).end(done); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should get with 200 response and with an array as the body', function (done) { |
| 60 | + loggedInAgent.get('/api/members/secret-stash').expect(200).end(function (err, response) { |
| 61 | + if (err) return done(err); |
| 62 | + expect(response.body).to.an.array; |
| 63 | + done(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + }); |
| 68 | + |
| 69 | +}); |
0 commit comments