WebApiManagerTests.coffee 1.8 KB

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