|
|
@@ -18,13 +18,14 @@ describe "CollaboratorsHandler", ->
|
|
|
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
|
|
|
"./CollaboratorsEmailHandler": @CollaboratorsEmailHandler = {}
|
|
|
"../Errors/Errors": Errors
|
|
|
+ "../Project/ProjectEditorHandler": @ProjectEditorHandler = {}
|
|
|
|
|
|
@project_id = "mock-project-id"
|
|
|
@user_id = "mock-user-id"
|
|
|
@adding_user_id = "adding-user-id"
|
|
|
@email = "joe@sharelatex.com"
|
|
|
@callback = sinon.stub()
|
|
|
-
|
|
|
+
|
|
|
describe "getMemberIdsWithPrivilegeLevels", ->
|
|
|
describe "with project", ->
|
|
|
beforeEach ->
|
|
|
@@ -46,16 +47,16 @@ describe "CollaboratorsHandler", ->
|
|
|
{ id: "read-write-ref-2", privilegeLevel: "readAndWrite" }
|
|
|
])
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "with a missing project", ->
|
|
|
beforeEach ->
|
|
|
@Project.findOne = sinon.stub().yields(null, null)
|
|
|
-
|
|
|
+
|
|
|
it "should return a NotFoundError", (done) ->
|
|
|
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels @project_id, (error) ->
|
|
|
error.should.be.instanceof Errors.NotFoundError
|
|
|
done()
|
|
|
-
|
|
|
+
|
|
|
describe "getMemberIds", ->
|
|
|
beforeEach ->
|
|
|
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels = sinon.stub()
|
|
|
@@ -68,7 +69,7 @@ describe "CollaboratorsHandler", ->
|
|
|
@callback
|
|
|
.calledWith(null, ["member-id-1", "member-id-2"])
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "getMembersWithPrivilegeLevels", ->
|
|
|
beforeEach ->
|
|
|
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels = sinon.stub()
|
|
|
@@ -86,7 +87,7 @@ describe "CollaboratorsHandler", ->
|
|
|
@UserGetter.getUser.withArgs("read-write-ref-2").yields(null, { _id: "read-write-ref-2" })
|
|
|
@UserGetter.getUser.withArgs("doesnt-exist").yields(null, null)
|
|
|
@CollaboratorHandler.getMembersWithPrivilegeLevels @project_id, @callback
|
|
|
-
|
|
|
+
|
|
|
it "should return an array of members with their privilege levels", ->
|
|
|
@callback
|
|
|
.calledWith(null, [
|
|
|
@@ -96,7 +97,7 @@ describe "CollaboratorsHandler", ->
|
|
|
{ user: { _id: "read-write-ref-2" }, privilegeLevel: "readAndWrite" }
|
|
|
])
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "getMemberIdPrivilegeLevel", ->
|
|
|
beforeEach ->
|
|
|
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels = sinon.stub()
|
|
|
@@ -116,7 +117,7 @@ describe "CollaboratorsHandler", ->
|
|
|
@CollaboratorHandler.getMemberIdPrivilegeLevel "member-id-3", @project_id, (error, level) ->
|
|
|
expect(level).to.equal false
|
|
|
done()
|
|
|
-
|
|
|
+
|
|
|
describe "isUserMemberOfProject", ->
|
|
|
beforeEach ->
|
|
|
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels = sinon.stub()
|
|
|
@@ -128,7 +129,7 @@ describe "CollaboratorsHandler", ->
|
|
|
{ id: @user_id, privilegeLevel: "readAndWrite" }
|
|
|
])
|
|
|
@CollaboratorHandler.isUserMemberOfProject @user_id, @project_id, @callback
|
|
|
-
|
|
|
+
|
|
|
it "should return true and the privilegeLevel", ->
|
|
|
@callback
|
|
|
.calledWith(null, true, "readAndWrite")
|
|
|
@@ -140,12 +141,12 @@ describe "CollaboratorsHandler", ->
|
|
|
{ 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 "getProjectsUserIsCollaboratorOf", ->
|
|
|
beforeEach ->
|
|
|
@fields = "mock fields"
|
|
|
@@ -153,7 +154,7 @@ describe "CollaboratorsHandler", ->
|
|
|
@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.getProjectsUserIsCollaboratorOf @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"])
|
|
|
@@ -172,7 +173,7 @@ describe "CollaboratorsHandler", ->
|
|
|
"$pull":{collaberator_refs:@user_id, readOnly_refs:@user_id}
|
|
|
})
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "addUserToProject", ->
|
|
|
beforeEach ->
|
|
|
@Project.update = sinon.stub().callsArg(2)
|
|
|
@@ -182,7 +183,7 @@ describe "CollaboratorsHandler", ->
|
|
|
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user = { _id: @user_id, email: @email })
|
|
|
@CollaboratorsEmailHandler.notifyUserOfProjectShare = sinon.stub()
|
|
|
@ContactManager.addContact = sinon.stub()
|
|
|
-
|
|
|
+
|
|
|
describe "as readOnly", ->
|
|
|
beforeEach ->
|
|
|
@CollaboratorHandler.addUserIdToProject @project_id, @adding_user_id, @user_id, "readOnly", @callback
|
|
|
@@ -195,7 +196,7 @@ describe "CollaboratorsHandler", ->
|
|
|
"$addToSet":{ readOnly_refs: @user_id }
|
|
|
})
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should flush the project to the TPDS", ->
|
|
|
@ProjectEntityHandler.flushProjectToThirdPartyDataStore
|
|
|
.calledWith(@project_id)
|
|
|
@@ -205,12 +206,12 @@ describe "CollaboratorsHandler", ->
|
|
|
@CollaboratorsEmailHandler.notifyUserOfProjectShare
|
|
|
.calledWith(@project_id, @email)
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should add the user as a contact for the adding user", ->
|
|
|
@ContactManager.addContact
|
|
|
.calledWith(@adding_user_id, @user_id)
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "as readAndWrite", ->
|
|
|
beforeEach ->
|
|
|
@CollaboratorHandler.addUserIdToProject @project_id, @adding_user_id, @user_id, "readAndWrite", @callback
|
|
|
@@ -223,19 +224,19 @@ describe "CollaboratorsHandler", ->
|
|
|
"$addToSet":{ collaberator_refs: @user_id }
|
|
|
})
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should flush the project to the TPDS", ->
|
|
|
@ProjectEntityHandler.flushProjectToThirdPartyDataStore
|
|
|
.calledWith(@project_id)
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "with invalid privilegeLevel", ->
|
|
|
beforeEach ->
|
|
|
@CollaboratorHandler.addUserIdToProject @project_id, @adding_user_id, @user_id, "notValid", @callback
|
|
|
|
|
|
it "should call the callback with an error", ->
|
|
|
@callback.calledWith(new Error()).should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "when user already exists as a collaborator", ->
|
|
|
beforeEach ->
|
|
|
@project.collaberator_refs = [@user_id]
|
|
|
@@ -243,7 +244,7 @@ describe "CollaboratorsHandler", ->
|
|
|
|
|
|
it "should not add the user again", ->
|
|
|
@Project.update.called.should.equal false
|
|
|
-
|
|
|
+
|
|
|
describe "addEmailToProject", ->
|
|
|
beforeEach ->
|
|
|
@UserCreator.getUserOrCreateHoldingAccount = sinon.stub().callsArgWith(1, null, @user = {_id: @user_id})
|
|
|
@@ -252,27 +253,27 @@ describe "CollaboratorsHandler", ->
|
|
|
describe "with a valid email", ->
|
|
|
beforeEach ->
|
|
|
@CollaboratorHandler.addEmailToProject @project_id, @adding_user_id, (@email = "Joe@example.com"), (@privilegeLevel = "readAndWrite"), @callback
|
|
|
-
|
|
|
+
|
|
|
it "should get the user with the lowercased email", ->
|
|
|
@UserCreator.getUserOrCreateHoldingAccount
|
|
|
.calledWith(@email.toLowerCase())
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should add the user to the project by id", ->
|
|
|
@CollaboratorHandler.addUserIdToProject
|
|
|
.calledWith(@project_id, @adding_user_id, @user_id, @privilegeLevel)
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should return the callback with the user_id", ->
|
|
|
@callback.calledWith(null, @user_id).should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "with an invalid email", ->
|
|
|
beforeEach ->
|
|
|
@CollaboratorHandler.addEmailToProject @project_id, @adding_user_id, "not-and-email", (@privilegeLevel = "readAndWrite"), @callback
|
|
|
-
|
|
|
+
|
|
|
it "should call the callback with an error", ->
|
|
|
@callback.calledWith(new Error()).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should not add any users to the proejct", ->
|
|
|
@CollaboratorHandler.addUserIdToProject.called.should.equal false
|
|
|
|
|
|
@@ -286,9 +287,67 @@ describe "CollaboratorsHandler", ->
|
|
|
)
|
|
|
@CollaboratorHandler.removeUserFromProject = sinon.stub().yields()
|
|
|
@CollaboratorHandler.removeUserFromAllProjets @user_id, done
|
|
|
-
|
|
|
+
|
|
|
it "should remove the user from each project", ->
|
|
|
for project_id in ["read-and-write-0", "read-and-write-1", "read-only-0", "read-only-1"]
|
|
|
@CollaboratorHandler.removeUserFromProject
|
|
|
.calledWith(project_id, @user_id)
|
|
|
- .should.equal true
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ describe 'getAllMembers', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @owning_user = {_id: 'owner-id', email: 'owner@example.com', features: {a: 1}}
|
|
|
+ @readwrite_user = {_id: 'readwrite-id', email: 'readwrite@example.com'}
|
|
|
+ @members = [
|
|
|
+ {user: @owning_user, privilegeLevel: "owner"},
|
|
|
+ {user: @readwrite_user, privilegeLevel: "readAndWrite"}
|
|
|
+ ]
|
|
|
+ @CollaboratorHandler.getMembersWithPrivilegeLevels = sinon.stub().callsArgWith(1, null, @members)
|
|
|
+ @ProjectEditorHandler.buildOwnerAndMembersViews = sinon.stub().returns(@views = {
|
|
|
+ owner: @owning_user,
|
|
|
+ ownerFeatures: @owning_user.features,
|
|
|
+ members: [ {_id: @readwrite_user._id, email: @readwrite_user.email} ]
|
|
|
+ })
|
|
|
+ @callback = sinon.stub()
|
|
|
+ @CollaboratorHandler.getAllMembers @project_id, @callback
|
|
|
+
|
|
|
+ it 'should not produce an error', ->
|
|
|
+ @callback.callCount.should.equal 1
|
|
|
+ expect(@callback.firstCall.args[0]).to.equal null
|
|
|
+
|
|
|
+ it 'should produce a list of members', ->
|
|
|
+ @callback.callCount.should.equal 1
|
|
|
+ expect(@callback.firstCall.args[1]).to.deep.equal @views.members
|
|
|
+
|
|
|
+ it 'should call getMembersWithPrivileges', ->
|
|
|
+ @CollaboratorHandler.getMembersWithPrivilegeLevels.callCount.should.equal 1
|
|
|
+ @CollaboratorHandler.getMembersWithPrivilegeLevels.firstCall.args[0].should.equal @project_id
|
|
|
+
|
|
|
+ it 'should call ProjectEditorHandler.buildOwnerAndMembersViews', ->
|
|
|
+ @ProjectEditorHandler.buildOwnerAndMembersViews.callCount.should.equal 1
|
|
|
+ @ProjectEditorHandler.buildOwnerAndMembersViews.firstCall.args[0].should.equal @members
|
|
|
+
|
|
|
+ describe 'when getMembersWithPrivileges produces an error', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @CollaboratorHandler.getMembersWithPrivilegeLevels = sinon.stub().callsArgWith(1, new Error('woops'))
|
|
|
+ @ProjectEditorHandler.buildOwnerAndMembersViews = sinon.stub().returns(@views = {
|
|
|
+ owner: @owning_user,
|
|
|
+ ownerFeatures: @owning_user.features,
|
|
|
+ members: [ {_id: @readwrite_user._id, email: @readwrite_user.email} ]
|
|
|
+ })
|
|
|
+ @callback = sinon.stub()
|
|
|
+ @CollaboratorHandler.getAllMembers @project_id, @callback
|
|
|
+
|
|
|
+ it 'should produce an error', ->
|
|
|
+ @callback.callCount.should.equal 1
|
|
|
+ expect(@callback.firstCall.args[0]).to.not.equal null
|
|
|
+ expect(@callback.firstCall.args[0]).to.be.instanceof Error
|
|
|
+
|
|
|
+ it 'should call getMembersWithPrivileges', ->
|
|
|
+ @CollaboratorHandler.getMembersWithPrivilegeLevels.callCount.should.equal 1
|
|
|
+ @CollaboratorHandler.getMembersWithPrivilegeLevels.firstCall.args[0].should.equal @project_id
|
|
|
+
|
|
|
+ it 'should not call ProjectEditorHandler.buildOwnerAndMembersViews', ->
|
|
|
+ @ProjectEditorHandler.buildOwnerAndMembersViews.callCount.should.equal 0
|