tellform/app/tests/form_submission.routes.test.js

215 lines
5.1 KiB
JavaScript
Raw Normal View History

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() {
2016-03-30 01:21:45 +00:00
var FormObj, _Submission, submissionSession;
2016-03-30 03:45:16 +00:00
beforeEach(function(done) {
2016-03-30 01:21:45 +00:00
// Create user credentials
credentials = {
2016-03-30 03:45:16 +00:00
email: 'test@test.com',
2016-11-09 20:25:38 +00:00
username: 'test',
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) {
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': ''}),
new Field({'fieldType':'checkbox', 'title':'nascar', 'fieldValue': ''}),
new Field({'fieldType':'checkbox', 'title':'hockey', 'fieldValue': ''})
]
2016-03-30 03:45:16 +00:00
});
2016-03-30 01:21:45 +00:00
FormObj.save(function(err, form) {
if (err) done(err);
_Submission = {
form_fields: [
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': 'David'},
{'fieldType':'checkbox', 'title':'nascar', 'fieldValue': true},
{'fieldType':'checkbox', 'title':'hockey', 'fieldValue': false}
],
form: form._id,
admin: user._id,
percentageComplete: 100,
timeElapsed: 11.55
};
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)
.send(_Submission)
.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)
.send(_Submission)
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)
.end(function(err, res) {
// Set assertion
should.not.exist(err);
// 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);
var submission_ids = _.pluck([submission], '_id');
//Delete form submissions
submissionSession.delete('/forms/' + FormObj._id + '/submissions')
.send({deleted_submissions: submission_ids})
.expect(200)
.end(function(err, res) {
// Set assertions
should.not.exist(err);
(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);
var submission_ids = _.pluck([submission], '_id');
// Attempt to delete form submissions
submissionSession.delete('/forms/' + FormObj._id + '/submissions')
.send({deleted_submissions: submission_ids})
.expect(401)
2016-03-30 03:45:16 +00:00
.end(function (err, res) {
2016-03-30 01:21:45 +00:00
// Set assertions
should.not.exist(err);
// 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();
});
});
});
});
});