UserRegistrationHandlerTests.js 11 KB

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