Skip to content

Commit 28bdbac

Browse files
author
Tim Noel
committed
MA-1057 #time 6h Tests for email object
1 parent 1bd6f13 commit 28bdbac

File tree

6 files changed

+273
-73
lines changed

6 files changed

+273
-73
lines changed

lib/SendGridCompatibility/Email.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
'use strict';
2+
13
function Email(options){
2-
self = options;
4+
for (var option in options) {
5+
this[option] = options[option];
6+
}
37
}
48

59
Email.prototype.addTo = function (address){
6-
if (typeof this.to === 'string'){
10+
if (this.to === undefined){
11+
this.to = address;
12+
} else if (typeof this.to === 'string'){
713
this.to = [this.to];
14+
this.to.push(address);
15+
} else {
16+
this.to.push(address);
817
}
9-
this.to.push(address);
1018
};
1119
Email.prototype.setFrom = function (address){
1220
this.from = address;
@@ -21,12 +29,18 @@ Email.prototype.setHtml = function (html){
2129
this.html = html;
2230
};
2331
Email.prototype.addHeader = function (key, value){
32+
if (this.headers === undefined){
33+
this.headers = {};
34+
}
2435
this.headers[key] = value;
2536
};
2637
Email.prototype.setHeaders = function (headers){
2738
this.headers = headers;
2839
};
2940
Email.prototype.addSubstitution = function (key, value){
41+
if (this.sub === undefined){
42+
this.sub = {};
43+
}
3044
if (typeof value === 'string'){
3145
this.sub[key] = [value];
3246
} else {
@@ -37,30 +51,33 @@ Email.prototype.setSubstitutions = function (substitutions){
3751
this.sub = substitutions;
3852
};
3953
Email.prototype.addSection = function (key, value){
54+
if (this.section === undefined){
55+
this.section = {};
56+
}
4057
this.section[key] = value;
4158
};
4259
Email.prototype.setSections = function (sections){
4360
this.section = sections;
4461
};
45-
Email.prototype.addUniqueArg = function (arg){
62+
Email.prototype.addUniqueArg = function (){
4663
throw new Error('Unique Argument compatibility is not supported.');
4764
};
48-
Email.prototype.setUniqueArgs = function (args){
65+
Email.prototype.setUniqueArgs = function (){
4966
throw new Error('Unique Argument compatibility is not supported.');
5067
};
51-
Email.prototype.addCategory = function (category){
68+
Email.prototype.addCategory = function (){
5269
throw new Error('Category compatibility is not supported.');
5370
};
54-
Email.prototype.setCategories = function (categories){
71+
Email.prototype.setCategories = function (){
5572
throw new Error('Category compatibility is not supported.');
5673
};
57-
Email.prototype.addFilter = function (filter){
74+
Email.prototype.addFilter = function (){
5875
throw new Error('Filter compatibility is not supported.');
5976
};
60-
Email.prototype.setFilters = function (filters){
77+
Email.prototype.setFilters = function (){
6178
throw new Error('Filter compatibility is not supported.');
6279
};
63-
Email.prototype.addFile = function (file){
80+
Email.prototype.addFile = function (){
6481
throw new Error('File compatibility is not supported.');
6582
};
6683

lib/SendGridCompatibility/index.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,55 @@
33
var transmission = require('../transmission');
44
var configuration = require('../configuration');
55

6-
var sendgrid = function(username, key, options) {
6+
var sendgrid = function(username, apiKey, options) {
77
var configOptions = options;
88
if (configOptions === undefined) {
9-
configOptions = {'key': key};
9+
configOptions = {key: apiKey};
1010
} else {
11-
configOptions['key'] = key;
11+
configOptions.key = apiKey;
1212
}
1313
configuration.setConfig(configOptions);
1414
};
1515

16+
var mergeObjects = function(obj1, obj2){
17+
var merged = {};
18+
for (var key1 in obj1) {
19+
merged[key1] = obj1[key1];
20+
}
21+
for (var key2 in obj2) {
22+
merged[key2] = obj2[key2];
23+
}
24+
return merged;
25+
};
26+
27+
var consolidateSubstitutionData = function(payload) {
28+
var substitutionData = {};
29+
if (payload.sub !== undefined && payload.section !== undefined){
30+
substitutionData = mergeObjects(payload.sub, payload.section);
31+
} else if (payload.sub !== undefined){
32+
substitutionData = payload.sub;
33+
} else if (payload.section !== undefined){
34+
substitutionData = payload.section;
35+
}
36+
return substitutionData;
37+
};
38+
1639
var translatePayload = function(payload) {
17-
var input = {
40+
var sub = consolidateSubstitutionData(payload)
41+
, input = {
1842
recipients: [],
1943
from: '',
2044
html: '',
2145
text: '',
2246
subject: ''
23-
}
47+
};
2448

2549
for (var i = 0; payload.to.length > i; i++){
26-
var to = { address: { email: payload.to[i] } };
50+
var to = {
51+
address: {
52+
email: payload.to[i]
53+
}
54+
};
2755
if (payload.toname !== undefined && payload.toname[i] !== undefined){
2856
to.address.name = payload.toname;
2957
}
@@ -38,19 +66,7 @@ var translatePayload = function(payload) {
3866
input.html = payload.html;
3967
input.replyTo = payload.replyto;
4068
input.customHeaders = payload.headers;
41-
if (payload.sub !== undefined && payload.section !== undefined){
42-
input.substitutionData = {};
43-
for (var key in payload.sub) {
44-
input.substitutionData[key] = payload.sub[key];
45-
}
46-
for (var key in payload.section) {
47-
input.substitutionData[key] = payload.section[key];
48-
}
49-
} else if (payload.sub !== undefined){
50-
input.substitutionData = payload.sub;
51-
} else if (payload.section !== undefined){
52-
input.substitutionData = payload.section;
53-
}
69+
input.substitutionData = (sub !== {}) ? sub : undefined;
5470
console.log(input);
5571

5672
return input;
@@ -59,13 +75,15 @@ var translatePayload = function(payload) {
5975
sendgrid.prototype.send = function(payload, callback) {
6076
var translated = translatePayload(payload);
6177
transmission.send(translated, function (err, res){
62-
if(err){
63-
console.log(err);
78+
if (err) {
79+
callback(err);
80+
} else {
81+
callback(null, res);
6482
}
6583
});
6684
};
6785

6886
sendgrid.prototype.Email = require('./Email');
69-
module.exports.Email = require('./Email');
87+
//module.exports.Email = require('./Email');
7088

7189
module.exports = sendgrid;

test/mocha.opts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--recursive
2+
--reporter spec
3+
--timeout 20000
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
var chai = require('chai')
2+
, expect = chai.expect
3+
, sinon = require('sinon')
4+
, sinonChai = require('sinon-chai')
5+
, email = require('../../../lib/SendGridCompatibility/Email');
6+
7+
chai.use(sinonChai);
8+
9+
describe('SendGrid Email Object', function() {
10+
var tmpEmail
11+
, payload = {
12+
13+
toname: ['Fakey Fakerson', 'Realy Realerson'],
14+
15+
fromname: 'A. Sender',
16+
subject: 'Sale',
17+
text: 'Look at this sale!',
18+
html: '<h1>Look </h1><h5>at this sale!</h5>',
19+
20+
replyto: '[email protected]',
21+
date: new Date(),
22+
files: [
23+
{
24+
filename: 'name.txt',
25+
path: '../../../../../../',
26+
url: 'www.google.com',
27+
content: '?'
28+
}
29+
],
30+
file_data: {asdf: 'asdf'},
31+
headers: {asdfasdf: 'asdfasdf'}
32+
};
33+
34+
describe('instantiation', function() {
35+
it('should handle empty input', function() {
36+
tmpEmail = new email();
37+
expect(Object.keys(tmpEmail).length).to.equal(0);
38+
});
39+
it('should handle a payload', function() {
40+
tmpEmail = new email(payload);
41+
for (var index in tmpEmail) {
42+
if(typeof tmpEmail[index] !== 'function'){
43+
expect(tmpEmail[index]).to.deep.equal(payload[index]);
44+
}
45+
}
46+
});
47+
});
48+
49+
describe('compatibility functions: ', function() {
50+
beforeEach(function() {
51+
tmpEmail = new email(payload);
52+
});
53+
it('addTo', function() {
54+
tmpEmail.to = undefined;
55+
tmpEmail.addTo('[email protected]');
56+
tmpEmail.addTo('[email protected]');
57+
tmpEmail.addTo('[email protected]');
58+
expect(tmpEmail.to).to.deep.equal(['[email protected]','[email protected]','[email protected]']);
59+
});
60+
it('setFrom', function(){
61+
tmpEmail.setFrom('[email protected]');
62+
expect(tmpEmail.from).to.equal('[email protected]');
63+
});
64+
it('setSubject', function(){
65+
tmpEmail.setSubject('new subject');
66+
expect(tmpEmail.subject).to.equal('new subject');
67+
});
68+
it('setText', function(){
69+
tmpEmail.setText('new text');
70+
expect(tmpEmail.text).to.equal('new text');
71+
});
72+
it('setHtml', function(){
73+
tmpEmail.setHtml('<p>new html</p>');
74+
expect(tmpEmail.html).to.equal('<p>new html</p>');
75+
});
76+
it('addHeader', function(){
77+
tmpEmail.headers = undefined;
78+
tmpEmail.addHeader('abcd', 'efgh');
79+
tmpEmail.addHeader('1234', '5678');
80+
expect(tmpEmail.headers).to.deep.equal({abcd: 'efgh', 1234: '5678'});
81+
});
82+
it('setHeaders', function(){
83+
tmpEmail.setHeaders({oldish: 'new'});
84+
expect(tmpEmail.headers).to.deep.equal({oldish: 'new'});
85+
});
86+
it('addSubstitution', function(){
87+
tmpEmail.addSubstitution('key', 'value');
88+
tmpEmail.addSubstitution('attributeName', ['attribute1', 'attribute2']);
89+
expect(tmpEmail.sub).to.deep.equal({key: ['value'], attributeName: ['attribute1', 'attribute2']});
90+
});
91+
it('setSubstitutions', function(){
92+
tmpEmail.setSubstitutions({index: ['element']});
93+
expect(tmpEmail.sub).to.deep.equal({index: ['element']});
94+
});
95+
it('addSection', function(){
96+
tmpEmail.addSection('key', 'value');
97+
tmpEmail.addSection('attributeName', 'attribute1');
98+
expect(tmpEmail.section).to.deep.equal({key: 'value', attributeName: 'attribute1'});
99+
});
100+
it('setSections', function(){
101+
tmpEmail.setSections({index: 'element'});
102+
expect(tmpEmail.section).to.deep.equal({index: 'element'});
103+
});
104+
it('addUniqueArg', function(){
105+
try {
106+
tmpEmail.addUniqueArg();
107+
} catch (err) {
108+
expect(err).to.deep.equal(new Error('Unique Argument compatibility is not supported.'));
109+
}
110+
});
111+
it('setUniqueArgs', function(){
112+
try {
113+
tmpEmail.setUniqueArgs();
114+
} catch (err) {
115+
expect(err).to.deep.equal(new Error('Unique Argument compatibility is not supported.'));
116+
}
117+
});
118+
it('addCategory', function(){
119+
try {
120+
tmpEmail.addCategory();
121+
} catch (err) {
122+
expect(err).to.deep.equal(new Error('Category compatibility is not supported.'));
123+
}
124+
});
125+
it('setCategories', function(){
126+
try {
127+
tmpEmail.setCategories();
128+
} catch (err) {
129+
expect(err).to.deep.equal(new Error('Category compatibility is not supported.'));
130+
}
131+
});
132+
it('addFilter', function(){
133+
try {
134+
tmpEmail.addFilter();
135+
} catch (err) {
136+
expect(err).to.deep.equal(new Error('Filter compatibility is not supported.'));
137+
}
138+
});
139+
it('setFilters', function(){
140+
try {
141+
tmpEmail.setFilters();
142+
} catch (err) {
143+
expect(err).to.deep.equal(new Error('Filter compatibility is not supported.'));
144+
}
145+
});
146+
it('addFile', function(){
147+
try {
148+
tmpEmail.addFile();
149+
} catch (err) {
150+
expect(err).to.deep.equal(new Error('File compatibility is not supported.'));
151+
}
152+
});
153+
});
154+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var chai = require('chai')
2+
, expect = chai.expect
3+
, proxyquire = require('proxyquire')
4+
, sinon = require('sinon')
5+
, sinonChai = require('sinon-chai')
6+
, configuration = require('../../../lib/configuration')
7+
, MockRequest = require('../../mocks/request.js');
8+
9+
chai.use(sinonChai);
10+
11+
describe('SendGrid Compatibility', function() {
12+
var sendgrid;
13+
14+
beforeEach(function() {
15+
sendgrid = proxyquire('../../../lib/SendGridCompatibility', {
16+
'request': MockRequest
17+
});
18+
});
19+
20+
describe('Instantiation', function() {
21+
it('should expose a send function', function() {
22+
expect(transmission.send).to.be.a.function;
23+
});
24+
});
25+
26+
describe('translatePayload Helper Method', function() {
27+
var sendSpy;
28+
29+
beforeEach(function() {
30+
sendSpy = sinon.spy(MockRequest, 'post');
31+
});
32+
33+
afterEach(function() {
34+
sendSpy.restore();
35+
});
36+
37+
it('should default the return path for sparkpost users', function() {
38+
sparkpost.send({}, function(err, res) {
39+
expect(sendSpy.args[0][0].json.return_path).to.equal('[email protected]');
40+
});
41+
});
42+
43+
it('should allow on prem users to override the return path', function() {
44+
transmission.send({returnPath: '[email protected]'}, function(err, res) {
45+
expect(sendSpy.args[0][0].json.return_path).to.equal('[email protected]');
46+
});
47+
});
48+
});
49+
});

0 commit comments

Comments
 (0)