diff --git a/services/document-updater/test/acceptance/js/PeekingADoc.js b/services/document-updater/test/acceptance/js/PeekingADoc.js new file mode 100644 index 0000000000..43e463ca51 --- /dev/null +++ b/services/document-updater/test/acceptance/js/PeekingADoc.js @@ -0,0 +1,99 @@ +const sinon = require('sinon') +const MockWebApi = require('./helpers/MockWebApi') +const DocUpdaterClient = require('./helpers/DocUpdaterClient') +const DocUpdaterApp = require('./helpers/DocUpdaterApp') + +describe('Peeking a document', function () { + before(function (done) { + this.lines = ['one', 'two', 'three'] + this.version = 42 + return DocUpdaterApp.ensureRunning(done) + }) + + describe('when the document is not loaded', function () { + before(function (done) { + this.project_id = DocUpdaterClient.randomId() + this.doc_id = DocUpdaterClient.randomId() + sinon.spy(MockWebApi, 'getDocument') + + MockWebApi.insertDoc(this.project_id, this.doc_id, { + lines: this.lines, + version: this.version, + }) + + return DocUpdaterClient.peekDoc( + this.project_id, + this.doc_id, + (error, res, returnedDoc) => { + this.error = error + this.res = res + this.returnedDoc = returnedDoc + return done() + } + ) + }) + + after(function () { + return MockWebApi.getDocument.restore() + }) + + it('should return a 404 response', function () { + this.res.statusCode.should.equal(404) + }) + + it('should not load the document from the web API', function () { + return MockWebApi.getDocument.called.should.equal(false) + }) + }) + + describe('when the document is already loaded', function () { + before(function (done) { + this.project_id = DocUpdaterClient.randomId() + this.doc_id = DocUpdaterClient.randomId() + + MockWebApi.insertDoc(this.project_id, this.doc_id, { + lines: this.lines, + version: this.version, + }) + return DocUpdaterClient.preloadDoc( + this.project_id, + this.doc_id, + error => { + if (error != null) { + throw error + } + sinon.spy(MockWebApi, 'getDocument') + return DocUpdaterClient.getDoc( + this.project_id, + this.doc_id, + (error, res, returnedDoc) => { + this.res = res + this.returnedDoc = returnedDoc + return done() + } + ) + } + ) + }) + + after(function () { + return MockWebApi.getDocument.restore() + }) + + it('should return a 200 response', function () { + this.res.statusCode.should.equal(200) + }) + + it('should return the document lines', function () { + return this.returnedDoc.lines.should.deep.equal(this.lines) + }) + + it('should return the document version', function () { + return this.returnedDoc.version.should.equal(this.version) + }) + + it('should not load the document from the web API', function () { + return MockWebApi.getDocument.called.should.equal(false) + }) + }) +}) diff --git a/services/document-updater/test/acceptance/js/helpers/DocUpdaterClient.js b/services/document-updater/test/acceptance/js/helpers/DocUpdaterClient.js index 71e7915c0f..9a3234628c 100644 --- a/services/document-updater/test/acceptance/js/helpers/DocUpdaterClient.js +++ b/services/document-updater/test/acceptance/js/helpers/DocUpdaterClient.js @@ -123,6 +123,18 @@ module.exports = DocUpdaterClient = { DocUpdaterClient.getDoc(projectId, docId, callback) }, + peekDoc(projectId, docId, callback) { + request.get( + `http://localhost:3003/project/${projectId}/doc/${docId}/peek`, + (error, res, body) => { + if (body != null && res.statusCode >= 200 && res.statusCode < 300) { + body = JSON.parse(body) + } + callback(error, res, body) + } + ) + }, + flushDoc(projectId, docId, callback) { request.post( `http://localhost:3003/project/${projectId}/doc/${docId}/flush`,