| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- const sinon = require('sinon')
- const modulePath = '../../../../app/src/Features/Docstore/DocstoreManager'
- const SandboxedModule = require('sandboxed-module')
- const Errors = require('../../../../app/src/Features/Errors/Errors')
- const tk = require('timekeeper')
- describe('DocstoreManager', function () {
- beforeEach(function () {
- this.requestDefaults = sinon.stub().returns((this.request = sinon.stub()))
- this.DocstoreManager = SandboxedModule.require(modulePath, {
- requires: {
- request: {
- defaults: this.requestDefaults,
- },
- '@overleaf/settings': (this.settings = {
- apis: {
- docstore: {
- url: 'docstore.overleaf.com',
- },
- },
- }),
- },
- })
- this.requestDefaults.calledWith({ jar: false }).should.equal(true)
- this.project_id = 'project-id-123'
- this.doc_id = 'doc-id-123'
- this.callback = sinon.stub()
- })
- describe('deleteDoc', function () {
- describe('with a successful response code', function () {
- // for assertions on the deletedAt timestamp, we need to freeze the clock.
- before(function () {
- tk.freeze(Date.now())
- })
- after(function () {
- tk.reset()
- })
- beforeEach(function () {
- this.request.patch = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 204 }, '')
- this.DocstoreManager.deleteDoc(
- this.project_id,
- this.doc_id,
- 'wombat.tex',
- new Date(),
- this.callback
- )
- })
- it('should delete the doc in the docstore api', function () {
- this.request.patch
- .calledWith({
- url: `${this.settings.apis.docstore.url}/project/${this.project_id}/doc/${this.doc_id}`,
- json: { deleted: true, deletedAt: new Date(), name: 'wombat.tex' },
- timeout: 30 * 1000,
- })
- .should.equal(true)
- })
- it('should call the callback without an error', function () {
- this.callback.calledWith(null).should.equal(true)
- })
- })
- describe('with a failed response code', function () {
- beforeEach(function () {
- this.request.patch = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 500 }, '')
- this.DocstoreManager.deleteDoc(
- this.project_id,
- this.doc_id,
- 'main.tex',
- new Date(),
- this.callback
- )
- })
- it('should call the callback with an error', function () {
- 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.patch = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 404 }, '')
- this.DocstoreManager.deleteDoc(
- this.project_id,
- this.doc_id,
- 'main.tex',
- new Date(),
- 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' }
- 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 }
- )
- 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 () {
- 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 () {
- 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 }, '')
- 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 () {
- 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 () {
- 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)
- this.DocstoreManager.getDoc(this.project_id, this.doc_id, this.callback)
- })
- it('should get the doc from the docstore api', function () {
- this.request.get.should.have.been.calledWith({
- url: `${this.settings.apis.docstore.url}/project/${this.project_id}/doc/${this.doc_id}`,
- timeout: 30 * 1000,
- json: true,
- })
- })
- it('should call the callback with the lines, version and rev', function () {
- 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 }, '')
- this.DocstoreManager.getDoc(this.project_id, this.doc_id, this.callback)
- })
- it('should call the callback with an error', function () {
- 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)
- 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 () {
- this.request.get.should.have.been.calledWith({
- url: `${this.settings.apis.docstore.url}/project/${this.project_id}/doc/${this.doc_id}`,
- qs: { include_deleted: 'true' },
- timeout: 30 * 1000,
- json: true,
- })
- })
- it('should call the callback with the lines, version and rev', function () {
- this.callback
- .calledWith(null, this.lines, this.rev, this.version, this.ranges)
- .should.equal(true)
- })
- })
- describe('with peek=true', function () {
- beforeEach(function () {
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 204 }, this.doc)
- this.DocstoreManager.getDoc(
- this.project_id,
- this.doc_id,
- { peek: true },
- this.callback
- )
- })
- it('should call the docstore peek url', function () {
- this.request.get.should.have.been.calledWith({
- url: `${this.settings.apis.docstore.url}/project/${this.project_id}/doc/${this.doc_id}/peek`,
- timeout: 30 * 1000,
- json: true,
- })
- })
- })
- describe('with a missing (404) response code', function () {
- beforeEach(function () {
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 404 }, '')
- this.DocstoreManager.getDoc(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', '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' }])
- )
- this.DocstoreManager.getAllDocs(this.project_id, this.callback)
- })
- it('should get all the project docs in the docstore api', function () {
- 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 () {
- 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 }, '')
- this.DocstoreManager.getAllDocs(this.project_id, this.callback)
- })
- it('should call the callback with an error', function () {
- this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(
- sinon.match.has(
- 'message',
- 'docstore api responded with non-success code: 500'
- )
- )
- )
- .should.equal(true)
- })
- })
- })
- describe('getAllDeletedDocs', function () {
- describe('with a successful response code', function () {
- beforeEach(function (done) {
- this.callback.callsFake(done)
- this.docs = [{ _id: 'mock-doc-id', name: 'foo.tex' }]
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 200 }, this.docs)
- this.DocstoreManager.getAllDeletedDocs(this.project_id, this.callback)
- })
- it('should get all the project docs in the docstore api', function () {
- this.request.get.should.have.been.calledWith({
- url: `${this.settings.apis.docstore.url}/project/${this.project_id}/doc-deleted`,
- timeout: 30 * 1000,
- json: true,
- })
- })
- it('should call the callback with the docs', function () {
- this.callback.should.have.been.calledWith(null, this.docs)
- })
- })
- describe('with an error', function () {
- beforeEach(function (done) {
- this.callback.callsFake(() => done())
- this.request.get = sinon
- .stub()
- .callsArgWith(1, new Error('connect failed'))
- this.DocstoreManager.getAllDocs(this.project_id, this.callback)
- })
- it('should call the callback with an error', function () {
- this.callback.should.have.been.calledWith(
- sinon.match
- .instanceOf(Error)
- .and(sinon.match.has('message', 'connect failed'))
- )
- })
- })
- describe('with a failed response code', function () {
- beforeEach(function (done) {
- this.callback.callsFake(() => done())
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 500 })
- this.DocstoreManager.getAllDocs(this.project_id, this.callback)
- })
- it('should call the callback with an error', function () {
- this.callback.should.have.been.calledWith(
- sinon.match
- .instanceOf(Error)
- .and(
- sinon.match.has(
- 'message',
- 'docstore api responded with non-success code: 500'
- )
- )
- )
- })
- })
- })
- 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' }])
- )
- this.DocstoreManager.getAllRanges(this.project_id, this.callback)
- })
- it('should get all the project doc ranges in the docstore api', function () {
- 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 () {
- 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 }, '')
- this.DocstoreManager.getAllRanges(this.project_id, this.callback)
- })
- it('should call the callback with an error', function () {
- 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 })
- this.DocstoreManager.archiveProject(this.project_id, this.callback)
- })
- it('should call the callback', function () {
- 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 })
- this.DocstoreManager.archiveProject(this.project_id, this.callback)
- })
- it('should call the callback with an error', function () {
- 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 })
- this.DocstoreManager.unarchiveProject(this.project_id, this.callback)
- })
- it('should call the callback', function () {
- 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 })
- this.DocstoreManager.unarchiveProject(this.project_id, this.callback)
- })
- it('should call the callback with an error', function () {
- 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 })
- this.DocstoreManager.destroyProject(this.project_id, this.callback)
- })
- it('should call the callback', function () {
- 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 })
- this.DocstoreManager.destroyProject(this.project_id, this.callback)
- })
- it('should call the callback with an error', function () {
- this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(
- sinon.match.has(
- 'message',
- 'docstore api responded with non-success code: 500'
- )
- )
- )
- .should.equal(true)
- })
- })
- })
- })
|