UserEmailsConfirmationHandlerTests.coffee 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "./UserGetter": @UserGetter =
  20. getUser: sinon.stub().yields(null, @mockUser)
  21. "../Email/EmailHandler": @EmailHandler = {}
  22. "../Helpers/EmailHelper": EmailHelper
  23. @mockUser = _id: "mock-user-id"
  24. @user_id = @mockUser._id
  25. @email = "mock@example.com"
  26. @callback = sinon.stub()
  27. describe "sendConfirmationEmail", ->
  28. beforeEach ->
  29. @OneTimeTokenHandler.getNewToken = sinon.stub().yields(null, @token = "new-token")
  30. @EmailHandler.sendEmail = sinon.stub().yields()
  31. describe 'successfully', ->
  32. beforeEach ->
  33. @UserEmailsConfirmationHandler.sendConfirmationEmail @user_id, @email, @callback
  34. it "should generate a token for the user which references their id and email", ->
  35. @OneTimeTokenHandler.getNewToken
  36. .calledWith(
  37. 'email_confirmation',
  38. {@user_id, @email},
  39. { expiresIn: 365 * 24 * 60 * 60 }
  40. )
  41. .should.equal true
  42. it 'should send an email to the user', ->
  43. @EmailHandler.sendEmail
  44. .calledWith('confirmEmail', {
  45. to: @email,
  46. confirmEmailUrl: 'emails.example.com/user/emails/confirm?token=new-token'
  47. sendingUser_id: @user_id
  48. })
  49. .should.equal true
  50. it 'should call the callback', ->
  51. @callback.called.should.equal true
  52. describe 'with invalid email', ->
  53. beforeEach ->
  54. @UserEmailsConfirmationHandler.sendConfirmationEmail @user_id, '!"£$%^&*()', @callback
  55. it 'should return an error', ->
  56. @callback.calledWith(sinon.match.instanceOf(Error)).should.equal true
  57. describe 'a custom template', ->
  58. beforeEach ->
  59. @UserEmailsConfirmationHandler.sendConfirmationEmail @user_id, @email, 'myCustomTemplate', @callback
  60. it 'should send an email with the given template', ->
  61. @EmailHandler.sendEmail
  62. .calledWith('myCustomTemplate')
  63. .should.equal true
  64. describe "confirmEmailFromToken", ->
  65. beforeEach ->
  66. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(
  67. null,
  68. {@user_id, @email}
  69. )
  70. @UserUpdater.confirmEmail = sinon.stub().yields()
  71. describe "successfully", ->
  72. beforeEach ->
  73. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  74. it "should call getValueFromTokenAndExpire", ->
  75. @OneTimeTokenHandler.getValueFromTokenAndExpire
  76. .calledWith('email_confirmation', @token)
  77. .should.equal true
  78. it "should confirm the email of the user_id", ->
  79. @UserUpdater.confirmEmail
  80. .calledWith(@user_id, @email)
  81. .should.equal true
  82. it "should call the callback", ->
  83. @callback.called.should.equal true
  84. describe 'with an expired token', ->
  85. beforeEach ->
  86. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(null, null)
  87. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  88. it "should call the callback with a NotFoundError", ->
  89. @callback.calledWith(sinon.match.instanceOf(Errors.NotFoundError)).should.equal true
  90. describe 'with no user_id in the token', ->
  91. beforeEach ->
  92. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(
  93. null,
  94. {@email}
  95. )
  96. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  97. it "should call the callback with a NotFoundError", ->
  98. @callback.calledWith(sinon.match.instanceOf(Errors.NotFoundError)).should.equal true
  99. describe 'with no email in the token', ->
  100. beforeEach ->
  101. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(
  102. null,
  103. {@user_id}
  104. )
  105. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  106. it "should call the callback with a NotFoundError", ->
  107. @callback.calledWith(sinon.match.instanceOf(Errors.NotFoundError)).should.equal true
  108. describe 'with no user found', ->
  109. beforeEach ->
  110. @UserGetter.getUser.yields(null, null)
  111. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  112. it "should call the callback with a NotFoundError", ->
  113. @callback.calledWith(sinon.match.instanceOf(Errors.NotFoundError)).should.equal true