2015-08-12 18:38:12 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var should = require('should'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
app = require('../../server'),
|
|
|
|
request = require('supertest'),
|
|
|
|
Session = require('supertest-session')({
|
|
|
|
app: app
|
|
|
|
}),
|
|
|
|
mongoose = require('mongoose'),
|
2015-09-15 22:21:49 +00:00
|
|
|
url = require('url'),
|
2015-08-12 18:38:12 +00:00
|
|
|
config = require('../../config/config'),
|
2015-09-15 22:21:49 +00:00
|
|
|
User = mongoose.model('User'),
|
2015-08-12 18:38:12 +00:00
|
|
|
tmpUser = mongoose.model(config.tempUserCollection),
|
2015-09-15 22:21:49 +00:00
|
|
|
agent = request.agent(app);
|
|
|
|
|
2015-08-12 18:38:12 +00:00
|
|
|
|
|
|
|
var mailosaur = require('mailosaur')(config.mailosaur.key),
|
|
|
|
mailbox = new mailosaur.Mailbox(config.mailosaur.mailbox_id);
|
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
var mandrill = require('node-mandrill')(config.mailer.options.auth.pass);
|
|
|
|
|
2015-08-12 18:38:12 +00:00
|
|
|
/**
|
|
|
|
* Globals
|
|
|
|
*/
|
|
|
|
var credentials, _User, _Session;
|
2015-09-15 22:21:49 +00:00
|
|
|
// Create user credentials
|
|
|
|
credentials = {
|
|
|
|
username: 'be1e58fb@mailosaur.in',
|
|
|
|
password: 'password'
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a new user
|
|
|
|
_User = {
|
|
|
|
firstName: 'Full',
|
|
|
|
lastName: 'Name',
|
|
|
|
email: credentials.username,
|
|
|
|
username: credentials.username,
|
|
|
|
password: credentials.password,
|
|
|
|
};
|
2015-08-12 18:38:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form routes tests
|
|
|
|
*/
|
|
|
|
describe('User CRUD tests', function() {
|
2015-09-15 22:21:49 +00:00
|
|
|
this.timeout(100000);
|
2015-08-12 18:38:12 +00:00
|
|
|
var userSession;
|
|
|
|
|
2015-09-03 18:21:56 +00:00
|
|
|
beforeEach(function() {
|
2015-08-12 18:38:12 +00:00
|
|
|
//Initialize Session
|
|
|
|
userSession = new Session();
|
|
|
|
|
|
|
|
// Create user credentials
|
|
|
|
credentials = {
|
|
|
|
username: 'be1e58fb@mailosaur.in',
|
|
|
|
password: 'password'
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a new user
|
|
|
|
_User = {
|
|
|
|
firstName: 'Full',
|
|
|
|
lastName: 'Name',
|
|
|
|
email: credentials.username,
|
|
|
|
username: credentials.username,
|
|
|
|
password: credentials.password,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
describe('Create, Verify and Activate a User', function() {
|
|
|
|
_User.email = _User.username = 'testActiveAccount5.be1e58fb@mailosaur.in';
|
2015-08-18 21:44:36 +00:00
|
|
|
var link, _tmpUser, activateToken = '';
|
2015-08-12 18:38:12 +00:00
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
before(function(done){
|
|
|
|
tmpUser.findOne({username: _User.username}, function (err, user) {
|
|
|
|
console.log(user);
|
|
|
|
if(user){
|
|
|
|
tmpUser.remove({username: _User.username}, function(err){
|
|
|
|
if(err) done(err)
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}else {done();}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to query /signup', function(done) {
|
|
|
|
|
|
|
|
userSession.post('/auth/signup')
|
2015-08-12 18:38:12 +00:00
|
|
|
.send(_User)
|
2015-09-03 18:21:56 +00:00
|
|
|
.expect(200, 'An email has been sent to you. Please check it to verify your account.')
|
2015-08-12 18:38:12 +00:00
|
|
|
.end(function(FormSaveErr, FormSaveRes) {
|
2015-09-15 22:21:49 +00:00
|
|
|
should.not.exist(FormSaveErr);
|
2015-09-03 18:21:56 +00:00
|
|
|
done();
|
2015-09-15 22:21:49 +00:00
|
|
|
});
|
|
|
|
});
|
2015-09-03 18:21:56 +00:00
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
it('should have created a temporary user', function(done){
|
|
|
|
tmpUser.findOne({username: _User.username}).exec(function (err, user) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(user);
|
|
|
|
_tmpUser = user;
|
|
|
|
|
|
|
|
_User.username.should.equal(user.username);
|
|
|
|
_User.firstName.should.equal(user.firstName);
|
|
|
|
_User.lastName.should.equal(user.lastName);
|
|
|
|
done();
|
|
|
|
|
|
|
|
// mandrill('/messages/search', {
|
|
|
|
// query: "subject:Confirm",
|
|
|
|
// senders: [
|
|
|
|
// "test@forms.polydaic.com"
|
|
|
|
// ],
|
|
|
|
// limit: 1
|
|
|
|
// }, function(error, emails) {
|
|
|
|
// if (error) console.log( JSON.stringify(error) );
|
|
|
|
|
|
|
|
// var confirmation_email = emails[0];
|
|
|
|
|
|
|
|
// mandrill('/messages/content', {
|
|
|
|
// id: confirmation_email._id
|
|
|
|
// }, function(error, email) {
|
|
|
|
// if (error) console.log( JSON.stringify(error) );
|
|
|
|
|
|
|
|
// // console.log(email);
|
|
|
|
// var link = _(email.text.split('\n')).reverse().value()[1];
|
|
|
|
// console.log(link);
|
|
|
|
// activateToken = _(url.parse(link).hash.split('/')).reverse().value()[0];
|
|
|
|
// console.log('actual activateToken: '+ activateToken);
|
|
|
|
// console.log('expected activateToken: ' + user.GENERATED_VERIFYING_URL);
|
|
|
|
|
|
|
|
// done();
|
|
|
|
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
});
|
|
|
|
});
|
2015-09-03 18:21:56 +00:00
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
it('should have sent a verification email', function(done){
|
2015-09-03 18:21:56 +00:00
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
mailbox.getEmails(function(err, _emails) {
|
|
|
|
if(err) done(err);
|
2015-09-03 18:21:56 +00:00
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
var emails = _emails;
|
2015-09-03 18:21:56 +00:00
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
console.log('mailbox.getEmails:');
|
|
|
|
console.log(emails[0].text.links);
|
2015-09-03 18:21:56 +00:00
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
var link = emails[0].text.links[0].href;
|
|
|
|
activateToken = _(url.parse(link).hash.split('/')).reverse().value()[0];
|
|
|
|
console.log('actual activateToken: '+ activateToken);
|
|
|
|
console.log('expected activateToken: ' + _tmpUser.GENERATED_VERIFYING_URL);
|
|
|
|
(activateToken).should.equal(_tmpUser.GENERATED_VERIFYING_URL);
|
2015-09-03 18:21:56 +00:00
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-09-03 18:21:56 +00:00
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
it('should be able to verify a User Account', function(done) {
|
|
|
|
userSession.get('/auth/verify/'+activateToken)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(VerifyErr, VerifyRes) {
|
|
|
|
should.not.exist(VerifyErr);
|
|
|
|
(VerifyRes.text).should.equal('User successfully verified');
|
|
|
|
done();
|
2015-08-12 18:38:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
it('should receive confirmation email after verifying a User Account', function(done) {
|
|
|
|
mailbox.getEmails(function(err, _emails) {
|
|
|
|
if(err) throw err;
|
|
|
|
var email = _emails[0];
|
|
|
|
|
|
|
|
console.log(email);
|
|
|
|
(email.subject).should.equal('Account successfully verified!');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-09-03 18:21:56 +00:00
|
|
|
|
|
|
|
// it('should be able to login and logout a User', function (done) {
|
|
|
|
// var username = 'testActiveAccount.be1e58fb@mailosaur.in';
|
|
|
|
// _User.email = _User.username = credentials.username = username;
|
|
|
|
// userSession.post('/auth/signup')
|
|
|
|
// .send(_User)
|
|
|
|
// .expect(200)
|
|
|
|
// .end(function(FormSaveErr, FormSaveRes) {
|
|
|
|
// (FormSaveRes.text).should.equal('An email has been sent to you. Please check it to verify your account.');
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-09-03 18:21:56 +00:00
|
|
|
// userSession.post('/auth/signin')
|
|
|
|
// .send(credentials)
|
|
|
|
// .expect('Content-Type', /json/)
|
|
|
|
// .expect(200)
|
|
|
|
// .end(function(signinErr, signinRes) {
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-09-03 18:21:56 +00:00
|
|
|
// // Handle signin error
|
|
|
|
// if (signinErr) throw signinErr;
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-09-03 18:21:56 +00:00
|
|
|
// userSession.get('/auth/signout')
|
|
|
|
// .expect(200)
|
|
|
|
// .end(function(signoutErr, signoutRes) {
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-09-03 18:21:56 +00:00
|
|
|
// // Handle signout error
|
|
|
|
// if (signoutErr) throw signoutErr;
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-09-03 18:21:56 +00:00
|
|
|
// (signoutRes.text).should.equal('Successfully logged out');
|
2015-08-12 18:38:12 +00:00
|
|
|
|
2015-09-03 18:21:56 +00:00
|
|
|
// done();
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// });
|
2015-08-12 18:38:12 +00:00
|
|
|
|
2015-09-03 18:21:56 +00:00
|
|
|
// it('should be able to reset a User\'s password');
|
2015-08-12 18:38:12 +00:00
|
|
|
|
2015-09-03 18:21:56 +00:00
|
|
|
// it('should be able to delete a User account without any problems');
|
2015-08-12 18:38:12 +00:00
|
|
|
|
|
|
|
afterEach(function(done) {
|
|
|
|
User.remove().exec(function () {
|
|
|
|
tmpUser.remove().exec(function(){
|
2015-09-03 18:21:56 +00:00
|
|
|
// mailbox.deleteAllEmail(function (err, body) {
|
|
|
|
// if(err) throw err;
|
2015-08-12 18:38:12 +00:00
|
|
|
userSession.destroy();
|
|
|
|
done();
|
2015-09-03 18:21:56 +00:00
|
|
|
// });
|
2015-08-12 18:38:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|