WebApiManagerTests.coffee 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "requestretry": @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. .calledWithMatch({
  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, attempts: 42}, "")
  61. @WebApiManager.getUserInfo @user_id, @callback
  62. it "should return the callback with an error", ->
  63. @callback
  64. .calledWith(sinon.match.has('message', "web returned a non-success status code: 500 (attempts: 42)"))
  65. .should.equal true
  66. describe "when the user cannot be found", ->
  67. beforeEach ->
  68. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 404}, "nothing")
  69. @WebApiManager.getUserInfo @user_id, @callback
  70. it "should return a null value", ->
  71. @callback
  72. .calledWith(null, null)
  73. .should.equal true
  74. describe "getProjectDetails", ->
  75. describe "successfully", ->
  76. beforeEach ->
  77. @body = JSON.stringify @project
  78. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  79. @WebApiManager.getProjectDetails @project_id, @callback
  80. it 'should get the project from the web api', ->
  81. @request.get
  82. .calledWithMatch({
  83. url: "#{@settings.apis.web.url}/project/#{@project_id}/details"
  84. auth:
  85. user: @settings.apis.web.user
  86. pass: @settings.apis.web.pass
  87. sendImmediately: true
  88. })
  89. .should.equal true
  90. it "should call the callback with the project", ->
  91. @callback.calledWith(null, @project).should.equal true
  92. describe "when the web API returns an error", ->
  93. beforeEach ->
  94. @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  95. @WebApiManager.getProjectDetails @project_id, @callback
  96. it "should return an error to the callback", ->
  97. @callback.calledWith(@error).should.equal true
  98. describe "when the web returns a failure error code", ->
  99. beforeEach ->
  100. @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500, attempts: 42 }, "")
  101. @WebApiManager.getProjectDetails @project_id, @callback
  102. it "should return the callback with an error", ->
  103. @callback
  104. .calledWith(sinon.match.has('message', "web returned a non-success status code: 500 (attempts: 42)"))
  105. .should.equal true