2014-11-10 06:27:08 -05:00
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
sinon = require("sinon")
|
|
|
|
modulePath = "../../../app/js/WebApiManager.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
2019-08-31 09:04:36 -04:00
|
|
|
{ CodedError } = require('../../../app/js/Errors')
|
2014-11-10 06:27:08 -05:00
|
|
|
|
|
|
|
describe 'WebApiManager', ->
|
|
|
|
beforeEach ->
|
|
|
|
@project_id = "project-id-123"
|
|
|
|
@user_id = "user-id-123"
|
2017-09-26 09:21:41 -04:00
|
|
|
@user = {_id: @user_id}
|
2014-11-10 06:27:08 -05:00
|
|
|
@callback = sinon.stub()
|
|
|
|
@WebApiManager = SandboxedModule.require modulePath, requires:
|
|
|
|
"request": @request = {}
|
|
|
|
"settings-sharelatex": @settings =
|
|
|
|
apis:
|
|
|
|
web:
|
|
|
|
url: "http://web.example.com"
|
2014-11-17 09:35:07 -05:00
|
|
|
user: "username"
|
|
|
|
pass: "password"
|
2014-11-10 06:27:08 -05:00
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
|
2019-10-30 09:52:36 -04:00
|
|
|
|
2014-11-10 06:27:08 -05:00
|
|
|
describe "joinProject", ->
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@response = {
|
|
|
|
project: { name: "Test project" }
|
2019-10-30 09:52:36 -04:00
|
|
|
privilegeLevel: "owner",
|
|
|
|
isRestrictedUser: true
|
2014-11-10 06:27:08 -05:00
|
|
|
}
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @response)
|
2017-09-26 09:21:41 -04:00
|
|
|
@WebApiManager.joinProject @project_id, @user, @callback
|
2019-10-30 09:52:36 -04:00
|
|
|
|
2014-11-10 06:27:08 -05:00
|
|
|
it "should send a request to web to join the project", ->
|
|
|
|
@request.post
|
|
|
|
.calledWith({
|
|
|
|
url: "#{@settings.apis.web.url}/project/#{@project_id}/join"
|
|
|
|
qs:
|
|
|
|
user_id: @user_id
|
2014-11-17 09:35:07 -05:00
|
|
|
auth:
|
|
|
|
user: @settings.apis.web.user
|
|
|
|
pass: @settings.apis.web.pass
|
|
|
|
sendImmediately: true
|
2014-11-10 06:27:08 -05:00
|
|
|
json: true
|
|
|
|
jar: false
|
2017-09-26 09:21:41 -04:00
|
|
|
headers: {}
|
2014-11-10 06:27:08 -05:00
|
|
|
})
|
|
|
|
.should.equal true
|
2019-10-30 09:52:36 -04:00
|
|
|
|
|
|
|
it "should return the project, privilegeLevel, and restricted flag", ->
|
2014-11-10 06:27:08 -05:00
|
|
|
@callback
|
2019-10-30 09:52:36 -04:00
|
|
|
.calledWith(null, @response.project, @response.privilegeLevel, @response.isRestrictedUser)
|
2014-11-10 06:27:08 -05:00
|
|
|
.should.equal true
|
2019-10-30 09:52:36 -04:00
|
|
|
|
2014-11-10 06:27:08 -05:00
|
|
|
describe "with an error from web", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 500}, null)
|
|
|
|
@WebApiManager.joinProject @project_id, @user_id, @callback
|
2019-10-30 09:52:36 -04:00
|
|
|
|
2014-11-10 06:27:08 -05:00
|
|
|
it "should call the callback with an error", ->
|
|
|
|
@callback
|
2020-05-01 06:43:18 -04:00
|
|
|
.calledWith(sinon.match({message: "non-success status code from web: 500"}))
|
2014-11-10 06:27:08 -05:00
|
|
|
.should.equal true
|
2019-10-30 09:52:36 -04:00
|
|
|
|
|
|
|
describe "with no data from web", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}, null)
|
|
|
|
@WebApiManager.joinProject @project_id, @user_id, @callback
|
|
|
|
|
|
|
|
it "should call the callback with an error", ->
|
|
|
|
@callback
|
2020-05-01 06:43:18 -04:00
|
|
|
.calledWith(sinon.match({message: "no data returned from joinProject request"}))
|
2019-10-30 09:52:36 -04:00
|
|
|
.should.equal true
|
|
|
|
|
2019-08-31 09:04:36 -04:00
|
|
|
describe "when the project is over its rate limit", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 429}, null)
|
|
|
|
@WebApiManager.joinProject @project_id, @user_id, @callback
|
|
|
|
|
|
|
|
it "should call the callback with a TooManyRequests error code", ->
|
|
|
|
@callback
|
2020-05-01 06:43:18 -04:00
|
|
|
.calledWith(sinon.match({message: "rate-limit hit when joining project", code: "TooManyRequests"}))
|
2019-08-31 09:04:36 -04:00
|
|
|
.should.equal true
|