diff --git a/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteController.coffee b/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteController.coffee index a1bb0bdaf5..4dcf5134f5 100644 --- a/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteController.coffee +++ b/services/web/app/coffee/Features/Collaborators/CollaboratorsInviteController.coffee @@ -58,18 +58,25 @@ module.exports = CollaboratorsInviteController = if err? logger.err {projectId, token}, "error getting invite by token" return next(err) - # TODO: render a not-valid view instead + _renderInvalidPage = () -> + res.render "project/invite/not-valid", {invite} if !invite logger.log {projectId, token}, "no invite found for token" - return res.render "project/invite/not-valid" + return _renderInvalidPage() Project.findOne {_id: projectId}, {owner_ref: 1, name: 1}, (err, project) -> if err? logger.err {err, projectId}, "error getting project" - return callback(err) + return next(err) + if !project + logger.log {projectId}, "no project found" + return _renderInvalidPage() User.findOne {_id: project.owner_ref}, {email: 1, first_name: 1, last_name: 1}, (err, owner) -> if err? logger.err {err, projectId}, "error getting project owner" - return callback(err) + return next(err) + if !owner + logger.log {projectId}, "no project owner found" + return _renderInvalidPage() res.render "project/invite/show", {invite, project, owner} acceptInvite: (req, res, next) -> diff --git a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteControllerTests.coffee b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteControllerTests.coffee index 7e351fa30c..af10cb48de 100644 --- a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsInviteControllerTests.coffee @@ -213,6 +213,14 @@ describe "CollaboratorsInviteController", -> it 'should call getInviteByToken', -> @CollaboratorsInviteHandler.getInviteByToken.callCount.should.equal 1 + it 'should call Project.findOne', -> + @Project.findOne.callCount.should.equal 1 + @Project.findOne.calledWith({_id: @project_id}).should.equal true + + it 'should call User.findOne', -> + @User.findOne.callCount.should.equal 1 + @User.findOne.calledWith({_id: @fakeProject.owner_ref}).should.equal true + describe 'when the getInviteByToken produces an error', -> beforeEach -> @@ -227,6 +235,12 @@ describe "CollaboratorsInviteController", -> it 'should call getInviteByToken', -> @CollaboratorsInviteHandler.getInviteByToken.callCount.should.equal 1 + it 'should not call Project.findOne', -> + @Project.findOne.callCount.should.equal 0 + + it 'should not call User.findOne', -> + @User.findOne.callCount.should.equal 0 + describe 'when the getInviteByToken does not produce an invite', -> beforeEach -> @@ -243,6 +257,92 @@ describe "CollaboratorsInviteController", -> it 'should call getInviteByToken', -> @CollaboratorsInviteHandler.getInviteByToken.callCount.should.equal 1 + it 'should not call Project.findOne', -> + @Project.findOne.callCount.should.equal 0 + + it 'should not call User.findOne', -> + @User.findOne.callCount.should.equal 0 + + describe 'when Project.findOne produces an error', -> + + beforeEach -> + @Project.findOne.callsArgWith(2, new Error('woops')) + @CollaboratorsInviteController.viewInvite @req, @res, @next + + it 'should produce an error', -> + @next.callCount.should.equal 1 + expect(@next.firstCall.args[0]).to.be.instanceof Error + + it 'should call getInviteByToken', -> + @CollaboratorsInviteHandler.getInviteByToken.callCount.should.equal 1 + + it 'should call Project.findOne', -> + @Project.findOne.callCount.should.equal 1 + @Project.findOne.calledWith({_id: @project_id}).should.equal true + + it 'should not call User.findOne', -> + @User.findOne.callCount.should.equal 0 + + describe 'when Project.findOne does not find a project', -> + + beforeEach -> + @Project.findOne.callsArgWith(2, null, null) + @CollaboratorsInviteController.viewInvite @req, @res, @next + + it 'should render the not-valid view template', -> + @res.render.callCount.should.equal 1 + @res.render.calledWith('project/invite/not-valid').should.equal true + + it 'should not call next', -> + @next.callCount.should.equal 0 + + it 'should call Project.findOne', -> + @Project.findOne.callCount.should.equal 1 + @Project.findOne.calledWith({_id: @project_id}).should.equal true + + it 'should not call User.findOne', -> + @User.findOne.callCount.should.equal 0 + + describe 'when User.findOne produces an error', -> + + beforeEach -> + @User.findOne.callsArgWith(2, new Error('woops')) + @CollaboratorsInviteController.viewInvite @req, @res, @next + + it 'should produce an error', -> + @next.callCount.should.equal 1 + expect(@next.firstCall.args[0]).to.be.instanceof Error + + it 'should call getInviteByToken', -> + @CollaboratorsInviteHandler.getInviteByToken.callCount.should.equal 1 + + it 'should call Project.findOne', -> + @Project.findOne.callCount.should.equal 1 + @Project.findOne.calledWith({_id: @project_id}).should.equal true + + it 'should call User.findOne', -> + @User.findOne.callCount.should.equal 1 + + describe 'when User.findOne does not find a user', -> + + beforeEach -> + @User.findOne.callsArgWith(2, null, null) + @CollaboratorsInviteController.viewInvite @req, @res, @next + + it 'should render the not-valid view template', -> + @res.render.callCount.should.equal 1 + @res.render.calledWith('project/invite/not-valid').should.equal true + + it 'should not call next', -> + @next.callCount.should.equal 0 + + it 'should call Project.findOne', -> + @Project.findOne.callCount.should.equal 1 + @Project.findOne.calledWith({_id: @project_id}).should.equal true + + it 'should call User.findOne', -> + @User.findOne.callCount.should.equal 1 + describe "revokeInvite", -> beforeEach ->