added settings check on sending lifcycle emails out

This commit is contained in:
Henry Oswald 2014-03-05 11:22:52 +00:00
parent eb92e5832b
commit 5bacb2d784
4 changed files with 52 additions and 20 deletions

View file

@ -1,14 +1,14 @@
EmailTemplator = require "./EmailTemplator"
settings = require("settings-sharelatex")
EmailBuilder = require "./EmailBuilder"
EmailSender = require "./EmailSender"
module.exports =
sendEmail : (emailType, opts, callback)->
email = EmailTemplator.buildEmail emailType, opts
email = EmailBuilder.buildEmail emailType, opts
if email.type == "lifecycle" and !settings.email.lifecycle
return callback()
opts.html = email.html
opts.subject = email.subject
EmailSender.sendEmail opts, (err)->
callback(err)
# module.exports.sendEmail "welcome", {first_name:"henry", to:"henry.oswald@gmail.com"}, ->

View file

@ -4,8 +4,8 @@ Settings = require('settings-sharelatex')
metrics = require("../../infrastructure/Metrics")
ses = require('node-ses')
if Settings.ses?.key? and Settings.ses?.key != "" and Settings.ses?.secret? and Settings.ses?.secret != ""
client = ses.createClient({ key: Settings.ses.key, secret: Settings.ses.secret });
if Settings.email?.ses? and Settings.email.ses?.key? and Settings.email.ses?.key != "" and Settings.email.ses?.secret? and Settings.email.ses?.secret != ""
client = ses.createClient({ key: Settings.email.ses.key, secret: Settings.email.ses.secret });
else
logger.warn "AWS SES credentials are not configured. No emails will be sent."
client =

View file

@ -26,6 +26,7 @@ module.exports =
secret: ""
bucketName : ""
# Databases
# ---------
mongo:
@ -155,6 +156,11 @@ module.exports =
# ShareLaTeX's help desk is provided by tenderapp.com
# tenderUrl: ""
#
# email:
# fromAddress: ""
# replyTo: ""
# lifecycle: false
# ShareLaTeX uses Amazon's SES api to send transactional emails.
# Uncomment these lines and provide your credentials to be able to send emails.
# ses:

View file

@ -10,13 +10,14 @@ describe "EmailHandler", ->
beforeEach ->
@settings = {}
@EmailTemplator =
@settings =
email:{}
@EmailBuilder =
buildEmail:sinon.stub()
@EmailSender =
sendEmail:sinon.stub()
@EmailHandler = SandboxedModule.require modulePath, requires:
"./EmailTemplator":@EmailTemplator
"./EmailBuilder":@EmailBuilder
"./EmailSender":@EmailSender
"settings-sharelatex":@settings
"logger-sharelatex": log:->
@ -26,7 +27,7 @@ describe "EmailHandler", ->
describe "send email", ->
it "should use the correct options", (done)->
@EmailTemplator.buildEmail.returns({html:@html})
@EmailBuilder.buildEmail.returns({html:@html})
@EmailSender.sendEmail.callsArgWith(1)
opts =
@ -36,10 +37,8 @@ describe "EmailHandler", ->
args.html.should.equal @html
done()
it "should return the erroor", (done)->
@EmailTemplator.buildEmail.returns({html:@html})
@EmailBuilder.buildEmail.returns({html:@html})
@EmailSender.sendEmail.callsArgWith(1, "error")
opts =
@ -48,3 +47,30 @@ describe "EmailHandler", ->
@EmailHandler.sendEmail "welcome", opts, (err)=>
err.should.equal "error"
done()
it "should not send an email if lifecycle is not enabled", (done)->
@settings.email.lifecycle = false
@EmailBuilder.buildEmail.returns({type:"lifecycle"})
@EmailHandler.sendEmail "welcome", {}, =>
@EmailSender.sendEmail.called.should.equal false
done()
it "should send an email if lifecycle is not enabled but the type is notification", (done)->
@settings.email.lifecycle = false
@EmailBuilder.buildEmail.returns({type:"notification"})
@EmailSender.sendEmail.callsArgWith(1)
opts =
to: "bob@bob.com"
@EmailHandler.sendEmail "welcome", opts, =>
@EmailSender.sendEmail.called.should.equal true
done()
it "should send lifecycle email if it is enabled", (done)->
@settings.email.lifecycle = true
@EmailBuilder.buildEmail.returns({type:"lifecycle"})
@EmailSender.sendEmail.callsArgWith(1)
opts =
to: "bob@bob.com"
@EmailHandler.sendEmail "welcome", opts, =>
@EmailSender.sendEmail.called.should.equal true
done()