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')
|
2021-03-23 15:08:32 -04:00
|
|
|
const { expect } = require('chai')
|
2020-02-17 12:35:16 -05:00
|
|
|
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 = {}),
|
2021-07-12 12:47:16 -04:00
|
|
|
'@overleaf/settings': (this.settings = {
|
2020-02-17 12:35:16 -05:00
|
|
|
apis: {
|
|
|
|
web: {
|
|
|
|
url: 'http://example.com',
|
|
|
|
user: 'sharelatex',
|
2021-07-13 07:04:43 -04:00
|
|
|
pass: 'password',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
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',
|
2021-07-13 07:04:43 -04:00
|
|
|
extra_param: 'blah',
|
2020-02-17 12:35:16 -05:00
|
|
|
}
|
|
|
|
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,
|
2021-07-13 07:04:43 -04:00
|
|
|
sendImmediately: true,
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
.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,
|
2021-07-13 07:04:43 -04:00
|
|
|
last_name: this.user_info.last_name,
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
.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,
|
2021-07-13 07:04:43 -04:00
|
|
|
sendImmediately: true,
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
.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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|