forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail-service-test.js
More file actions
158 lines (122 loc) · 5.25 KB
/
Copy pathemail-service-test.js
File metadata and controls
158 lines (122 loc) · 5.25 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* eslint-disable no-unused-expressions */
const EmailService = require('../../lib/services/email-service')
const path = require('path')
const sinon = require('sinon')
const chai = require('chai')
const expect = chai.expect
const sinonChai = require('sinon-chai')
chai.use(sinonChai)
chai.should()
const templatePath = path.join(__dirname, '../../default-templates/emails')
describe('Email Service', function () {
describe('EmailService constructor', () => {
it('should set up a nodemailer instance', () => {
const templatePath = '../../config/email-templates'
const config = {
host: 'smtp.gmail.com',
auth: {
user: 'alice@gmail.com',
pass: '12345'
}
}
const emailService = new EmailService(templatePath, config)
expect(emailService.mailer.options.host).to.equal('smtp.gmail.com')
expect(emailService.mailer).to.respondTo('sendMail')
expect(emailService.templatePath).to.equal(templatePath)
})
it('should init a sender address if explicitly passed in', () => {
const sender = 'Solid Server <solid@databox.me>'
const config = { host: 'smtp.gmail.com', auth: {}, sender }
const emailService = new EmailService(templatePath, config)
expect(emailService.sender).to.equal(sender)
})
it('should construct a default sender if not passed in', () => {
const config = { host: 'databox.me', auth: {} }
const emailService = new EmailService(templatePath, config)
expect(emailService.sender).to.equal('no-reply@databox.me')
})
})
describe('sendMail()', () => {
it('passes through the sendMail call to the initialized mailer', () => {
const sendMail = sinon.stub().returns(Promise.resolve())
const config = { host: 'databox.me', auth: {} }
const emailService = new EmailService(templatePath, config)
emailService.mailer.sendMail = sendMail
const email = { subject: 'Test' }
return emailService.sendMail(email)
.then(() => {
expect(sendMail).to.have.been.calledWith(email)
})
})
it('uses the provided from:, if present', () => {
const config = { host: 'databox.me', auth: {} }
const emailService = new EmailService(templatePath, config)
const email = { subject: 'Test', from: 'alice@example.com' }
emailService.mailer.sendMail = (email) => { return Promise.resolve(email) }
return emailService.sendMail(email)
.then(email => {
expect(email.from).to.equal('alice@example.com')
})
})
it('uses the default sender if a from: is not provided', () => {
const config = { host: 'databox.me', auth: {}, sender: 'solid@example.com' }
const emailService = new EmailService(templatePath, config)
const email = { subject: 'Test', from: null }
emailService.mailer.sendMail = (email) => { return Promise.resolve(email) }
return emailService.sendMail(email)
.then(email => {
expect(email.from).to.equal(config.sender)
})
})
})
describe('templatePathFor()', () => {
it('should compose filename based on base path and template name', () => {
const config = { host: 'databox.me', auth: {} }
const templatePath = '../../config/email-templates'
const emailService = new EmailService(templatePath, config)
const templateFile = emailService.templatePathFor('welcome')
expect(templateFile.endsWith('email-templates/welcome'))
})
})
describe('readTemplate()', () => {
it('should read a template if it exists', () => {
const config = { host: 'databox.me', auth: {} }
const emailService = new EmailService(templatePath, config)
const template = emailService.readTemplate('welcome')
expect(template).to.respondTo('render')
})
it('should throw an error if a template does not exist', () => {
const config = { host: 'databox.me', auth: {} }
const emailService = new EmailService(templatePath, config)
expect(() => { emailService.readTemplate('invalid-template') })
.to.throw(/Cannot find email template/)
})
})
describe('sendWithTemplate()', () => {
it('should reject with error if template does not exist', done => {
const config = { host: 'databox.me', auth: {} }
const emailService = new EmailService(templatePath, config)
const data = {}
emailService.sendWithTemplate('invalid-template', data)
.catch(error => {
expect(error.message.startsWith('Cannot find email template'))
.to.be.true
done()
})
})
it('should render an email from template and send it', () => {
const config = { host: 'databox.me', auth: {} }
const emailService = new EmailService(templatePath, config)
emailService.sendMail = (email) => { return Promise.resolve(email) }
emailService.sendMail = sinon.spy(emailService, 'sendMail')
const data = { webid: 'https://alice.example.com#me' }
return emailService.sendWithTemplate('welcome', data)
.then(renderedEmail => {
expect(emailService.sendMail).to.be.called
expect(renderedEmail.subject).to.exist
expect(renderedEmail.text.endsWith('Your Web Id: https://alice.example.com#me'))
.to.be.true
})
})
})
})