2014-02-12 05:23:40 -05:00
|
|
|
chai = require('chai')
|
|
|
|
assert = require('chai').assert
|
|
|
|
should = chai.should()
|
2014-05-07 09:31:46 -04:00
|
|
|
expect = chai.expect
|
2014-02-12 05:23:40 -05:00
|
|
|
sinon = require 'sinon'
|
|
|
|
modulePath = "../../../../app/js/Features/Project/ProjectEntityHandler"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
ObjectId = require("mongoose").Types.ObjectId
|
2016-03-23 11:14:49 -04:00
|
|
|
Errors = require "../../../../app/js/Features/Errors/Errors"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-05-06 07:35:27 -04:00
|
|
|
describe 'ProjectEntityHandler', ->
|
2014-02-12 05:23:40 -05:00
|
|
|
project_id = '4eecb1c1bffa66588e0000a1'
|
2014-05-08 08:42:30 -04:00
|
|
|
doc_id = '4eecb1c1bffa66588e0000a2'
|
2014-02-12 05:23:40 -05:00
|
|
|
folder_id = "4eecaffcbffa66588e000008"
|
|
|
|
rootFolderId = "4eecaffcbffa66588e000007"
|
2017-11-02 05:44:23 -04:00
|
|
|
userId = 1234
|
2017-09-28 10:01:45 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@TpdsUpdateSender =
|
2014-10-15 09:11:02 -04:00
|
|
|
addDoc:sinon.stub().callsArg(1)
|
|
|
|
addFile:sinon.stub().callsArg(1)
|
2014-02-12 05:23:40 -05:00
|
|
|
@ProjectModel = class Project
|
|
|
|
constructor:(options)->
|
|
|
|
@._id = project_id
|
|
|
|
@name = "project_name_here"
|
|
|
|
@rev = 0
|
|
|
|
rootFolder:[@rootFolder]
|
|
|
|
|
|
|
|
@project = new @ProjectModel()
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
@ProjectLocator =
|
2016-02-24 10:36:39 -05:00
|
|
|
findElement : sinon.stub()
|
2018-02-01 10:31:42 -05:00
|
|
|
@DocumentUpdaterHandler =
|
2017-11-30 10:46:37 -05:00
|
|
|
updateProjectStructure: sinon.stub().yields()
|
2014-05-08 08:42:30 -04:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
@callback = sinon.stub()
|
2014-05-08 08:42:30 -04:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
@ProjectEntityHandler = SandboxedModule.require modulePath, requires:
|
|
|
|
'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub(), err:->}
|
|
|
|
'../Docstore/DocstoreManager': @DocstoreManager = {}
|
|
|
|
'../../Features/DocumentUpdater/DocumentUpdaterHandler':@DocumentUpdaterHandler
|
|
|
|
'../../models/Project': Project:@ProjectModel
|
|
|
|
'./ProjectLocator': @ProjectLocator
|
|
|
|
"./ProjectGetter": @ProjectGetter = {}
|
|
|
|
'../ThirdPartyDataStore/TpdsUpdateSender':@TpdsUpdateSender
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-05-06 07:35:27 -04:00
|
|
|
describe "getting folders, docs and files", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
|
|
|
@project.rootFolder = [
|
2014-05-06 07:35:27 -04:00
|
|
|
docs: [@doc1 = {
|
|
|
|
name : "doc1"
|
|
|
|
_id : "doc1_id"
|
|
|
|
}]
|
|
|
|
fileRefs: [@file1 = {
|
|
|
|
rev : 1
|
|
|
|
_id : "file1_id"
|
|
|
|
name : "file1"
|
|
|
|
}]
|
|
|
|
folders: [@folder1 = {
|
2014-02-12 05:23:40 -05:00
|
|
|
name : "folder1"
|
2014-05-06 07:35:27 -04:00
|
|
|
docs : [@doc2 = {
|
|
|
|
name : "doc2"
|
|
|
|
_id : "doc2_id"
|
2014-02-12 05:23:40 -05:00
|
|
|
}]
|
2014-05-06 07:35:27 -04:00
|
|
|
fileRefs : [@file2 = {
|
|
|
|
rev : 2
|
2014-02-12 05:23:40 -05:00
|
|
|
name : "file2"
|
|
|
|
_id : "file2_id"
|
|
|
|
}]
|
|
|
|
folders : []
|
2014-05-01 12:19:21 -04:00
|
|
|
}]
|
|
|
|
]
|
2018-02-01 10:31:42 -05:00
|
|
|
@ProjectGetter.getProjectWithoutDocLines = sinon.stub().yields(null, @project)
|
2014-04-30 10:49:07 -04:00
|
|
|
|
2014-05-06 07:35:27 -04:00
|
|
|
describe "getAllDocs", ->
|
|
|
|
beforeEach ->
|
|
|
|
@docs = [{
|
|
|
|
_id: @doc1._id
|
|
|
|
lines: @lines1 = ["one"]
|
|
|
|
rev: @rev1 = 1
|
|
|
|
}, {
|
|
|
|
_id: @doc2._id
|
|
|
|
lines: @lines2 = ["two"]
|
|
|
|
rev: @rev2 = 2
|
|
|
|
}]
|
|
|
|
@DocstoreManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
|
|
|
|
@ProjectEntityHandler.getAllDocs project_id, @callback
|
2014-04-30 10:49:07 -04:00
|
|
|
|
2014-05-06 07:35:27 -04:00
|
|
|
it "should get the doc lines and rev from the docstore", ->
|
|
|
|
@DocstoreManager.getAllDocs
|
|
|
|
.calledWith(project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with the docs with the lines and rev included", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(null, {
|
|
|
|
"/doc1": {
|
|
|
|
_id: @doc1._id
|
|
|
|
lines: @lines1
|
|
|
|
name: @doc1.name
|
|
|
|
rev: @rev1
|
|
|
|
}
|
|
|
|
"/folder1/doc2": {
|
|
|
|
_id: @doc2._id
|
|
|
|
lines: @lines2
|
|
|
|
name: @doc2.name
|
|
|
|
rev: @rev2
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
describe "getAllFiles", ->
|
2017-08-14 10:40:46 -04:00
|
|
|
beforeEach ->
|
|
|
|
@callback = sinon.stub()
|
2018-02-01 10:31:42 -05:00
|
|
|
@ProjectEntityHandler.getAllFiles project_id, @callback
|
2017-08-14 10:40:46 -04:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
it "should call the callback with the files", ->
|
2017-08-14 10:40:46 -04:00
|
|
|
@callback
|
|
|
|
.calledWith(null, {
|
2018-02-01 10:31:42 -05:00
|
|
|
"/file1": @file1
|
|
|
|
"/folder1/file2": @file2
|
2017-08-14 10:40:46 -04:00
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "getAllDocPathsFromProject", ->
|
|
|
|
beforeEach ->
|
|
|
|
@docs = [{
|
|
|
|
_id: @doc1._id
|
|
|
|
lines: @lines1 = ["one"]
|
|
|
|
rev: @rev1 = 1
|
|
|
|
}, {
|
|
|
|
_id: @doc2._id
|
|
|
|
lines: @lines2 = ["two"]
|
|
|
|
rev: @rev2 = 2
|
|
|
|
}]
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@ProjectEntityHandler.getAllDocPathsFromProject @project, @callback
|
|
|
|
|
|
|
|
it "should call the callback with the path for each doc_id", ->
|
|
|
|
@expected = {}
|
|
|
|
@expected[@doc1._id] = "/#{@doc1.name}"
|
|
|
|
@expected[@doc2._id] = "/folder1/#{@doc2.name}"
|
|
|
|
@callback
|
|
|
|
.calledWith(null, @expected)
|
|
|
|
.should.equal true
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
describe "_getAllFolders", ->
|
|
|
|
beforeEach ->
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@ProjectEntityHandler._getAllFolders project_id, @callback
|
|
|
|
|
|
|
|
it "should get the project without the docs lines", ->
|
|
|
|
@ProjectGetter.getProjectWithoutDocLines
|
|
|
|
.calledWith(project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with the folders", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(null, {
|
|
|
|
"/": @project.rootFolder[0]
|
|
|
|
"/folder1": @folder1
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "_getAllFoldersFromProject", ->
|
|
|
|
beforeEach ->
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@ProjectEntityHandler._getAllFoldersFromProject @project, @callback
|
|
|
|
|
|
|
|
it "should call the callback with the folders", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(null, {
|
|
|
|
"/": @project.rootFolder[0]
|
|
|
|
"/folder1": @folder1
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
2014-05-06 07:35:27 -04:00
|
|
|
describe "flushProjectToThirdPartyDataStore", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@project = {
|
|
|
|
_id: project_id
|
|
|
|
name: "Mock project name"
|
|
|
|
}
|
2018-02-01 10:31:42 -05:00
|
|
|
@DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().yields()
|
2014-05-06 07:35:27 -04:00
|
|
|
@docs = {
|
|
|
|
"/doc/one": @doc1 = { _id: "mock-doc-1", lines: ["one"], rev: 5 }
|
|
|
|
"/doc/two": @doc2 = { _id: "mock-doc-2", lines: ["two"], rev: 6 }
|
|
|
|
}
|
|
|
|
@files = {
|
|
|
|
"/file/one": @file1 = { _id: "mock-file-1", rev: 7 }
|
|
|
|
"/file/two": @file2 = { _id: "mock-file-2", rev: 8 }
|
|
|
|
}
|
2018-02-01 10:31:42 -05:00
|
|
|
@ProjectEntityHandler.getAllDocs = sinon.stub().yields(null, @docs)
|
|
|
|
@ProjectEntityHandler.getAllFiles = sinon.stub().yields(null, @files)
|
2014-05-06 07:35:27 -04:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
@ProjectGetter.getProject = sinon.stub().yields(null, @project)
|
2016-02-26 03:23:43 -05:00
|
|
|
|
2014-05-06 07:35:27 -04:00
|
|
|
@ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, () -> done()
|
|
|
|
|
|
|
|
it "should flush the project from the doc updater", ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@DocumentUpdaterHandler.flushProjectToMongo.calledWith(project_id).should.equal true
|
2014-05-06 07:35:27 -04:00
|
|
|
|
|
|
|
it "should look up the project in mongo", ->
|
2016-02-26 03:23:43 -05:00
|
|
|
@ProjectGetter.getProject.calledWith(project_id).should.equal true
|
2014-05-06 07:35:27 -04:00
|
|
|
|
|
|
|
it "should get all the docs in the project", ->
|
2016-02-26 03:23:43 -05:00
|
|
|
@ProjectEntityHandler.getAllDocs.calledWith(project_id).should.equal true
|
2014-05-06 07:35:27 -04:00
|
|
|
|
|
|
|
it "should get all the files in the project", ->
|
2016-02-26 03:23:43 -05:00
|
|
|
@ProjectEntityHandler.getAllFiles.calledWith(project_id).should.equal true
|
2014-05-06 07:35:27 -04:00
|
|
|
|
|
|
|
it "should flush each doc to the TPDS", ->
|
|
|
|
for path, doc of @docs
|
2018-02-01 10:31:42 -05:00
|
|
|
@TpdsUpdateSender.addDoc
|
2014-05-06 07:35:27 -04:00
|
|
|
.calledWith({
|
|
|
|
project_id: project_id,
|
2014-05-20 08:18:59 -04:00
|
|
|
doc_id: doc._id
|
2014-05-06 07:35:27 -04:00
|
|
|
project_name: @project.name
|
|
|
|
rev: doc.rev
|
|
|
|
path: path
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should flush each file to the TPDS", ->
|
|
|
|
for path, file of @files
|
2018-02-01 10:31:42 -05:00
|
|
|
@TpdsUpdateSender.addFile
|
2014-05-06 07:35:27 -04:00
|
|
|
.calledWith({
|
|
|
|
project_id: project_id,
|
|
|
|
file_id: file._id
|
|
|
|
project_name: @project.name
|
|
|
|
rev: file.rev
|
|
|
|
path: path
|
|
|
|
})
|
|
|
|
.should.equal true
|
2017-09-28 10:01:45 -04:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
describe 'getDoc', ->
|
2014-04-30 06:15:29 -04:00
|
|
|
beforeEach ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@lines = ["mock", "doc", "lines"]
|
|
|
|
@rev = 5
|
|
|
|
@version = 42
|
|
|
|
@ranges = {"mock": "ranges"}
|
2014-04-30 06:15:29 -04:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
@DocstoreManager.getDoc = sinon.stub().callsArgWith(3, null, @lines, @rev, @version, @ranges)
|
2014-04-30 06:15:29 -04:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
describe 'without pathname option', ->
|
2014-04-30 06:15:29 -04:00
|
|
|
beforeEach ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@ProjectEntityHandler.getDoc project_id, doc_id, @callback
|
2014-04-30 06:15:29 -04:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
it "should call the docstore", ->
|
|
|
|
@DocstoreManager.getDoc
|
|
|
|
.calledWith(project_id, doc_id)
|
2014-04-30 06:15:29 -04:00
|
|
|
.should.equal true
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
it "should call the callback with the lines, version and rev", ->
|
|
|
|
@callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
|
2014-04-30 06:15:29 -04:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
describe 'with pathname option', ->
|
|
|
|
beforeEach ->
|
|
|
|
@project = 'a project'
|
|
|
|
@path = mongo: "mongo.path", fileSystem: "/file/system/path"
|
|
|
|
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, {}, @path)
|
|
|
|
@ProjectEntityHandler.getDoc project_id, doc_id, {pathname: true}, @callback
|
2014-04-30 06:15:29 -04:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
it "should call the project locator", ->
|
|
|
|
@ProjectLocator.findElement
|
|
|
|
.calledWith({project_id: project_id, element_id: doc_id, type: 'doc'})
|
2014-04-30 06:15:29 -04:00
|
|
|
.should.equal true
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
it "should call the docstore", ->
|
|
|
|
@DocstoreManager.getDoc
|
|
|
|
.calledWith(project_id, doc_id)
|
2017-12-14 11:03:43 -05:00
|
|
|
.should.equal true
|
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
it "should return the pathname if option given", ->
|
|
|
|
@callback.calledWith(null, @lines, @rev, @version, @ranges, @path.fileSystem).should.equal true
|