Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
add user route tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rhutchison committed Jul 28, 2015
1 parent 839f805 commit 4bbc4a3
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions modules/users/tests/server/user.server.routes.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict';

var should = require('should'),
request = require('supertest'),
path = require('path'),
mongoose = require('mongoose'),
User = mongoose.model('User'),
express = require(path.resolve('./config/lib/express'));

/**
* Globals
*/
var app, agent, credentials, user, admin;

/**
* User routes tests
*/
describe('User CRUD tests', function () {
before(function (done) {
// Get application
app = express.init(mongoose);
agent = request.agent(app);

done();
});

beforeEach(function (done) {
// Create user credentials
credentials = {
username: 'username',
password: 'password'
};

// Create a new user
user = new User({
firstName: 'Full',
lastName: 'Name',
displayName: 'Full Name',
email: 'test@test.com',
username: credentials.username,
password: credentials.password,
provider: 'local'
});

// Save a user to the test db and create new article
user.save(function () {
done();
});
});

it('should not be able to retrieve a list of users if not admin', function (done) {
agent.post('/api/auth/signin')
.send(credentials)
.expect(200)
.end(function (signinErr, signinRes) {
// Handle signin error
if (signinErr) {
return done(signinErr);
}

// Save a new article
agent.get('/api/users')
.expect(403)
.end(function (usersGetErr, usersGetRes) {
if (usersGetErr) {
return done(usersGetErr);
}

return done();
});
});
});

it('should be able to retrieve a list of users if admin', function (done) {
user.roles = ['user', 'admin'];

user.save(function () {
agent.post('/api/auth/signin')
.send(credentials)
.expect(200)
.end(function (signinErr, signinRes) {
// Handle signin error
if (signinErr) {
return done(signinErr);
}

// Save a new article
agent.get('/api/users')
.expect(200)
.end(function (usersGetErr, usersGetRes) {
if (usersGetErr) {
return done(usersGetErr);
}

usersGetRes.body.should.be.instanceof(Array).and.have.lengthOf(1);

// Call the assertion callback
done();
});
});
});
});

afterEach(function (done) {
User.remove().exec(done);
});
});

0 comments on commit 4bbc4a3

Please sign in to comment.