2014-03-04 13:10:06 -05:00
|
|
|
should = require('chai').should()
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
path = require('path')
|
|
|
|
sinon = require('sinon')
|
|
|
|
modulePath = path.join __dirname, "../../../../app/js/Features/Email/EmailSender.js"
|
|
|
|
expect = require("chai").expect
|
|
|
|
|
2016-03-09 07:51:19 -05:00
|
|
|
describe "EmailSender", ->
|
2014-03-04 13:10:06 -05:00
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
|
2017-01-14 09:52:32 -05:00
|
|
|
@RateLimiter =
|
|
|
|
addCount:sinon.stub()
|
|
|
|
|
2014-03-05 06:31:40 -05:00
|
|
|
@settings =
|
|
|
|
email:
|
2014-03-06 10:37:25 -05:00
|
|
|
transport: "ses"
|
2014-03-12 07:43:36 -04:00
|
|
|
parameters:
|
|
|
|
AWSAccessKeyID: "key"
|
|
|
|
AWSSecretKey: "secret"
|
2014-03-05 06:31:40 -05:00
|
|
|
fromAddress: "bob@bob.com"
|
|
|
|
replyToAddress: "sally@gmail.com"
|
|
|
|
|
2014-03-06 10:37:25 -05:00
|
|
|
@sesClient =
|
|
|
|
sendMail: sinon.stub()
|
2017-01-14 09:52:32 -05:00
|
|
|
|
2014-03-06 10:37:25 -05:00
|
|
|
@ses =
|
|
|
|
createTransport: => @sesClient
|
2017-01-14 09:52:32 -05:00
|
|
|
|
|
|
|
|
2014-03-04 13:10:06 -05:00
|
|
|
@sender = SandboxedModule.require modulePath, requires:
|
2014-03-06 10:37:25 -05:00
|
|
|
'nodemailer': @ses
|
2014-03-04 13:10:06 -05:00
|
|
|
"settings-sharelatex":@settings
|
2017-01-14 09:52:32 -05:00
|
|
|
'../../infrastructure/RateLimiter':@RateLimiter
|
2014-03-06 10:37:25 -05:00
|
|
|
"logger-sharelatex":
|
2014-03-04 13:10:06 -05:00
|
|
|
log:->
|
|
|
|
warn:->
|
|
|
|
err:->
|
2016-03-09 07:51:19 -05:00
|
|
|
"../../infrastructure/Metrics": inc:->
|
2016-10-03 10:25:38 -04:00
|
|
|
|
2016-03-09 07:51:19 -05:00
|
|
|
|
2014-03-04 13:10:06 -05:00
|
|
|
|
2014-03-05 06:31:40 -05:00
|
|
|
@opts =
|
|
|
|
to: "bob@bob.com"
|
|
|
|
subject: "new email"
|
|
|
|
html: "<hello></hello>"
|
|
|
|
|
2014-03-04 13:10:06 -05:00
|
|
|
describe "sendEmail", ->
|
|
|
|
|
|
|
|
it "should set the properties on the email to send", (done)->
|
2014-03-06 10:37:25 -05:00
|
|
|
@sesClient.sendMail.callsArgWith(1)
|
2014-03-04 13:10:06 -05:00
|
|
|
|
2014-03-05 06:31:40 -05:00
|
|
|
@sender.sendEmail @opts, =>
|
2014-03-06 10:37:25 -05:00
|
|
|
args = @sesClient.sendMail.args[0][0]
|
2014-03-12 07:43:36 -04:00
|
|
|
args.html.should.equal @opts.html
|
2014-03-05 06:31:40 -05:00
|
|
|
args.to.should.equal @opts.to
|
|
|
|
args.subject.should.equal @opts.subject
|
2014-03-04 13:10:06 -05:00
|
|
|
done()
|
|
|
|
|
2017-03-27 11:17:38 -04:00
|
|
|
it "should return a non-specific error", (done)->
|
2014-03-06 10:37:25 -05:00
|
|
|
@sesClient.sendMail.callsArgWith(1, "error")
|
2014-03-04 13:10:06 -05:00
|
|
|
@sender.sendEmail {}, (err)=>
|
2017-03-27 11:17:38 -04:00
|
|
|
err.should.exist
|
|
|
|
err.toString().should.equal 'Error: Cannot send email'
|
2014-03-04 13:10:06 -05:00
|
|
|
done()
|
2014-03-05 06:31:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
it "should use the from address from settings", (done)->
|
2014-03-06 10:37:25 -05:00
|
|
|
@sesClient.sendMail.callsArgWith(1)
|
2014-03-05 06:31:40 -05:00
|
|
|
|
|
|
|
@sender.sendEmail @opts, =>
|
2014-03-06 10:37:25 -05:00
|
|
|
args = @sesClient.sendMail.args[0][0]
|
2014-03-05 06:31:40 -05:00
|
|
|
args.from.should.equal @settings.email.fromAddress
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should use the reply to address from settings", (done)->
|
2014-03-06 10:37:25 -05:00
|
|
|
@sesClient.sendMail.callsArgWith(1)
|
2014-03-05 06:31:40 -05:00
|
|
|
|
|
|
|
@sender.sendEmail @opts, =>
|
2014-03-06 10:37:25 -05:00
|
|
|
args = @sesClient.sendMail.args[0][0]
|
2014-03-05 06:31:40 -05:00
|
|
|
args.replyTo.should.equal @settings.email.replyToAddress
|
|
|
|
done()
|
|
|
|
|
2014-03-05 08:33:41 -05:00
|
|
|
|
|
|
|
it "should use the reply to address in options as an override", (done)->
|
2014-03-06 10:37:25 -05:00
|
|
|
@sesClient.sendMail.callsArgWith(1)
|
2014-03-05 08:33:41 -05:00
|
|
|
|
|
|
|
@opts.replyTo = "someone@else.com"
|
|
|
|
@sender.sendEmail @opts, =>
|
2014-03-06 10:37:25 -05:00
|
|
|
args = @sesClient.sendMail.args[0][0]
|
2014-03-05 08:33:41 -05:00
|
|
|
args.replyTo.should.equal @opts.replyTo
|
|
|
|
done()
|
2016-10-03 10:25:38 -04:00
|
|
|
|
2017-01-14 09:52:32 -05:00
|
|
|
|
|
|
|
it "should not send an email when the rate limiter says no", (done)->
|
|
|
|
@opts.sendingUser_id = "12321312321"
|
|
|
|
@RateLimiter.addCount.callsArgWith(1, null, false)
|
|
|
|
@sender.sendEmail @opts, =>
|
|
|
|
@sesClient.sendMail.called.should.equal false
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should send the email when the rate limtier says continue", (done)->
|
|
|
|
@sesClient.sendMail.callsArgWith(1)
|
|
|
|
@opts.sendingUser_id = "12321312321"
|
|
|
|
@RateLimiter.addCount.callsArgWith(1, null, true)
|
|
|
|
@sender.sendEmail @opts, =>
|
|
|
|
@sesClient.sendMail.called.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should not check the rate limiter when there is no sendingUser_id", (done)->
|
|
|
|
@sesClient.sendMail.callsArgWith(1)
|
|
|
|
@sender.sendEmail @opts, =>
|
|
|
|
@sesClient.sendMail.called.should.equal true
|
|
|
|
@RateLimiter.addCount.called.should.equal false
|
|
|
|
done()
|
|
|
|
|
2016-10-03 10:25:38 -04:00
|
|
|
describe 'with plain-text email content', () ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@opts.text = "hello there"
|
|
|
|
|
|
|
|
it "should set the text property on the email to send", (done)->
|
|
|
|
@sesClient.sendMail.callsArgWith(1)
|
|
|
|
|
|
|
|
@sender.sendEmail @opts, =>
|
|
|
|
args = @sesClient.sendMail.args[0][0]
|
|
|
|
args.html.should.equal @opts.html
|
|
|
|
args.text.should.equal @opts.text
|
|
|
|
args.to.should.equal @opts.to
|
|
|
|
args.subject.should.equal @opts.subject
|
|
|
|
done()
|