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

Commit

Permalink
adding route tests for user/admin CRUD operations
Browse files Browse the repository at this point in the history
  • Loading branch information
lirantal committed Sep 21, 2015
1 parent 1eddc47 commit de354ee
Showing 1 changed file with 167 additions and 6 deletions.
173 changes: 167 additions & 6 deletions modules/users/tests/server/user.server.routes.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var should = require('should'),
/**
* Globals
*/
var app, agent, credentials, user, admin;
var app, agent, credentials, user, _user, admin;

/**
* User routes tests
Expand All @@ -32,22 +32,75 @@ describe('User CRUD tests', function () {
};

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

user = new User(_user);

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

it('should be able to register a new user', function (done) {

_user.username = 'register_new_user';
_user.email = 'register_new_user_@test.com';

agent.post('/api/auth/signup')
.send(_user)
.expect(200)
.end(function (signupErr, signupRes) {
// Handle signpu error
if (signupErr) {
return done(signupErr);
}

signupRes.body.username.should.equal(_user.username);
signupRes.body.email.should.equal(_user.email);
// Assert a proper profile image has been set, even if by default
signupRes.body.profileImageURL.should.not.be.empty();
// Assert we have just the default 'user' role
signupRes.body.roles.should.be.instanceof(Array).and.have.lengthOf(1);
signupRes.body.roles.indexOf('user').should.equal(0);
return done();
});
});

it('should be able to login successfully and logout successfully', function (done) {
agent.post('/api/auth/signin')
.send(credentials)
.expect(200)
.end(function (signinErr, signinRes) {
// Handle signin error
if (signinErr) {
return done(signinErr);
}

// Logout
agent.get('/api/auth/signout')
.expect(302)
.end(function (signoutErr, signoutRes) {
if (signoutErr) {
return done(signoutErr);
}

signoutRes.redirect.should.equal(true);
signoutRes.text.should.equal('Moved Temporarily. Redirecting to /');
return done();
});
});
});

it('should not be able to retrieve a list of users if not admin', function (done) {
agent.post('/api/auth/signin')
.send(credentials)
Expand All @@ -74,7 +127,8 @@ describe('User CRUD tests', function () {
it('should be able to retrieve a list of users if admin', function (done) {
user.roles = ['user', 'admin'];

user.save(function () {
user.save(function (err) {
should.not.exist(err);
agent.post('/api/auth/signin')
.send(credentials)
.expect(200)
Expand All @@ -95,7 +149,114 @@ describe('User CRUD tests', function () {
usersGetRes.body.should.be.instanceof(Array).and.have.lengthOf(1);

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

it('should be able to get a single user details if admin', function (done) {
user.roles = ['user', 'admin'];

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

// Get single user information from the database
agent.get('/api/users/' + user._id)
.expect(200)
.end(function (userInfoErr, userInfoRes) {
if (userInfoErr) {
return done(userInfoErr);
}

userInfoRes.body.should.be.instanceof(Object);
userInfoRes.body._id.should.be.equal(String(user._id));

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

it('should be able to update a single user details if admin', function (done) {
user.roles = ['user', 'admin'];

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

// Get single user information from the database

var userUpdate = {
firstName: 'admin_update_first',
lastName: 'admin_update_last',
roles: ['admin']
};

agent.put('/api/users/' + user._id)
.send(userUpdate)
.expect(200)
.end(function (userInfoErr, userInfoRes) {
if (userInfoErr) {
return done(userInfoErr);
}

userInfoRes.body.should.be.instanceof(Object);
userInfoRes.body.firstName.should.be.equal('admin_update_first');
userInfoRes.body.lastName.should.be.equal('admin_update_last');
userInfoRes.body.roles.should.be.instanceof(Array).and.have.lengthOf(1);
userInfoRes.body._id.should.be.equal(String(user._id));

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

it('should be able to delete a single user if admin', function (done) {
user.roles = ['user', 'admin'];

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

agent.delete('/api/users/' + user._id)
//.send(userUpdate)
.expect(200)
.end(function (userInfoErr, userInfoRes) {
if (userInfoErr) {
return done(userInfoErr);
}

userInfoRes.body.should.be.instanceof(Object);
userInfoRes.body._id.should.be.equal(String(user._id));

// Call the assertion callback
return done();
});
});
});
Expand Down

0 comments on commit de354ee

Please sign in to comment.