This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding route tests for user/admin CRUD operations
- Loading branch information
Showing
1 changed file
with
167 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: '[email protected]', | ||
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 = '[email protected]'; | ||
|
||
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) | ||
|
@@ -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) | ||
|
@@ -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(); | ||
}); | ||
}); | ||
}); | ||
|