2016-03-30 01:21:45 +00:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var should = require('should'),
|
|
|
|
|
_ = require('lodash'),
|
|
|
|
|
app = require('../../server'),
|
|
|
|
|
request = require('supertest'),
|
|
|
|
|
Session = require('supertest-session'),
|
|
|
|
|
mongoose = require('mongoose'),
|
|
|
|
|
User = mongoose.model('User'),
|
|
|
|
|
Form = mongoose.model('Form'),
|
|
|
|
|
Field = mongoose.model('Field'),
|
|
|
|
|
FormSubmission = mongoose.model('FormSubmission'),
|
|
|
|
|
agent = request.agent(app);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Globals
|
|
|
|
|
*/
|
2016-03-30 03:45:16 +00:00
|
|
|
|
var credentials, user;
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Form routes tests
|
|
|
|
|
*/
|
2016-03-30 03:45:16 +00:00
|
|
|
|
describe('Form Submission Routes Unit tests', function() {
|
2017-11-01 19:46:15 +00:00
|
|
|
|
var FormObj, _Submission, submissionSession, _SubmissionBody;
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
2016-03-30 03:45:16 +00:00
|
|
|
|
beforeEach(function(done) {
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
|
|
|
|
// Create user credentials
|
|
|
|
|
credentials = {
|
2017-11-01 22:13:31 +00:00
|
|
|
|
email: 'test423@test.com',
|
|
|
|
|
username: 'test534',
|
2016-03-30 01:21:45 +00:00
|
|
|
|
password: 'password'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Create a new user
|
|
|
|
|
user = new User({
|
|
|
|
|
firstName: 'Full',
|
|
|
|
|
lastName: 'Name',
|
2016-11-09 20:25:38 +00:00
|
|
|
|
email: credentials.email,
|
2016-03-30 01:21:45 +00:00
|
|
|
|
username: credentials.username,
|
|
|
|
|
password: credentials.password,
|
|
|
|
|
provider: 'local'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Save a user to the test db and create new Form
|
|
|
|
|
user.save(function(err) {
|
2017-11-01 22:13:31 +00:00
|
|
|
|
if(err) {
|
|
|
|
|
return done(err);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 03:45:16 +00:00
|
|
|
|
FormObj = new Form({
|
2016-03-30 01:21:45 +00:00
|
|
|
|
title: 'Form Title',
|
2016-11-09 20:25:38 +00:00
|
|
|
|
language: 'en',
|
2016-03-30 01:21:45 +00:00
|
|
|
|
admin: user._id,
|
|
|
|
|
form_fields: [
|
|
|
|
|
new Field({'fieldType':'textfield', 'title':'First Name', 'fieldValue': ''}),
|
2017-11-21 18:42:07 +00:00
|
|
|
|
new Field({'fieldType':'legal', 'title':'nascar', 'fieldValue': ''}),
|
|
|
|
|
new Field({'fieldType':'legal', 'title':'hockey', 'fieldValue': ''})
|
2017-11-01 22:13:31 +00:00
|
|
|
|
],
|
|
|
|
|
selfNotifications: {
|
|
|
|
|
fromField: mongoose.Types.ObjectId(),
|
|
|
|
|
toEmails: 'john@smith.com',
|
|
|
|
|
subject: 'Hello there',
|
|
|
|
|
htmlTemplate: '<p> A form was submitted </p>',
|
|
|
|
|
enabled: true
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
respondentNotifications: {
|
|
|
|
|
toField: mongoose.Types.ObjectId(),
|
|
|
|
|
fromEmails: 'john@smith.com',
|
|
|
|
|
subject: 'Tellform: Thank you for filling out this TellForm',
|
|
|
|
|
htmlTemplate:'Hello, <br><br> We’ve received your submission. <br><br> Thank you & have a nice day!',
|
|
|
|
|
enabled: true
|
|
|
|
|
}
|
2016-03-30 03:45:16 +00:00
|
|
|
|
});
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
2017-04-21 04:49:27 +00:00
|
|
|
|
FormObj.save(function(formSaveErr, form) {
|
|
|
|
|
if (formSaveErr) done(formSaveErr);
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
|
|
|
|
_Submission = {
|
2017-10-11 05:07:13 +00:00
|
|
|
|
form: form._id,
|
2016-03-30 01:21:45 +00:00
|
|
|
|
form_fields: [
|
2017-10-11 05:07:13 +00:00
|
|
|
|
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': 'David', _id: '', isSubmission: false, deletePreserved: false},
|
2017-11-21 18:42:07 +00:00
|
|
|
|
{'fieldType':'legal', 'title':'nascar', 'fieldValue': true, _id: '', isSubmission: false, deletePreserved: true},
|
|
|
|
|
{'fieldType':'legal', 'title':'hockey', 'fieldValue': false, _id: '', isSubmission: false, deletePreserved: false}
|
2016-03-30 01:21:45 +00:00
|
|
|
|
],
|
|
|
|
|
percentageComplete: 100,
|
2017-10-11 05:07:13 +00:00
|
|
|
|
timeElapsed: 11.55,
|
|
|
|
|
ipAddr: '123.233.232.232',
|
|
|
|
|
geoLocation: {
|
|
|
|
|
Country: 'Canada',
|
|
|
|
|
City: 'Vancouver'
|
|
|
|
|
},
|
|
|
|
|
device:{
|
|
|
|
|
type: 'Mobile',
|
|
|
|
|
name: 'iPhone'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_SubmissionBody ={
|
|
|
|
|
_id: form._id,
|
|
|
|
|
form_fields: [
|
|
|
|
|
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': 'David', _id: '', isSubmission: false, deletePreserved: false},
|
2017-11-21 18:42:07 +00:00
|
|
|
|
{'fieldType':'legal', 'title':'nascar', 'fieldValue': true, _id: '', isSubmission: false, deletePreserved: true},
|
|
|
|
|
{'fieldType':'legal', 'title':'hockey', 'fieldValue': false, _id: '', isSubmission: false, deletePreserved: false}
|
2017-10-11 05:07:13 +00:00
|
|
|
|
],
|
|
|
|
|
percentageComplete: 100,
|
|
|
|
|
timeElapsed: 11.55,
|
|
|
|
|
ipAddr: '123.233.232.232',
|
|
|
|
|
geoLocation: {
|
|
|
|
|
Country: 'Canada',
|
|
|
|
|
City: 'Vancouver'
|
|
|
|
|
},
|
|
|
|
|
device:{
|
|
|
|
|
type: 'Mobile',
|
|
|
|
|
name: 'iPhone'
|
|
|
|
|
}
|
2016-03-30 01:21:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FormObj = form;
|
|
|
|
|
|
2016-03-30 03:45:16 +00:00
|
|
|
|
//Initialize Session
|
|
|
|
|
submissionSession = Session(app);
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it(' > should be able to create a Form Submission without signing in', function(done) {
|
|
|
|
|
|
|
|
|
|
//Create Submission
|
|
|
|
|
submissionSession.post('/forms/' + FormObj._id)
|
2017-10-11 05:07:13 +00:00
|
|
|
|
.send(_SubmissionBody)
|
2016-03-30 01:21:45 +00:00
|
|
|
|
.expect(200)
|
|
|
|
|
.end(function(err, res) {
|
|
|
|
|
|
|
|
|
|
should.not.exist(err);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it(' > should be able to get Form Submissions if signed in', function(done) {
|
2016-03-30 03:45:16 +00:00
|
|
|
|
//Create Submission
|
|
|
|
|
submissionSession.post('/forms/' + FormObj._id)
|
2017-10-11 05:07:13 +00:00
|
|
|
|
.send(_SubmissionBody)
|
2016-03-30 01:21:45 +00:00
|
|
|
|
.expect(200)
|
2016-03-30 03:45:16 +00:00
|
|
|
|
.end(function(err, res) {
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
2016-03-30 03:45:16 +00:00
|
|
|
|
should.not.exist(err);
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
2016-03-30 03:45:16 +00:00
|
|
|
|
submissionSession.post('/auth/signin')
|
|
|
|
|
.send(credentials)
|
|
|
|
|
.expect('Content-Type', /json/)
|
2016-03-30 01:21:45 +00:00
|
|
|
|
.expect(200)
|
2016-03-30 03:45:16 +00:00
|
|
|
|
.end(function(signinErr, signinRes) {
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
2016-03-30 03:45:16 +00:00
|
|
|
|
should.not.exist(signinErr);
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
|
|
|
|
submissionSession.get('/forms/' + FormObj._id + '/submissions')
|
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
|
.expect(200)
|
2017-04-21 04:49:27 +00:00
|
|
|
|
.end(function(submissionErr, res) {
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
|
|
|
|
// Set assertion
|
2017-04-21 04:49:27 +00:00
|
|
|
|
should.not.exist(submissionErr);
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
|
|
|
|
// Call the assertion callback
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-03-30 03:45:16 +00:00
|
|
|
|
it(' > should be able to delete Form Submission if signed in', function(done) {
|
|
|
|
|
// Create new FormSubmission model instance
|
|
|
|
|
var SubmissionObj = new FormSubmission(_Submission);
|
|
|
|
|
|
|
|
|
|
SubmissionObj.save(function (err, submission) {
|
|
|
|
|
should.not.exist(err);
|
|
|
|
|
|
|
|
|
|
// Sign n as user
|
|
|
|
|
submissionSession.post('/auth/signin')
|
|
|
|
|
.send(credentials)
|
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
|
.expect(200)
|
|
|
|
|
.end(function(signinErr, signinRes) {
|
|
|
|
|
// Handle signin error
|
|
|
|
|
should.not.exist(signinErr);
|
|
|
|
|
|
2017-07-21 04:21:20 +00:00
|
|
|
|
var submission_ids = _.map([submission], '_id');
|
2016-03-30 03:45:16 +00:00
|
|
|
|
|
|
|
|
|
//Delete form submissions
|
|
|
|
|
submissionSession.delete('/forms/' + FormObj._id + '/submissions')
|
|
|
|
|
.send({deleted_submissions: submission_ids})
|
|
|
|
|
.expect(200)
|
2017-04-21 04:49:27 +00:00
|
|
|
|
.end(function(submissionErr, res) {
|
2016-03-30 03:45:16 +00:00
|
|
|
|
|
|
|
|
|
// Set assertions
|
2017-04-21 04:49:27 +00:00
|
|
|
|
should.not.exist(submissionErr);
|
2016-03-30 03:45:16 +00:00
|
|
|
|
(res.text).should.equal('Form submissions successfully deleted');
|
|
|
|
|
|
|
|
|
|
// Call the assertion callback
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-03-30 01:21:45 +00:00
|
|
|
|
it(' > should not be able to get Form Submissions if not signed in', function(done) {
|
|
|
|
|
// Attempt to fetch form submissions
|
|
|
|
|
submissionSession.get('/forms/' + FormObj._id + '/submissions')
|
|
|
|
|
.expect(401)
|
|
|
|
|
.end(function(err, res) {
|
2016-03-30 03:45:16 +00:00
|
|
|
|
should.not.exist(err);
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
|
|
|
|
// Call the assertion callback
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-03-30 03:45:16 +00:00
|
|
|
|
|
2016-03-30 01:21:45 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it(' > should not be able to delete Form Submission if not signed in', function(done) {
|
|
|
|
|
var SubmissionObj = new FormSubmission(_Submission);
|
|
|
|
|
|
|
|
|
|
SubmissionObj.save(function (err, submission) {
|
|
|
|
|
should.not.exist(err);
|
|
|
|
|
|
2017-07-21 04:21:20 +00:00
|
|
|
|
var submission_ids = _.map([submission], '_id');
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
|
|
|
|
// Attempt to delete form submissions
|
|
|
|
|
submissionSession.delete('/forms/' + FormObj._id + '/submissions')
|
|
|
|
|
.send({deleted_submissions: submission_ids})
|
|
|
|
|
.expect(401)
|
2017-04-21 04:49:27 +00:00
|
|
|
|
.end(function (submissionErr, res) {
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
|
|
|
|
// Set assertions
|
2017-04-21 04:49:27 +00:00
|
|
|
|
should.not.exist(submissionErr);
|
2016-03-30 01:21:45 +00:00
|
|
|
|
|
|
|
|
|
// Call the assertion callback
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(function(done) {//logout current user if there is one
|
|
|
|
|
FormSubmission.remove().exec(function() {
|
|
|
|
|
Form.remove().exec(function (err) {
|
|
|
|
|
User.remove({}).exec(function() {
|
|
|
|
|
submissionSession.destroy();
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|