2014-04-28 11:45:59 -04:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
2014-05-20 08:04:33 -04:00
|
|
|
assert = require("chai").assert
|
2014-04-28 11:45:59 -04:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = require('path').join __dirname, '../../../app/js/HttpController'
|
2014-04-30 08:06:12 -04:00
|
|
|
ObjectId = require("mongojs").ObjectId
|
2014-04-28 11:45:59 -04:00
|
|
|
|
|
|
|
describe "HttpController", ->
|
|
|
|
beforeEach ->
|
|
|
|
@HttpController = SandboxedModule.require modulePath, requires:
|
|
|
|
"./DocManager": @DocManager = {}
|
2015-06-02 18:24:45 -04:00
|
|
|
"./DocArchiveManager": @DocArchiveManager = {}
|
2014-04-29 06:49:09 -04:00
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
|
2016-11-28 09:55:16 -05:00
|
|
|
"./HealthChecker": {}
|
2014-05-20 08:04:33 -04:00
|
|
|
@res = { send: sinon.stub(), json: sinon.stub(), setHeader:sinon.stub() }
|
2019-08-02 09:09:48 -04:00
|
|
|
@res.status = sinon.stub().returns(@res)
|
2015-02-03 09:05:08 -05:00
|
|
|
@req = { query:{}}
|
2014-04-28 11:45:59 -04:00
|
|
|
@next = sinon.stub()
|
|
|
|
@project_id = "mock-project-id"
|
|
|
|
@doc_id = "mock-doc-id"
|
|
|
|
@doc = {
|
|
|
|
_id: @doc_id
|
2014-05-20 08:04:33 -04:00
|
|
|
lines: ["mock", "lines", " here", "", "", " spaces "]
|
2014-04-30 08:06:12 -04:00
|
|
|
version: 42
|
|
|
|
rev: 5
|
2014-04-28 11:45:59 -04:00
|
|
|
}
|
2015-02-03 09:05:08 -05:00
|
|
|
@deletedDoc = {
|
|
|
|
deleted:true
|
|
|
|
_id: @doc_id
|
|
|
|
lines: ["mock", "lines", " here", "", "", " spaces "]
|
|
|
|
version: 42
|
|
|
|
rev: 5
|
|
|
|
}
|
2014-04-28 11:45:59 -04:00
|
|
|
|
|
|
|
describe "getDoc", ->
|
2014-04-29 06:49:09 -04:00
|
|
|
|
2014-06-06 07:37:42 -04:00
|
|
|
describe "without deleted docs", ->
|
|
|
|
beforeEach ->
|
2015-02-03 09:05:08 -05:00
|
|
|
@req.params =
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
2017-03-15 18:09:56 -04:00
|
|
|
@DocManager.getFullDoc = sinon.stub().callsArgWith(2, null, @doc)
|
2014-06-06 07:37:42 -04:00
|
|
|
@HttpController.getDoc @req, @res, @next
|
|
|
|
|
2016-11-30 10:08:56 -05:00
|
|
|
it "should get the document with the version (including deleted)", ->
|
2017-03-15 18:09:56 -04:00
|
|
|
@DocManager.getFullDoc
|
|
|
|
.calledWith(@project_id, @doc_id)
|
2014-06-06 07:37:42 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the doc as JSON", ->
|
|
|
|
@res.json
|
|
|
|
.calledWith({
|
|
|
|
_id: @doc_id
|
|
|
|
lines: @doc.lines
|
|
|
|
rev: @doc.rev
|
2016-11-28 09:55:16 -05:00
|
|
|
version: @doc.version
|
2014-06-06 07:37:42 -04:00
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
2015-02-03 09:05:08 -05:00
|
|
|
describe "which is deleted", ->
|
2014-06-06 07:37:42 -04:00
|
|
|
beforeEach ->
|
2015-02-03 09:05:08 -05:00
|
|
|
@req.params =
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
2017-03-15 18:09:56 -04:00
|
|
|
@DocManager.getFullDoc = sinon.stub().callsArgWith(2, null, @deletedDoc)
|
2015-02-03 09:05:08 -05:00
|
|
|
|
|
|
|
it "should get the doc from the doc manager", ->
|
2014-06-06 07:37:42 -04:00
|
|
|
@HttpController.getDoc @req, @res, @next
|
2017-03-15 18:09:56 -04:00
|
|
|
@DocManager.getFullDoc.calledWith(@project_id, @doc_id).should.equal true
|
2014-06-06 07:37:42 -04:00
|
|
|
|
2015-02-03 09:05:08 -05:00
|
|
|
it "should return 404 if the query string delete is not set ", ->
|
|
|
|
@HttpController.getDoc @req, @res, @next
|
|
|
|
@res.send.calledWith(404).should.equal true
|
|
|
|
|
|
|
|
it "should return the doc as JSON if include_deleted is set to true", ->
|
|
|
|
@req.query.include_deleted = "true"
|
|
|
|
@HttpController.getDoc @req, @res, @next
|
|
|
|
@res.json
|
|
|
|
.calledWith({
|
|
|
|
_id: @doc_id
|
|
|
|
lines: @doc.lines
|
|
|
|
rev: @doc.rev
|
|
|
|
deleted: true
|
2016-11-28 09:55:16 -05:00
|
|
|
version: @doc.version
|
2015-02-03 09:05:08 -05:00
|
|
|
})
|
2014-06-06 07:37:42 -04:00
|
|
|
.should.equal true
|
2014-04-30 08:06:12 -04:00
|
|
|
|
2014-05-20 08:04:33 -04:00
|
|
|
describe "getRawDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
2017-03-15 18:09:56 -04:00
|
|
|
@DocManager.getDocLines = sinon.stub().callsArgWith(2, null, @doc)
|
2014-05-20 08:04:33 -04:00
|
|
|
@HttpController.getRawDoc @req, @res, @next
|
|
|
|
|
2016-11-30 10:08:56 -05:00
|
|
|
it "should get the document without the version", ->
|
2017-03-15 18:09:56 -04:00
|
|
|
@DocManager.getDocLines
|
|
|
|
.calledWith(@project_id, @doc_id)
|
2014-05-20 08:04:33 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should set the content type header", ->
|
|
|
|
@res.setHeader.calledWith('content-type', 'text/plain').should.equal true
|
|
|
|
|
|
|
|
it "should send the raw version of the doc", ->
|
|
|
|
assert.deepEqual @res.send.args[0][0], "#{@doc.lines[0]}\n#{@doc.lines[1]}\n#{@doc.lines[2]}\n#{@doc.lines[3]}\n#{@doc.lines[4]}\n#{@doc.lines[5]}"
|
|
|
|
|
2014-04-30 08:06:12 -04:00
|
|
|
describe "getAllDocs", ->
|
2014-05-07 11:38:10 -04:00
|
|
|
describe "normally", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
project_id: @project_id
|
|
|
|
@docs = [{
|
|
|
|
_id: ObjectId()
|
|
|
|
lines: ["mock", "lines", "one"]
|
|
|
|
rev: 2
|
|
|
|
}, {
|
|
|
|
_id: ObjectId()
|
|
|
|
lines: ["mock", "lines", "two"]
|
|
|
|
rev: 4
|
|
|
|
}]
|
2016-12-05 12:27:31 -05:00
|
|
|
@DocManager.getAllNonDeletedDocs = sinon.stub().callsArgWith(2, null, @docs)
|
2014-05-07 11:38:10 -04:00
|
|
|
@HttpController.getAllDocs @req, @res, @next
|
2014-04-30 08:06:12 -04:00
|
|
|
|
2016-12-05 11:31:51 -05:00
|
|
|
it "should get all the (non-deleted) docs", ->
|
|
|
|
@DocManager.getAllNonDeletedDocs
|
2016-12-05 12:27:31 -05:00
|
|
|
.calledWith(@project_id, {lines: true, rev: true})
|
2014-05-07 11:38:10 -04:00
|
|
|
.should.equal true
|
2014-04-30 08:06:12 -04:00
|
|
|
|
2014-05-07 11:38:10 -04:00
|
|
|
it "should return the doc as JSON", ->
|
|
|
|
@res.json
|
|
|
|
.calledWith([{
|
|
|
|
_id: @docs[0]._id.toString()
|
|
|
|
lines: @docs[0].lines
|
|
|
|
rev: @docs[0].rev
|
|
|
|
}, {
|
|
|
|
_id: @docs[1]._id.toString()
|
|
|
|
lines: @docs[1].lines
|
|
|
|
rev: @docs[1].rev
|
|
|
|
}])
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "with a null doc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
project_id: @project_id
|
|
|
|
@docs = [{
|
|
|
|
_id: ObjectId()
|
|
|
|
lines: ["mock", "lines", "one"]
|
|
|
|
rev: 2
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
_id: ObjectId()
|
|
|
|
lines: ["mock", "lines", "two"]
|
|
|
|
rev: 4
|
|
|
|
}]
|
2016-12-05 12:27:31 -05:00
|
|
|
@DocManager.getAllNonDeletedDocs = sinon.stub().callsArgWith(2, null, @docs)
|
2014-05-07 11:38:10 -04:00
|
|
|
@HttpController.getAllDocs @req, @res, @next
|
|
|
|
|
|
|
|
it "should return the non null docs as JSON", ->
|
|
|
|
@res.json
|
|
|
|
.calledWith([{
|
|
|
|
_id: @docs[0]._id.toString()
|
|
|
|
lines: @docs[0].lines
|
|
|
|
rev: @docs[0].rev
|
|
|
|
}, {
|
|
|
|
_id: @docs[2]._id.toString()
|
|
|
|
lines: @docs[2].lines
|
|
|
|
rev: @docs[2].rev
|
|
|
|
}])
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should log out an error", ->
|
|
|
|
@logger.error
|
|
|
|
.calledWith(
|
2017-08-22 10:20:43 -04:00
|
|
|
err: sinon.match.has('message', "null doc")
|
2014-05-07 11:38:10 -04:00
|
|
|
project_id: @project_id
|
|
|
|
"encountered null doc"
|
|
|
|
)
|
|
|
|
.should.equal true
|
2014-04-29 06:49:09 -04:00
|
|
|
|
2016-12-09 09:37:24 -05:00
|
|
|
describe "getAllRanges", ->
|
|
|
|
describe "normally", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
project_id: @project_id
|
|
|
|
@docs = [{
|
|
|
|
_id: ObjectId()
|
|
|
|
ranges: {"mock_ranges": "one"}
|
|
|
|
}, {
|
|
|
|
_id: ObjectId()
|
|
|
|
ranges: {"mock_ranges": "two"}
|
|
|
|
}]
|
|
|
|
@DocManager.getAllNonDeletedDocs = sinon.stub().callsArgWith(2, null, @docs)
|
|
|
|
@HttpController.getAllRanges @req, @res, @next
|
|
|
|
|
|
|
|
it "should get all the (non-deleted) doc ranges", ->
|
|
|
|
@DocManager.getAllNonDeletedDocs
|
|
|
|
.calledWith(@project_id, {ranges: true})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the doc as JSON", ->
|
|
|
|
@res.json
|
|
|
|
.calledWith([{
|
|
|
|
_id: @docs[0]._id.toString()
|
|
|
|
ranges: @docs[0].ranges
|
|
|
|
}, {
|
|
|
|
_id: @docs[1]._id.toString()
|
|
|
|
ranges: @docs[1].ranges
|
|
|
|
}])
|
|
|
|
.should.equal true
|
|
|
|
|
2014-04-29 06:49:09 -04:00
|
|
|
describe "updateDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
|
|
|
|
describe "when the doc lines exist and were updated", ->
|
2014-04-28 11:45:59 -04:00
|
|
|
beforeEach ->
|
2014-04-29 06:49:09 -04:00
|
|
|
@req.body =
|
|
|
|
lines: @lines = ["hello", "world"]
|
2016-12-02 10:17:38 -05:00
|
|
|
version: @version = 42
|
2016-12-05 09:21:49 -05:00
|
|
|
ranges: @ranges = { changes: "mock" }
|
2016-11-28 09:55:16 -05:00
|
|
|
@DocManager.updateDoc = sinon.stub().yields(null, true, @rev = 5)
|
2014-04-29 06:49:09 -04:00
|
|
|
@HttpController.updateDoc @req, @res, @next
|
|
|
|
|
|
|
|
it "should update the document", ->
|
|
|
|
@DocManager.updateDoc
|
2016-12-05 09:21:49 -05:00
|
|
|
.calledWith(@project_id, @doc_id, @lines, @version, @ranges)
|
2014-04-28 11:45:59 -04:00
|
|
|
.should.equal true
|
|
|
|
|
2014-04-29 06:49:09 -04:00
|
|
|
it "should return a modified status", ->
|
|
|
|
@res.json
|
2014-05-08 10:43:08 -04:00
|
|
|
.calledWith(modified: true, rev: @rev)
|
2014-04-28 11:45:59 -04:00
|
|
|
.should.equal true
|
|
|
|
|
2014-04-29 06:49:09 -04:00
|
|
|
describe "when the doc lines exist and were not updated", ->
|
2014-04-28 11:45:59 -04:00
|
|
|
beforeEach ->
|
2014-04-29 06:49:09 -04:00
|
|
|
@req.body =
|
|
|
|
lines: @lines = ["hello", "world"]
|
2016-12-02 10:17:38 -05:00
|
|
|
version: @version = 42
|
2017-01-19 06:15:14 -05:00
|
|
|
ranges: {}
|
2016-11-28 09:55:16 -05:00
|
|
|
@DocManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
|
2014-04-29 06:49:09 -04:00
|
|
|
@HttpController.updateDoc @req, @res, @next
|
2014-04-28 11:45:59 -04:00
|
|
|
|
2014-04-29 06:49:09 -04:00
|
|
|
it "should return a modified status", ->
|
|
|
|
@res.json
|
2014-05-08 10:43:08 -04:00
|
|
|
.calledWith(modified: false, rev: @rev)
|
2014-04-29 06:49:09 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when the doc lines are not provided", ->
|
|
|
|
beforeEach ->
|
2017-01-19 06:15:14 -05:00
|
|
|
@req.body = { version: 42, ranges: {} }
|
2016-11-28 09:55:16 -05:00
|
|
|
@DocManager.updateDoc = sinon.stub().yields(null, false)
|
2014-04-29 06:49:09 -04:00
|
|
|
@HttpController.updateDoc @req, @res, @next
|
|
|
|
|
|
|
|
it "should not update the document", ->
|
|
|
|
@DocManager.updateDoc.called.should.equal false
|
|
|
|
|
|
|
|
it "should return a 400 (bad request) response", ->
|
2014-04-28 11:45:59 -04:00
|
|
|
@res.send
|
2014-04-29 06:49:09 -04:00
|
|
|
.calledWith(400)
|
|
|
|
.should.equal true
|
2014-04-29 11:36:10 -04:00
|
|
|
|
2017-01-19 06:15:14 -05:00
|
|
|
describe "when the doc version are not provided", ->
|
2016-11-28 09:55:16 -05:00
|
|
|
beforeEach ->
|
2017-01-19 06:15:14 -05:00
|
|
|
@req.body = { version: 42, lines: ["hello world"] }
|
|
|
|
@DocManager.updateDoc = sinon.stub().yields(null, false)
|
|
|
|
@HttpController.updateDoc @req, @res, @next
|
|
|
|
|
|
|
|
it "should not update the document", ->
|
|
|
|
@DocManager.updateDoc.called.should.equal false
|
|
|
|
|
|
|
|
it "should return a 400 (bad request) response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(400)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when the doc ranges is not provided", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.body = { lines : [ "foo" ], version: 42 }
|
2016-12-02 10:17:38 -05:00
|
|
|
@DocManager.updateDoc = sinon.stub().yields(null, false)
|
2016-11-28 09:55:16 -05:00
|
|
|
@HttpController.updateDoc @req, @res, @next
|
|
|
|
|
2016-12-02 10:17:38 -05:00
|
|
|
it "should not update the document", ->
|
|
|
|
@DocManager.updateDoc.called.should.equal false
|
2016-11-28 09:55:16 -05:00
|
|
|
|
2016-12-02 10:17:38 -05:00
|
|
|
it "should return a 400 (bad request) response", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(400)
|
2016-11-28 09:55:16 -05:00
|
|
|
.should.equal true
|
|
|
|
|
2019-08-02 09:09:48 -04:00
|
|
|
describe "when the doc body is too large", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.body =
|
|
|
|
lines: @lines = Array(2049).fill('a'.repeat(1024))
|
|
|
|
version: @version = 42
|
|
|
|
ranges: @ranges = { changes: "mock" }
|
|
|
|
@HttpController.updateDoc @req, @res, @next
|
|
|
|
|
|
|
|
it "should return a 413 (too large) response", ->
|
|
|
|
sinon.assert.calledWith(@res.status, 413)
|
|
|
|
|
|
|
|
it "should report that the document body is too large", ->
|
|
|
|
sinon.assert.calledWith(@res.send, "document body too large")
|
|
|
|
|
2014-04-29 11:36:10 -04:00
|
|
|
describe "deleteDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
@DocManager.deleteDoc = sinon.stub().callsArg(2)
|
|
|
|
@HttpController.deleteDoc @req, @res, @next
|
|
|
|
|
|
|
|
it "should delete the document", ->
|
|
|
|
@DocManager.deleteDoc
|
|
|
|
.calledWith(@project_id, @doc_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a 204 (No Content)", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
|
|
|
.should.equal true
|
2015-06-02 18:24:45 -04:00
|
|
|
|
|
|
|
describe "archiveAllDocs", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
project_id: @project_id
|
|
|
|
@DocArchiveManager.archiveAllDocs = sinon.stub().callsArg(1)
|
|
|
|
@HttpController.archiveAllDocs @req, @res, @next
|
|
|
|
|
|
|
|
it "should archive the project", ->
|
|
|
|
@DocArchiveManager.archiveAllDocs
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a 204 (No Content)", ->
|
|
|
|
@res.send
|
|
|
|
.calledWith(204)
|
2017-08-22 10:20:43 -04:00
|
|
|
.should.equal true
|
2019-07-02 07:45:54 -04:00
|
|
|
|
|
|
|
describe "destroyAllDocs", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
project_id: @project_id
|
|
|
|
@DocArchiveManager.destroyAllDocs = sinon.stub().callsArg(1)
|
|
|
|
@HttpController.destroyAllDocs @req, @res, @next
|
|
|
|
|
|
|
|
it "should destroy the docs", ->
|
|
|
|
sinon.assert.calledWith(@DocArchiveManager.destroyAllDocs, @project_id)
|
|
|
|
|
|
|
|
it "should return 204", ->
|
|
|
|
sinon.assert.calledWith(@res.send, 204)
|
|
|
|
|