| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- /* eslint-disable
- max-len,
- no-return-assign,
- */
- // 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 chai = require('chai')
- chai.should()
- const sinon = require('sinon')
- const modulePath = '../../../../app/src/Features/Docstore/DocstoreManager'
- const SandboxedModule = require('sandboxed-module')
- const Errors = require('../../../../app/src/Features/Errors/Errors.js')
- describe('DocstoreManager', function() {
- beforeEach(function() {
- this.requestDefaults = sinon.stub().returns((this.request = sinon.stub()))
- this.DocstoreManager = SandboxedModule.require(modulePath, {
- globals: {
- console: console
- },
- requires: {
- request: {
- defaults: this.requestDefaults
- },
- 'settings-sharelatex': (this.settings = {
- apis: {
- docstore: {
- url: 'docstore.sharelatex.com'
- }
- }
- }),
- 'logger-sharelatex': (this.logger = {
- log: sinon.stub(),
- warn: sinon.stub(),
- error: sinon.stub(),
- err() {}
- })
- }
- })
- this.requestDefaults.calledWith({ jar: false }).should.equal(true)
- this.project_id = 'project-id-123'
- this.doc_id = 'doc-id-123'
- return (this.callback = sinon.stub())
- })
- describe('deleteDoc', function() {
- describe('with a successful response code', function() {
- beforeEach(function() {
- this.request.del = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 204 }, '')
- return this.DocstoreManager.deleteDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should delete the doc in the docstore api', function() {
- return this.request.del
- .calledWith({
- url: `${this.settings.apis.docstore.url}/project/${
- this.project_id
- }/doc/${this.doc_id}`,
- timeout: 30 * 1000
- })
- .should.equal(true)
- })
- it('should call the callback without an error', function() {
- return this.callback.calledWith(null).should.equal(true)
- })
- })
- describe('with a failed response code', function() {
- beforeEach(function() {
- this.request.del = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 500 }, '')
- return this.DocstoreManager.deleteDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should call the callback with an error', function() {
- return this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(
- sinon.match.has(
- 'message',
- 'docstore api responded with non-success code: 500'
- )
- )
- )
- .should.equal(true)
- })
- })
- describe('with a missing (404) response code', function() {
- beforeEach(function() {
- this.request.del = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 404 }, '')
- return this.DocstoreManager.deleteDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should call the callback with an error', function() {
- this.callback
- .calledWith(
- sinon.match
- .instanceOf(Errors.NotFoundError)
- .and(
- sinon.match.has(
- 'message',
- 'tried to delete doc not in docstore'
- )
- )
- )
- .should.equal(true)
- })
- })
- })
- describe('updateDoc', function() {
- beforeEach(function() {
- this.lines = ['mock', 'doc', 'lines']
- this.rev = 5
- this.version = 42
- this.ranges = { mock: 'ranges' }
- return (this.modified = true)
- })
- describe('with a successful response code', function() {
- beforeEach(function() {
- this.request.post = sinon
- .stub()
- .callsArgWith(
- 1,
- null,
- { statusCode: 204 },
- { modified: this.modified, rev: this.rev }
- )
- return this.DocstoreManager.updateDoc(
- this.project_id,
- this.doc_id,
- this.lines,
- this.version,
- this.ranges,
- this.callback
- )
- })
- it('should update the doc in the docstore api', function() {
- return this.request.post
- .calledWith({
- url: `${this.settings.apis.docstore.url}/project/${
- this.project_id
- }/doc/${this.doc_id}`,
- timeout: 30 * 1000,
- json: {
- lines: this.lines,
- version: this.version,
- ranges: this.ranges
- }
- })
- .should.equal(true)
- })
- it('should call the callback with the modified status and revision', function() {
- return this.callback
- .calledWith(null, this.modified, this.rev)
- .should.equal(true)
- })
- })
- describe('with a failed response code', function() {
- beforeEach(function() {
- this.request.post = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 500 }, '')
- return this.DocstoreManager.updateDoc(
- this.project_id,
- this.doc_id,
- this.lines,
- this.version,
- this.ranges,
- this.callback
- )
- })
- it('should call the callback with an error', function() {
- return this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(
- sinon.match.has(
- 'message',
- 'docstore api responded with non-success code: 500'
- )
- )
- )
- .should.equal(true)
- })
- })
- })
- describe('getDoc', function() {
- beforeEach(function() {
- return (this.doc = {
- lines: (this.lines = ['mock', 'doc', 'lines']),
- rev: (this.rev = 5),
- version: (this.version = 42),
- ranges: (this.ranges = { mock: 'ranges' })
- })
- })
- describe('with a successful response code', function() {
- beforeEach(function() {
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 204 }, this.doc)
- return this.DocstoreManager.getDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should get the doc from the docstore api', function() {
- return this.request.get
- .calledWith({
- url: `${this.settings.apis.docstore.url}/project/${
- this.project_id
- }/doc/${this.doc_id}`,
- timeout: 30 * 1000,
- json: true
- })
- .should.equal(true)
- })
- it('should call the callback with the lines, version and rev', function() {
- return this.callback
- .calledWith(null, this.lines, this.rev, this.version, this.ranges)
- .should.equal(true)
- })
- })
- describe('with a failed response code', function() {
- beforeEach(function() {
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 500 }, '')
- return this.DocstoreManager.getDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should call the callback with an error', function() {
- return this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(
- sinon.match.has(
- 'message',
- 'docstore api responded with non-success code: 500'
- )
- )
- )
- .should.equal(true)
- })
- })
- describe('with include_deleted=true', function() {
- beforeEach(function() {
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 204 }, this.doc)
- return this.DocstoreManager.getDoc(
- this.project_id,
- this.doc_id,
- { include_deleted: true },
- this.callback
- )
- })
- it('should get the doc from the docstore api (including deleted)', function() {
- return this.request.get
- .calledWith({
- url: `${this.settings.apis.docstore.url}/project/${
- this.project_id
- }/doc/${this.doc_id}?include_deleted=true`,
- timeout: 30 * 1000,
- json: true
- })
- .should.equal(true)
- })
- it('should call the callback with the lines, version and rev', function() {
- return this.callback
- .calledWith(null, this.lines, this.rev, this.version, this.ranges)
- .should.equal(true)
- })
- })
- describe('with a missing (404) response code', function() {
- beforeEach(function() {
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 404 }, '')
- return this.DocstoreManager.getDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should call the callback with an error', function() {
- return this.callback
- .calledWith(
- sinon.match
- .instanceOf(Errors.NotFoundError)
- .and(sinon.match.has('message', 'doc not found in docstore'))
- )
- .should.equal(true)
- })
- })
- })
- describe('getAllDocs', function() {
- describe('with a successful response code', function() {
- beforeEach(function() {
- this.request.get = sinon
- .stub()
- .callsArgWith(
- 1,
- null,
- { statusCode: 204 },
- (this.docs = [{ _id: 'mock-doc-id' }])
- )
- return this.DocstoreManager.getAllDocs(this.project_id, this.callback)
- })
- it('should get all the project docs in the docstore api', function() {
- return this.request.get
- .calledWith({
- url: `${this.settings.apis.docstore.url}/project/${
- this.project_id
- }/doc`,
- timeout: 30 * 1000,
- json: true
- })
- .should.equal(true)
- })
- it('should call the callback with the docs', function() {
- return this.callback.calledWith(null, this.docs).should.equal(true)
- })
- })
- describe('with a failed response code', function() {
- beforeEach(function() {
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 500 }, '')
- return this.DocstoreManager.getAllDocs(this.project_id, this.callback)
- })
- it('should call the callback with an error', function() {
- return this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(
- sinon.match.has(
- 'message',
- 'docstore api responded with non-success code: 500'
- )
- )
- )
- .should.equal(true)
- })
- })
- })
- describe('getAllRanges', function() {
- describe('with a successful response code', function() {
- beforeEach(function() {
- this.request.get = sinon
- .stub()
- .callsArgWith(
- 1,
- null,
- { statusCode: 204 },
- (this.docs = [{ _id: 'mock-doc-id', ranges: 'mock-ranges' }])
- )
- return this.DocstoreManager.getAllRanges(this.project_id, this.callback)
- })
- it('should get all the project doc ranges in the docstore api', function() {
- return this.request.get
- .calledWith({
- url: `${this.settings.apis.docstore.url}/project/${
- this.project_id
- }/ranges`,
- timeout: 30 * 1000,
- json: true
- })
- .should.equal(true)
- })
- it('should call the callback with the docs', function() {
- return this.callback.calledWith(null, this.docs).should.equal(true)
- })
- })
- describe('with a failed response code', function() {
- beforeEach(function() {
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 500 }, '')
- return this.DocstoreManager.getAllRanges(this.project_id, this.callback)
- })
- it('should call the callback with an error', function() {
- return this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(
- sinon.match.has(
- 'message',
- 'docstore api responded with non-success code: 500'
- )
- )
- )
- .should.equal(true)
- })
- })
- })
- describe('archiveProject', function() {
- describe('with a successful response code', function() {
- beforeEach(function() {
- this.request.post = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 204 })
- return this.DocstoreManager.archiveProject(
- this.project_id,
- this.callback
- )
- })
- it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- describe('with a failed response code', function() {
- beforeEach(function() {
- this.request.post = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 500 })
- return this.DocstoreManager.archiveProject(
- this.project_id,
- this.callback
- )
- })
- it('should call the callback with an error', function() {
- return this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(
- sinon.match.has(
- 'message',
- 'docstore api responded with non-success code: 500'
- )
- )
- )
- .should.equal(true)
- })
- })
- })
- describe('unarchiveProject', function() {
- describe('with a successful response code', function() {
- beforeEach(function() {
- this.request.post = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 204 })
- return this.DocstoreManager.unarchiveProject(
- this.project_id,
- this.callback
- )
- })
- it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- describe('with a failed response code', function() {
- beforeEach(function() {
- this.request.post = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 500 })
- return this.DocstoreManager.unarchiveProject(
- this.project_id,
- this.callback
- )
- })
- it('should call the callback with an error', function() {
- return this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(
- sinon.match.has(
- 'message',
- 'docstore api responded with non-success code: 500'
- )
- )
- )
- .should.equal(true)
- })
- })
- })
- describe('destroyProject', function() {
- describe('with a successful response code', function() {
- beforeEach(function() {
- this.request.post = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 204 })
- return this.DocstoreManager.destroyProject(
- this.project_id,
- this.callback
- )
- })
- it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- describe('with a failed response code', function() {
- beforeEach(function() {
- this.request.post = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 500 })
- return this.DocstoreManager.destroyProject(
- this.project_id,
- this.callback
- )
- })
- it('should call the callback with an error', function() {
- return this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(
- sinon.match.has(
- 'message',
- 'docstore api responded with non-success code: 500'
- )
- )
- )
- .should.equal(true)
- })
- })
- })
- })
|