WebApiManagerTests.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/WebApiManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "WebApiManager", ->
  8. beforeEach ->
  9. @WebApiManager = SandboxedModule.require modulePath, requires:
  10. "request": @request = {}
  11. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  12. 'settings-sharelatex': @settings =
  13. apis:
  14. web:
  15. url: "http://example.com"
  16. user: "sharelatex"
  17. pass: "password"
  18. @callback = sinon.stub()
  19. @user_id = "mock-user-id"
  20. @user_info =
  21. email: "leo@sharelatex.com"
  22. id: @user_id
  23. first_name: "Leo"
  24. last_nane: "Lion"
  25. extra_param: "blah"
  26. describe "getUserInfo", ->
  27. describe "successfully", ->
  28. beforeEach ->
  29. @body = JSON.stringify @user_info
  30. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  31. @WebApiManager.getUserInfo @user_id, @callback
  32. it 'should get the user from the web api', ->
  33. url =
  34. @request.get
  35. .calledWith({
  36. url: "#{@settings.apis.web.url}/user/#{@user_id}/personal_info"
  37. auth:
  38. user: @settings.apis.web.user
  39. pass: @settings.apis.web.pass
  40. sendImmediately: true
  41. })
  42. .should.equal true
  43. it "should call the callback with only the email, id and names", ->
  44. @callback.calledWith(null, {
  45. id: @user_id
  46. email: @user_info.email
  47. first_name: @user_info.first_name
  48. last_name: @user_info.last_name
  49. }).should.equal true
  50. describe "when the web API returns an error", ->
  51. beforeEach ->
  52. @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  53. @WebApiManager.getUserInfo @user_id, @callback
  54. it "should return an error to the callback", ->
  55. @callback.calledWith(@error).should.equal true
  56. describe "when the web returns a failure error code", ->
  57. beforeEach ->
  58. @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  59. @WebApiManager.getUserInfo @user_id, @callback
  60. it "should return the callback with an error", ->
  61. @callback
  62. .calledWith(new Error("web returned failure status code: 500"))
  63. .should.equal true