AuthenticationControllerTests.coffee 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Authentication/AuthenticationController.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "AuthenticationController", ->
  8. beforeEach ->
  9. @AuthenticationController = SandboxedModule.require modulePath, requires:
  10. "../WebApi/WebApiManager": @WebApiManager = {}
  11. "../Users/UserFormatter": @UserFormatter = {}
  12. "logger-sharelatex": @logger = { log: sinon.stub() }
  13. @callback = sinon.stub()
  14. describe "authClient", ->
  15. beforeEach ->
  16. @auth_token = "super-secret-auth-token"
  17. @client =
  18. params: {}
  19. set: (key, value, callback) ->
  20. @params[key] = value
  21. callback()
  22. @user =
  23. id: "user-id-123"
  24. email: "doug@sharelatex.com"
  25. first_name: "Douglas"
  26. last_name: "Adams"
  27. @WebApiManager.getUserDetailsFromAuthToken = sinon.stub().callsArgWith(1, null, @user)
  28. @UserFormatter.formatUserForClientSide = sinon.stub().returns({
  29. id: @user.id
  30. first_name: @user.first_name
  31. last_name: @user.last_name
  32. email: @user.email
  33. gravatar_url: "//gravatar/url"
  34. })
  35. @AuthenticationController.authClient(@client, auth_token: @auth_token, @callback)
  36. it "should get the user's data from the web api", ->
  37. @WebApiManager.getUserDetailsFromAuthToken
  38. .calledWith(@auth_token)
  39. .should.equal true
  40. it "should set the user's data and auth_token on the client object", ->
  41. @client.params.should.deep.equal {
  42. id: @user.id
  43. first_name: @user.first_name
  44. last_name: @user.last_name
  45. email: @user.email
  46. gravatar_url: "//gravatar/url"
  47. auth_token: @auth_token
  48. }
  49. it "should call the callback with the user details (including the gravatar URL, but not the auth_token)", ->
  50. @callback
  51. .calledWith(null, {
  52. id: @user.id
  53. email: @user.email
  54. first_name: @user.first_name
  55. last_name: @user.last_name
  56. gravatar_url: "//gravatar/url"
  57. }).should.equal true
  58. it "should log the request", ->
  59. @logger.log
  60. .calledWith(auth_token: @auth_token, "authenticating user")
  61. .should.equal true
  62. @logger.log
  63. .calledWith(user: @user, auth_token: @auth_token, "authenticated user")
  64. .should.equal true