UserEmailsConfirmationHandlerTests.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const should = require('chai').should()
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. const path = require('path')
  17. const sinon = require('sinon')
  18. const modulePath = path.join(
  19. __dirname,
  20. '../../../../app/src/Features/User/UserEmailsConfirmationHandler'
  21. )
  22. const { expect } = require('chai')
  23. const Errors = require('../../../../app/src/Features/Errors/Errors')
  24. const EmailHelper = require('../../../../app/src/Features/Helpers/EmailHelper')
  25. describe('UserEmailsConfirmationHandler', function() {
  26. beforeEach(function() {
  27. this.UserEmailsConfirmationHandler = SandboxedModule.require(modulePath, {
  28. globals: {
  29. console: console
  30. },
  31. requires: {
  32. 'settings-sharelatex': (this.settings = {
  33. siteUrl: 'emails.example.com'
  34. }),
  35. 'logger-sharelatex': (this.logger = { log: sinon.stub() }),
  36. '../Security/OneTimeTokenHandler': (this.OneTimeTokenHandler = {}),
  37. '../Errors/Errors': Errors,
  38. './UserUpdater': (this.UserUpdater = {}),
  39. './UserGetter': (this.UserGetter = {
  40. getUser: sinon.stub().yields(null, this.mockUser)
  41. }),
  42. '../Email/EmailHandler': (this.EmailHandler = {}),
  43. '../Helpers/EmailHelper': EmailHelper
  44. }
  45. })
  46. this.mockUser = {
  47. _id: 'mock-user-id',
  48. email: 'mock@example.com',
  49. emails: [{ email: 'mock@example.com' }]
  50. }
  51. this.user_id = this.mockUser._id
  52. this.email = this.mockUser.email
  53. return (this.callback = sinon.stub())
  54. })
  55. describe('sendConfirmationEmail', function() {
  56. beforeEach(function() {
  57. this.OneTimeTokenHandler.getNewToken = sinon
  58. .stub()
  59. .yields(null, (this.token = 'new-token'))
  60. return (this.EmailHandler.sendEmail = sinon.stub().yields())
  61. })
  62. describe('successfully', function() {
  63. beforeEach(function() {
  64. return this.UserEmailsConfirmationHandler.sendConfirmationEmail(
  65. this.user_id,
  66. this.email,
  67. this.callback
  68. )
  69. })
  70. it('should generate a token for the user which references their id and email', function() {
  71. return this.OneTimeTokenHandler.getNewToken
  72. .calledWith(
  73. 'email_confirmation',
  74. { user_id: this.user_id, email: this.email },
  75. { expiresIn: 365 * 24 * 60 * 60 }
  76. )
  77. .should.equal(true)
  78. })
  79. it('should send an email to the user', function() {
  80. return this.EmailHandler.sendEmail
  81. .calledWith('confirmEmail', {
  82. to: this.email,
  83. confirmEmailUrl:
  84. 'emails.example.com/user/emails/confirm?token=new-token',
  85. sendingUser_id: this.user_id
  86. })
  87. .should.equal(true)
  88. })
  89. it('should call the callback', function() {
  90. return this.callback.called.should.equal(true)
  91. })
  92. })
  93. describe('with invalid email', function() {
  94. beforeEach(function() {
  95. return this.UserEmailsConfirmationHandler.sendConfirmationEmail(
  96. this.user_id,
  97. '!"£$%^&*()',
  98. this.callback
  99. )
  100. })
  101. it('should return an error', function() {
  102. return this.callback
  103. .calledWith(sinon.match.instanceOf(Error))
  104. .should.equal(true)
  105. })
  106. })
  107. describe('a custom template', function() {
  108. beforeEach(function() {
  109. return this.UserEmailsConfirmationHandler.sendConfirmationEmail(
  110. this.user_id,
  111. this.email,
  112. 'myCustomTemplate',
  113. this.callback
  114. )
  115. })
  116. it('should send an email with the given template', function() {
  117. return this.EmailHandler.sendEmail
  118. .calledWith('myCustomTemplate')
  119. .should.equal(true)
  120. })
  121. })
  122. })
  123. describe('confirmEmailFromToken', function() {
  124. beforeEach(function() {
  125. this.OneTimeTokenHandler.getValueFromTokenAndExpire = sinon
  126. .stub()
  127. .yields(null, { user_id: this.user_id, email: this.email })
  128. return (this.UserUpdater.confirmEmail = sinon.stub().yields())
  129. })
  130. describe('successfully', function() {
  131. beforeEach(function() {
  132. return this.UserEmailsConfirmationHandler.confirmEmailFromToken(
  133. (this.token = 'mock-token'),
  134. this.callback
  135. )
  136. })
  137. it('should call getValueFromTokenAndExpire', function() {
  138. return this.OneTimeTokenHandler.getValueFromTokenAndExpire
  139. .calledWith('email_confirmation', this.token)
  140. .should.equal(true)
  141. })
  142. it('should confirm the email of the user_id', function() {
  143. return this.UserUpdater.confirmEmail
  144. .calledWith(this.user_id, this.email)
  145. .should.equal(true)
  146. })
  147. it('should call the callback', function() {
  148. return this.callback.called.should.equal(true)
  149. })
  150. })
  151. describe('with an expired token', function() {
  152. beforeEach(function() {
  153. this.OneTimeTokenHandler.getValueFromTokenAndExpire = sinon
  154. .stub()
  155. .yields(null, null)
  156. return this.UserEmailsConfirmationHandler.confirmEmailFromToken(
  157. (this.token = 'mock-token'),
  158. this.callback
  159. )
  160. })
  161. it('should call the callback with a NotFoundError', function() {
  162. return this.callback
  163. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  164. .should.equal(true)
  165. })
  166. })
  167. describe('with no user_id in the token', function() {
  168. beforeEach(function() {
  169. this.OneTimeTokenHandler.getValueFromTokenAndExpire = sinon
  170. .stub()
  171. .yields(null, { email: this.email })
  172. return this.UserEmailsConfirmationHandler.confirmEmailFromToken(
  173. (this.token = 'mock-token'),
  174. this.callback
  175. )
  176. })
  177. it('should call the callback with a NotFoundError', function() {
  178. return this.callback
  179. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  180. .should.equal(true)
  181. })
  182. })
  183. describe('with no email in the token', function() {
  184. beforeEach(function() {
  185. this.OneTimeTokenHandler.getValueFromTokenAndExpire = sinon
  186. .stub()
  187. .yields(null, { user_id: this.user_id })
  188. return this.UserEmailsConfirmationHandler.confirmEmailFromToken(
  189. (this.token = 'mock-token'),
  190. this.callback
  191. )
  192. })
  193. it('should call the callback with a NotFoundError', function() {
  194. return this.callback
  195. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  196. .should.equal(true)
  197. })
  198. })
  199. describe('with no user found', function() {
  200. beforeEach(function() {
  201. this.UserGetter.getUser.yields(null, null)
  202. return this.UserEmailsConfirmationHandler.confirmEmailFromToken(
  203. (this.token = 'mock-token'),
  204. this.callback
  205. )
  206. })
  207. it('should call the callback with a NotFoundError', function() {
  208. return this.callback
  209. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  210. .should.equal(true)
  211. })
  212. })
  213. describe('with secondary email missing on user', function() {
  214. beforeEach(function() {
  215. this.OneTimeTokenHandler.getValueFromTokenAndExpire = sinon
  216. .stub()
  217. .yields(null, { user_id: this.user_id, email: 'deleted@email.com' })
  218. return this.UserEmailsConfirmationHandler.confirmEmailFromToken(
  219. (this.token = 'mock-token'),
  220. this.callback
  221. )
  222. })
  223. it('should call the callback with a NotFoundError', function() {
  224. console.log(this.callback.lastCall.args)
  225. return this.callback
  226. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  227. .should.equal(true)
  228. })
  229. })
  230. })
  231. })