/
sendgrid.test.js
142 lines (112 loc) · 4 KB
/
sendgrid.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var default_payload = {
to : process.env.TO || "hello@example.com",
from : process.env.FROM || "swift@sendgrid.com",
subject : "[sendgrid-nodejs] ",
text : "This is a text body",
html : "<h2>This is an html body</h2>"
}
var sinon = require('sinon')
, nock = require('nock');
describe('SendGrid', function () {
it('should export the Email object', function() {
expect(sendgrid.Email).to.not.be.undefined;
});
it('should export the SmtpapiHeaders object', function() {
expect(sendgrid.SmtpapiHeaders).to.not.be.undefined;
});
it('version should be set', function() {
expect(sendgrid.version).to.equal("0.3.0-rc.1.6");
});
it('SMTP_PORT should be set and settable', function() {
expect(sendgrid.port).to.equal(587);
sendgrid.port = 465;
expect(sendgrid.port).to.equal(465);
});
describe('#send', function() {
var payload, mock, webApi, postParams, postParamsString;
beforeEach(function() {
payload = Object.create(default_payload);
webApi = nock('https://sendgrid.com')
.filteringRequestBody(function(path) {
postParamsString = path;
return '*';
})
.post('/api/mail.send.json', '*');
});
it('has an optional callback', function(done) {
expect(function() {
sendgrid.send(payload);
}).to.not.throw(Error);
done();
});
it('reports errors to the user', function(done) {
mock = webApi.reply(500, { message: "error", errors: ["some error"] });
sendgrid.send({}, function(err, json) {
expect(err.stack);
expect(err.message).to.equal("some error");
done();
});
});
it('reports http errors to the user', function(done) {
var https = require('https')
, realRequest = https.request
, stub;
function fakeRequest(options, cb) {
var req = realRequest(options, cb);
process.nextTick(function() {
req.emit('error', 'some http error');
});
return req;
}
stub = sinon.stub(https, 'request', fakeRequest);
sendgrid.send({}, function(err, json) {
expect(err).to.equal("some http error");
https.request.restore();
done();
});
});
it("returns success if message is 'success'", function(done) {
mock = webApi.reply(200, { message: "success" });
sendgrid.send({}, function(err, json) {
expect(err).to.be.null;
expect(json['message']).to.equal('success');
done();
});
});
it('sends the basic message parameters', function(done) {
mock = webApi.reply(200, { message: "success" });
sendgrid.send(payload, function(err, json) {
expect(postParamsString).to.include(API_USER);
expect(postParamsString).to.include(API_KEY);
expect(postParamsString).to.include(default_payload.to);
expect(postParamsString).to.include(default_payload.from);
expect(postParamsString).to.include(default_payload.subject);
expect(postParamsString).to.include(default_payload.text);
expect(postParamsString).to.include(default_payload.html);
done();
});
});
it('supports an optional toname and fromname', function(done) {
mock = webApi.reply(200, { message: "success" });
payload.toname = "to name";
payload.fromname= "from name";
sendgrid.send(payload, function(err, json) {
expect(postParamsString).to.include('to name');
expect(postParamsString).to.include('from name');
done();
});
});
it('encodes unicode strings in parameters', function(done) {
mock = webApi.reply(200, { message: "success" });
payload.subject = "A unicode ✔ subject";
sendgrid.send(payload, function(err, json) {
var encodedCheckmark = '✔';
expect(postParamsString).to.include(encodedCheckmark);
done();
});
});
afterEach(function() {
if(mock) expect(mock.isDone()).to.be.true;
});
});
});