WebApiManagerTests.coffee 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. chai = require('chai')
  2. should = chai.should()
  3. sinon = require("sinon")
  4. modulePath = "../../../app/js/WebApiManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe 'WebApiManager', ->
  7. beforeEach ->
  8. @project_id = "project-id-123"
  9. @user_id = "user-id-123"
  10. @callback = sinon.stub()
  11. @WebApiManager = SandboxedModule.require modulePath, requires:
  12. "request": @request = {}
  13. "settings-sharelatex": @settings =
  14. apis:
  15. web:
  16. url: "http://web.example.com"
  17. user: "username"
  18. pass: "password"
  19. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  20. describe "joinProject", ->
  21. describe "successfully", ->
  22. beforeEach ->
  23. @response = {
  24. project: { name: "Test project" }
  25. privilegeLevel: "owner"
  26. }
  27. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @response)
  28. @WebApiManager.joinProject @project_id, @user_id, @callback
  29. it "should send a request to web to join the project", ->
  30. @request.post
  31. .calledWith({
  32. url: "#{@settings.apis.web.url}/project/#{@project_id}/join"
  33. qs:
  34. user_id: @user_id
  35. auth:
  36. user: @settings.apis.web.user
  37. pass: @settings.apis.web.pass
  38. sendImmediately: true
  39. json: true
  40. jar: false
  41. })
  42. .should.equal true
  43. it "should return the project and privilegeLevel", ->
  44. @callback
  45. .calledWith(null, @response.project, @response.privilegeLevel)
  46. .should.equal true
  47. describe "with an error from web", ->
  48. beforeEach ->
  49. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 500}, null)
  50. @WebApiManager.joinProject @project_id, @user_id, @callback
  51. it "should call the callback with an error", ->
  52. @callback
  53. .calledWith(new Error("non-success code from web: 500"))
  54. .should.equal true