AuthenticationManagerTests.coffee 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. describe "AuthenticationManager", ->
  10. beforeEach ->
  11. @AuthenticationManager = SandboxedModule.require modulePath, requires:
  12. "../../models/User": User: @User = {}
  13. "../../infrastructure/mongojs":
  14. db: @db =
  15. users: {}
  16. ObjectId: ObjectId
  17. "bcrypt": @bcrypt = {}
  18. @callback = sinon.stub()
  19. describe "authenticate", ->
  20. describe "when the user exists in the database", ->
  21. beforeEach ->
  22. @user =
  23. _id: "user-id"
  24. email: @email = "USER@sharelatex.com"
  25. @unencryptedPassword = "banana"
  26. @User.findOne = sinon.stub().callsArgWith(1, null, @user)
  27. describe "when the hashed password matches", ->
  28. beforeEach (done) ->
  29. @user.hashedPassword = @hashedPassword = "asdfjadflasdf"
  30. @bcrypt.compare = sinon.stub().callsArgWith(2, null, true)
  31. @AuthenticationManager.authenticate email: @email, @unencryptedPassword, (error, user) =>
  32. @callback(error, user)
  33. done()
  34. it "should look up the correct user in the database", ->
  35. @User.findOne.calledWith(email: @email).should.equal true
  36. it "should check that the passwords match", ->
  37. @bcrypt.compare
  38. .calledWith(@unencryptedPassword, @hashedPassword)
  39. .should.equal true
  40. it "should return the user", ->
  41. @callback.calledWith(null, @user).should.equal true
  42. describe "when the encrypted passwords do not match", ->
  43. beforeEach ->
  44. @AuthenticationManager._encryptPassword = sinon.stub().returns("Not the encrypted password")
  45. @AuthenticationManager.authenticate(email: @email, @unencryptedPassword, @callback)
  46. it "should not return the user", ->
  47. @callback.calledWith(null, null).should.equal true
  48. describe "when the user does not exist in the database", ->
  49. beforeEach ->
  50. @User.findOne = sinon.stub().callsArgWith(1, null, null)
  51. @AuthenticationManager.authenticate(email: @email, @unencrpytedPassword, @callback)
  52. it "should not return a user", ->
  53. @callback.calledWith(null, null).should.equal true
  54. describe "setUserPassword", ->
  55. beforeEach ->
  56. @user_id = ObjectId()
  57. @password = "banana"
  58. @hashedPassword = "asdkjfa;osiuvandf"
  59. @salt = "saltaasdfasdfasdf"
  60. @bcrypt.genSalt = sinon.stub().callsArgWith(1, null, @salt)
  61. @bcrypt.hash = sinon.stub().callsArgWith(2, null, @hashedPassword)
  62. @db.users.update = sinon.stub().callsArg(2)
  63. @AuthenticationManager.setUserPassword(@user_id, @password, @callback)
  64. it "should update the user's password in the database", ->
  65. @db.users.update
  66. .calledWith({
  67. _id: ObjectId(@user_id.toString())
  68. }, {
  69. $set: {
  70. "hashedPassword": @hashedPassword
  71. }
  72. $unset: password: true
  73. })
  74. .should.equal true
  75. it "should hash the password", ->
  76. @bcrypt.genSalt
  77. .calledWith(7)
  78. .should.equal true
  79. @bcrypt.hash
  80. .calledWith(@password, @salt)
  81. .should.equal true
  82. it "should call the callback", ->
  83. @callback.called.should.equal true