AuthenticationManagerTests.coffee 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Authentication/AuthenticationManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. events = require "events"
  8. ObjectId = require("mongojs").ObjectId
  9. Errors = require "../../../../app/js/Features/Errors/Errors"
  10. describe "AuthenticationManager", ->
  11. beforeEach ->
  12. @settings = { security: { bcryptRounds: 12 } }
  13. @AuthenticationManager = SandboxedModule.require modulePath, requires:
  14. "../../models/User": User: @User = {}
  15. "../../infrastructure/mongojs":
  16. db: @db =
  17. users: {}
  18. ObjectId: ObjectId
  19. "bcrypt": @bcrypt = {}
  20. "settings-sharelatex": @settings
  21. "../V1/V1Handler": @V1Handler = {}
  22. "../User/UserGetter": @UserGetter = {}
  23. @callback = sinon.stub()
  24. describe "authenticate", ->
  25. describe "when the user exists in the database", ->
  26. beforeEach ->
  27. @user =
  28. _id: "user-id"
  29. email: @email = "USER@sharelatex.com"
  30. @unencryptedPassword = "banana"
  31. @User.findOne = sinon.stub().callsArgWith(1, null, @user)
  32. describe "when the hashed password matches", ->
  33. beforeEach (done) ->
  34. @user.hashedPassword = @hashedPassword = "asdfjadflasdf"
  35. @bcrypt.compare = sinon.stub().callsArgWith(2, null, true)
  36. @bcrypt.getRounds = sinon.stub().returns 12
  37. @AuthenticationManager.authenticate email: @email, @unencryptedPassword, (error, user) =>
  38. @callback(error, user)
  39. done()
  40. it "should look up the correct user in the database", ->
  41. @User.findOne.calledWith(email: @email).should.equal true
  42. it "should check that the passwords match", ->
  43. @bcrypt.compare
  44. .calledWith(@unencryptedPassword, @hashedPassword)
  45. .should.equal true
  46. it "should return the user", ->
  47. @callback.calledWith(null, @user).should.equal true
  48. describe "when the encrypted passwords do not match", ->
  49. beforeEach ->
  50. @AuthenticationManager._encryptPassword = sinon.stub().returns("Not the encrypted password")
  51. @AuthenticationManager.authenticate(email: @email, @unencryptedPassword, @callback)
  52. it "should not return the user", ->
  53. @callback.calledWith(null, null).should.equal true
  54. describe "when the hashed password matches but the number of rounds is too low", ->
  55. beforeEach (done) ->
  56. @user.hashedPassword = @hashedPassword = "asdfjadflasdf"
  57. @bcrypt.compare = sinon.stub().callsArgWith(2, null, true)
  58. @bcrypt.getRounds = sinon.stub().returns 7
  59. @AuthenticationManager.setUserPassword = sinon.stub().callsArgWith(2, null)
  60. @AuthenticationManager.authenticate email: @email, @unencryptedPassword, (error, user) =>
  61. @callback(error, user)
  62. done()
  63. it "should look up the correct user in the database", ->
  64. @User.findOne.calledWith(email: @email).should.equal true
  65. it "should check that the passwords match", ->
  66. @bcrypt.compare
  67. .calledWith(@unencryptedPassword, @hashedPassword)
  68. .should.equal true
  69. it "should check the number of rounds", ->
  70. @bcrypt.getRounds.called.should.equal true
  71. it "should set the users password (with a higher number of rounds)", ->
  72. @AuthenticationManager.setUserPassword
  73. .calledWith("user-id", @unencryptedPassword)
  74. .should.equal true
  75. it "should return the user", ->
  76. @callback.calledWith(null, @user).should.equal true
  77. describe "when the user does not exist in the database", ->
  78. beforeEach ->
  79. @User.findOne = sinon.stub().callsArgWith(1, null, null)
  80. @AuthenticationManager.authenticate(email: @email, @unencrpytedPassword, @callback)
  81. it "should not return a user", ->
  82. @callback.calledWith(null, null).should.equal true
  83. describe "validateEmail", ->
  84. describe "valid", ->
  85. it "should return null", ->
  86. result = @AuthenticationManager.validateEmail 'foo@example.com'
  87. expect(result).to.equal null
  88. describe "invalid", ->
  89. it "should return validation error object for no email", ->
  90. result = @AuthenticationManager.validateEmail ''
  91. expect(result).to.not.equal null
  92. expect(result.message).to.equal 'email not valid'
  93. it "should return validation error object for invalid", ->
  94. result = @AuthenticationManager.validateEmail 'notanemail'
  95. expect(result).to.not.equal null
  96. expect(result.message).to.equal 'email not valid'
  97. describe "validatePassword", ->
  98. it "should return null if valid", ->
  99. result = @AuthenticationManager.validatePassword 'banana'
  100. expect(result).to.equal null
  101. describe "invalid", ->
  102. beforeEach ->
  103. @settings.passwordStrengthOptions =
  104. length:
  105. max:10
  106. min:6
  107. it "should return validation error object if not set", ->
  108. result = @AuthenticationManager.validatePassword()
  109. expect(result).to.not.equal null
  110. expect(result.message).to.equal 'password not set'
  111. it "should return validation error object if too short", ->
  112. result = @AuthenticationManager.validatePassword 'dsd'
  113. expect(result).to.not.equal null
  114. expect(result.message).to.equal 'password is too short'
  115. it "should return validation error object if too long", ->
  116. result = @AuthenticationManager.validatePassword 'dsdsadsadsadsadsadkjsadjsadjsadljs'
  117. expect(result).to.not.equal null
  118. expect(result.message).to.equal 'password is too long'
  119. describe "setUserPassword", ->
  120. beforeEach ->
  121. @user_id = ObjectId()
  122. @password = "banana"
  123. @hashedPassword = "asdkjfa;osiuvandf"
  124. @salt = "saltaasdfasdfasdf"
  125. @bcrypt.genSalt = sinon.stub().callsArgWith(1, null, @salt)
  126. @bcrypt.hash = sinon.stub().callsArgWith(2, null, @hashedPassword)
  127. @db.users.update = sinon.stub().callsArg(2)
  128. describe "too long", ->
  129. beforeEach ->
  130. @settings.passwordStrengthOptions =
  131. length:
  132. max:10
  133. @password = "dsdsadsadsadsadsadkjsadjsadjsadljs"
  134. it "should return and error", (done)->
  135. @AuthenticationManager.setUserPassword @user_id, @password, (err)->
  136. expect(err).to.exist
  137. done()
  138. it "should not start the bcrypt process", (done)->
  139. @AuthenticationManager.setUserPassword @user_id, @password, (err)=>
  140. @bcrypt.genSalt.called.should.equal false
  141. @bcrypt.hash.called.should.equal false
  142. done()
  143. describe "too short", ->
  144. beforeEach ->
  145. @settings.passwordStrengthOptions =
  146. length:
  147. max:10
  148. min:6
  149. @password = "dsd"
  150. it "should return and error", (done)->
  151. @AuthenticationManager.setUserPassword @user_id, @password, (err)->
  152. expect(err).to.exist
  153. done()
  154. it "should not start the bcrypt process", (done)->
  155. @AuthenticationManager.setUserPassword @user_id, @password, (err)=>
  156. @bcrypt.genSalt.called.should.equal false
  157. @bcrypt.hash.called.should.equal false
  158. done()
  159. describe "password set attempt", ->
  160. describe "with SL user in SL", ->
  161. beforeEach ->
  162. @UserGetter.getUser = sinon.stub().yields(null, { overleaf: null })
  163. @AuthenticationManager.setUserPassword(@user_id, @password, @callback)
  164. it 'should look up the user', ->
  165. @UserGetter.getUser.calledWith(@user_id).should.equal true
  166. it "should update the user's password in the database", ->
  167. args = @db.users.update.lastCall.args
  168. expect(args[0]).to.deep.equal {_id: ObjectId(@user_id.toString())}
  169. expect(args[1]).to.deep.equal {
  170. $set: {
  171. "hashedPassword": @hashedPassword
  172. }
  173. $unset: password: true
  174. }
  175. it "should hash the password", ->
  176. @bcrypt.genSalt
  177. .calledWith(12)
  178. .should.equal true
  179. @bcrypt.hash
  180. .calledWith(@password, @salt)
  181. .should.equal true
  182. it "should call the callback", ->
  183. @callback.called.should.equal true
  184. describe "with SL user in v2", ->
  185. beforeEach (done) ->
  186. @settings.overleaf = true
  187. @UserGetter.getUser = sinon.stub().yields(null, { overleaf: null })
  188. @AuthenticationManager.setUserPassword @user_id, @password, (err, changed) =>
  189. @callback(err, changed)
  190. done()
  191. it "should error", ->
  192. @callback.calledWith(new Errors.SLInV2Error("Password Reset Attempt")).should.equal true
  193. describe "with v2 user in SL", ->
  194. beforeEach (done) ->
  195. @UserGetter.getUser = sinon.stub().yields(null, { overleaf: {id: 1} })
  196. @AuthenticationManager.setUserPassword @user_id, @password, (err, changed) =>
  197. @callback(err, changed)
  198. done()
  199. it "should error", ->
  200. @callback.calledWith(new Errors.NotInV2Error("Password Reset Attempt")).should.equal true
  201. describe "with v2 user in v2", ->
  202. beforeEach (done) ->
  203. @settings.overleaf = true
  204. @UserGetter.getUser = sinon.stub().yields(null, { overleaf: {id: 1} })
  205. @V1Handler.doPasswordReset = sinon.stub().yields(null, true)
  206. @AuthenticationManager.setUserPassword @user_id, @password, (err, changed) =>
  207. @callback(err, changed)
  208. done()
  209. it "should set the password in v2", ->
  210. @callback.calledWith(null, true).should.equal true