AuthenticationManagerTests.coffee 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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, @unencryptedPassword, @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
  84. describe "getAuthToken", ->
  85. beforeEach ->
  86. @auth_token = "auth-token"
  87. describe "when the user has an auth token set", ->
  88. beforeEach ->
  89. @db.users.findOne = sinon.stub().callsArgWith(2, null, auth_token: @auth_token)
  90. @AuthenticationManager.getAuthToken(@user_id, @callback)
  91. it "should look up the auth token in the db", ->
  92. @db.users.findOne
  93. .calledWith({
  94. _id: ObjectId(@user_id.toString())
  95. }, {
  96. auth_token: true
  97. })
  98. .should.equal true
  99. it "should return the auth token", ->
  100. @callback.calledWith(null, @auth_token).should.equal true
  101. describe "when the user does not have an auth token set", ->
  102. beforeEach ->
  103. @db.users.findOne = sinon.stub().callsArgWith(2, null, auth_token: null)
  104. @db.users.update = sinon.stub().callsArgWith(2, null)
  105. @AuthenticationManager._createSecureToken = sinon.stub().callsArgWith(0, null, @auth_token)
  106. @AuthenticationManager.getAuthToken(@user_id, @callback)
  107. it "should generate a new auth token", ->
  108. @AuthenticationManager._createSecureToken.called.should.equal true
  109. it "should set the auth token on the user document in the db", ->
  110. @db.users.update
  111. .calledWith({
  112. _id: ObjectId(@user_id.toString())
  113. }, {
  114. $set: auth_token: @auth_token
  115. })
  116. .should.equal true
  117. it "should return the auth token", ->
  118. @callback.calledWith(null, @auth_token).should.equal true