2014-02-12 05:23:40 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../../app/js/Features/Documents/DocumentController.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
events = require "events"
|
|
|
|
MockRequest = require "../helpers/MockRequest"
|
|
|
|
MockResponse = require "../helpers/MockResponse"
|
2016-03-23 11:14:49 -04:00
|
|
|
Errors = require "../../../../app/js/Features/Errors/Errors"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "DocumentController", ->
|
|
|
|
beforeEach ->
|
2015-08-19 06:58:59 -04:00
|
|
|
@DocumentController = SandboxedModule.require modulePath, requires:
|
|
|
|
"logger-sharelatex":
|
|
|
|
log:->
|
|
|
|
err:->
|
2018-04-13 08:43:21 -04:00
|
|
|
"../Project/ProjectGetter": @ProjectGetter = {}
|
2018-04-06 09:50:56 -04:00
|
|
|
"../Project/ProjectLocator": @ProjectLocator = {}
|
2014-02-12 05:23:40 -05:00
|
|
|
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
|
2018-02-01 10:31:42 -05:00
|
|
|
"../Project/ProjectEntityUpdateHandler": @ProjectEntityUpdateHandler = {}
|
2014-02-12 05:23:40 -05:00
|
|
|
@res = new MockResponse()
|
|
|
|
@req = new MockRequest()
|
|
|
|
@next = sinon.stub()
|
|
|
|
@project_id = "project-id-123"
|
|
|
|
@doc_id = "doc-id-123"
|
|
|
|
@doc_lines = ["one", "two", "three"]
|
2014-05-08 08:42:30 -04:00
|
|
|
@version = 42
|
2016-12-08 09:09:06 -05:00
|
|
|
@ranges = {"mock": "ranges"}
|
2017-09-28 10:01:45 -04:00
|
|
|
@pathname = '/a/b/c/file.tex'
|
2014-05-08 08:42:30 -04:00
|
|
|
@rev = 5
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "getDocument", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
|
2018-04-13 08:43:21 -04:00
|
|
|
describe "when the project exists without project history enabled", ->
|
|
|
|
beforeEach ->
|
|
|
|
@project = _id: @project_id
|
|
|
|
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
|
|
|
|
|
|
|
|
describe "when the document exists", ->
|
|
|
|
beforeEach ->
|
|
|
|
@doc = _id: @doc_id
|
|
|
|
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, fileSystem: @pathname)
|
|
|
|
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version, @ranges)
|
|
|
|
@DocumentController.getDocument(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should get the project", ->
|
|
|
|
@ProjectGetter.getProject
|
|
|
|
.calledWith(@project_id, rootFolder: true, overleaf: true)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should get the pathname of the document", ->
|
|
|
|
@ProjectLocator.findElement
|
|
|
|
.calledWith({project: @project, element_id: @doc_id, type: 'doc'})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should get the document content", ->
|
|
|
|
@ProjectEntityHandler.getDoc
|
|
|
|
.calledWith(@project_id, @doc_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the document data to the client as JSON", ->
|
|
|
|
@res.type.should.equal "json"
|
|
|
|
@res.body.should.equal JSON.stringify
|
|
|
|
lines: @doc_lines
|
|
|
|
version: @version
|
|
|
|
ranges: @ranges
|
|
|
|
pathname: @pathname
|
|
|
|
|
|
|
|
describe "when the document doesn't exist", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, new Errors.NotFoundError("not found"))
|
|
|
|
@DocumentController.getDocument(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with the NotFoundError", ->
|
|
|
|
@next.calledWith(new Errors.NotFoundError("not found"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when project exists with project history enabled", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
2018-04-06 09:50:56 -04:00
|
|
|
@doc = _id: @doc_id
|
2018-04-13 08:43:21 -04:00
|
|
|
@projectHistoryId = 1234
|
|
|
|
@project = _id: @project_id, overleaf: history: id: @projectHistoryId
|
|
|
|
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
|
2018-04-06 09:50:56 -04:00
|
|
|
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, fileSystem: @pathname)
|
|
|
|
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version, @ranges)
|
2014-02-12 05:23:40 -05:00
|
|
|
@DocumentController.getDocument(@req, @res, @next)
|
|
|
|
|
2018-04-13 08:43:21 -04:00
|
|
|
it "should return the history id to the client as JSON", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
@res.type.should.equal "json"
|
|
|
|
@res.body.should.equal JSON.stringify
|
|
|
|
lines: @doc_lines
|
2016-11-29 12:16:56 -05:00
|
|
|
version: @version
|
2016-12-08 09:09:06 -05:00
|
|
|
ranges: @ranges
|
2017-09-28 10:01:45 -04:00
|
|
|
pathname: @pathname
|
2018-04-13 08:43:21 -04:00
|
|
|
projectHistoryId: @projectHistoryId
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-04-13 08:43:21 -04:00
|
|
|
describe "when the project does not exist", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
2018-04-13 08:43:21 -04:00
|
|
|
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, null)
|
2014-02-12 05:23:40 -05:00
|
|
|
@DocumentController.getDocument(@req, @res, @next)
|
|
|
|
|
2018-04-13 08:43:21 -04:00
|
|
|
it "returns a 404", ->
|
|
|
|
@res.statusCode.should.equal 404
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "setDocument", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
doc_id: @doc_id
|
|
|
|
|
|
|
|
describe "when the document exists", ->
|
|
|
|
beforeEach ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@ProjectEntityUpdateHandler.updateDocLines = sinon.stub().yields()
|
2014-02-12 05:23:40 -05:00
|
|
|
@req.body =
|
|
|
|
lines: @doc_lines
|
2016-11-29 12:16:56 -05:00
|
|
|
version: @version
|
2016-12-08 09:09:06 -05:00
|
|
|
ranges: @ranges
|
2014-02-12 05:23:40 -05:00
|
|
|
@DocumentController.setDocument(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should update the document in Mongo", ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@ProjectEntityUpdateHandler.updateDocLines
|
2016-12-08 09:09:06 -05:00
|
|
|
.calledWith(@project_id, @doc_id, @doc_lines, @version, @ranges)
|
2014-02-12 05:23:40 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a successful response", ->
|
|
|
|
@res.success.should.equal true
|
|
|
|
|
|
|
|
describe "when the document doesn't exist", ->
|
|
|
|
beforeEach ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@ProjectEntityUpdateHandler.updateDocLines = sinon.stub().yields(new Errors.NotFoundError("document does not exist"))
|
2014-02-12 05:23:40 -05:00
|
|
|
@req.body =
|
|
|
|
lines: @doc_lines
|
|
|
|
@DocumentController.setDocument(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should call next with the NotFoundError", ->
|
|
|
|
@next.calledWith(new Errors.NotFoundError("not found"))
|
|
|
|
.should.equal true
|