Merge pull request #339 from sharelatex/hof-project-getter

use ProjectGetter rather than Project directly
This commit is contained in:
Hayden Faulds 2018-02-16 11:34:21 +00:00 committed by GitHub
commit 5456f4224c
20 changed files with 71 additions and 65 deletions

View file

@ -1,5 +1,5 @@
CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler")
Project = require("../../models/Project").Project
ProjectGetter = require('../Project/ProjectGetter')
User = require("../../models/User").User
PrivilegeLevels = require("./PrivilegeLevels")
PublicAccessLevels = require("./PublicAccessLevels")
@ -14,7 +14,7 @@ module.exports = AuthorizationManager =
if !ObjectId.isValid(project_id)
return callback(new Error("invalid project id"))
# Note, the Project property in the DB is `publicAccesLevel`, without the second `s`
Project.findOne { _id: project_id }, { publicAccesLevel: 1 }, (error, project) ->
ProjectGetter.getProject project_id, publicAccesLevel: 1, (error, project) ->
return callback(error) if error?
if !project?
return callback new Errors.NotFoundError("no project found with id #{project_id}")

View file

@ -1,5 +1,6 @@
UserCreator = require('../User/UserCreator')
Project = require("../../models/Project").Project
ProjectGetter = require('../Project/ProjectGetter')
logger = require('logger-sharelatex')
UserGetter = require "../User/UserGetter"
ContactManager = require "../Contacts/ContactManager"
@ -23,7 +24,7 @@ module.exports = CollaboratorsHandler =
tokenAccessReadOnly_refs: 1,
tokenAccessReadAndWrite_refs: 1
publicAccesLevel: 1
Project.findOne { _id: project_id }, projection, (error, project) ->
ProjectGetter.getProject project_id, projection, (error, project) ->
return callback(error) if error?
return callback new Errors.NotFoundError("no project found with id #{project_id}") if !project?
members = []
@ -197,7 +198,7 @@ module.exports = CollaboratorsHandler =
async.series jobs, callback
addUserIdToProject: (project_id, adding_user_id, user_id, privilegeLevel, callback = (error) ->)->
Project.findOne { _id: project_id }, { collaberator_refs: 1, readOnly_refs: 1 }, (error, project) ->
ProjectGetter.getProject project_id, { collaberator_refs: 1, readOnly_refs: 1 }, (error, project) ->
return callback(error) if error?
existing_users = (project.collaberator_refs or [])
existing_users = existing_users.concat(project.readOnly_refs or [])

View file

@ -1,5 +1,5 @@
Metrics = require "metrics-sharelatex"
Project = require("../../models/Project").Project
ProjectGetter = require('../Project/ProjectGetter')
CompileManager = require("./CompileManager")
ClsiManager = require("./ClsiManager")
logger = require "logger-sharelatex"
@ -83,7 +83,7 @@ module.exports = CompileController =
timeInterval : 60 * 60
RateLimiter.addCount rateLimitOpts, callback
Project.findById project_id, {name: 1}, (err, project)->
ProjectGetter.getProject project_id, name: 1, (err, project) ->
res.contentType("application/pdf")
if !!req.query.popupDownload
logger.log project_id: project_id, "download pdf as popup download"

View file

@ -1,7 +1,7 @@
Settings = require('settings-sharelatex')
RedisWrapper = require("../../infrastructure/RedisWrapper")
rclient = RedisWrapper.client("clsi_recently_compiled")
Project = require("../../models/Project").Project
ProjectGetter = require('../Project/ProjectGetter')
ProjectRootDocManager = require "../Project/ProjectRootDocManager"
UserGetter = require "../User/UserGetter"
ClsiManager = require "./ClsiManager"
@ -57,7 +57,7 @@ module.exports = CompileManager =
ClsiManager.deleteAuxFiles project_id, user_id, limits, callback
getProjectCompileLimits: (project_id, callback = (error, limits) ->) ->
Project.findById project_id, {owner_ref: 1}, (error, project) ->
ProjectGetter.getProject project_id, owner_ref: 1, (error, project) ->
return callback(error) if error?
UserGetter.getUser project.owner_ref, {"features":1}, (err, owner)->
return callback(error) if error?
@ -103,7 +103,7 @@ module.exports = CompileManager =
callback err, canCompile
_ensureRootDocumentIsSet: (project_id, callback = (error) ->) ->
Project.findById project_id, 'rootDoc_id', (error, project)=>
ProjectGetter.getProject project_id, rootDoc_id: 1, (error, project) ->
return callback(error) if error?
if !project?
return callback new Error("project not found")

View file

@ -1,6 +1,6 @@
logger = require "logger-sharelatex"
Metrics = require "metrics-sharelatex"
Project = require("../../models/Project").Project
ProjectGetter = require('../Project/ProjectGetter')
ProjectZipStreamManager = require "./ProjectZipStreamManager"
DocumentUpdaterHandler = require "../DocumentUpdater/DocumentUpdaterHandler"
@ -11,7 +11,7 @@ module.exports = ProjectDownloadsController =
logger.log project_id: project_id, "downloading project"
DocumentUpdaterHandler.flushProjectToMongo project_id, (error)->
return next(error) if error?
Project.findById project_id, "name", (error, project) ->
ProjectGetter.getProject project_id, name: true, (error, project) ->
return next(error) if error?
ProjectZipStreamManager.createZipStreamForProject project_id, (error, stream) ->
return next(error) if error?

View file

@ -2,8 +2,8 @@ archiver = require "archiver"
async = require "async"
logger = require "logger-sharelatex"
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
ProjectGetter = require('../Project/ProjectGetter')
FileStoreHandler = require("../FileStore/FileStoreHandler")
Project = require("../../models/Project").Project
module.exports = ProjectZipStreamManager =
createZipStreamForMultipleProjects: (project_ids, callback = (error, stream) ->) ->
@ -20,7 +20,7 @@ module.exports = ProjectZipStreamManager =
for project_id in project_ids or []
do (project_id) ->
jobs.push (callback) ->
Project.findById project_id, "name", (error, project) ->
ProjectGetter.getProject project_id, name: true, (error, project) ->
return callback(error) if error?
logger.log project_id: project_id, name: project.name, "appending project to zip stream"
ProjectZipStreamManager.createZipStreamForProject project_id, (error, stream) ->

View file

@ -39,7 +39,7 @@ module.exports = EditorHttpController =
ProjectGetter.getProjectWithoutDocLines project_id, (error, project) ->
return callback(error) if error?
return callback(new Error("not found")) if !project?
CollaboratorsHandler.getInvitedMembersWithPrivilegeLevels project, (error, members) ->
CollaboratorsHandler.getInvitedMembersWithPrivilegeLevels project_id, (error, members) ->
return callback(error) if error?
UserGetter.getUser user_id, { isAdmin: true }, (error, user) ->
return callback(error) if error?

View file

@ -30,7 +30,7 @@ module.exports = ProjectDetailsHandler =
callback(err, details)
getProjectDescription: (project_id, callback)->
Project.findOne _id:project_id, "description", (err, project)->
ProjectGetter.getProject project_id, description: true, (err, project)->
callback(err, project?.description)
setProjectDescription: (project_id, description, callback)->

View file

@ -1,5 +1,5 @@
logger = require("logger-sharelatex")
Project = require("../../models/Project").Project
ProjectGetter = require('../Project/ProjectGetter')
UserGetter = require("../User/UserGetter")
SubscriptionLocator = require("./SubscriptionLocator")
Settings = require("settings-sharelatex")
@ -8,10 +8,10 @@ CollaboratorsInvitesHandler = require("../Collaborators/CollaboratorsInviteHandl
module.exports =
allowedNumberOfCollaboratorsInProject: (project_id, callback) ->
Project.findById project_id, 'owner_ref', (error, project) =>
ProjectGetter.getProject project_id, owner_ref: true, (error, project) =>
return callback(error) if error?
@allowedNumberOfCollaboratorsForUser project.owner_ref, callback
allowedNumberOfCollaboratorsForUser: (user_id, callback) ->
UserGetter.getUser user_id, {features: 1}, (error, user) ->
return callback(error) if error?

View file

@ -1,7 +1,7 @@
settings = require('settings-sharelatex')
logger = require('logger-sharelatex')
path = require('path')
Project = require('../../models/Project').Project
ProjectGetter = require('../Project/ProjectGetter')
keys = require('../../infrastructure/Keys')
metrics = require("metrics-sharelatex")
request = require("request")
@ -123,7 +123,7 @@ module.exports = TpdsUpdateSender =
TpdsUpdateSender._enqueue "poll-dropbox:#{user_id}", "standardHttpRequest", options, callback
getProjectsUsersIds = (project_id, callback = (err, owner_id, allUserIds)->)->
Project.findById project_id, "_id owner_ref", (err, project) ->
ProjectGetter.getProject project_id, {_id: true, owner_ref: true}, (err, project) ->
return callback(err) if err?
CollaboratorsHandler.getInvitedMemberIds project_id, (err, member_ids) ->
return callback(err) if err?

View file

@ -10,7 +10,7 @@ describe "AuthorizationManager", ->
beforeEach ->
@AuthorizationManager = SandboxedModule.require modulePath, requires:
"../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
"../../models/Project": Project: @Project = {}
'../Project/ProjectGetter': @ProjectGetter = {}
"../../models/User": User: @User = {}
"../Errors/Errors": Errors
"../TokenAccess/TokenAccessHandler": @TokenAccessHandler = {
@ -23,14 +23,14 @@ describe "AuthorizationManager", ->
describe "getPrivilegeLevelForProject", ->
beforeEach ->
@Project.findOne = sinon.stub()
@ProjectGetter.getProject = sinon.stub()
@AuthorizationManager.isUserSiteAdmin = sinon.stub()
@CollaboratorsHandler.getMemberIdPrivilegeLevel = sinon.stub()
describe 'with a token-based project', ->
beforeEach ->
@Project.findOne
.withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
@ProjectGetter.getProject
.withArgs(@project_id, { publicAccesLevel: 1 })
.yields(null, { publicAccesLevel: "tokenBased" })
describe "with a user_id with a privilege level", ->
@ -152,8 +152,8 @@ describe "AuthorizationManager", ->
describe "with a private project", ->
beforeEach ->
@Project.findOne
.withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
@ProjectGetter.getProject
.withArgs(@project_id, { publicAccesLevel: 1 })
.yields(null, { publicAccesLevel: "private" })
describe "with a user_id with a privilege level", ->
@ -204,8 +204,8 @@ describe "AuthorizationManager", ->
describe "with a public project", ->
beforeEach ->
@Project.findOne
.withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
@ProjectGetter.getProject
.withArgs(@project_id, { publicAccesLevel: 1 })
.yields(null, { publicAccesLevel: "readAndWrite" })
describe "with a user_id with a privilege level", ->
@ -256,8 +256,8 @@ describe "AuthorizationManager", ->
describe "when the project doesn't exist", ->
beforeEach ->
@Project.findOne
.withArgs({ _id: @project_id }, { publicAccesLevel: 1 })
@ProjectGetter.getProject
.withArgs(@project_id, { publicAccesLevel: 1 })
.yields(null, null)
it "should return a NotFoundError", ->
@ -273,7 +273,7 @@ describe "AuthorizationManager", ->
it "should return a error", (done)->
@AuthorizationManager.getPrivilegeLevelForProject undefined, "not project id", @token, (err) =>
@Project.findOne.called.should.equal false
@ProjectGetter.getProject.called.should.equal false
expect(err).to.exist
done()

View file

@ -17,6 +17,7 @@ describe "CollaboratorsHandler", ->
"../Contacts/ContactManager": @ContactManager = {}
"../../models/Project": Project: @Project = {}
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
"../Project/ProjectGetter": @ProjectGetter = {}
"./CollaboratorsEmailHandler": @CollaboratorsEmailHandler = {}
"../Errors/Errors": Errors
"../Project/ProjectEditorHandler": @ProjectEditorHandler = {}
@ -30,9 +31,9 @@ describe "CollaboratorsHandler", ->
describe "getMemberIdsWithPrivilegeLevels", ->
describe "with project", ->
beforeEach ->
@Project.findOne = sinon.stub()
@Project.findOne.withArgs(
{_id: @project_id},
@ProjectGetter.getProject = sinon.stub()
@ProjectGetter.getProject.withArgs(
@project_id,
{owner_ref: 1, collaberator_refs: 1, readOnly_refs: 1,
tokenAccessReadOnly_refs: 1, tokenAccessReadAndWrite_refs: 1, publicAccesLevel: 1}
).yields(null, @project = {
@ -55,7 +56,7 @@ describe "CollaboratorsHandler", ->
describe "with a missing project", ->
beforeEach ->
@Project.findOne = sinon.stub().yields(null, null)
@ProjectGetter.getProject = sinon.stub().yields(null, null)
it "should return a NotFoundError", (done) ->
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels @project_id, (error) ->
@ -260,7 +261,7 @@ describe "CollaboratorsHandler", ->
describe "addUserToProject", ->
beforeEach ->
@Project.update = sinon.stub().callsArg(2)
@Project.findOne = sinon.stub().callsArgWith(2, null, @project = {})
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project = {})
@ProjectEntityHandler.flushProjectToThirdPartyDataStore = sinon.stub().callsArg(1)
@CollaboratorHandler.addEmailToProject = sinon.stub().callsArgWith(4, null, @user_id)
@ContactManager.addContact = sinon.stub()

View file

@ -40,7 +40,7 @@ describe "CompileController", ->
@CompileController = SandboxedModule.require modulePath, requires:
"settings-sharelatex": @settings
"request": @request = sinon.stub()
"../../models/Project": Project: @Project = {}
'../Project/ProjectGetter': @ProjectGetter = {}
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
"metrics-sharelatex": @Metrics = { inc: sinon.stub() }
"./CompileManager":@CompileManager
@ -117,7 +117,7 @@ describe "CompileController", ->
getSafeProjectName: () => @safe_name = "safe-name"
@req.query = {pdfng:true}
@Project.findById = sinon.stub().callsArgWith(2, null, @project)
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
describe "when downloading for embedding", ->
beforeEach ->
@ -126,7 +126,7 @@ describe "CompileController", ->
@CompileController.downloadPdf(@req, @res, @next)
it "should look up the project", ->
@Project.findById
@ProjectGetter.getProject
.calledWith(@project_id, {name: 1})
.should.equal true

View file

@ -18,7 +18,7 @@ describe "CompileManager", ->
"../../infrastructure/RedisWrapper":
client: () => @rclient = { auth: () -> }
"../Project/ProjectRootDocManager": @ProjectRootDocManager = {}
"../../models/Project": Project: @Project = {}
'../Project/ProjectGetter': @ProjectGetter = {}
"../User/UserGetter": @UserGetter = {}
"./ClsiManager": @ClsiManager = {}
"../../infrastructure/RateLimiter": @ratelimiter
@ -103,12 +103,12 @@ describe "CompileManager", ->
compileTimeout: @timeout = 42
compileGroup: @group = "priority"
}
@Project.findById = sinon.stub().callsArgWith(2, null, @project = { owner_ref: @owner_id = "owner-id-123" })
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project = { owner_ref: @owner_id = "owner-id-123" })
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user = { features: @features })
@CompileManager.getProjectCompileLimits @project_id, @callback
it "should look up the owner of the project", ->
@Project.findById
@ProjectGetter.getProject
.calledWith(@project_id, { owner_ref: 1 })
.should.equal true
@ -174,7 +174,7 @@ describe "CompileManager", ->
describe "_ensureRootDocumentIsSet", ->
beforeEach ->
@project = {}
@Project.findById = sinon.stub().callsArgWith(2, null, @project)
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
@ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArgWith(1, null)
describe "when the root doc is set", ->
@ -183,8 +183,8 @@ describe "CompileManager", ->
@CompileManager._ensureRootDocumentIsSet(@project_id, @callback)
it "should find the project with only the rootDoc_id fiel", ->
@Project.findById
.calledWith(@project_id, "rootDoc_id")
@ProjectGetter.getProject
.calledWith(@project_id, rootDoc_id: 1)
.should.equal true
it "should not try to update the project rootDoc_id", ->
@ -199,8 +199,8 @@ describe "CompileManager", ->
@CompileManager._ensureRootDocumentIsSet(@project_id, @callback)
it "should find the project with only the rootDoc_id fiel", ->
@Project.findById
.calledWith(@project_id, "rootDoc_id")
@ProjectGetter.getProject
.calledWith(@project_id, rootDoc_id: 1)
.should.equal true
it "should update the project rootDoc_id", ->
@ -213,7 +213,7 @@ describe "CompileManager", ->
describe "when the project does not exist", ->
beforeEach ->
@Project.findById = sinon.stub().callsArgWith(2, null, null)
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, null)
@CompileManager._ensureRootDocumentIsSet(@project_id, @callback)
it "should call the callback with an error", ->

View file

@ -16,7 +16,7 @@ describe "ProjectDownloadsController", ->
@DocumentUpdaterHandler = sinon.stub()
@ProjectDownloadsController = SandboxedModule.require modulePath, requires:
"./ProjectZipStreamManager" : @ProjectZipStreamManager = {}
"../../models/Project" : Project: @Project = {}
"../Project/ProjectGetter" : @ProjectGetter = {}
"metrics-sharelatex": @metrics = {}
"logger-sharelatex" : @logger = {log: sinon.stub()}
"../DocumentUpdater/DocumentUpdaterHandler": @DocumentUpdaterHandler
@ -31,7 +31,7 @@ describe "ProjectDownloadsController", ->
@res.contentType = sinon.stub()
@res.header = sinon.stub()
@project_name = "project name with accênts"
@Project.findById = sinon.stub().callsArgWith(2, null, name: @project_name)
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, name: @project_name)
@DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith(1)
@metrics.inc = sinon.stub()
@ProjectDownloadsController.downloadProject @req, @res, @next
@ -56,7 +56,9 @@ describe "ProjectDownloadsController", ->
.should.equal true
it "should look up the project's name", ->
@Project.findById.calledWith(@project_id, "name").should.equal(true)
@ProjectGetter.getProject
.calledWith(@project_id, name: true)
.should.equal(true)
it "should name the downloaded file after the project", ->
@res.setContentDisposition

View file

@ -18,7 +18,7 @@ describe "ProjectZipStreamManager", ->
"logger-sharelatex": @logger = {error: sinon.stub(), log: sinon.stub()}
"../Project/ProjectEntityHandler" : @ProjectEntityHandler = {}
"../FileStore/FileStoreHandler": @FileStoreHandler = {}
"../../models/Project": Project: @Project = {}
'../Project/ProjectGetter': @ProjectGetter = {}
describe "createZipStreamForMultipleProjects", ->
@ -40,9 +40,9 @@ describe "ProjectZipStreamManager", ->
0
sinon.spy @ProjectZipStreamManager, "createZipStreamForProject"
@Project.findById = (project_id, fields, callback) =>
@ProjectGetter.getProject = (project_id, fields, callback) =>
callback null, name: @project_names[project_id]
sinon.spy @Project, "findById"
sinon.spy @ProjectGetter, "getProject"
@ProjectZipStreamManager.createZipStreamForMultipleProjects @project_ids, (args...) =>
@callback args...
@ -65,8 +65,8 @@ describe "ProjectZipStreamManager", ->
it "should get the names of each project", ->
for project_id in @project_ids
@Project.findById
.calledWith(project_id, "name")
@ProjectGetter.getProject
.calledWith(project_id, name: true)
.should.equal true
it "should add all of the projects to the zip", ->

View file

@ -134,7 +134,7 @@ describe "EditorHttpController", ->
it "should get the list of users in the project", ->
@CollaboratorsHandler.getInvitedMembersWithPrivilegeLevels
.calledWith(@project)
.calledWith(@project_id)
.should.equal true
it "should look up the user", ->

View file

@ -75,15 +75,17 @@ describe 'ProjectDetailsHandler', ->
describe "getProjectDescription", ->
it "should make a call to mongo just for the description", (done)->
@ProjectModel.findOne.callsArgWith(2)
@ProjectGetter.getProject.callsArgWith(2)
@handler.getProjectDescription @project_id, (err, description)=>
@ProjectModel.findOne.calledWith({_id:@project_id}, "description").should.equal true
@ProjectGetter.getProject
.calledWith(@project_id, description: true)
.should.equal true
done()
it "should return what the mongo call returns", (done)->
err = "error"
description = "cool project"
@ProjectModel.findOne.callsArgWith(2, err, {description:description})
@ProjectGetter.getProject.callsArgWith(2, err, {description:description})
@handler.getProjectDescription @project_id, (returnedErr, returnedDescription)=>
err.should.equal returnedErr
description.should.equal returnedDescription

View file

@ -8,8 +8,8 @@ describe "LimitationsManager", ->
beforeEach ->
@project = { _id: @project_id = "project-id" }
@user = { _id: @user_id = "user-id", features:{} }
@Project =
findById: (project_id, fields, callback) =>
@ProjectGetter =
getProject: (project_id, fields, callback) =>
if project_id == @project_id
callback null, @project
else
@ -25,7 +25,7 @@ describe "LimitationsManager", ->
getUsersSubscription: sinon.stub()
@LimitationsManager = SandboxedModule.require modulePath, requires:
'../../models/Project' : Project: @Project
'../Project/ProjectGetter': @ProjectGetter
'../User/UserGetter' : @UserGetter
'./SubscriptionLocator':@SubscriptionLocator
'settings-sharelatex' : @Settings = {}

View file

@ -24,7 +24,7 @@ describe 'TpdsUpdateSender', ->
member_ids = [collaberator_ref_1, read_only_ref_1, user_id]
@CollaboratorsHandler =
getInvitedMemberIds: sinon.stub().yields(null, member_ids)
@Project = findById:sinon.stub().callsArgWith(2, null, project)
@ProjectGetter = getProject: sinon.stub().callsArgWith(2, null, project)
@docstoreUrl = "docstore.sharelatex.env"
@request = sinon.stub().returns(pipe:->)
@settings =
@ -39,7 +39,7 @@ describe 'TpdsUpdateSender', ->
@updateSender = SandboxedModule.require modulePath, requires:
"settings-sharelatex": @settings
"logger-sharelatex":{log:->}
'../../models/Project': Project:@Project
'../Project/ProjectGetter': @ProjectGetter
'request':@request
'../Collaborators/CollaboratorsHandler': @CollaboratorsHandler
"metrics-sharelatex":