UserEmailsConfirmationHandlerTests.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/User/UserEmailsConfirmationHandler"
  7. expect = require("chai").expect
  8. Errors = require "../../../../app/js/Features/Errors/Errors"
  9. EmailHelper = require "../../../../app/js/Features/Helpers/EmailHelper"
  10. describe "UserEmailsConfirmationHandler", ->
  11. beforeEach ->
  12. @UserEmailsConfirmationHandler = SandboxedModule.require modulePath, requires:
  13. "settings-sharelatex": @settings =
  14. siteUrl: "emails.example.com"
  15. "logger-sharelatex": @logger = { log: sinon.stub() }
  16. "../Security/OneTimeTokenHandler": @OneTimeTokenHandler = {}
  17. "../Errors/Errors": Errors
  18. "./UserUpdater": @UserUpdater = {}
  19. "../Email/EmailHandler": @EmailHandler = {}
  20. "../Helpers/EmailHelper": EmailHelper
  21. @user_id = "mock-user-id"
  22. @email = "mock@example.com"
  23. @callback = sinon.stub()
  24. describe "sendConfirmationEmail", ->
  25. beforeEach ->
  26. @OneTimeTokenHandler.getNewToken = sinon.stub().yields(null, @token = "new-token")
  27. @EmailHandler.sendEmail = sinon.stub().yields()
  28. describe 'successfully', ->
  29. beforeEach ->
  30. @UserEmailsConfirmationHandler.sendConfirmationEmail @user_id, @email, @callback
  31. it "should generate a token for the user which references their id and email", ->
  32. @OneTimeTokenHandler.getNewToken
  33. .calledWith(
  34. 'email_confirmation',
  35. {@user_id, @email},
  36. { expiresIn: 365 * 24 * 60 * 60 }
  37. )
  38. .should.equal true
  39. it 'should send an email to the user', ->
  40. @EmailHandler.sendEmail
  41. .calledWith('confirmEmail', {
  42. to: @email,
  43. confirmEmailUrl: 'emails.example.com/user/emails/confirm?token=new-token'
  44. })
  45. .should.equal true
  46. it 'should call the callback', ->
  47. @callback.called.should.equal true
  48. describe 'with invalid email', ->
  49. beforeEach ->
  50. @UserEmailsConfirmationHandler.sendConfirmationEmail @user_id, '!"£$%^&*()', @callback
  51. it 'should return an error', ->
  52. @callback.calledWith(sinon.match.instanceOf(Error)).should.equal true
  53. describe 'a custom template', ->
  54. beforeEach ->
  55. @UserEmailsConfirmationHandler.sendConfirmationEmail @user_id, @email, 'myCustomTemplate', @callback
  56. it 'should send an email with the given template', ->
  57. @EmailHandler.sendEmail
  58. .calledWith('myCustomTemplate')
  59. .should.equal true
  60. describe "confirmEmailFromToken", ->
  61. beforeEach ->
  62. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(
  63. null,
  64. {@user_id, @email}
  65. )
  66. @UserUpdater.confirmEmail = sinon.stub().yields()
  67. describe "successfully", ->
  68. beforeEach ->
  69. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  70. it "should call getValueFromTokenAndExpire", ->
  71. @OneTimeTokenHandler.getValueFromTokenAndExpire
  72. .calledWith('email_confirmation', @token)
  73. .should.equal true
  74. it "should confirm the email of the user_id", ->
  75. @UserUpdater.confirmEmail
  76. .calledWith(@user_id, @email)
  77. .should.equal true
  78. it "should call the callback", ->
  79. @callback.called.should.equal true
  80. describe 'with an expired token', ->
  81. beforeEach ->
  82. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(null, null)
  83. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  84. it "should call the callback with a NotFoundError", ->
  85. @callback.calledWith(sinon.match.instanceOf(Errors.NotFoundError)).should.equal true
  86. describe 'with no user_id in the token', ->
  87. beforeEach ->
  88. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(
  89. null,
  90. {@email}
  91. )
  92. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  93. it "should call the callback with a NotFoundError", ->
  94. @callback.calledWith(sinon.match.instanceOf(Errors.NotFoundError)).should.equal true
  95. describe 'with no email in the token', ->
  96. beforeEach ->
  97. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(
  98. null,
  99. {@user_id}
  100. )
  101. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  102. it "should call the callback with a NotFoundError", ->
  103. @callback.calledWith(sinon.match.instanceOf(Errors.NotFoundError)).should.equal true