forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail-service.js
More file actions
156 lines (121 loc) · 5.12 KB
/
email-service.js
File metadata and controls
156 lines (121 loc) · 5.12 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
const EmailService = require('../lib/models/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-email-templates')
describe('Email Service', function () {
describe('EmailService constructor', () => {
it('should set up a nodemailer instance', () => {
let templatePath = '../config/email-templates'
let config = {
host: 'smtp.gmail.com',
auth: {
user: 'alice@gmail.com',
pass: '12345'
}
}
let 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', () => {
let sender = 'Solid Server <solid@databox.me>'
let config = { host: 'smtp.gmail.com', auth: {}, sender }
let emailService = new EmailService(templatePath, config)
expect(emailService.sender).to.equal(sender)
})
it('should construct a default sender if not passed in', () => {
let config = { host: 'databox.me', auth: {} }
let 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', () => {
let sendMail = sinon.stub().returns(Promise.resolve())
let config = { host: 'databox.me', auth: {} }
let emailService = new EmailService(templatePath, config)
emailService.mailer.sendMail = sendMail
let email = { subject: 'Test' }
return emailService.sendMail(email)
.then(() => {
expect(sendMail).to.have.been.calledWith(email)
})
})
it('uses the provided from:, if present', () => {
let config = { host: 'databox.me', auth: {} }
let emailService = new EmailService(templatePath, config)
let 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', () => {
let config = { host: 'databox.me', auth: {}, sender: 'solid@example.com' }
let emailService = new EmailService(templatePath, config)
let 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', () => {
let config = { host: 'databox.me', auth: {} }
let templatePath = '../config/email-templates'
let emailService = new EmailService(templatePath, config)
let templateFile = emailService.templatePathFor('welcome')
expect(templateFile.endsWith('email-templates/welcome'))
})
})
describe('readTemplate()', () => {
it('should read a template if it exists', () => {
let config = { host: 'databox.me', auth: {} }
let emailService = new EmailService(templatePath, config)
let template = emailService.readTemplate('welcome')
expect(template).to.respondTo('render')
})
it('should throw an error if a template does not exist', () => {
let config = { host: 'databox.me', auth: {} }
let 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 => {
let config = { host: 'databox.me', auth: {} }
let emailService = new EmailService(templatePath, config)
let 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', () => {
let config = { host: 'databox.me', auth: {} }
let emailService = new EmailService(templatePath, config)
emailService.sendMail = (email) => { return Promise.resolve(email) }
emailService.sendMail = sinon.spy(emailService, 'sendMail')
let 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
})
})
})
})