UserRegistrationHandlerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* eslint-disable
  2. node/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. const path = require('path')
  17. const modulePath = path.join(
  18. __dirname,
  19. '../../../../app/src/Features/User/UserRegistrationHandler'
  20. )
  21. const sinon = require('sinon')
  22. const { expect } = require('chai')
  23. const EmailHelper = require('../../../../app/src/Features/Helpers/EmailHelper')
  24. describe('UserRegistrationHandler', function () {
  25. beforeEach(function () {
  26. this.analyticsId = '123456'
  27. this.user = {
  28. _id: (this.user_id = '31j2lk21kjl'),
  29. analyticsId: this.analyticsId,
  30. }
  31. this.User = { updateOne: sinon.stub().callsArgWith(2) }
  32. this.UserGetter = { getUserByAnyEmail: sinon.stub() }
  33. this.UserCreator = {
  34. createNewUser: sinon.stub().callsArgWith(2, null, this.user),
  35. }
  36. this.AuthenticationManager = {
  37. validateEmail: sinon.stub().returns(null),
  38. validatePassword: sinon.stub().returns(null),
  39. setUserPassword: sinon.stub().callsArgWith(2),
  40. }
  41. this.NewsLetterManager = { subscribe: sinon.stub().callsArgWith(1) }
  42. this.EmailHandler = { sendEmail: sinon.stub().callsArgWith(2) }
  43. this.OneTimeTokenHandler = { getNewToken: sinon.stub() }
  44. this.handler = SandboxedModule.require(modulePath, {
  45. requires: {
  46. '../../models/User': { User: this.User },
  47. './UserGetter': this.UserGetter,
  48. './UserCreator': this.UserCreator,
  49. '../Authentication/AuthenticationManager': this.AuthenticationManager,
  50. '../Newsletter/NewsletterManager': this.NewsLetterManager,
  51. crypto: (this.crypto = {}),
  52. '../Email/EmailHandler': this.EmailHandler,
  53. '../Security/OneTimeTokenHandler': this.OneTimeTokenHandler,
  54. '../Analytics/AnalyticsManager': (this.AnalyticsManager = {
  55. recordEventForUser: sinon.stub(),
  56. setUserPropertyForUser: sinon.stub(),
  57. identifyUser: sinon.stub(),
  58. }),
  59. '@overleaf/settings': (this.settings = {
  60. siteUrl: 'http://sl.example.com',
  61. }),
  62. '../Helpers/EmailHelper': EmailHelper,
  63. },
  64. })
  65. this.passingRequest = {
  66. email: 'something@email.com',
  67. password: '123',
  68. analyticsId: this.analyticsId,
  69. }
  70. })
  71. describe('validate Register Request', function () {
  72. it('allows passing validation through', function () {
  73. const result = this.handler._registrationRequestIsValid(
  74. this.passingRequest
  75. )
  76. return result.should.equal(true)
  77. })
  78. describe('failing email validation', function () {
  79. beforeEach(function () {
  80. return this.AuthenticationManager.validateEmail.returns({
  81. message: 'email not set',
  82. })
  83. })
  84. it('does not allow through', function () {
  85. const result = this.handler._registrationRequestIsValid(
  86. this.passingRequest
  87. )
  88. return result.should.equal(false)
  89. })
  90. })
  91. describe('failing password validation', function () {
  92. beforeEach(function () {
  93. return this.AuthenticationManager.validatePassword.returns({
  94. message: 'password is too short',
  95. })
  96. })
  97. it('does not allow through', function () {
  98. const result = this.handler._registrationRequestIsValid(
  99. this.passingRequest
  100. )
  101. return result.should.equal(false)
  102. })
  103. })
  104. })
  105. describe('registerNewUser', function () {
  106. describe('holdingAccount', function (done) {
  107. beforeEach(function () {
  108. this.user.holdingAccount = true
  109. this.handler._registrationRequestIsValid = sinon.stub().returns(true)
  110. return this.UserGetter.getUserByAnyEmail.callsArgWith(
  111. 1,
  112. null,
  113. this.user
  114. )
  115. })
  116. it('should not create a new user if there is a holding account there', function (done) {
  117. return this.handler.registerNewUser(this.passingRequest, err => {
  118. this.UserCreator.createNewUser.called.should.equal(false)
  119. return done()
  120. })
  121. })
  122. it('should set holding account to false', function (done) {
  123. return this.handler.registerNewUser(this.passingRequest, err => {
  124. const update = this.User.updateOne.args[0]
  125. assert.deepEqual(update[0], { _id: this.user._id })
  126. assert.deepEqual(update[1], { $set: { holdingAccount: false } })
  127. return done()
  128. })
  129. })
  130. })
  131. describe('invalidRequest', function () {
  132. it('should not create a new user if the the request is not valid', function (done) {
  133. this.handler._registrationRequestIsValid = sinon.stub().returns(false)
  134. return this.handler.registerNewUser(this.passingRequest, err => {
  135. expect(err).to.exist
  136. this.UserCreator.createNewUser.called.should.equal(false)
  137. return done()
  138. })
  139. })
  140. it('should return email registered in the error if there is a non holdingAccount there', function (done) {
  141. this.UserGetter.getUserByAnyEmail.callsArgWith(
  142. 1,
  143. null,
  144. (this.user = { holdingAccount: false })
  145. )
  146. return this.handler.registerNewUser(
  147. this.passingRequest,
  148. (err, user) => {
  149. expect(err).to.be.instanceOf(Error)
  150. expect(err).to.have.property('message', 'EmailAlreadyRegistered')
  151. user.should.deep.equal(this.user)
  152. return done()
  153. }
  154. )
  155. })
  156. })
  157. describe('validRequest', function () {
  158. beforeEach(function () {
  159. this.handler._registrationRequestIsValid = sinon.stub().returns(true)
  160. return this.UserGetter.getUserByAnyEmail.callsArgWith(1)
  161. })
  162. it('should create a new user', function (done) {
  163. this.handler.registerNewUser(this.passingRequest, err => {
  164. sinon.assert.calledWith(this.UserCreator.createNewUser, {
  165. email: this.passingRequest.email,
  166. holdingAccount: false,
  167. first_name: this.passingRequest.first_name,
  168. last_name: this.passingRequest.last_name,
  169. analyticsId: this.user.analyticsId,
  170. })
  171. done()
  172. })
  173. })
  174. it('lower case email', function (done) {
  175. this.passingRequest.email = 'soMe@eMail.cOm'
  176. return this.handler.registerNewUser(this.passingRequest, err => {
  177. this.UserCreator.createNewUser.args[0][0].email.should.equal(
  178. 'some@email.com'
  179. )
  180. return done()
  181. })
  182. })
  183. it('trim white space from email', function (done) {
  184. this.passingRequest.email = ' some@email.com '
  185. return this.handler.registerNewUser(this.passingRequest, err => {
  186. this.UserCreator.createNewUser.args[0][0].email.should.equal(
  187. 'some@email.com'
  188. )
  189. return done()
  190. })
  191. })
  192. it('should set the password', function (done) {
  193. return this.handler.registerNewUser(this.passingRequest, err => {
  194. this.AuthenticationManager.setUserPassword
  195. .calledWith(this.user, this.passingRequest.password)
  196. .should.equal(true)
  197. return done()
  198. })
  199. })
  200. it('should add the user to the newsletter if accepted terms', function (done) {
  201. this.passingRequest.subscribeToNewsletter = 'true'
  202. return this.handler.registerNewUser(this.passingRequest, err => {
  203. this.NewsLetterManager.subscribe
  204. .calledWith(this.user)
  205. .should.equal(true)
  206. return done()
  207. })
  208. })
  209. it('should not add the user to the newsletter if not accepted terms', function (done) {
  210. return this.handler.registerNewUser(this.passingRequest, err => {
  211. this.NewsLetterManager.subscribe
  212. .calledWith(this.user)
  213. .should.equal(false)
  214. return done()
  215. })
  216. })
  217. })
  218. it('should call the ReferalAllocator', function (done) {
  219. return done()
  220. })
  221. })
  222. describe('registerNewUserAndSendActivationEmail', function () {
  223. beforeEach(function () {
  224. this.email = 'Email@example.com'
  225. this.crypto.randomBytes = sinon.stub().returns({
  226. toString: () => {
  227. return (this.password = 'mock-password')
  228. },
  229. })
  230. this.OneTimeTokenHandler.getNewToken.yields(
  231. null,
  232. (this.token = 'mock-token')
  233. )
  234. this.handler.registerNewUser = sinon.stub()
  235. return (this.callback = sinon.stub())
  236. })
  237. describe('with a new user', function () {
  238. beforeEach(function () {
  239. this.user.email = this.email.toLowerCase()
  240. this.handler.registerNewUser.callsArgWith(1, null, this.user)
  241. return this.handler.registerNewUserAndSendActivationEmail(
  242. this.email,
  243. this.callback
  244. )
  245. })
  246. it('should ask the UserRegistrationHandler to register user', function () {
  247. sinon.assert.calledWith(this.handler.registerNewUser, {
  248. email: this.email,
  249. password: this.password,
  250. })
  251. })
  252. it('should generate a new password reset token', function () {
  253. const data = {
  254. user_id: this.user._id.toString(),
  255. email: this.user.email,
  256. }
  257. return this.OneTimeTokenHandler.getNewToken
  258. .calledWith('password', data, { expiresIn: 7 * 24 * 60 * 60 })
  259. .should.equal(true)
  260. })
  261. it('should send a registered email', function () {
  262. return this.EmailHandler.sendEmail
  263. .calledWith('registered', {
  264. to: this.user.email,
  265. setNewPasswordUrl: `${this.settings.siteUrl}/user/activate?token=${this.token}&user_id=${this.user_id}`,
  266. })
  267. .should.equal(true)
  268. })
  269. it('should return the user', function () {
  270. return this.callback
  271. .calledWith(
  272. null,
  273. this.user,
  274. `${this.settings.siteUrl}/user/activate?token=${this.token}&user_id=${this.user_id}`
  275. )
  276. .should.equal(true)
  277. })
  278. })
  279. describe('with a user that already exists', function () {
  280. beforeEach(function () {
  281. this.handler.registerNewUser.callsArgWith(
  282. 1,
  283. new Error('EmailAlreadyRegistered'),
  284. this.user
  285. )
  286. return this.handler.registerNewUserAndSendActivationEmail(
  287. this.email,
  288. this.callback
  289. )
  290. })
  291. it('should still generate a new password token and email', function () {
  292. this.OneTimeTokenHandler.getNewToken.called.should.equal(true)
  293. return this.EmailHandler.sendEmail.called.should.equal(true)
  294. })
  295. })
  296. })
  297. })