|
1
|
|
|
var chai = require('chai') |
|
2
|
|
|
var server = require('../../app') |
|
3
|
|
|
/* eslint-disable no-unused-vars */ |
|
4
|
|
|
var should = chai.should() |
|
5
|
|
|
var nock = require('nock') |
|
6
|
|
|
var nodemailer = require('nodemailer') |
|
7
|
|
|
var sinon = require('sinon') |
|
8
|
|
|
var expect = require('chai').expect |
|
9
|
|
|
|
|
10
|
|
|
describe('Test Email API', function () { |
|
11
|
|
|
it('it should send an email', function (done) { |
|
12
|
|
|
var transportStub = { sendMail: function (options, callback) { callback(false, true) } } |
|
13
|
|
|
var sendMailSpy = sinon.spy(transportStub, 'sendMail') |
|
|
|
|
|
|
14
|
|
|
var mailerStub = sinon.stub(nodemailer, 'createTransport').returns(transportStub) |
|
15
|
|
|
|
|
16
|
|
|
var mail = { |
|
17
|
|
|
to: '[email protected]', |
|
18
|
|
|
subject: 'Hello ✔', |
|
19
|
|
|
text: 'Hello world 🐴', |
|
20
|
|
|
html: '<b>Hello world 🐴</b>' |
|
21
|
|
|
} |
|
22
|
|
|
chai.request(server) |
|
23
|
|
|
.post('/api/email') |
|
24
|
|
|
.send(mail) |
|
25
|
|
|
.end(function (err, res) { |
|
26
|
|
|
should.not.exist(err) |
|
27
|
|
|
res.should.have.status(200) |
|
28
|
|
|
res.body.should.be.a('object') |
|
29
|
|
|
res.body.should.have.property('message').eql('Message sent') |
|
30
|
|
|
mailerStub.restore() |
|
31
|
|
|
done() |
|
32
|
|
|
}) |
|
33
|
|
|
}) |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
it('it should send an email', function (done) { |
|
37
|
|
|
var transportStub = { sendMail: function (options, callback) { callback(true, false) } } |
|
38
|
|
|
var sendMailSpy = sinon.spy(transportStub, 'sendMail') |
|
|
|
|
|
|
39
|
|
|
var mailerStub = sinon.stub(nodemailer, 'createTransport').returns(transportStub) |
|
40
|
|
|
|
|
41
|
|
|
var mail = { |
|
42
|
|
|
to: '[email protected]', |
|
43
|
|
|
subject: 'Hello ✔', |
|
44
|
|
|
text: 'Hello world 🐴', |
|
45
|
|
|
html: '<b>Hello world 🐴</b>' |
|
46
|
|
|
} |
|
47
|
|
|
chai.request(server) |
|
48
|
|
|
.post('/api/email') |
|
49
|
|
|
.send(mail) |
|
50
|
|
|
.end(function (err, res) { |
|
51
|
|
|
should.exist(err) |
|
52
|
|
|
res.should.have.status(404) |
|
53
|
|
|
res.body.should.be.a('object') |
|
54
|
|
|
res.body.should.have.property('message').eql('Error creating message') |
|
55
|
|
|
mailerStub.restore() |
|
56
|
|
|
done() |
|
57
|
|
|
}) |
|
58
|
|
|
}) |
|
59
|
|
|
}) |
|
60
|
|
|
|