Refactor addUserToProject for better access by groups

This commit is contained in:
James Allen
2015-10-14 17:29:58 +01:00
parent c4e4f2c77a
commit d996ed6e47
8 changed files with 75 additions and 58 deletions
@@ -14,12 +14,10 @@ describe "CollaboratorsController", ->
@CollaboratorsController = SandboxedModule.require modulePath, requires:
"../Project/ProjectGetter": @ProjectGetter = {}
"./CollaboratorsHandler": @CollaboratorsHandler = {}
"./CollaboratorsEmailHandler": @CollaboratorsEmailHandler = {}
"../User/UserGetter": @UserGetter = {}
"../Editor/EditorRealTimeController": @EditorRealTimeController = {}
'../Subscription/LimitationsManager' : @LimitationsManager = {}
'../Project/ProjectEditorHandler' : @ProjectEditorHandler = {}
"../Contacts/ContactManager": @ContactManager = {}
'../User/UserGetter': @UserGetter = {}
@res = new MockResponse()
@req = new MockRequest()
@@ -72,13 +70,11 @@ describe "CollaboratorsController", ->
@user_view = {
id: @user_id, first_name: "Joe", last_name: "Example", email: "joe@example.com"
}
@LimitationsManager.isCollaboratorLimitReached = sinon.stub().callsArgWith(1, null, false)
@LimitationsManager.canAddXCollaborators = sinon.stub().callsArgWith(2, null, true)
@ProjectEditorHandler.buildUserModelView = sinon.stub().returns(@user_view)
@CollaboratorsHandler.addEmailToProject = sinon.stub().callsArgWith(3, null, @user_id)
@CollaboratorsHandler.addEmailToProject = sinon.stub().callsArgWith(4, null, @user_id)
@UserGetter.getUser = sinon.stub().callsArgWith(1, null, @user)
@CollaboratorsEmailHandler.notifyUserOfProjectShare = sinon.stub()
@EditorRealTimeController.emitToRoom = sinon.stub()
@ContactManager.addContact = sinon.stub()
@callback = sinon.stub()
describe "when the project can accept more collaborators", ->
@@ -87,23 +83,13 @@ describe "CollaboratorsController", ->
it "should add the user to the project", ->
@CollaboratorsHandler.addEmailToProject
.calledWith(@project_id, @email.toLowerCase(), @privileges)
.calledWith(@project_id, @adding_user_id, @email.toLowerCase(), @privileges)
.should.equal true
it "should emit a userAddedToProject event", ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "userAddedToProject", @user_view, @privileges)
.should.equal true
it "should send an email to the shared-with user", ->
@CollaboratorsEmailHandler.notifyUserOfProjectShare
.calledWith(@project_id, @email.toLowerCase())
.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
it "should send the user as the response body", ->
@res.json
@@ -114,7 +100,7 @@ describe "CollaboratorsController", ->
describe "when the project cannot accept more collaborators", ->
beforeEach ->
@LimitationsManager.isCollaboratorLimitReached = sinon.stub().callsArgWith(1, null, true)
@LimitationsManager.canAddXCollaborators = sinon.stub().callsArgWith(2, null, false)
@CollaboratorsController.addUserToProject @req, @res, @next
it "should not add the user to the project", ->
@@ -11,11 +11,16 @@ describe "CollaboratorsHandler", ->
@CollaboratorHandler = SandboxedModule.require modulePath, requires:
"logger-sharelatex": @logger = { log: sinon.stub(), err: sinon.stub() }
'../User/UserCreator': @UserCreator = {}
'../User/UserGetter': @UserGetter = {}
"../Contacts/ContactManager": @ContactManager = {}
"../../models/Project": Project: @Project = {}
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
"./CollaboratorsEmailHandler": @CollaboratorsEmailHandler = {}
@project_id = "mock-project-id"
@user_id = "mock-user-id"
@adding_user_id = "adding-user-id"
@email = "joe@sharelatex.com"
@callback = sinon.stub()
describe "removeUserFromProject", ->
@@ -36,10 +41,14 @@ describe "CollaboratorsHandler", ->
beforeEach ->
@Project.update = sinon.stub().callsArg(2)
@ProjectEntityHandler.flushProjectToThirdPartyDataStore = sinon.stub().callsArg(1)
@CollaboratorHandler.addEmailToProject = sinon.stub().callsArgWith(4, null, @user_id)
@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.addUserToProject @project_id, @user_id, "readOnly", @callback
@CollaboratorHandler.addUserIdToProject @project_id, @adding_user_id, @user_id, "readOnly", @callback
it "should add the user to the readOnly_refs", ->
@Project.update
@@ -54,10 +63,20 @@ describe "CollaboratorsHandler", ->
@ProjectEntityHandler.flushProjectToThirdPartyDataStore
.calledWith(@project_id)
.should.equal true
it "should send an email to the shared-with user", ->
@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.addUserToProject @project_id, @user_id, "readAndWrite", @callback
@CollaboratorHandler.addUserIdToProject @project_id, @adding_user_id, @user_id, "readAndWrite", @callback
it "should add the user to the collaberator_refs", ->
@Project.update
@@ -75,7 +94,7 @@ describe "CollaboratorsHandler", ->
describe "with invalid privilegeLevel", ->
beforeEach ->
@CollaboratorHandler.addUserToProject @project_id, @user_id, "notValid", @callback
@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
@@ -83,11 +102,11 @@ describe "CollaboratorsHandler", ->
describe "addEmailToProject", ->
beforeEach ->
@UserCreator.getUserOrCreateHoldingAccount = sinon.stub().callsArgWith(1, null, @user = {_id: @user_id})
@CollaboratorHandler.addUserToProject = sinon.stub().callsArg(3)
@CollaboratorHandler.addUserIdToProject = sinon.stub().callsArg(4)
describe "with a valid email", ->
beforeEach ->
@CollaboratorHandler.addEmailToProject @project_id, (@email = "Joe@example.com"), (@privilegeLevel = "readAndWrite"), @callback
@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
@@ -95,8 +114,8 @@ describe "CollaboratorsHandler", ->
.should.equal true
it "should add the user to the project by id", ->
@CollaboratorHandler.addUserToProject
.calledWith(@project_id, @user_id, @privilegeLevel)
@CollaboratorHandler.addUserIdToProject
.calledWith(@project_id, @adding_user_id, @user_id, @privilegeLevel)
.should.equal true
it "should return the callback with the user_id", ->
@@ -104,13 +123,13 @@ describe "CollaboratorsHandler", ->
describe "with an invalid email", ->
beforeEach ->
@CollaboratorHandler.addEmailToProject @project_id, "not-and-email", (@privilegeLevel = "readAndWrite"), @callback
@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.addUserToProject.called.should.equal false
@CollaboratorHandler.addUserIdToProject.called.should.equal false
@@ -62,7 +62,7 @@ describe "LimitationsManager", ->
it "should return the total number of collaborators", ->
@callback.calledWith(null, 3).should.equal true
describe "isCollaboratorLimitReached", ->
describe "canAddXCollaborators", ->
beforeEach ->
sinon.stub @LimitationsManager,
"currentNumberOfCollaboratorsInProject",
@@ -76,7 +76,16 @@ describe "LimitationsManager", ->
beforeEach ->
@current_number = 1
@allowed_number = 2
@LimitationsManager.isCollaboratorLimitReached(@project_id, @callback)
@LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
it "should return true", ->
@callback.calledWith(null, true).should.equal true
describe "when the project has fewer collaborators than allowed but I want to add more than allowed", ->
beforeEach ->
@current_number = 1
@allowed_number = 2
@LimitationsManager.canAddXCollaborators(@project_id, 2, @callback)
it "should return false", ->
@callback.calledWith(null, false).should.equal true
@@ -85,19 +94,19 @@ describe "LimitationsManager", ->
beforeEach ->
@current_number = 3
@allowed_number = 2
@LimitationsManager.isCollaboratorLimitReached(@project_id, @callback)
@LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
it "should return true", ->
@callback.calledWith(null, true).should.equal true
it "should return false", ->
@callback.calledWith(null, false).should.equal true
describe "when the project has infinite collaborators", ->
beforeEach ->
@current_number = 100
@allowed_number = -1
@LimitationsManager.isCollaboratorLimitReached(@project_id, @callback)
@LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
it "should return false", ->
@callback.calledWith(null, false).should.equal true
it "should return true", ->
@callback.calledWith(null, true).should.equal true
describe "userHasSubscription", ->