2020-02-17 12:35:01 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:34:50 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 12:35:16 -05:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
const should = chai.should()
|
|
|
|
const { expect } = chai
|
|
|
|
const modulePath = '../../../../app/js/WebApiManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('WebApiManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.WebApiManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
requestretry: (this.request = {}),
|
|
|
|
'logger-sharelatex': (this.logger = {
|
|
|
|
log: sinon.stub(),
|
|
|
|
error: sinon.stub()
|
|
|
|
}),
|
|
|
|
'settings-sharelatex': (this.settings = {
|
|
|
|
apis: {
|
|
|
|
web: {
|
|
|
|
url: 'http://example.com',
|
|
|
|
user: 'sharelatex',
|
|
|
|
pass: 'password'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.user_id = 'mock-user-id'
|
|
|
|
this.project_id = 'mock-project-id'
|
|
|
|
this.user_info = {
|
|
|
|
email: 'leo@sharelatex.com',
|
|
|
|
id: this.user_id,
|
|
|
|
first_name: 'Leo',
|
|
|
|
last_nane: 'Lion',
|
|
|
|
extra_param: 'blah'
|
|
|
|
}
|
|
|
|
return (this.project = { features: 'mock-features' })
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getUserInfo', function () {
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.body = JSON.stringify(this.user_info)
|
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 200 }, this.body)
|
|
|
|
return this.WebApiManager.getUserInfo(this.user_id, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the user from the web api', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.request.get
|
|
|
|
.calledWithMatch({
|
|
|
|
url: `${this.settings.apis.web.url}/user/${this.user_id}/personal_info`,
|
|
|
|
auth: {
|
|
|
|
user: this.settings.apis.web.user,
|
|
|
|
pass: this.settings.apis.web.pass,
|
|
|
|
sendImmediately: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with only the email, id and names', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, {
|
|
|
|
id: this.user_id,
|
|
|
|
email: this.user_info.email,
|
|
|
|
first_name: this.user_info.first_name,
|
|
|
|
last_name: this.user_info.last_name
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when the web API returns an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(
|
|
|
|
1,
|
|
|
|
(this.error = new Error('something went wrong')),
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
return this.WebApiManager.getUserInfo(this.user_id, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return an error to the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(this.error).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when the web returns a failure error code', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 500, attempts: 42 }, '')
|
|
|
|
return this.WebApiManager.getUserInfo(this.user_id, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return the callback with an error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(
|
|
|
|
sinon.match.has(
|
|
|
|
'message',
|
|
|
|
'web returned a non-success status code: 500 (attempts: 42)'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when the user cannot be found', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 404 }, 'nothing')
|
|
|
|
return this.WebApiManager.getUserInfo(this.user_id, this.callback)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return a null value', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null, null).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('getProjectDetails', function () {
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.body = JSON.stringify(this.project)
|
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 200 }, this.body)
|
|
|
|
return this.WebApiManager.getProjectDetails(
|
|
|
|
this.project_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the project from the web api', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.request.get
|
|
|
|
.calledWithMatch({
|
|
|
|
url: `${this.settings.apis.web.url}/project/${this.project_id}/details`,
|
|
|
|
auth: {
|
|
|
|
user: this.settings.apis.web.user,
|
|
|
|
pass: this.settings.apis.web.pass,
|
|
|
|
sendImmediately: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the project', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null, this.project).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when the web API returns an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(
|
|
|
|
1,
|
|
|
|
(this.error = new Error('something went wrong')),
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
return this.WebApiManager.getProjectDetails(
|
|
|
|
this.project_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return an error to the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(this.error).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when the web returns a failure error code', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 500, attempts: 42 }, '')
|
|
|
|
return this.WebApiManager.getProjectDetails(
|
|
|
|
this.project_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return the callback with an error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(
|
|
|
|
sinon.match.has(
|
|
|
|
'message',
|
|
|
|
'web returned a non-success status code: 500 (attempts: 42)'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|