mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Add in required abstracted functions to CollaboratorsHandler
This commit is contained in:
parent
020e1b9b34
commit
120a142733
2 changed files with 127 additions and 0 deletions
|
@ -6,8 +6,48 @@ logger = require('logger-sharelatex')
|
|||
UserGetter = require "../User/UserGetter"
|
||||
ContactManager = require "../Contacts/ContactManager"
|
||||
CollaboratorsEmailHandler = require "./CollaboratorsEmailHandler"
|
||||
async = require "async"
|
||||
|
||||
module.exports = CollaboratorsHandler =
|
||||
getMemberIdsWithPrivilegeLevels: (project_id, callback = (error, members) ->) ->
|
||||
Project.findOne { _id: project_id }, { collaberator_refs: 1, readOnly_refs: 1 }, (error, project) ->
|
||||
return callback(error) if error?
|
||||
return callback null, null if !project?
|
||||
members = []
|
||||
for member_id in project.readOnly_refs or []
|
||||
members.push { id: member_id, privilegeLevel: "readOnly" }
|
||||
for member_id in project.collaberator_refs or []
|
||||
members.push { id: member_id, privilegeLevel: "readAndWrite" }
|
||||
return callback null, members
|
||||
|
||||
getMembersWithPrivilegeLevels: (project_id, callback = (error, members) ->) ->
|
||||
CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members) ->
|
||||
return callback(error) if error?
|
||||
async.mapLimit (members or []), 3,
|
||||
(member, cb) ->
|
||||
UserGetter.getUser member.id, (error, user) ->
|
||||
return cb(error) if error?
|
||||
return cb(null, { user: user, privilegeLevel: member.privilegeLevel })
|
||||
callback
|
||||
|
||||
getMemberCount: (project_id, callback = (error, count) ->) ->
|
||||
CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members) ->
|
||||
return callback(error) if error?
|
||||
return callback null, (members or []).length
|
||||
|
||||
isUserMemberOfProject: (user_id, project_id, callback = (error, isMember, privilegeLevel) ->) ->
|
||||
CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members) ->
|
||||
return callback(error) if error?
|
||||
for member in members or []
|
||||
if member.id.toString() == user_id.toString()
|
||||
return callback null, true, member.privilegeLevel
|
||||
return callback null, false, null
|
||||
|
||||
getProjectsUserIsMemberOf: (user_id, fields, callback = (error, readAndWriteProjects, readOnlyProjects) ->) ->
|
||||
Project.find {collaberator_refs:user_id}, fields, (err, readAndWriteProjects)=>
|
||||
Project.find {readOnly_refs:user_id}, fields, (err, readOnlyProjects)=>
|
||||
callback(err, readAndWriteProjects, readOnlyProjects)
|
||||
|
||||
removeUserFromProject: (project_id, user_id, callback = (error) ->)->
|
||||
logger.log user_id: user_id, project_id: project_id, "removing user"
|
||||
conditions = _id:project_id
|
||||
|
|
|
@ -23,6 +23,93 @@ describe "CollaboratorsHandler", ->
|
|||
@email = "joe@sharelatex.com"
|
||||
@callback = sinon.stub()
|
||||
|
||||
describe "getMemberIdsWithPrivilegeLevels", ->
|
||||
beforeEach ->
|
||||
@Project.findOne = sinon.stub()
|
||||
@Project.findOne.withArgs({_id: @project_id}, {collaberator_refs: 1, readOnly_refs: 1}).yields(null, @project = {
|
||||
readOnly_refs: [ "read-only-ref-1", "read-only-ref-2" ]
|
||||
collaberator_refs: [ "read-write-ref-1", "read-write-ref-2" ]
|
||||
})
|
||||
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels @project_id, @callback
|
||||
|
||||
it "should return an array of member ids with their privilege levels", ->
|
||||
@callback
|
||||
.calledWith(null, [
|
||||
{ id: "read-only-ref-1", privilegeLevel: "readOnly" }
|
||||
{ id: "read-only-ref-2", privilegeLevel: "readOnly" }
|
||||
{ id: "read-write-ref-1", privilegeLevel: "readAndWrite" }
|
||||
{ id: "read-write-ref-2", privilegeLevel: "readAndWrite" }
|
||||
])
|
||||
.should.equal true
|
||||
|
||||
describe "getMembersWithPrivilegeLevels", ->
|
||||
beforeEach ->
|
||||
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels = sinon.stub()
|
||||
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels.withArgs(@project_id).yields(null, [
|
||||
{ id: "read-only-ref-1", privilegeLevel: "readOnly" }
|
||||
{ id: "read-only-ref-2", privilegeLevel: "readOnly" }
|
||||
{ id: "read-write-ref-1", privilegeLevel: "readAndWrite" }
|
||||
{ id: "read-write-ref-2", privilegeLevel: "readAndWrite" }
|
||||
])
|
||||
@UserGetter.getUser = sinon.stub()
|
||||
@UserGetter.getUser.withArgs("read-only-ref-1").yields(null, { _id: "read-only-ref-1" })
|
||||
@UserGetter.getUser.withArgs("read-only-ref-2").yields(null, { _id: "read-only-ref-2" })
|
||||
@UserGetter.getUser.withArgs("read-write-ref-1").yields(null, { _id: "read-write-ref-1" })
|
||||
@UserGetter.getUser.withArgs("read-write-ref-2").yields(null, { _id: "read-write-ref-2" })
|
||||
@CollaboratorHandler.getMembersWithPrivilegeLevels @project_id, @callback
|
||||
|
||||
it "should return an array of members with their privilege levels", ->
|
||||
@callback
|
||||
.calledWith(undefined, [
|
||||
{ user: { _id: "read-only-ref-1" }, privilegeLevel: "readOnly" }
|
||||
{ user: { _id: "read-only-ref-2" }, privilegeLevel: "readOnly" }
|
||||
{ user: { _id: "read-write-ref-1" }, privilegeLevel: "readAndWrite" }
|
||||
{ user: { _id: "read-write-ref-2" }, privilegeLevel: "readAndWrite" }
|
||||
])
|
||||
.should.equal true
|
||||
|
||||
describe "isUserMemberOfProject", ->
|
||||
beforeEach ->
|
||||
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels = sinon.stub()
|
||||
|
||||
describe "when user is a member of the project", ->
|
||||
beforeEach ->
|
||||
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels.withArgs(@project_id).yields(null, [
|
||||
{ id: "not-the-user", privilegeLevel: "readOnly" }
|
||||
{ id: @user_id, privilegeLevel: "readAndWrite" }
|
||||
])
|
||||
@CollaboratorHandler.isUserMemberOfProject @user_id, @project_id, @callback
|
||||
|
||||
it "should return true and the privilegeLevel", ->
|
||||
@callback
|
||||
.calledWith(null, true, "readAndWrite")
|
||||
.should.equal true
|
||||
|
||||
describe "when user is not a member of the project", ->
|
||||
beforeEach ->
|
||||
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels.withArgs(@project_id).yields(null, [
|
||||
{ id: "not-the-user", privilegeLevel: "readOnly" }
|
||||
])
|
||||
@CollaboratorHandler.isUserMemberOfProject @user_id, @project_id, @callback
|
||||
|
||||
it "should return false", ->
|
||||
@callback
|
||||
.calledWith(null, false, null)
|
||||
.should.equal true
|
||||
|
||||
describe "getProjectsUserIsMemberOf", ->
|
||||
beforeEach ->
|
||||
@fields = "mock fields"
|
||||
@Project.find = sinon.stub()
|
||||
@Project.find.withArgs({collaberator_refs:@user_id}, @fields).yields(null, ["mock-read-write-project-1", "mock-read-write-project-2"])
|
||||
@Project.find.withArgs({readOnly_refs:@user_id}, @fields).yields(null, ["mock-read-only-project-1", "mock-read-only-project-2"])
|
||||
@CollaboratorHandler.getProjectsUserIsMemberOf @user_id, @fields, @callback
|
||||
|
||||
it "should call the callback with the projects", ->
|
||||
@callback
|
||||
.calledWith(null, ["mock-read-write-project-1", "mock-read-write-project-2"], ["mock-read-only-project-1", "mock-read-only-project-2"])
|
||||
.should.equal true
|
||||
|
||||
describe "removeUserFromProject", ->
|
||||
beforeEach ->
|
||||
@Project.update = sinon.stub().callsArg(2)
|
||||
|
|
Loading…
Reference in a new issue