tellform/app/tests/user.server.routes.test.js

114 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-03-30 01:21:45 +00:00
'use strict';
var should = require('should'),
app = require('../../server'),
Session = require('supertest-session'),
mongoose = require('mongoose'),
User = mongoose.model('User'),
config = require('../../config/config'),
tmpUser = mongoose.model(config.tempUserCollection);
2016-03-30 01:21:45 +00:00
/**
* Globals
*/
2017-04-23 02:49:24 +00:00
var credentials, _User, activateToken, userSession;
2016-03-30 03:45:16 +00:00
2016-03-30 01:21:45 +00:00
/**
* Form routes tests
*/
describe('User CRUD tests', function() {
2016-04-21 17:56:54 +00:00
this.timeout(30000);
2016-03-30 01:21:45 +00:00
beforeEach(function() {
// Create user credentials
credentials = {
2016-11-09 20:25:38 +00:00
email: 'test732@test.com',
username: 'test732',
password: 'password3223'
2016-03-30 01:21:45 +00:00
};
//Create a new user
_User = {
2016-11-09 20:25:38 +00:00
email: credentials.email,
2016-03-30 01:21:45 +00:00
username: credentials.username,
2017-04-23 19:46:15 +00:00
password: credentials.password
2016-03-30 01:21:45 +00:00
};
2016-11-09 20:25:38 +00:00
2016-04-21 17:56:54 +00:00
//Initialize Session
2016-11-09 20:25:38 +00:00
userSession = Session(app);
2016-03-30 01:21:45 +00:00
});
2016-11-09 20:25:38 +00:00
it(' > Create, Verify and Activate a User > ', function() {
2016-03-30 01:21:45 +00:00
it('should be able to create a temporary (non-activated) User', function(done) {
userSession.post('/auth/signup')
.send(_User)
2016-03-30 03:45:16 +00:00
.expect(200)
2017-04-23 02:49:24 +00:00
.end(function(FormSaveErr) {
2016-11-09 20:25:38 +00:00
console.log('CREATING USER');
2016-03-30 01:21:45 +00:00
// Handle error
2016-03-30 03:45:16 +00:00
should.not.exist(FormSaveErr);
2016-11-09 20:25:38 +00:00
2016-04-21 17:56:54 +00:00
tmpUser.findOne({username: _User.username}, function (err, user) {
should.not.exist(err);
should.exist(user);
_User.username.should.equal(user.username);
_User.firstName.should.equal(user.firstName);
_User.lastName.should.equal(user.lastName);
activateToken = user.GENERATED_VERIFYING_URL;
2016-11-09 20:25:38 +00:00
2016-04-21 17:56:54 +00:00
userSession.get('/auth/verify/'+activateToken)
.expect(200)
.end(function(VerifyErr, VerifyRes) {
// Handle error
2017-04-23 02:49:24 +00:00
if (VerifyErr) {
return done(VerifyErr);
}
2017-04-23 19:55:26 +00:00
2016-04-21 17:56:54 +00:00
(VerifyRes.text).should.equal('User successfully verified');
2016-11-09 20:25:38 +00:00
2016-04-21 17:56:54 +00:00
userSession.post('/auth/signin')
.send(credentials)
.expect('Content-Type', /json/)
.expect(200)
.end(function(signinErr, signinRes) {
// Handle signin error
2017-04-23 02:49:24 +00:00
if (signinErr) {
return done(signinErr);
}
2016-04-21 17:56:54 +00:00
var user = signinRes.body;
(user.username).should.equal(credentials.username);
userSession.get('/auth/signout')
.expect(200)
.end(function(signoutErr, signoutRes) {
// Handle signout error
2017-04-23 02:49:24 +00:00
if (signoutErr) {
2017-04-23 19:55:26 +00:00
return done(signoutErr);
}
2016-04-21 17:56:54 +00:00
(signoutRes.text).should.equal('You have successfully logged out.');
done();
});
});
});
});
2016-03-30 01:21:45 +00:00
});
});
2016-04-21 17:56:54 +00:00
});
2016-03-30 01:21:45 +00:00
afterEach(function(done) {
User.remove().exec(function () {
tmpUser.remove().exec(function(){
2017-04-23 02:49:24 +00:00
userSession.destroy();
done();
2016-03-30 01:21:45 +00:00
});
});
});
});