| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- const sinon = require('sinon')
- const modulePath = '../../../../app/js/PersistenceManager.js'
- const SandboxedModule = require('sandboxed-module')
- const Errors = require('../../../../app/js/Errors')
- describe('PersistenceManager', function () {
- beforeEach(function () {
- this.request = sinon.stub()
- this.request.defaults = () => this.request
- this.Metrics = {
- Timer: class Timer {},
- inc: sinon.stub(),
- }
- this.Metrics.Timer.prototype.done = sinon.stub()
- this.Settings = {}
- this.PersistenceManager = SandboxedModule.require(modulePath, {
- requires: {
- requestretry: this.request,
- '@overleaf/settings': this.Settings,
- './Metrics': this.Metrics,
- './Errors': Errors,
- },
- })
- this.project_id = 'project-id-123'
- this.projectHistoryId = 'history-id-123'
- this.doc_id = 'doc-id-123'
- this.lines = ['one', 'two', 'three']
- this.version = 42
- this.callback = sinon.stub()
- this.ranges = { comments: 'mock', entries: 'mock' }
- this.pathname = '/a/b/c.tex'
- this.lastUpdatedAt = Date.now()
- this.lastUpdatedBy = 'last-author-id'
- this.Settings.apis = {
- web: {
- url: (this.url = 'www.example.com'),
- user: (this.user = 'overleaf'),
- pass: (this.pass = 'password'),
- },
- }
- })
- describe('getDoc', function () {
- beforeEach(function () {
- this.webResponse = {
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- }
- })
- describe('with a successful response from the web api', function () {
- beforeEach(function () {
- this.request.callsArgWith(
- 1,
- null,
- { statusCode: 200 },
- JSON.stringify(this.webResponse)
- )
- this.PersistenceManager.getDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should call the web api', function () {
- this.request
- .calledWith({
- url: `${this.url}/project/${this.project_id}/doc/${this.doc_id}`,
- method: 'GET',
- headers: {
- accept: 'application/json',
- },
- auth: {
- user: this.user,
- pass: this.pass,
- sendImmediately: true,
- },
- jar: false,
- timeout: 5000,
- })
- .should.equal(true)
- })
- it('should call the callback with the doc lines, version and ranges', function () {
- this.callback
- .calledWith(
- null,
- this.lines,
- this.version,
- this.ranges,
- this.pathname,
- this.projectHistoryId
- )
- .should.equal(true)
- })
- it('should time the execution', function () {
- this.Metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should increment the metric', function () {
- this.Metrics.inc
- .calledWith('getDoc', 1, { status: 200 })
- .should.equal(true)
- })
- })
- describe('with the peek option', function () {
- beforeEach(function () {
- this.request.yields(
- null,
- { statusCode: 200 },
- JSON.stringify(this.webResponse)
- )
- this.PersistenceManager.getDoc(
- this.project_id,
- this.doc_id,
- { peek: true },
- this.callback
- )
- })
- it('should call the web api with a peek param', function () {
- this.request
- .calledWith({
- url: `${this.url}/project/${this.project_id}/doc/${this.doc_id}`,
- qs: { peek: 'true' },
- method: 'GET',
- headers: {
- accept: 'application/json',
- },
- auth: {
- user: this.user,
- pass: this.pass,
- sendImmediately: true,
- },
- jar: false,
- timeout: 5000,
- })
- .should.equal(true)
- })
- })
- describe('when request returns an error', function () {
- beforeEach(function () {
- this.error = new Error('oops')
- this.error.code = 'EOOPS'
- this.request.callsArgWith(1, this.error, null, null)
- this.PersistenceManager.getDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should return a generic connection error', function () {
- this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(sinon.match.has('message', 'error connecting to web API'))
- )
- .should.equal(true)
- })
- it('should time the execution', function () {
- this.Metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should increment the metric', function () {
- this.Metrics.inc
- .calledWith('getDoc', 1, { status: 'EOOPS' })
- .should.equal(true)
- })
- })
- describe('when the request returns 404', function () {
- beforeEach(function () {
- this.request.callsArgWith(1, null, { statusCode: 404 }, '')
- this.PersistenceManager.getDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should return a NotFoundError', function () {
- this.callback
- .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
- .should.equal(true)
- })
- it('should time the execution', function () {
- this.Metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should increment the metric', function () {
- this.Metrics.inc
- .calledWith('getDoc', 1, { status: 404 })
- .should.equal(true)
- })
- })
- describe('when the request returns 413', function () {
- beforeEach(function () {
- this.request.callsArgWith(1, null, { statusCode: 413 }, '')
- this.PersistenceManager.getDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should return a FileTooLargeError', function () {
- this.callback
- .calledWith(sinon.match.instanceOf(Errors.FileTooLargeError))
- .should.equal(true)
- })
- it('should time the execution', function () {
- this.Metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should increment the metric', function () {
- this.Metrics.inc
- .calledWith('getDoc', 1, { status: 413 })
- .should.equal(true)
- })
- })
- describe('when the request returns an error status code', function () {
- beforeEach(function () {
- this.request.callsArgWith(1, null, { statusCode: 500 }, '')
- this.PersistenceManager.getDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should return an error', function () {
- this.callback
- .calledWith(sinon.match.instanceOf(Error))
- .should.equal(true)
- })
- it('should time the execution', function () {
- this.Metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should increment the metric', function () {
- this.Metrics.inc
- .calledWith('getDoc', 1, { status: 500 })
- .should.equal(true)
- })
- })
- describe('when request returns an doc without lines', function () {
- beforeEach(function () {
- delete this.webResponse.lines
- this.request.callsArgWith(
- 1,
- null,
- { statusCode: 200 },
- JSON.stringify(this.webResponse)
- )
- this.PersistenceManager.getDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should return and error', function () {
- this.callback
- .calledWith(sinon.match.instanceOf(Error))
- .should.equal(true)
- })
- })
- describe('when request returns an doc without a version', function () {
- beforeEach(function () {
- delete this.webResponse.version
- this.request.callsArgWith(
- 1,
- null,
- { statusCode: 200 },
- JSON.stringify(this.webResponse)
- )
- this.PersistenceManager.getDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should return and error', function () {
- this.callback
- .calledWith(sinon.match.instanceOf(Error))
- .should.equal(true)
- })
- })
- describe('when request returns an doc without a pathname', function () {
- beforeEach(function () {
- delete this.webResponse.pathname
- this.request.callsArgWith(
- 1,
- null,
- { statusCode: 200 },
- JSON.stringify(this.webResponse)
- )
- this.PersistenceManager.getDoc(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should return and error', function () {
- this.callback
- .calledWith(sinon.match.instanceOf(Error))
- .should.equal(true)
- })
- })
- })
- describe('setDoc', function () {
- describe('with a successful response from the web api', function () {
- beforeEach(function () {
- this.request.callsArgWith(1, null, { statusCode: 200 })
- this.PersistenceManager.setDoc(
- this.project_id,
- this.doc_id,
- this.lines,
- this.version,
- this.ranges,
- this.lastUpdatedAt,
- this.lastUpdatedBy,
- this.callback
- )
- })
- it('should call the web api', function () {
- this.request
- .calledWith({
- url: `${this.url}/project/${this.project_id}/doc/${this.doc_id}`,
- json: {
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- lastUpdatedAt: this.lastUpdatedAt,
- lastUpdatedBy: this.lastUpdatedBy,
- },
- method: 'POST',
- auth: {
- user: this.user,
- pass: this.pass,
- sendImmediately: true,
- },
- jar: false,
- timeout: 5000,
- })
- .should.equal(true)
- })
- it('should call the callback without error', function () {
- this.callback.calledWith(null).should.equal(true)
- })
- it('should time the execution', function () {
- this.Metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should increment the metric', function () {
- this.Metrics.inc
- .calledWith('setDoc', 1, { status: 200 })
- .should.equal(true)
- })
- })
- describe('when request returns an error', function () {
- beforeEach(function () {
- this.error = new Error('oops')
- this.error.code = 'EOOPS'
- this.request.callsArgWith(1, this.error, null, null)
- this.PersistenceManager.setDoc(
- this.project_id,
- this.doc_id,
- this.lines,
- this.version,
- this.ranges,
- this.lastUpdatedAt,
- this.lastUpdatedBy,
- this.callback
- )
- })
- it('should return a generic connection error', function () {
- this.callback
- .calledWith(
- sinon.match
- .instanceOf(Error)
- .and(sinon.match.has('message', 'error connecting to web API'))
- )
- .should.equal(true)
- })
- it('should time the execution', function () {
- this.Metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should increment the metric', function () {
- this.Metrics.inc
- .calledWith('setDoc', 1, { status: 'EOOPS' })
- .should.equal(true)
- })
- })
- describe('when the request returns 404', function () {
- beforeEach(function () {
- this.request.callsArgWith(1, null, { statusCode: 404 }, '')
- this.PersistenceManager.setDoc(
- this.project_id,
- this.doc_id,
- this.lines,
- this.version,
- this.ranges,
- this.lastUpdatedAt,
- this.lastUpdatedBy,
- this.callback
- )
- })
- it('should return a NotFoundError', function () {
- this.callback
- .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
- .should.equal(true)
- })
- it('should time the execution', function () {
- this.Metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should increment the metric', function () {
- this.Metrics.inc
- .calledWith('setDoc', 1, { status: 404 })
- .should.equal(true)
- })
- })
- describe('when the request returns 413', function () {
- beforeEach(function () {
- this.request.callsArgWith(1, null, { statusCode: 413 }, '')
- this.PersistenceManager.setDoc(
- this.project_id,
- this.doc_id,
- this.lines,
- this.version,
- this.ranges,
- this.lastUpdatedAt,
- this.lastUpdatedBy,
- this.callback
- )
- })
- it('should return a FileTooLargeError', function () {
- this.callback
- .calledWith(sinon.match.instanceOf(Errors.FileTooLargeError))
- .should.equal(true)
- })
- it('should time the execution', function () {
- this.Metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should increment the metric', function () {
- this.Metrics.inc
- .calledWith('setDoc', 1, { status: 413 })
- .should.equal(true)
- })
- })
- describe('when the request returns an error status code', function () {
- beforeEach(function () {
- this.request.callsArgWith(1, null, { statusCode: 500 }, '')
- this.PersistenceManager.setDoc(
- this.project_id,
- this.doc_id,
- this.lines,
- this.version,
- this.ranges,
- this.lastUpdatedAt,
- this.lastUpdatedBy,
- this.callback
- )
- })
- it('should return an error', function () {
- this.callback
- .calledWith(sinon.match.instanceOf(Error))
- .should.equal(true)
- })
- it('should time the execution', function () {
- this.Metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should increment the metric', function () {
- this.Metrics.inc
- .calledWith('setDoc', 1, { status: 500 })
- .should.equal(true)
- })
- })
- })
- })
|