| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- /* eslint-disable
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const sinon = require('sinon')
- const { expect } = require('chai')
- const modulePath = '../../../../app/js/DocumentUpdaterManager.js'
- const SandboxedModule = require('sandboxed-module')
- describe('DocumentUpdaterManager', function () {
- beforeEach(function () {
- this.DocumentUpdaterManager = SandboxedModule.require(modulePath, {
- requires: {
- request: (this.request = {}),
- '@overleaf/settings': (this.settings = {
- apis: { documentupdater: { url: 'http://example.com' } },
- }),
- },
- })
- this.callback = sinon.stub()
- this.lines = ['one', 'two', 'three']
- return (this.version = 42)
- })
- describe('getDocument', function () {
- describe('successfully', function () {
- beforeEach(function () {
- this.body = JSON.stringify({
- lines: this.lines,
- version: this.version,
- ops: [],
- })
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 200 }, this.body)
- return this.DocumentUpdaterManager.getDocument(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should get the document from the document updater', function () {
- const url = `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}`
- return this.request.get.calledWith(url).should.equal(true)
- })
- return it('should call the callback with the content and version', function () {
- return this.callback
- .calledWith(null, this.lines.join('\n'), this.version)
- .should.equal(true)
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function () {
- 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
- )
- })
- return it('should return an error to the callback', function () {
- return this.callback.calledWith(this.error).should.equal(true)
- })
- })
- 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)
- })
- })
- return describe('when the document updater returns a failure error code', function () {
- beforeEach(function () {
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 500 }, '')
- return this.DocumentUpdaterManager.getDocument(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- return it('should return the callback with an error', function () {
- return this.callback
- .calledWith(
- sinon.match.has(
- 'message',
- 'doc updater returned a non-success status code: 500'
- )
- )
- .should.equal(true)
- })
- })
- })
- return describe('setDocument', function () {
- beforeEach(function () {
- this.content = 'mock content'
- return (this.user_id = 'user-id-123')
- })
- describe('successfully', function () {
- beforeEach(function () {
- 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
- )
- })
- it('should set the document in the document updater', function () {
- 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,
- undoing: true,
- },
- })
- .should.equal(true)
- })
- return it('should call the callback', function () {
- return this.callback.calledWith(null).should.equal(true)
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function () {
- 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
- )
- })
- return it('should return an error to the callback', function () {
- return this.callback.calledWith(this.error).should.equal(true)
- })
- })
- return describe('when the document updater returns a failure error code', function () {
- beforeEach(function () {
- 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
- )
- })
- return it('should return the callback with an error', function () {
- return this.callback
- .calledWith(
- sinon.match.has(
- 'message',
- 'doc updater returned a non-success status code: 500'
- )
- )
- .should.equal(true)
- })
- })
- })
- })
|