2020-02-16 09:02:55 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
2021-03-12 17:16:01 -05:00
|
|
|
const { assert, expect } = require('chai')
|
2024-11-08 05:21:56 -05:00
|
|
|
const modulePath = require('node:path').join(
|
2020-02-16 09:02:55 -05:00
|
|
|
__dirname,
|
|
|
|
'../../../app/js/HttpController'
|
|
|
|
)
|
2024-07-15 07:10:48 -04:00
|
|
|
const { ObjectId } = require('mongodb-legacy')
|
2022-04-26 07:16:36 -04:00
|
|
|
const Errors = require('../../../app/js/Errors')
|
2020-02-16 09:02:55 -05:00
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('HttpController', function () {
|
|
|
|
beforeEach(function () {
|
2020-09-10 13:07:14 -04:00
|
|
|
const settings = {
|
2021-07-13 07:04:48 -04:00
|
|
|
max_doc_length: 2 * 1024 * 1024,
|
2020-09-10 13:07:14 -04:00
|
|
|
}
|
2022-04-26 07:16:36 -04:00
|
|
|
this.DocArchiveManager = {
|
|
|
|
unArchiveAllDocs: sinon.stub().yields(),
|
|
|
|
}
|
|
|
|
this.DocManager = {}
|
2020-02-16 09:02:55 -05:00
|
|
|
this.HttpController = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2022-04-26 07:16:36 -04:00
|
|
|
'./DocManager': this.DocManager,
|
|
|
|
'./DocArchiveManager': this.DocArchiveManager,
|
2021-07-12 12:47:20 -04:00
|
|
|
'@overleaf/settings': settings,
|
2021-07-13 07:04:48 -04:00
|
|
|
'./HealthChecker': {},
|
2022-04-26 07:16:36 -04:00
|
|
|
'./Errors': Errors,
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
this.res = {
|
|
|
|
send: sinon.stub(),
|
2020-03-18 10:07:01 -04:00
|
|
|
sendStatus: sinon.stub(),
|
2020-02-16 09:02:55 -05:00
|
|
|
json: sinon.stub(),
|
2021-07-13 07:04:48 -04:00
|
|
|
setHeader: sinon.stub(),
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
|
|
|
this.res.status = sinon.stub().returns(this.res)
|
|
|
|
this.req = { query: {} }
|
|
|
|
this.next = sinon.stub()
|
2022-04-19 08:28:47 -04:00
|
|
|
this.projectId = 'mock-project-id'
|
|
|
|
this.docId = 'mock-doc-id'
|
2020-02-16 09:02:55 -05:00
|
|
|
this.doc = {
|
2022-04-19 08:28:47 -04:00
|
|
|
_id: this.docId,
|
2020-02-16 09:02:55 -05:00
|
|
|
lines: ['mock', 'lines', ' here', '', '', ' spaces '],
|
|
|
|
version: 42,
|
2021-07-13 07:04:48 -04:00
|
|
|
rev: 5,
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
this.deletedDoc = {
|
2020-02-16 09:02:55 -05:00
|
|
|
deleted: true,
|
2022-04-19 08:28:47 -04:00
|
|
|
_id: this.docId,
|
2020-02-16 09:02:55 -05:00
|
|
|
lines: ['mock', 'lines', ' here', '', '', ' spaces '],
|
|
|
|
version: 42,
|
2021-07-13 07:04:48 -04:00
|
|
|
rev: 5,
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('getDoc', function () {
|
|
|
|
describe('without deleted docs', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.req.params = {
|
2022-04-19 08:28:47 -04:00
|
|
|
project_id: this.projectId,
|
|
|
|
doc_id: this.docId,
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
|
|
|
this.DocManager.getFullDoc = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, this.doc)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.getDoc(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should get the document with the version (including deleted)', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.DocManager.getFullDoc
|
|
|
|
.calledWith(this.projectId, this.docId)
|
2020-02-16 09:02:55 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should return the doc as JSON', function () {
|
|
|
|
this.res.json
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith({
|
2022-04-19 08:28:47 -04:00
|
|
|
_id: this.docId,
|
2020-02-16 09:02:55 -05:00
|
|
|
lines: this.doc.lines,
|
|
|
|
rev: this.doc.rev,
|
2021-07-13 07:04:48 -04:00
|
|
|
version: this.doc.version,
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
describe('which is deleted', function () {
|
2020-05-28 09:20:54 -04:00
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.req.params = {
|
2022-04-19 08:28:47 -04:00
|
|
|
project_id: this.projectId,
|
|
|
|
doc_id: this.docId,
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
this.DocManager.getFullDoc = sinon
|
2020-02-16 09:02:55 -05:00
|
|
|
.stub()
|
2022-04-19 08:28:47 -04:00
|
|
|
.callsArgWith(2, null, this.deletedDoc)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should get the doc from the doc manager', function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.HttpController.getDoc(this.req, this.res, this.next)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.DocManager.getFullDoc
|
|
|
|
.calledWith(this.projectId, this.docId)
|
2020-02-16 09:02:55 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should return 404 if the query string delete is not set ', function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.HttpController.getDoc(this.req, this.res, this.next)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.res.sendStatus.calledWith(404).should.equal(true)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should return the doc as JSON if include_deleted is set to true', function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.req.query.include_deleted = 'true'
|
|
|
|
this.HttpController.getDoc(this.req, this.res, this.next)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.res.json
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith({
|
2022-04-19 08:28:47 -04:00
|
|
|
_id: this.docId,
|
2020-02-16 09:02:55 -05:00
|
|
|
lines: this.doc.lines,
|
|
|
|
rev: this.doc.rev,
|
|
|
|
deleted: true,
|
2021-07-13 07:04:48 -04:00
|
|
|
version: this.doc.version,
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('getRawDoc', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.req.params = {
|
2022-04-19 08:28:47 -04:00
|
|
|
project_id: this.projectId,
|
|
|
|
doc_id: this.docId,
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
|
|
|
this.DocManager.getDocLines = sinon.stub().callsArgWith(2, null, this.doc)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.getRawDoc(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should get the document without the version', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.DocManager.getDocLines
|
|
|
|
.calledWith(this.projectId, this.docId)
|
2020-02-16 09:02:55 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should set the content type header', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.res.setHeader
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith('content-type', 'text/plain')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should send the raw version of the doc', function () {
|
|
|
|
assert.deepEqual(
|
2020-02-16 09:02:55 -05:00
|
|
|
this.res.send.args[0][0],
|
|
|
|
`${this.doc.lines[0]}\n${this.doc.lines[1]}\n${this.doc.lines[2]}\n${this.doc.lines[3]}\n${this.doc.lines[4]}\n${this.doc.lines[5]}`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('getAllDocs', function () {
|
|
|
|
describe('normally', function () {
|
|
|
|
beforeEach(function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.req.params = { project_id: this.projectId }
|
2020-02-16 09:02:55 -05:00
|
|
|
this.docs = [
|
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(),
|
2020-02-16 09:02:55 -05:00
|
|
|
lines: ['mock', 'lines', 'one'],
|
2021-07-13 07:04:48 -04:00
|
|
|
rev: 2,
|
2020-02-16 09:02:55 -05:00
|
|
|
},
|
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(),
|
2020-02-16 09:02:55 -05:00
|
|
|
lines: ['mock', 'lines', 'two'],
|
2021-07-13 07:04:48 -04:00
|
|
|
rev: 4,
|
|
|
|
},
|
2020-02-16 09:02:55 -05:00
|
|
|
]
|
|
|
|
this.DocManager.getAllNonDeletedDocs = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, this.docs)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.getAllDocs(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should get all the (non-deleted) docs', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.DocManager.getAllNonDeletedDocs
|
|
|
|
.calledWith(this.projectId, { lines: true, rev: true })
|
2020-02-16 09:02:55 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should return the doc as JSON', function () {
|
|
|
|
this.res.json
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith([
|
|
|
|
{
|
|
|
|
_id: this.docs[0]._id.toString(),
|
|
|
|
lines: this.docs[0].lines,
|
2021-07-13 07:04:48 -04:00
|
|
|
rev: this.docs[0].rev,
|
2020-02-16 09:02:55 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
_id: this.docs[1]._id.toString(),
|
|
|
|
lines: this.docs[1].lines,
|
2021-07-13 07:04:48 -04:00
|
|
|
rev: this.docs[1].rev,
|
|
|
|
},
|
2020-02-16 09:02:55 -05:00
|
|
|
])
|
|
|
|
.should.equal(true)
|
2022-10-11 07:33:01 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with null lines', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.req.params = { project_id: this.projectId }
|
|
|
|
this.docs = [
|
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(),
|
2022-10-11 07:33:01 -04:00
|
|
|
lines: null,
|
|
|
|
rev: 2,
|
|
|
|
},
|
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(),
|
2022-10-11 07:33:01 -04:00
|
|
|
lines: ['mock', 'lines', 'two'],
|
|
|
|
rev: 4,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
this.DocManager.getAllNonDeletedDocs = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, this.docs)
|
|
|
|
this.HttpController.getAllDocs(this.req, this.res, this.next)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the doc with fallback lines', function () {
|
|
|
|
this.res.json
|
|
|
|
.calledWith([
|
|
|
|
{
|
|
|
|
_id: this.docs[0]._id.toString(),
|
|
|
|
lines: [],
|
|
|
|
rev: this.docs[0].rev,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
_id: this.docs[1]._id.toString(),
|
|
|
|
lines: this.docs[1].lines,
|
|
|
|
rev: this.docs[1].rev,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
.should.equal(true)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
describe('with a null doc', function () {
|
2020-05-28 09:20:54 -04:00
|
|
|
beforeEach(function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.req.params = { project_id: this.projectId }
|
2020-02-16 09:02:55 -05:00
|
|
|
this.docs = [
|
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(),
|
2020-02-16 09:02:55 -05:00
|
|
|
lines: ['mock', 'lines', 'one'],
|
2021-07-13 07:04:48 -04:00
|
|
|
rev: 2,
|
2020-02-16 09:02:55 -05:00
|
|
|
},
|
|
|
|
null,
|
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(),
|
2020-02-16 09:02:55 -05:00
|
|
|
lines: ['mock', 'lines', 'two'],
|
2021-07-13 07:04:48 -04:00
|
|
|
rev: 4,
|
|
|
|
},
|
2020-02-16 09:02:55 -05:00
|
|
|
]
|
|
|
|
this.DocManager.getAllNonDeletedDocs = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, this.docs)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.getAllDocs(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should return the non null docs as JSON', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.res.json
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith([
|
|
|
|
{
|
|
|
|
_id: this.docs[0]._id.toString(),
|
|
|
|
lines: this.docs[0].lines,
|
2021-07-13 07:04:48 -04:00
|
|
|
rev: this.docs[0].rev,
|
2020-02-16 09:02:55 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
_id: this.docs[2]._id.toString(),
|
|
|
|
lines: this.docs[2].lines,
|
2021-07-13 07:04:48 -04:00
|
|
|
rev: this.docs[2].rev,
|
|
|
|
},
|
2020-02-16 09:02:55 -05:00
|
|
|
])
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should log out an error', function () {
|
|
|
|
this.logger.error
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith(
|
|
|
|
{
|
|
|
|
err: sinon.match.has('message', 'null doc'),
|
2022-04-19 08:28:47 -04:00
|
|
|
projectId: this.projectId,
|
2020-02-16 09:02:55 -05:00
|
|
|
},
|
|
|
|
'encountered null doc'
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('getAllRanges', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
describe('normally', function () {
|
2020-05-28 09:20:54 -04:00
|
|
|
beforeEach(function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.req.params = { project_id: this.projectId }
|
2020-02-16 09:02:55 -05:00
|
|
|
this.docs = [
|
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(),
|
2021-07-13 07:04:48 -04:00
|
|
|
ranges: { mock_ranges: 'one' },
|
2020-02-16 09:02:55 -05:00
|
|
|
},
|
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(),
|
2021-07-13 07:04:48 -04:00
|
|
|
ranges: { mock_ranges: 'two' },
|
|
|
|
},
|
2020-02-16 09:02:55 -05:00
|
|
|
]
|
|
|
|
this.DocManager.getAllNonDeletedDocs = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, this.docs)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.getAllRanges(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should get all the (non-deleted) doc ranges', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.DocManager.getAllNonDeletedDocs
|
|
|
|
.calledWith(this.projectId, { ranges: true })
|
2020-02-16 09:02:55 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should return the doc as JSON', function () {
|
|
|
|
this.res.json
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith([
|
|
|
|
{
|
|
|
|
_id: this.docs[0]._id.toString(),
|
2021-07-13 07:04:48 -04:00
|
|
|
ranges: this.docs[0].ranges,
|
2020-02-16 09:02:55 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
_id: this.docs[1]._id.toString(),
|
2021-07-13 07:04:48 -04:00
|
|
|
ranges: this.docs[1].ranges,
|
|
|
|
},
|
2020-02-16 09:02:55 -05:00
|
|
|
])
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('updateDoc', function () {
|
|
|
|
beforeEach(function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.req.params = {
|
|
|
|
project_id: this.projectId,
|
|
|
|
doc_id: this.docId,
|
|
|
|
}
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the doc lines exist and were updated', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.req.body = {
|
|
|
|
lines: (this.lines = ['hello', 'world']),
|
|
|
|
version: (this.version = 42),
|
2021-07-13 07:04:48 -04:00
|
|
|
ranges: (this.ranges = { changes: 'mock' }),
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
|
|
|
this.DocManager.updateDoc = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, true, (this.rev = 5))
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.updateDoc(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should update the document', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.DocManager.updateDoc
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith(
|
2022-04-19 08:28:47 -04:00
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
2020-02-16 09:02:55 -05:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ranges
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should return a modified status', function () {
|
|
|
|
this.res.json
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith({ modified: true, rev: this.rev })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the doc lines exist and were not updated', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.req.body = {
|
|
|
|
lines: (this.lines = ['hello', 'world']),
|
|
|
|
version: (this.version = 42),
|
2021-07-13 07:04:48 -04:00
|
|
|
ranges: {},
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
|
|
|
this.DocManager.updateDoc = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, false, (this.rev = 5))
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.updateDoc(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should return a modified status', function () {
|
|
|
|
this.res.json
|
2020-02-16 09:02:55 -05:00
|
|
|
.calledWith({ modified: false, rev: this.rev })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the doc lines are not provided', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.req.body = { version: 42, ranges: {} }
|
|
|
|
this.DocManager.updateDoc = sinon.stub().yields(null, false)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.updateDoc(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should not update the document', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.DocManager.updateDoc.called.should.equal(false)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should return a 400 (bad request) response', function () {
|
|
|
|
this.res.sendStatus.calledWith(400).should.equal(true)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the doc version are not provided', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.req.body = { version: 42, lines: ['hello world'] }
|
|
|
|
this.DocManager.updateDoc = sinon.stub().yields(null, false)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.updateDoc(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should not update the document', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.DocManager.updateDoc.called.should.equal(false)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should return a 400 (bad request) response', function () {
|
|
|
|
this.res.sendStatus.calledWith(400).should.equal(true)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('when the doc ranges is not provided', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.req.body = { lines: ['foo'], version: 42 }
|
|
|
|
this.DocManager.updateDoc = sinon.stub().yields(null, false)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.updateDoc(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should not update the document', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.DocManager.updateDoc.called.should.equal(false)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should return a 400 (bad request) response', function () {
|
|
|
|
this.res.sendStatus.calledWith(400).should.equal(true)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
describe('when the doc body is too large', function () {
|
2020-05-28 09:20:54 -04:00
|
|
|
beforeEach(function () {
|
2020-02-16 09:02:55 -05:00
|
|
|
this.req.body = {
|
|
|
|
lines: (this.lines = Array(2049).fill('a'.repeat(1024))),
|
|
|
|
version: (this.version = 42),
|
2021-07-13 07:04:48 -04:00
|
|
|
ranges: (this.ranges = { changes: 'mock' }),
|
2020-02-16 09:02:55 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.updateDoc(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should return a 413 (too large) response', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
sinon.assert.calledWith(this.res.status, 413)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should report that the document body is too large', function () {
|
|
|
|
sinon.assert.calledWith(this.res.send, 'document body too large')
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-15 08:13:48 -05:00
|
|
|
describe('patchDoc', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.req.params = {
|
2022-04-19 08:28:47 -04:00
|
|
|
project_id: this.projectId,
|
|
|
|
doc_id: this.docId,
|
2021-02-15 08:13:48 -05:00
|
|
|
}
|
|
|
|
this.req.body = { name: 'foo.tex' }
|
|
|
|
this.DocManager.patchDoc = sinon.stub().yields(null)
|
|
|
|
this.HttpController.patchDoc(this.req, this.res, this.next)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should delete the document', function () {
|
|
|
|
expect(this.DocManager.patchDoc).to.have.been.calledWith(
|
2022-04-19 08:28:47 -04:00
|
|
|
this.projectId,
|
|
|
|
this.docId
|
2021-02-15 08:13:48 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return a 204 (No Content)', function () {
|
|
|
|
expect(this.res.sendStatus).to.have.been.calledWith(204)
|
|
|
|
})
|
2021-02-17 06:31:27 -05:00
|
|
|
|
|
|
|
describe('with an invalid payload', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.req.body = { cannot: 'happen' }
|
|
|
|
|
|
|
|
this.DocManager.patchDoc = sinon.stub().yields(null)
|
|
|
|
this.HttpController.patchDoc(this.req, this.res, this.next)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should log a message', function () {
|
|
|
|
expect(this.logger.fatal).to.have.been.calledWith(
|
|
|
|
{ field: 'cannot' },
|
|
|
|
'joi validation for pathDoc is broken'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not pass the invalid field along', function () {
|
|
|
|
expect(this.DocManager.patchDoc).to.have.been.calledWith(
|
2022-04-19 08:28:47 -04:00
|
|
|
this.projectId,
|
|
|
|
this.docId,
|
2021-02-17 06:31:27 -05:00
|
|
|
{}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2021-02-15 08:13:48 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
describe('archiveAllDocs', function () {
|
|
|
|
beforeEach(function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.req.params = { project_id: this.projectId }
|
2020-02-16 09:02:55 -05:00
|
|
|
this.DocArchiveManager.archiveAllDocs = sinon.stub().callsArg(1)
|
2022-04-19 08:28:47 -04:00
|
|
|
this.HttpController.archiveAllDocs(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should archive the project', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.DocArchiveManager.archiveAllDocs
|
|
|
|
.calledWith(this.projectId)
|
2020-02-16 09:02:55 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should return a 204 (No Content)', function () {
|
|
|
|
this.res.sendStatus.calledWith(204).should.equal(true)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-04-26 07:16:36 -04:00
|
|
|
describe('unArchiveAllDocs', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.req.params = { project_id: this.projectId }
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('on success', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.res.sendStatus.callsFake(() => done())
|
|
|
|
this.HttpController.unArchiveAllDocs(this.req, this.res, this.next)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns a 200', function () {
|
|
|
|
expect(this.res.sendStatus).to.have.been.calledWith(200)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("when the archived rev doesn't match", function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.res.sendStatus.callsFake(() => done())
|
|
|
|
this.DocArchiveManager.unArchiveAllDocs.yields(
|
|
|
|
new Errors.DocRevValueError('bad rev')
|
|
|
|
)
|
|
|
|
this.HttpController.unArchiveAllDocs(this.req, this.res, this.next)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns a 409', function () {
|
|
|
|
expect(this.res.sendStatus).to.have.been.calledWith(409)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('destroyProject', function () {
|
2020-05-28 09:20:54 -04:00
|
|
|
beforeEach(function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
this.req.params = { project_id: this.projectId }
|
2022-04-26 07:16:36 -04:00
|
|
|
this.DocArchiveManager.destroyProject = sinon.stub().callsArg(1)
|
|
|
|
this.HttpController.destroyProject(this.req, this.res, this.next)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
|
2020-05-28 09:20:54 -04:00
|
|
|
it('should destroy the docs', function () {
|
2022-04-19 08:28:47 -04:00
|
|
|
sinon.assert.calledWith(
|
2022-04-26 07:16:36 -04:00
|
|
|
this.DocArchiveManager.destroyProject,
|
2022-04-19 08:28:47 -04:00
|
|
|
this.projectId
|
2020-02-16 09:02:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
it('should return 204', function () {
|
|
|
|
sinon.assert.calledWith(this.res.sendStatus, 204)
|
2020-02-16 09:02:55 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|