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/DocumentUpdaterManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('DocumentUpdaterManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.DocumentUpdaterManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
request: (this.request = {}),
|
2021-07-12 12:47:16 -04:00
|
|
|
'@overleaf/settings': (this.settings = {
|
2021-07-13 07:04:43 -04:00
|
|
|
apis: { documentupdater: { url: 'http://example.com' } },
|
|
|
|
}),
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.lines = ['one', 'two', 'three']
|
|
|
|
return (this.version = 42)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getDocument', function () {
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.body = JSON.stringify({
|
|
|
|
lines: this.lines,
|
|
|
|
version: this.version,
|
2021-07-13 07:04:43 -04:00
|
|
|
ops: [],
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 200 }, this.body)
|
|
|
|
return this.DocumentUpdaterManager.getDocument(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the document from the document updater', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const url = `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}`
|
|
|
|
return this.request.get.calledWith(url).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the content and version', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, this.lines.join('\n'), this.version)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when the document updater 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.DocumentUpdaterManager.getDocument(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-07-06 05:18:07 -04:00
|
|
|
describe('when the document updater returns not found', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 404 }, '')
|
|
|
|
return this.DocumentUpdaterManager.getDocument(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the callback with a "not found" error', function () {
|
|
|
|
return this.callback
|
|
|
|
.calledWith(sinon.match.has('message', 'doc not found'))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when the document updater 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 }, '')
|
|
|
|
return this.DocumentUpdaterManager.getDocument(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_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',
|
|
|
|
'doc updater returned a non-success status code: 500'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('setDocument', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.content = 'mock content'
|
|
|
|
return (this.user_id = 'user-id-123')
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.request.post = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 200 })
|
|
|
|
return this.DocumentUpdaterManager.setDocument(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.content,
|
|
|
|
this.user_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should set the document in the document updater', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const url = `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}`
|
|
|
|
return this.request.post
|
|
|
|
.calledWith({
|
|
|
|
url,
|
|
|
|
json: {
|
|
|
|
lines: this.content.split('\n'),
|
|
|
|
source: 'restore',
|
|
|
|
user_id: this.user_id,
|
2021-07-13 07:04:43 -04:00
|
|
|
undoing: 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', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when the document updater API returns an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.request.post = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(
|
|
|
|
1,
|
|
|
|
(this.error = new Error('something went wrong')),
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
return this.DocumentUpdaterManager.setDocument(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.content,
|
|
|
|
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
|
|
|
return describe('when the document updater returns a failure error code', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.request.post = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 500 }, '')
|
|
|
|
return this.DocumentUpdaterManager.setDocument(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.content,
|
|
|
|
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',
|
|
|
|
'doc updater returned a non-success status code: 500'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|