move .putElement into ProjectEntityHandler with tests

This commit is contained in:
Henry Oswald 2016-02-24 15:36:39 +00:00
parent a8060109fd
commit 09ac8f8c7a
3 changed files with 117 additions and 58 deletions

View file

@ -118,7 +118,7 @@ module.exports = ProjectEntityHandler =
# which hasn't been created in docstore.
DocstoreManager.updateDoc project._id.toString(), doc._id.toString(), docLines, (err, modified, rev) ->
return callback(err) if err?
Project.putElement project._id, folder_id, doc, "doc", (err, result)=>
ProjectEntityHandler._putElement project._id, folder_id, doc, "doc", (err, result)=>
return callback(err) if err?
tpdsUpdateSender.addDoc {
project_id: project._id,
@ -147,7 +147,7 @@ module.exports = ProjectEntityHandler =
if err?
logger.err err:err, project_id: project._id, folder_id: folder_id, file_name: fileName, fileRef:fileRef, "error uploading image to s3"
return callback(err)
Project.putElement project._id, folder_id, fileRef, "file", (err, result)=>
ProjectEntityHandler._putElement project._id, folder_id, fileRef, "file", (err, result)=>
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:result.path.fileSystem, project_name:project.name, rev:fileRef.rev}, ->
callback(err, fileRef, folder_id)
@ -192,7 +192,7 @@ module.exports = ProjectEntityHandler =
FileStoreHandler.copyFile originalProject_id, origonalFileRef._id, project._id, fileRef._id, (err)->
if err?
logger.err err:err, project_id:project._id, folder_id:folder_id, originalProject_id:originalProject_id, origonalFileRef:origonalFileRef, "error coping file in s3"
Project.putElement project._id, folder_id, fileRef, "file", (err, result)=>
ProjectEntityHandler._putElement project._id, folder_id, fileRef, "file", (err, result)=>
tpdsUpdateSender.addFile {project_id:project._id, file_id:fileRef._id, path:result.path.fileSystem, rev:fileRef.rev, project_name:project.name}, (error) ->
callback(error, fileRef, folder_id)
@ -240,7 +240,7 @@ module.exports = ProjectEntityHandler =
return callback(err) if err?
confirmFolder project, parentFolder_id, (parentFolder_id)=>
logger.log project: project_id, parentFolder_id:parentFolder_id, folderName:folderName, "new folder added"
Project.putElement project._id, parentFolder_id, folder, "folder", (err, result)=>
ProjectEntityHandler._putElement project._id, parentFolder_id, folder, "folder", (err, result)=>
if callback?
callback(err, folder, parentFolder_id)
@ -300,7 +300,7 @@ module.exports = ProjectEntityHandler =
return callback(error) if error?
self._removeElementFromMongoArray Project, project_id, path.mongo, (err)->
return callback(err) if err?
Project.putElement project_id, destinationFolder_id, entity, entityType, (err, result)->
ProjectEntityHandler._putElement project_id, destinationFolder_id, entity, entityType, (err, result)->
return callback(err) if err?
opts =
project_id:project_id
@ -424,6 +424,45 @@ module.exports = ProjectEntityHandler =
}
}, {}, callback
_putElement: (project_id, folder_id, element, type, callback = (err, path)->)->
sanitizeTypeOfElement = (elementType)->
lastChar = elementType.slice -1
if lastChar != "s"
elementType +="s"
if elementType == "files"
elementType = "fileRefs"
return elementType
if !element?
e = new Error("no element passed to be inserted")
logger.err project_id:project_id, folder_id:folder_id, element:element, type:type, "failed trying to insert element as it was null"
return callback(e)
type = sanitizeTypeOfElement type
ProjectGetter.getProject project_id, "rootFolder", (err, project)=>
if err?
return callback(err)
if !folder_id?
folder_id = project.rootFolder[0]._id
projectLocator.findElement {project:project, element_id:folder_id, type:"folders"}, (err, folder, path)=>
newPath =
fileSystem: "#{path.fileSystem}/#{element.name}"
mongo: path.mongo
if err?
logger.err err:err, project_id:project_id, folder_id:folder_id, type:type, element:element, "error finding folder for _putElement"
return callback(err)
logger.log project_id: project_id, element_id: element._id, fileType: type, folder_id: folder_id, "adding element to project"
id = element._id+''
element._id = require('mongoose').Types.ObjectId(id)
conditions = _id:project_id
mongopath = "#{path.mongo}.#{type}"
update = "$push":{}
update["$push"][mongopath] = element
Project.update conditions, update, {}, (err)->
if err?
logger.err err: err, project_id: project_id, 'error saving in putElement project'
return callback(err)
callback(err, {path:newPath})
confirmFolder = (project, folder_id, callback)->
logger.log folder_id:folder_id, project_id:project._id, "confirming folder in project"
if folder_id+'' == 'undefined'

View file

@ -66,14 +66,6 @@ ProjectSchema.statics.findAllUsersProjects = (user_id, requiredFields, callback)
this.find {readOnly_refs:user_id}, requiredFields, (err, readOnlyProjects)=>
callback(err, projects, collabertions, readOnlyProjects)
sanitizeTypeOfElement = (elementType)->
lastChar = elementType.slice -1
if lastChar != "s"
elementType +="s"
if elementType == "files"
elementType = "fileRefs"
return elementType
countElements = (project, callback)->
countFolder = (folder, cb)->
@ -87,36 +79,6 @@ countElements = (project, callback)->
countFolder project.rootFolder[0], callback
ProjectSchema.statics.putElement = (project_id, folder_id, element, type, callback)->
if !element?
e = new Error("no element passed to be inserted")
logger.err project_id:project_id, folder_id:folder_id, element:element, type:type, "failed trying to insert element as it was null"
return callback(e)
type = sanitizeTypeOfElement type
require('../Features/Project/ProjectGetter').getProject project_id, "rootFolder", (err, project)=>
if err?
callback(err)
if !folder_id?
folder_id = project.rootFolder[0]._id
require('../Features/Project/ProjectLocator').findElement {project:project, element_id:folder_id, type:"folders"}, (err, folder, path)=>
newPath =
fileSystem: "#{path.fileSystem}/#{element.name}"
mongo: path.mongo # TODO: This is not correct
if err?
callback(err)
logger.log project_id: project_id, element_id: element._id, fileType: type, folder_id: folder_id, "adding element to project"
id = element._id+''
element._id = concreteObjectId(id)
conditions = _id:project_id
mongopath = "#{path.mongo}.#{type}"
update = "$push":{}
update["$push"][mongopath] = element
this.update conditions, update, {}, (err)->
if(err)
logger.err err: err, project_id: project_id, 'error saving in putElement project'
if callback?
callback(err, {path:newPath})
getIndexOf = (searchEntity, id)->
length = searchEntity.length
count = 0

View file

@ -28,6 +28,7 @@ describe 'ProjectEntityHandler', ->
folders:[
{name:"level1", folders:[]}
]
@ProjectUpdateStub = sinon.stub()
@ProjectModel = class Project
constructor:(options)->
@._id = project_id
@ -35,6 +36,7 @@ describe 'ProjectEntityHandler', ->
@rev = 0
save:(callback)->callback()
rootFolder:[@rootFolder]
@ProjectModel.update = @ProjectUpdateStub
@DocModel = class Doc
constructor:(options)->
@ -57,8 +59,10 @@ describe 'ProjectEntityHandler', ->
@ProjectModel.getProject = (project_id, fields, callback)=> callback(null, @project)
@ProjectGetter =
getProjectWithOnlyFolders : (project_id, callback)=> callback(null, @project)
@ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:"somehintg"}})
getProject:sinon.stub()
@projectUpdater = markAsUpdated:sinon.stub()
@projectLocator =
findElement : sinon.stub()
@ProjectEntityHandler = SandboxedModule.require modulePath, requires:
'../../models/Project': Project:@ProjectModel
'../../models/Doc': Doc:@DocModel
@ -66,10 +70,10 @@ describe 'ProjectEntityHandler', ->
'../../models/File': File:@FileModel
'../FileStore/FileStoreHandler':@FileStoreHandler
'../ThirdPartyDataStore/TpdsUpdateSender':@tpdsUpdateSender
'./ProjectLocator':@projectLocator = {}
'./ProjectLocator': @projectLocator
'../../Features/DocumentUpdater/DocumentUpdaterHandler':@documentUpdaterHandler = {}
'../Docstore/DocstoreManager': @DocstoreManager = {}
'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub()}
'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub(), err:->}
'./ProjectUpdateHandler': @projectUpdater
"./ProjectGetter": @ProjectGetter
@ -226,7 +230,7 @@ describe 'ProjectEntityHandler', ->
fileSystem: "/somewhere/else.txt"
}
@ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArg(3)
@ProjectModel.putElement = sinon.stub().callsArgWith(4, null, path: @pathAfterMove)
@ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, path: @pathAfterMove)
@tpdsUpdateSender.moveEntity = sinon.stub().callsArg(1)
describe "moving a doc", ->
@ -259,7 +263,7 @@ describe 'ProjectEntityHandler', ->
.should.equal true
it "should put the element back in the new folder", ->
@ProjectModel.putElement
@ProjectEntityHandler._putElement
.calledWith(
project_id,
folder_id,
@ -327,7 +331,7 @@ describe 'ProjectEntityHandler', ->
.should.equal true
it "should put the element back in the new folder", ->
@ProjectModel.putElement
@ProjectEntityHandler._putElement
.calledWith(
project_id,
@move_to_folder_id,
@ -407,7 +411,7 @@ describe 'ProjectEntityHandler', ->
@lines = ['1234','abc']
@path = "/path/to/doc"
@ProjectModel.putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:@path}})
@ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:@path}})
@callback = sinon.stub()
@tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
@DocstoreManager.updateDoc = sinon.stub().callsArgWith(3, null, true, 0)
@ -415,12 +419,12 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.addDoc project_id, folder_id, @name, @lines, @callback
# Created doc
@doc = @ProjectModel.putElement.args[0][2]
@doc = @ProjectEntityHandler._putElement.args[0][2]
@doc.name.should.equal @name
expect(@doc.lines).to.be.undefined
it 'should call put element', ->
@ProjectModel.putElement
@ProjectEntityHandler._putElement
.calledWith(project_id, folder_id, @doc)
.should.equal true
@ -482,7 +486,7 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.addFile project_id, folder_id, fileName, @filePath, (err, fileRef, parentFolder)->
it 'should put file into folder by calling put element', (done)->
@ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
@ProjectEntityHandler._putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
passedProject_id.should.equal project_id
passedFolder_id.should.equal folder_id
passedFileRef.name.should.equal fileName
@ -492,6 +496,7 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
it 'should return doc and parent folder', (done)->
@ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
@ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
parentFolder.should.equal folder_id
fileRef.name.should.equal fileName
@ -502,7 +507,7 @@ describe 'ProjectEntityHandler', ->
opts =
path : "/somehwere/idsadsds"
project_id : project_id
@ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
@ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
@tpdsUpdateSender.addFile = (options)=>
options.project_id.should.equal project_id
@ -572,7 +577,7 @@ describe 'ProjectEntityHandler', ->
folderName = "folder1234"
it 'should call put element', (done)->
@ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFolder, passedType, callback)->
@ProjectEntityHandler._putElement = (passedProject_id, passedFolder_id, passedFolder, passedType, callback)->
passedProject_id.should.equal project_id
passedFolder_id.should.equal folder_id
passedFolder.name.should.equal folderName
@ -581,6 +586,7 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
it 'should return the folder and parent folder', (done)->
@ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4)
@ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
parentFolder.should.equal folder_id
folder.name.should.equal folderName
@ -873,14 +879,17 @@ describe 'ProjectEntityHandler', ->
oldFileRef = {name:fileName, _id:"oldFileRef"}
beforeEach ->
@ProjectModel.getProject = (project_id, fields, callback)=> callback(null, {name:@project.name, _id:@project._id})
@ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
it 'should copy the file in FileStoreHandler', (done)->
@ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
@ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)=>
@FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
done()
it 'should put file into folder by calling put element', (done)->
@ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
@ProjectEntityHandler._putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
passedProject_id.should.equal project_id
passedFolder_id.should.equal folder_id
passedFileRef.name.should.equal fileName
@ -900,7 +909,7 @@ describe 'ProjectEntityHandler', ->
opts =
path : "/somehwere/idsadsds"
project_id : project_id
@ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
@ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
@tpdsUpdateSender.addFile = (options)=>
options.project_id.should.equal project_id
@ -1009,4 +1018,53 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.unsetRootDoc.called.should.equal false
it "should call the callback", ->
@callback.called.should.equal true
@callback.called.should.equal true
describe "_putElement", ->
beforeEach ->
@project_id = project_id
@project =
_id: ObjectId(project_id)
rootFolder: [_id:ObjectId()]
@folder =
_id: ObjectId()
name: "someFolder"
@doc =
_id: ObjectId()
name: "new.tex"
@path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
@ProjectGetter.getProject.callsArgWith(2, null, @project)
@projectLocator.findElement.callsArgWith(1, null, @folder, @path)
@ProjectUpdateStub.callsArgWith(3)
describe "updating the project", ->
it "should use the correct mongo path", (done)->
@ProjectEntityHandler._putElement @project_id, @folder._id, @doc, "docs", (err)=>
@ProjectModel.update.args[0][0]._id.should.equal @project_id
assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
done()
it "should add an s onto the type if not included", (done)->
@ProjectEntityHandler._putElement @project_id, @folder._id, @doc, "doc", (err)=>
assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
done()
it "should not call update if elemenet is null", (done)->
@ProjectEntityHandler._putElement @project_id, @folder._id, null, "doc", (err)=>
@ProjectModel.update.called.should.equal false
done()
it "should default to root folder insert", (done)->
@ProjectEntityHandler._putElement @project_id, null, @doc, "doc", (err)=>
@projectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
done()