WebApiManagerTests.coffee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. @project_id = "mock-project-id"
  21. @user_info =
  22. email: "leo@sharelatex.com"
  23. id: @user_id
  24. first_name: "Leo"
  25. last_nane: "Lion"
  26. extra_param: "blah"
  27. @project =
  28. features: "mock-features"
  29. describe "getUserInfo", ->
  30. describe "successfully", ->
  31. beforeEach ->
  32. @body = JSON.stringify @user_info
  33. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  34. @WebApiManager.getUserInfo @user_id, @callback
  35. it 'should get the user from the web api', ->
  36. @request.get
  37. .calledWith({
  38. url: "#{@settings.apis.web.url}/user/#{@user_id}/personal_info"
  39. auth:
  40. user: @settings.apis.web.user
  41. pass: @settings.apis.web.pass
  42. sendImmediately: true
  43. })
  44. .should.equal true
  45. it "should call the callback with only the email, id and names", ->
  46. @callback.calledWith(null, {
  47. id: @user_id
  48. email: @user_info.email
  49. first_name: @user_info.first_name
  50. last_name: @user_info.last_name
  51. }).should.equal true
  52. describe "when the web API returns an error", ->
  53. beforeEach ->
  54. @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  55. @WebApiManager.getUserInfo @user_id, @callback
  56. it "should return an error to the callback", ->
  57. @callback.calledWith(@error).should.equal true
  58. describe "when the web returns a failure error code", ->
  59. beforeEach ->
  60. @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  61. @WebApiManager.getUserInfo @user_id, @callback
  62. it "should return the callback with an error", ->
  63. @callback
  64. .calledWith(new Error("web returned failure status code: 500"))
  65. .should.equal true
  66. describe "getProjectDetails", ->
  67. describe "successfully", ->
  68. beforeEach ->
  69. @body = JSON.stringify @project
  70. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  71. @WebApiManager.getProjectDetails @project_id, @callback
  72. it 'should get the project from the web api', ->
  73. @request.get
  74. .calledWith({
  75. url: "#{@settings.apis.web.url}/project/#{@project_id}/details"
  76. auth:
  77. user: @settings.apis.web.user
  78. pass: @settings.apis.web.pass
  79. sendImmediately: true
  80. })
  81. .should.equal true
  82. it "should call the callback with the project", ->
  83. @callback.calledWith(null, @project).should.equal true
  84. describe "when the web API returns an error", ->
  85. beforeEach ->
  86. @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  87. @WebApiManager.getProjectDetails @project_id, @callback
  88. it "should return an error to the callback", ->
  89. @callback.calledWith(@error).should.equal true
  90. describe "when the web returns a failure error code", ->
  91. beforeEach ->
  92. @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  93. @WebApiManager.getProjectDetails @project_id, @callback
  94. it "should return the callback with an error", ->
  95. @callback
  96. .calledWith(new Error("web returned failure status code: 500"))
  97. .should.equal true