Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
posabsolute committed Apr 26, 2015
1 parent fbb1d71 commit e449dd5
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 16 deletions.
11 changes: 10 additions & 1 deletion gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function(grunt) {
nunjucks: {
options:{
paths : "src",
langs : ["en_US"],
langs : ["en_US", "fr_CA"],
filters : function(env, options){
env.addFilter('trans', function(str, obj) {
var lang = options.lang || 'en_US';
Expand Down Expand Up @@ -143,6 +143,14 @@ module.exports = function(grunt) {
files: ['src/*.html','src/**/*.html'],
tasks: ['nunjucks','premailer:inline']
}
},
mochaTest: {
api: {
options: {
reporter: 'spec'
},
src: ['tests/*.js']
}
}
});

Expand Down Expand Up @@ -180,6 +188,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-litmus');
grunt.loadNpmTasks('grunt-nodemailer');
grunt.loadNpmTasks('grunt-premailer');
grunt.loadNpmTasks('grunt-mocha-test');

grunt.registerTask('default',['watch']);
grunt.registerTask('css',['sass']);
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
"grunt-contrib-connect": "^0.9.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-litmus": "^0.1.8",
"grunt-mocha-test": "^0.12.7",
"grunt-nodemailer": "^0.3.0",
"grunt-nunjucks": "^0.1.4",
"grunt-nunjucks-2-html": "^0.2.3",
"grunt-premailer": "^0.2.11",
"grunt-sass": "^0.18.0",
"jasmine-node": "^1.14.5",
"le_node": "^0.2.1",
"mocha": "^2.2.4",
"node-hipchat": "^0.4.5",
"nodemailer": "^1.3.2",
"nunjucks": "^1.3.3",
"supertest": "^0.15.0",
"yamljs": "^0.2.1"
},
"dependencies": {
Expand Down
2 changes: 2 additions & 0 deletions src/server/configs/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module.exports = {
"failOver" : "SendGrid",
// active logs
"sendLogs" : ['hipchat','logentries'],

"sync" : true,
// logs configs
"logs" : {
"hipchat":{
Expand Down
36 changes: 27 additions & 9 deletions src/server/controllers/controller.emails.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ var emails_controller = {
// get post data
var data = req.body.data.variables || {},
// Get language used
local = req.body.data.locale || "en_US",
locale = req.body.data.locale || "en_US",
// render template with nunjucks
tplURL = req.body.data.collection + "/" + req.body.data.template + ".html",
tplURL = templates_controller.getTemplate(locale, req.body.data.collection, req.body.data.template),
templateHtml = templates_controller.renderTemplate(tplURL, data, res),
// get service from post data
service = req.body.service.name || configs.service || 'smtp',
Expand All @@ -39,12 +39,18 @@ var emails_controller = {

mailOptions.html = templateHtml;
mailOptions.failOver = req.body.service.failover;
mailOptions.service = service;
mailOptions.template = tplURL;
// send mail with defined transport object
emails_controller.sendEmail(mailOptions, transporter);
emails_controller.sendEmail(mailOptions, transporter, undefined, res);

// You can choose to be sync but your response will be bound by your email service speed.
if(!configs.sync){
res.send("Email has been sent to "+service);
res.status(200).json({
"service": service,
"async" : true,
template : tplURL
});
}

},
Expand All @@ -59,32 +65,44 @@ var emails_controller = {
auth: serviceAuthConfigs.services[service].auth
});
},
sendEmail : function(mailOptions, transporter, failOver){
sendEmail : function(mailOptions, transporter, failOver, res){
transporter.sendMail(mailOptions, function(error, info){
if(error){
// We can resend the email if we have a failover provider
var failOverService = mailOptions.failOver || configs.failOver || undefined;
if(failOverService && !failOver){
var transporter = emails_controller.getTransporter(failOverService);
emails_controller.sendEmail(mailOptions, transporter, true);
emails_controller.sendEmail(mailOptions, transporter, true, res);
}
// you can setup logs in configs.js
if(configs.sendLogs && configs.sendLogs.length){
logs_service.log(error, "crit");
var level = error.level || "crit";
logs_service.log(error, level);
}
// Inker is async by default sending you back the fastest response possible
// Errors must be logged elsewhere
// if we are sync it wait for the service provider to respond & send back the error directly
if(configs.sync){
res.send(error);
var status = error.responseCode || 400;
res.status(status).json({
"provider": mailOptions.service,
"async" : false,
"template" : mailOptions.template,
"providerInfo" : error
});
}
console.log(error);
// success
}else{
// Inker is async by default sending you back the fastest response possible
// if we are sync it wait for the service provider to respond
if(configs.sync){
res.send(info);
res.status(200).json({
"provider": mailOptions.service,
"async" : false,
"template" : mailOptions.template,
"providerInfo" : info
});
}
console.log(info);
}
Expand Down
16 changes: 11 additions & 5 deletions src/server/controllers/controller.templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,29 @@ var templates_controller = {
// get post data
var data = req.query || {},
locale = req.params.locale || "en_US",
tplURL = locale + '/' + req.params.folder + '/' + req.params.name + '.html',
templateHtml = templates_controller.renderTemplate(tplURL, data);
tplURL = templates_controller.getTemplate(locale, req.params.folder, req.params.name),
templateHtml = templates_controller.renderTemplate(tplURL, data, res);

//res.render(templateHtml);
res.send(templateHtml);
},
/**
* Find template path
* @return {string} return template url
*/
getTemplate : function(locale, folder, name){
return locale + '/' + folder + '/' + name + '.html';
},
/**
* Render template with custom data using nunjucks
* @return {html} return complete template html
*/
renderTemplate : function(template, data){
renderTemplate : function(template, data, res){
try{
return nunjucks.render(template, data);
}catch(error){
logs_service.log(error.message, "crit");
res.status(400);
res.send(error);
res.status(400).json({"error": error.message});
}
}
};
Expand Down
4 changes: 3 additions & 1 deletion src/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ var server = app.listen(3000, function () {

console.log('Example app listening at http://%s:%s', host, port);

});
});

module.exports = app;
1 change: 1 addition & 0 deletions src/templates/data_example/_content.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ <h4>{{ '{{ name }}' }} last step and you're done!</h4>
{{ '{% endfor %}' }}
</p>
<p>We need you to update your account information. If there is ever a problem with your account, this information will make it easier for you to log back in.</p>
<p>{{ 'test_string' | trans }}</p>
</td>
<td class="expander"></td>
</tr>
Expand Down
59 changes: 59 additions & 0 deletions tests/emails.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var request = require('supertest');
var configs = require('../src/server/configs/configs');
var api = require('../src/server/server');

describe('Email Delivery Service', function() {

it('401 if auth token is wrong', function(done) {
request(api)
.post('/emails')
.set('x-authorization-token', '123myapikey')
.auth('incorrect', 'credentials')
.expect(401, done);
});

it('401 if auth token is undefined', function(done) {
request(api)
.get('/emails')
.expect(401, done);
});

it('400 if template is undefined', function(done) {
request(api)
.get('/emails')
.set('x-authorization-token', configs.authToken)
.expect(400, done);
});

it('200 when auth token is right & template is right', function(done) {
request(api)
.get('/collections/data_example/templates/index?name=Cedric&loop[]=1&loop[]=2&loop[]=3')
.set('x-authorization-token', configs.authToken)
.expect(200, done);
});

it('Expect french data to be used', function(done) {
request(api)
.get('/collections/data_example/templates/index/locale/fr_CA?name=Cedric&loop[]=1&loop[]=2&loop[]=3')
.set('x-authorization-token', configs.authToken)
.expect(function(res){
if (res.text.indexOf("fr_CA") === -1) {
return "Missing french string set in fr_CA";
}
})
.end(done);
});

it('Expect data to be added to the template', function(done) {
request(api)
.get('/collections/data_example/templates/index?name=Cedric')
.set('x-authorization-token', configs.authToken)
.expect(function(res){
if (res.text.indexOf("Cedric") === -1) {
return "missing name from template";
}
})
.end(done);
});

});
Empty file added tests/logs.spec.js
Empty file.
60 changes: 60 additions & 0 deletions tests/templates.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var request = require('supertest');
var configs = require('../src/server/configs/configs');
var api = require('../src/server/server');

describe('Template generation', function() {

it('401 if auth token is wrong', function(done) {
request(api)
.get('/collections/data_example/templates/index?name=Cedric&loop[]=1&loop[]=2&loop[]=3')
.set('x-authorization-token', '123myapikey')
.auth('incorrect', 'credentials')
.expect(401, done);
});

it('401 if auth token is undefined', function(done) {
request(api)
.get('/collections/data_example/templates/index?name=Cedric&loop[]=1&loop[]=2&loop[]=3')
.expect(401, done);
});


it('400 if template is undefined', function(done) {
request(api)
.get('/collections/data_examples/templates/index')
.set('x-authorization-token', configs.authToken)
.expect(400, done);
});

it('200 when auth token is right & template is right', function(done) {
request(api)
.get('/collections/data_example/templates/index?name=Cedric&loop[]=1&loop[]=2&loop[]=3')
.set('x-authorization-token', configs.authToken)
.expect(200, done);
});

it('Expect french data to be used', function(done) {
request(api)
.get('/collections/data_example/templates/index/locale/fr_CA?name=Cedric&loop[]=1&loop[]=2&loop[]=3')
.set('x-authorization-token', configs.authToken)
.expect(function(res){
if (res.text.indexOf("fr_CA") === -1) {
return "Missing french string set in fr_CA";
}
})
.end(done);
});

it('Expect data to be added to the template', function(done) {
request(api)
.get('/collections/data_example/templates/index?name=Cedric')
.set('x-authorization-token', configs.authToken)
.expect(function(res){
if (res.text.indexOf("Cedric") === -1) {
return "missing name from template";
}
})
.end(done);
});

});

0 comments on commit e449dd5

Please sign in to comment.