Finish adding project and owner details to the accept-invite page

This commit is contained in:
Shane Kilkelly 2016-07-26 14:14:14 +01:00
parent 367b138cae
commit 855cc28483
2 changed files with 111 additions and 4 deletions

View file

@ -58,18 +58,25 @@ module.exports = CollaboratorsInviteController =
if err? if err?
logger.err {projectId, token}, "error getting invite by token" logger.err {projectId, token}, "error getting invite by token"
return next(err) return next(err)
# TODO: render a not-valid view instead _renderInvalidPage = () ->
res.render "project/invite/not-valid", {invite}
if !invite if !invite
logger.log {projectId, token}, "no invite found for token" 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) -> Project.findOne {_id: projectId}, {owner_ref: 1, name: 1}, (err, project) ->
if err? if err?
logger.err {err, projectId}, "error getting project" 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) -> User.findOne {_id: project.owner_ref}, {email: 1, first_name: 1, last_name: 1}, (err, owner) ->
if err? if err?
logger.err {err, projectId}, "error getting project owner" 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} res.render "project/invite/show", {invite, project, owner}
acceptInvite: (req, res, next) -> acceptInvite: (req, res, next) ->

View file

@ -213,6 +213,14 @@ describe "CollaboratorsInviteController", ->
it 'should call getInviteByToken', -> it 'should call getInviteByToken', ->
@CollaboratorsInviteHandler.getInviteByToken.callCount.should.equal 1 @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', -> describe 'when the getInviteByToken produces an error', ->
beforeEach -> beforeEach ->
@ -227,6 +235,12 @@ describe "CollaboratorsInviteController", ->
it 'should call getInviteByToken', -> it 'should call getInviteByToken', ->
@CollaboratorsInviteHandler.getInviteByToken.callCount.should.equal 1 @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', -> describe 'when the getInviteByToken does not produce an invite', ->
beforeEach -> beforeEach ->
@ -243,6 +257,92 @@ describe "CollaboratorsInviteController", ->
it 'should call getInviteByToken', -> it 'should call getInviteByToken', ->
@CollaboratorsInviteHandler.getInviteByToken.callCount.should.equal 1 @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", -> describe "revokeInvite", ->
beforeEach -> beforeEach ->