|
|
@@ -18,12 +18,106 @@ describe "CollaboratorsInviteController", ->
|
|
|
'../Subscription/LimitationsManager' : @LimitationsManager = {}
|
|
|
'../Project/ProjectEditorHandler' : @ProjectEditorHandler = {}
|
|
|
'../User/UserGetter': @UserGetter = {}
|
|
|
+ 'logger-sharelatex': @logger = {err: sinon.stub(), error: sinon.stub(), log: sinon.stub()}
|
|
|
@res = new MockResponse()
|
|
|
@req = new MockRequest()
|
|
|
|
|
|
@project_id = "project-id-123"
|
|
|
@callback = sinon.stub()
|
|
|
|
|
|
+ describe 'inviteToProject', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @targetEmail = "user@example.com"
|
|
|
+ @req.params =
|
|
|
+ Project_id: @project_id
|
|
|
+ @req.session =
|
|
|
+ user: _id: @current_user_id = "current-user-id"
|
|
|
+ @req.body =
|
|
|
+ email: @targetEmail
|
|
|
+ privileges: @privileges = "readAndWrite"
|
|
|
+ @res.json = sinon.stub()
|
|
|
+ @res.sendStatus = sinon.stub()
|
|
|
+ @invite = {
|
|
|
+ _id: ObjectId(),
|
|
|
+ token: "htnseuthaouse",
|
|
|
+ sendingUserId: @current_user_id,
|
|
|
+ projectId: @targetEmail,
|
|
|
+ targetEmail: 'user@example.com'
|
|
|
+ createdAt: new Date(),
|
|
|
+ expiresAt: new Date()
|
|
|
+ }
|
|
|
+ @LimitationsManager.canAddXCollaborators = sinon.stub().callsArgWith(2, null, true)
|
|
|
+ @CollaboratorsInviteHandler.inviteToProject = sinon.stub().callsArgWith(4, null, @invite)
|
|
|
+ @callback = sinon.stub()
|
|
|
+ @next = sinon.stub()
|
|
|
+
|
|
|
+ describe 'when all goes well', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @LimitationsManager.canAddXCollaborators = sinon.stub().callsArgWith(2, null, true)
|
|
|
+ @CollaboratorsInviteController.inviteToProject @req, @res, @next
|
|
|
+
|
|
|
+ it 'should produce json response', ->
|
|
|
+ @res.json.callCount.should.equal 1
|
|
|
+ ({inviteId: @invite._id}).should.deep.equal(@res.json.firstCall.args[0])
|
|
|
+
|
|
|
+ it 'should have called canAddXCollaborators', ->
|
|
|
+ @LimitationsManager.canAddXCollaborators.callCount.should.equal 1
|
|
|
+ @LimitationsManager.canAddXCollaborators.calledWith(@project_id).should.equal true
|
|
|
+
|
|
|
+ it 'should have called inviteToProject', ->
|
|
|
+ @CollaboratorsInviteHandler.inviteToProject.callCount.should.equal 1
|
|
|
+ @CollaboratorsInviteHandler.inviteToProject.calledWith(@project_id,@current_user_id,@targetEmail,@privileges).should.equal true
|
|
|
+
|
|
|
+ describe 'when the user is not allowed to add more collaborators', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @LimitationsManager.canAddXCollaborators = sinon.stub().callsArgWith(2, null, false)
|
|
|
+ @CollaboratorsInviteController.inviteToProject @req, @res, @next
|
|
|
+
|
|
|
+ it 'should produce json response without inviteId', ->
|
|
|
+ @res.json.callCount.should.equal 1
|
|
|
+ ({inviteId: null}).should.deep.equal(@res.json.firstCall.args[0])
|
|
|
+
|
|
|
+ it 'should not have called inviteToProject', ->
|
|
|
+ @CollaboratorsInviteHandler.inviteToProject.callCount.should.equal 0
|
|
|
+
|
|
|
+ describe 'when canAddXCollaborators produces an error', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @err = new Error('woops')
|
|
|
+ @LimitationsManager.canAddXCollaborators = sinon.stub().callsArgWith(2, @err)
|
|
|
+ @CollaboratorsInviteController.inviteToProject @req, @res, @next
|
|
|
+
|
|
|
+ it 'should call next with an error', ->
|
|
|
+ @next.callCount.should.equal 1
|
|
|
+ @next.calledWith(@err).should.equal true
|
|
|
+
|
|
|
+ it 'should not have called inviteToProject', ->
|
|
|
+ @CollaboratorsInviteHandler.inviteToProject.callCount.should.equal 0
|
|
|
+
|
|
|
+ describe 'when inviteToProject produces an error', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @err = new Error('woops')
|
|
|
+ @CollaboratorsInviteHandler.inviteToProject = sinon.stub().callsArgWith(4, @err)
|
|
|
+ @CollaboratorsInviteController.inviteToProject @req, @res, @next
|
|
|
+
|
|
|
+ it 'should call next with an error', ->
|
|
|
+ @next.callCount.should.equal 1
|
|
|
+ @next.calledWith(@err).should.equal true
|
|
|
+
|
|
|
+ it 'should have called canAddXCollaborators', ->
|
|
|
+ @LimitationsManager.canAddXCollaborators.callCount.should.equal 1
|
|
|
+ @LimitationsManager.canAddXCollaborators.calledWith(@project_id).should.equal true
|
|
|
+
|
|
|
+ it 'should have called inviteToProject', ->
|
|
|
+ @CollaboratorsInviteHandler.inviteToProject.callCount.should.equal 1
|
|
|
+ @CollaboratorsInviteHandler.inviteToProject.calledWith(@project_id,@current_user_id,@targetEmail,@privileges).should.equal true
|
|
|
+
|
|
|
+ ##
|
|
|
+
|
|
|
describe "viewInvite", ->
|
|
|
|
|
|
beforeEach ->
|
|
|
@@ -33,6 +127,7 @@ describe "CollaboratorsInviteController", ->
|
|
|
@req.session =
|
|
|
user: _id: @current_user_id = "current-user-id"
|
|
|
@res.render = sinon.stub()
|
|
|
+ @res.sendStatus = sinon.stub()
|
|
|
@invite = {
|
|
|
_id: ObjectId(),
|
|
|
token: "htnseuthaouse",
|
|
|
@@ -54,4 +149,129 @@ describe "CollaboratorsInviteController", ->
|
|
|
|
|
|
it 'should render the view template', ->
|
|
|
@res.render.callCount.should.equal 1
|
|
|
- @res.render.firstCall.args[0].should.equal 'project/invite'
|
|
|
+ @res.render.calledWith('project/invite').should.equal true
|
|
|
+
|
|
|
+ it 'should not call next', ->
|
|
|
+ @next.callCount.should.equal 0
|
|
|
+
|
|
|
+ it 'should call getInviteByToken', ->
|
|
|
+ @CollaboratorsInviteHandler.getInviteByToken.callCount.should.equal 1
|
|
|
+
|
|
|
+ describe 'when the getInviteByToken produces an error', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @err = new Error('woops')
|
|
|
+ @CollaboratorsInviteHandler.getInviteByToken.callsArgWith(2, @err)
|
|
|
+ @CollaboratorsInviteController.viewInvite @req, @res, @next
|
|
|
+
|
|
|
+ it 'should call next with the error', ->
|
|
|
+ @next.callCount.should.equal 1
|
|
|
+ @next.calledWith(@err).should.equal true
|
|
|
+
|
|
|
+ it 'should call getInviteByToken', ->
|
|
|
+ @CollaboratorsInviteHandler.getInviteByToken.callCount.should.equal 1
|
|
|
+
|
|
|
+ describe 'when the getInviteByToken does not produce an invite', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @CollaboratorsInviteHandler.getInviteByToken.callsArgWith(2, null, null)
|
|
|
+ @CollaboratorsInviteController.viewInvite @req, @res, @next
|
|
|
+
|
|
|
+ it 'should produce a 404 response', ->
|
|
|
+ @res.sendStatus.callCount.should.equal 1
|
|
|
+ @res.sendStatus.calledWith(404).should.equal true
|
|
|
+
|
|
|
+ it 'should not render the view template', ->
|
|
|
+ @res.render.callCount.should.equal 0
|
|
|
+
|
|
|
+ it 'should not call next', ->
|
|
|
+ @next.callCount.should.equal 0
|
|
|
+
|
|
|
+ it 'should call getInviteByToken', ->
|
|
|
+ @CollaboratorsInviteHandler.getInviteByToken.callCount.should.equal 1
|
|
|
+
|
|
|
+ describe "revokeInvite", ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @req.params =
|
|
|
+ Project_id: @project_id
|
|
|
+ invite_id: @invite_id = "thuseoautoh"
|
|
|
+ @req.session =
|
|
|
+ user: _id: @current_user_id = "current-user-id"
|
|
|
+ @res.render = sinon.stub()
|
|
|
+ @res.sendStatus = sinon.stub()
|
|
|
+ @CollaboratorsInviteHandler.revokeInvite = sinon.stub().callsArgWith(2, null)
|
|
|
+ @callback = sinon.stub()
|
|
|
+ @next = sinon.stub()
|
|
|
+
|
|
|
+ describe 'when revokeInvite does not produce an error', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @CollaboratorsInviteController.revokeInvite @req, @res, @next
|
|
|
+
|
|
|
+ it 'should produce a 201 response', ->
|
|
|
+ @res.sendStatus.callCount.should.equal 1
|
|
|
+ @res.sendStatus.calledWith(201).should.equal true
|
|
|
+
|
|
|
+ it 'should have called revokeInvite', ->
|
|
|
+ @CollaboratorsInviteHandler.revokeInvite.callCount.should.equal 1
|
|
|
+
|
|
|
+ describe 'when revokeInvite produces an error', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @err = new Error('woops')
|
|
|
+ @CollaboratorsInviteHandler.revokeInvite = sinon.stub().callsArgWith(2, @err)
|
|
|
+ @CollaboratorsInviteController.revokeInvite @req, @res, @next
|
|
|
+
|
|
|
+ it 'should not produce a 201 response', ->
|
|
|
+ @res.sendStatus.callCount.should.equal 0
|
|
|
+
|
|
|
+ it 'should call next with the error', ->
|
|
|
+ @next.callCount.should.equal 1
|
|
|
+ @next.calledWith(@err).should.equal true
|
|
|
+
|
|
|
+ it 'should have called revokeInvite', ->
|
|
|
+ @CollaboratorsInviteHandler.revokeInvite.callCount.should.equal 1
|
|
|
+
|
|
|
+ describe "acceptInvite", ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @req.params =
|
|
|
+ Project_id: @project_id
|
|
|
+ invite_id: @invite_id = "thuseoautoh"
|
|
|
+ @req.session =
|
|
|
+ user: _id: @current_user_id = "current-user-id"
|
|
|
+ @res.render = sinon.stub()
|
|
|
+ @res.sendStatus = sinon.stub()
|
|
|
+ @CollaboratorsInviteHandler.acceptInvite = sinon.stub().callsArgWith(3, null)
|
|
|
+ @callback = sinon.stub()
|
|
|
+ @next = sinon.stub()
|
|
|
+
|
|
|
+ describe 'when acceptInvite does not produce an error', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @CollaboratorsInviteController.acceptInvite @req, @res, @next
|
|
|
+
|
|
|
+ it 'should produce a 201 response', ->
|
|
|
+ @res.sendStatus.callCount.should.equal 1
|
|
|
+ @res.sendStatus.calledWith(201).should.equal true
|
|
|
+
|
|
|
+ it 'should have called acceptInvite', ->
|
|
|
+ @CollaboratorsInviteHandler.acceptInvite.callCount.should.equal 1
|
|
|
+
|
|
|
+ describe 'when revokeInvite produces an error', ->
|
|
|
+
|
|
|
+ beforeEach ->
|
|
|
+ @err = new Error('woops')
|
|
|
+ @CollaboratorsInviteHandler.acceptInvite = sinon.stub().callsArgWith(3, @err)
|
|
|
+ @CollaboratorsInviteController.acceptInvite @req, @res, @next
|
|
|
+
|
|
|
+ it 'should not produce a 201 response', ->
|
|
|
+ @res.sendStatus.callCount.should.equal 0
|
|
|
+
|
|
|
+ it 'should call next with the error', ->
|
|
|
+ @next.callCount.should.equal 1
|
|
|
+ @next.calledWith(@err).should.equal true
|
|
|
+
|
|
|
+ it 'should have called acceptInvite', ->
|
|
|
+ @CollaboratorsInviteHandler.acceptInvite.callCount.should.equal 1
|