2015-10-07 01:16:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var should = require('should'),
|
2015-12-12 20:08:48 +00:00
|
|
|
lodash = require('lodash'),
|
2015-10-07 01:16:47 +00:00
|
|
|
app = require('../../server'),
|
|
|
|
request = require('supertest'),
|
2016-03-30 03:45:16 +00:00
|
|
|
Session = require('supertest-session'),
|
2015-10-07 01:16:47 +00:00
|
|
|
mongoose = require('mongoose'),
|
|
|
|
User = mongoose.model('User'),
|
|
|
|
Form = mongoose.model('Form'),
|
|
|
|
Field = mongoose.model('Field'),
|
2017-11-02 18:14:25 +00:00
|
|
|
FormSubmission = mongoose.model('FormSubmission'),
|
|
|
|
async = require('async');
|
2015-10-07 01:16:47 +00:00
|
|
|
|
2016-11-09 20:25:38 +00:00
|
|
|
/**
|
|
|
|
* Globals
|
|
|
|
*/
|
2017-04-23 19:46:15 +00:00
|
|
|
var user, myForm, userSession;
|
|
|
|
|
2017-04-23 19:55:26 +00:00
|
|
|
// Create user credentials
|
2017-04-23 19:46:15 +00:00
|
|
|
var credentials = {
|
2017-10-11 05:07:13 +00:00
|
|
|
username: 'aeokjqjqkqaeoaoe',
|
|
|
|
email: 'aeoaekjqjqqjkoeoa@test.com',
|
2017-04-23 19:46:15 +00:00
|
|
|
password: 'password'
|
|
|
|
};
|
2015-10-07 01:16:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form routes tests
|
|
|
|
*/
|
2015-10-30 18:40:02 +00:00
|
|
|
describe('Form Routes Unit tests', function() {
|
2015-10-07 01:16:47 +00:00
|
|
|
|
|
|
|
beforeEach(function(done) {
|
|
|
|
// Create a new user
|
|
|
|
user = new User({
|
|
|
|
firstName: 'Full',
|
|
|
|
lastName: 'Name',
|
2016-11-09 20:25:38 +00:00
|
|
|
email: credentials.email,
|
2015-10-07 01:16:47 +00:00
|
|
|
username: credentials.username,
|
|
|
|
password: credentials.password,
|
|
|
|
provider: 'local'
|
|
|
|
});
|
|
|
|
|
2017-10-11 05:07:13 +00:00
|
|
|
|
2015-10-07 01:16:47 +00:00
|
|
|
// Save a user to the test db and create new Form
|
|
|
|
user.save(function(err) {
|
2015-12-12 20:08:48 +00:00
|
|
|
should.not.exist(err);
|
2015-10-30 18:40:02 +00:00
|
|
|
myForm = {
|
2015-10-07 01:16:47 +00:00
|
|
|
title: 'Form Title',
|
2016-11-09 20:25:38 +00:00
|
|
|
language: 'en',
|
2016-03-30 03:45:16 +00:00
|
|
|
admin: user.id,
|
|
|
|
form_fields: [
|
2015-10-07 01:16:47 +00:00
|
|
|
new Field({'fieldType':'textfield', 'title':'First Name', 'fieldValue': ''}),
|
|
|
|
new Field({'fieldType':'checkbox', 'title':'nascar', 'fieldValue': ''}),
|
|
|
|
new Field({'fieldType':'checkbox', 'title':'hockey', 'fieldValue': ''})
|
2017-04-23 19:46:15 +00:00
|
|
|
],
|
|
|
|
isLive: true
|
2015-10-07 01:16:47 +00:00
|
|
|
};
|
2016-11-09 20:25:38 +00:00
|
|
|
|
2017-04-23 19:55:26 +00:00
|
|
|
//Initialize Session
|
|
|
|
userSession = Session(app);
|
2016-04-21 17:56:54 +00:00
|
|
|
|
2015-10-07 01:16:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-23 19:55:26 +00:00
|
|
|
it(' > should not be able to create a Form if not logged in', function(done) {
|
2017-10-11 05:07:13 +00:00
|
|
|
|
2016-04-21 17:56:54 +00:00
|
|
|
userSession.post('/forms')
|
2016-03-30 01:21:45 +00:00
|
|
|
.send({form: myForm})
|
|
|
|
.expect(401)
|
|
|
|
.end(function(FormSaveErr, FormSaveRes) {
|
2017-11-02 18:14:25 +00:00
|
|
|
console.log(FormSaveRes.text);
|
2016-03-30 01:21:45 +00:00
|
|
|
// Call the assertion callback
|
|
|
|
done(FormSaveErr);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it(' > should not be able to get list of users\' Forms if not logged in', function(done) {
|
2016-04-21 17:56:54 +00:00
|
|
|
userSession.get('/forms')
|
2016-03-30 01:21:45 +00:00
|
|
|
.expect(401)
|
|
|
|
.end(function(FormSaveErr, FormSaveRes) {
|
|
|
|
// Call the assertion callback
|
|
|
|
done(FormSaveErr);
|
|
|
|
});
|
|
|
|
});
|
2015-10-07 01:16:47 +00:00
|
|
|
|
2017-11-02 18:14:25 +00:00
|
|
|
it(' > should be able to read/get a live Form if not signed in', function(done) {
|
2016-03-30 01:21:45 +00:00
|
|
|
// Create new Form model instance
|
|
|
|
var FormObj = new Form(myForm);
|
|
|
|
|
|
|
|
// Save the Form
|
|
|
|
FormObj.save(function(err, form) {
|
|
|
|
if(err) return done(err);
|
|
|
|
|
2017-04-23 19:46:15 +00:00
|
|
|
userSession.get('/subdomain/' + credentials.username + '/forms/' + form._id + '/render')
|
2016-03-30 01:21:45 +00:00
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if(err) return done(err)
|
|
|
|
|
|
|
|
// Set assertion
|
|
|
|
(res.body).should.be.an.Object.with.property('title', myForm.title);
|
|
|
|
|
|
|
|
// Call the assertion callback
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-02 18:14:25 +00:00
|
|
|
it(' > should be able to read/get a non-live Form if not signed in', function(done) {
|
|
|
|
// Create new Form model instance
|
|
|
|
var FormObj = new Form(myForm);
|
|
|
|
FormObj.isLive = false;
|
|
|
|
|
|
|
|
// Save the Form
|
|
|
|
FormObj.save(function(err, form) {
|
|
|
|
if(err) return done(err);
|
|
|
|
|
|
|
|
userSession.get('/subdomain/' + credentials.username + '/forms/' + form._id + '/render')
|
|
|
|
.expect(401, {message: 'Form is Not Public'})
|
|
|
|
.end(function(err, res) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-03-30 01:21:45 +00:00
|
|
|
it(' > should not be able to delete an Form if not signed in', function(done) {
|
|
|
|
// Set Form user
|
|
|
|
myForm.admin = user;
|
|
|
|
|
|
|
|
// Create new Form model instance
|
|
|
|
var FormObj = new Form(myForm);
|
|
|
|
|
|
|
|
// Save the Form
|
|
|
|
FormObj.save(function() {
|
|
|
|
// Try deleting Form
|
2016-04-21 17:56:54 +00:00
|
|
|
userSession.delete('/forms/' + FormObj._id)
|
2016-03-30 01:21:45 +00:00
|
|
|
.expect(401)
|
|
|
|
.end(function(FormDeleteErr, FormDeleteRes) {
|
|
|
|
|
|
|
|
// Handle Form error error
|
|
|
|
done(FormDeleteErr);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-23 19:46:15 +00:00
|
|
|
describe(' > Login as User', function() {
|
|
|
|
//Initialize Session
|
|
|
|
var authenticatedSession;
|
|
|
|
var loginSession = Session(app);
|
2016-03-30 01:21:45 +00:00
|
|
|
|
2017-04-23 19:46:15 +00:00
|
|
|
beforeEach(function(done) {
|
|
|
|
loginSession.post('/auth/signin')
|
2016-04-21 18:48:35 +00:00
|
|
|
.send(credentials)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(signinErr, signinRes) {
|
2017-04-23 19:46:15 +00:00
|
|
|
if(signinErr) {
|
|
|
|
return done(signinErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticatedSession = loginSession;
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-02 18:14:25 +00:00
|
|
|
it(' > should not be able to create a Form if body is empty', function(done) {
|
|
|
|
loginSession.post('/forms')
|
|
|
|
.send({form: null})
|
|
|
|
.expect(400, {"message":"Invalid Input"})
|
|
|
|
.end(function(FormSaveErr, FormSaveRes) {
|
|
|
|
// Call the assertion callback
|
|
|
|
done(FormSaveErr);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-23 19:46:15 +00:00
|
|
|
it(' > should not be able to save a Form if no title is provided', function(done) {
|
|
|
|
// Set Form with a invalid title field
|
|
|
|
myForm.title = '';
|
|
|
|
|
|
|
|
// Save a new Form
|
|
|
|
authenticatedSession.post('/forms')
|
|
|
|
.send({form: myForm})
|
2017-10-11 05:07:13 +00:00
|
|
|
.expect(500)
|
2017-04-23 19:46:15 +00:00
|
|
|
.end(function(FormSaveErr, FormSaveRes) {
|
|
|
|
// Handle Form save error
|
|
|
|
if (FormSaveErr) {
|
|
|
|
return done(FormSaveErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set message assertion
|
|
|
|
(FormSaveRes.body.message).should.equal('Form Title cannot be blank');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-11-02 18:14:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it(' > should be able to create a Form if form_fields are undefined', function(done) {
|
|
|
|
myForm.analytics = null;
|
|
|
|
myForm.form_fields = null;
|
2017-04-23 19:46:15 +00:00
|
|
|
|
2017-11-02 18:14:25 +00:00
|
|
|
loginSession.post('/forms')
|
|
|
|
.send({form: myForm})
|
|
|
|
.expect(200)
|
|
|
|
.end(function(FormSaveErr, FormSaveRes) {
|
|
|
|
// Call the assertion callback
|
|
|
|
done(FormSaveErr);
|
|
|
|
});
|
2017-04-23 19:46:15 +00:00
|
|
|
});
|
|
|
|
|
2017-11-02 18:14:25 +00:00
|
|
|
it(' > should be able to update a Form if signed in and Form is valid', function(done) {
|
2017-04-23 19:46:15 +00:00
|
|
|
|
|
|
|
// Save a new Form
|
|
|
|
loginSession.post('/forms')
|
|
|
|
.send({form: myForm})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(FormSaveErr, FormSaveRes) {
|
|
|
|
// Handle Form save error
|
|
|
|
if (FormSaveErr) {
|
|
|
|
return done(FormSaveErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update Form title
|
2017-11-02 18:14:25 +00:00
|
|
|
myForm.title = 'WHY YOU GOTTA BE SO FORMULAIC?';
|
2017-04-23 19:46:15 +00:00
|
|
|
|
|
|
|
// Update an existing Form
|
|
|
|
loginSession.put('/forms/' + FormSaveRes.body._id)
|
|
|
|
.send({form: myForm})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(FormUpdateErr, FormUpdateRes) {
|
|
|
|
// Handle Form update error
|
|
|
|
if (FormUpdateErr){
|
|
|
|
done(FormUpdateErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set assertions
|
|
|
|
(FormUpdateRes.body._id).should.equal(FormSaveRes.body._id);
|
2017-11-02 18:14:25 +00:00
|
|
|
(FormUpdateRes.body.title).should.match(myForm.title);
|
2017-04-23 19:46:15 +00:00
|
|
|
|
|
|
|
// Call the assertion callback
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it(' > should be able to delete a Form if signed in', function(done) {
|
2016-03-30 01:21:45 +00:00
|
|
|
|
2017-04-23 19:46:15 +00:00
|
|
|
// Save a new Form
|
|
|
|
loginSession.post('/forms')
|
|
|
|
.send({form: myForm})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(FormSaveErr, FormSaveRes) {
|
|
|
|
// Handle Form save error
|
|
|
|
if (FormSaveErr) {
|
|
|
|
return done(FormSaveErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete an existing Form
|
|
|
|
loginSession.delete('/forms/' + FormSaveRes.body._id)
|
|
|
|
.send(myForm)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(FormDeleteErr, FormDeleteRes) {
|
|
|
|
// Handle Form error error
|
|
|
|
if (FormDeleteErr) {
|
|
|
|
return done(FormDeleteErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set assertions
|
|
|
|
should.exist(FormDeleteRes.body);
|
|
|
|
(FormDeleteRes.body._id).should.equal(FormSaveRes.body._id);
|
|
|
|
|
|
|
|
// Call the assertion callback
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-02 18:14:25 +00:00
|
|
|
it(' > should be able to save new form while logged in', function(done){
|
2017-04-23 19:46:15 +00:00
|
|
|
// Save a new Form
|
|
|
|
authenticatedSession.post('/forms')
|
|
|
|
.send({form: myForm})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(FormSaveErr, FormSaveRes) {
|
|
|
|
// Handle Form save error
|
|
|
|
if (FormSaveErr) return done(FormSaveErr);
|
|
|
|
var _form = FormSaveRes.body;
|
|
|
|
|
|
|
|
// Get a list of Forms
|
|
|
|
authenticatedSession.get('/forms/'+_form._id)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(FormsGetErr, FormsGetRes) {
|
|
|
|
// Handle Form save error
|
|
|
|
if (FormsGetErr) return done(FormsGetErr);
|
|
|
|
|
|
|
|
var fetchedForm = FormsGetRes.body;
|
|
|
|
// Set assertions
|
|
|
|
(fetchedForm.admin.email).should.equal(user.email);
|
|
|
|
(fetchedForm.title).should.match(_form.title);
|
|
|
|
|
|
|
|
// Call the assertion callback
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-03-30 01:21:45 +00:00
|
|
|
|
2017-11-02 18:14:25 +00:00
|
|
|
it(' > should be able to get list of users\' forms sorted by date created while logged in', function(done) {
|
|
|
|
var myForm1 = {
|
|
|
|
title: 'First Form',
|
|
|
|
language: 'en',
|
|
|
|
admin: user.id,
|
|
|
|
form_fields: [
|
|
|
|
new Field({'fieldType':'textfield', 'title':'First Name', 'fieldValue': ''}),
|
|
|
|
new Field({'fieldType':'checkbox', 'title':'nascar', 'fieldValue': ''}),
|
|
|
|
new Field({'fieldType':'checkbox', 'title':'hockey', 'fieldValue': ''})
|
|
|
|
],
|
|
|
|
isLive: true
|
|
|
|
};
|
|
|
|
|
|
|
|
var myForm2 = {
|
|
|
|
title: 'Second Form',
|
|
|
|
language: 'en',
|
|
|
|
admin: user.id,
|
|
|
|
form_fields: [
|
|
|
|
new Field({'fieldType':'textfield', 'title':'Last Name', 'fieldValue': ''}),
|
|
|
|
new Field({'fieldType':'checkbox', 'title':'formula one', 'fieldValue': ''}),
|
|
|
|
new Field({'fieldType':'checkbox', 'title':'football', 'fieldValue': ''})
|
|
|
|
],
|
|
|
|
isLive: true
|
|
|
|
};
|
|
|
|
|
|
|
|
var FormObj1 = new Form(myForm1);
|
|
|
|
var FormObj2 = new Form(myForm2);
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function(callback) {
|
|
|
|
FormObj1.save(function(err){
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(callback) {
|
|
|
|
FormObj2.save(function(err){
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(callback) {
|
|
|
|
loginSession.get('/forms')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
res.body.length.should.equal(2);
|
|
|
|
res.body[0].title.should.equal('Second Form');
|
|
|
|
res.body[1].title.should.equal('First Form');
|
|
|
|
|
|
|
|
// Call the assertion callback
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], function (err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-23 19:46:15 +00:00
|
|
|
afterEach('should be able to signout user', function(done){
|
|
|
|
authenticatedSession.get('/auth/signout')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(signoutErr, signoutRes) {
|
2016-04-21 18:48:35 +00:00
|
|
|
// Handle signout error
|
2017-11-02 18:14:25 +00:00
|
|
|
if (signoutErr) {
|
|
|
|
return done(signoutErr);
|
|
|
|
}
|
2017-04-23 19:46:15 +00:00
|
|
|
authenticatedSession.destroy();
|
|
|
|
done();
|
2016-04-21 18:48:35 +00:00
|
|
|
});
|
|
|
|
});
|
2016-03-30 01:21:45 +00:00
|
|
|
});
|
2015-10-07 01:16:47 +00:00
|
|
|
|
|
|
|
afterEach(function(done) {
|
2015-10-30 18:40:02 +00:00
|
|
|
Form.remove({}).exec(function() {
|
2017-04-23 19:55:26 +00:00
|
|
|
User.remove({}).exec(function() {
|
|
|
|
userSession.destroy();
|
|
|
|
done();
|
|
|
|
});
|
2015-10-07 01:16:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|