EmailHandlerTests.coffee 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "../../../../app/js/Features/Email/EmailHandler"
  7. expect = require("chai").expect
  8. describe "EmailHandler", ->
  9. beforeEach ->
  10. @settings =
  11. email:{}
  12. @EmailBuilder =
  13. buildEmail:sinon.stub()
  14. @EmailSender =
  15. sendEmail:sinon.stub()
  16. @EmailHandler = SandboxedModule.require modulePath, requires:
  17. "./EmailBuilder":@EmailBuilder
  18. "./EmailSender":@EmailSender
  19. "settings-sharelatex":@settings
  20. "logger-sharelatex": log:->
  21. @html = "<html>hello</html>"
  22. describe "send email", ->
  23. it "should use the correct options", (done)->
  24. @EmailBuilder.buildEmail.returns({html:@html})
  25. @EmailSender.sendEmail.callsArgWith(1)
  26. opts =
  27. to: "bob@bob.com"
  28. @EmailHandler.sendEmail "welcome", opts, =>
  29. args = @EmailSender.sendEmail.args[0][0]
  30. args.html.should.equal @html
  31. done()
  32. it "should return the erroor", (done)->
  33. @EmailBuilder.buildEmail.returns({html:@html})
  34. @EmailSender.sendEmail.callsArgWith(1, "error")
  35. opts =
  36. to: "bob@bob.com"
  37. subject:"hello bob"
  38. @EmailHandler.sendEmail "welcome", opts, (err)=>
  39. err.should.equal "error"
  40. done()
  41. it "should not send an email if lifecycle is not enabled", (done)->
  42. @settings.email.lifecycle = false
  43. @EmailBuilder.buildEmail.returns({type:"lifecycle"})
  44. @EmailHandler.sendEmail "welcome", {}, =>
  45. @EmailSender.sendEmail.called.should.equal false
  46. done()
  47. it "should send an email if lifecycle is not enabled but the type is notification", (done)->
  48. @settings.email.lifecycle = false
  49. @EmailBuilder.buildEmail.returns({type:"notification"})
  50. @EmailSender.sendEmail.callsArgWith(1)
  51. opts =
  52. to: "bob@bob.com"
  53. @EmailHandler.sendEmail "welcome", opts, =>
  54. @EmailSender.sendEmail.called.should.equal true
  55. done()
  56. it "should send lifecycle email if it is enabled", (done)->
  57. @settings.email.lifecycle = true
  58. @EmailBuilder.buildEmail.returns({type:"lifecycle"})
  59. @EmailSender.sendEmail.callsArgWith(1)
  60. opts =
  61. to: "bob@bob.com"
  62. @EmailHandler.sendEmail "welcome", opts, =>
  63. @EmailSender.sendEmail.called.should.equal true
  64. done()
  65. describe 'with plain-text email content', () ->
  66. beforeEach ->
  67. @text = "hello there"
  68. it 'should pass along the text field', (done) ->
  69. @EmailBuilder.buildEmail.returns({html: @html, text: @text})
  70. @EmailSender.sendEmail.callsArgWith(1)
  71. opts =
  72. to: "bob@bob.com"
  73. @EmailHandler.sendEmail "welcome", opts, =>
  74. args = @EmailSender.sendEmail.args[0][0]
  75. args.html.should.equal @html
  76. args.text.should.equal @text
  77. done()