Test getInviteByToken.

This commit is contained in:
Shane Kilkelly 2016-07-22 14:21:34 +01:00
parent e34b124c73
commit b201f1a37a
3 changed files with 84 additions and 3 deletions

View file

@ -47,6 +47,7 @@ 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: should we render an expired view instead?
if !invite if !invite
logger.log {projectId, token}, "no invite found for token" logger.log {projectId, token}, "no invite found for token"
return res.sendStatus(404) return res.sendStatus(404)

View file

@ -44,7 +44,7 @@ module.exports = CollaboratorsInviteHandler =
logger.log {projectId, tokenString}, "fetching invite by token" logger.log {projectId, tokenString}, "fetching invite by token"
ProjectInvite.findOne {projectId: projectId, token: tokenString}, (err, invite) -> ProjectInvite.findOne {projectId: projectId, token: tokenString}, (err, invite) ->
if err? if err?
logger.err {err, projectId, inviteId}, "error fetching invite" logger.err {err, projectId}, "error fetching invite"
return callback(err) return callback(err)
if !invite if !invite
err = new Errors.NotFoundError("no invite found for token") err = new Errors.NotFoundError("no invite found for token")
@ -53,7 +53,7 @@ module.exports = CollaboratorsInviteHandler =
now = new Date() now = new Date()
# TODO: re-assess whether we should return null or a notfounderror # TODO: re-assess whether we should return null or a notfounderror
if invite.expiresAt < now if invite.expiresAt < now
logger.log {projectId, inviteId, expiresAt: invite.expiresAt}, "invite expired" logger.log {projectId, inviteId: invite._id, expiresAt: invite.expiresAt}, "invite expired"
return callback(null, null) return callback(null, null)
callback(null, invite) callback(null, invite)

View file

@ -35,6 +35,7 @@ describe "CollaboratorsInviteHandler", ->
@email = "user@example.com" @email = "user@example.com"
@userId = ObjectId() @userId = ObjectId()
@inviteId = ObjectId() @inviteId = ObjectId()
@token = 'hnhteaosuhtaeosuahs'
@privileges = "readAndWrite" @privileges = "readAndWrite"
describe 'inviteToProject', -> describe 'inviteToProject', ->
@ -56,6 +57,7 @@ describe "CollaboratorsInviteHandler", ->
it 'should not produce an error', (done) -> it 'should not produce an error', (done) ->
@call (err, invite) => @call (err, invite) =>
expect(err).to.not.be.instanceof Error expect(err).to.not.be.instanceof Error
expect(err).to.be.oneOf [null, undefined]
done() done()
it 'should produce the invite object', (done) -> it 'should produce the invite object', (done) ->
@ -96,7 +98,6 @@ describe "CollaboratorsInviteHandler", ->
beforeEach -> beforeEach ->
@ProjectInvite.remove.callsArgWith(1, null) @ProjectInvite.remove.callsArgWith(1, null)
@CollaboratorsEmailHandler.notifyUserOfProjectInvite = sinon.stub()
@call = (callback) => @call = (callback) =>
@CollaboratorsInviteHandler.revokeInvite @projectId, @inviteId, callback @CollaboratorsInviteHandler.revokeInvite @projectId, @inviteId, callback
@ -107,6 +108,7 @@ describe "CollaboratorsInviteHandler", ->
it 'should not produce an error', (done) -> it 'should not produce an error', (done) ->
@call (err) => @call (err) =>
expect(err).to.not.be.instanceof Error expect(err).to.not.be.instanceof Error
expect(err).to.be.oneOf [null, undefined]
done() done()
it 'should call ProjectInvite.remove', (done) -> it 'should call ProjectInvite.remove', (done) ->
@ -124,3 +126,81 @@ describe "CollaboratorsInviteHandler", ->
@call (err) => @call (err) =>
expect(err).to.be.instanceof Error expect(err).to.be.instanceof Error
done() done()
describe 'getInviteByToken', ->
beforeEach ->
@theDarkFuture = new Date()
@theDarkFuture.setYear(40000)
@fakeInvite =
_id: @inviteId
email: @email
token: @token
sendingUserId: @sendingUserId
projectId: @projectId
privileges: @privileges
createdAt: new Date()
expiresAt: @theDarkFuture
@ProjectInvite.findOne.callsArgWith(1, null, @fakeInvite)
@call = (callback) =>
@CollaboratorsInviteHandler.getInviteByToken @projectId, @token, callback
describe 'when all goes well', ->
beforeEach ->
it 'should not produce an error', (done) ->
@call (err) =>
expect(err).to.not.be.instanceof Error
expect(err).to.be.oneOf [null, undefined]
done()
it 'should produce the invite object', (done) ->
@call (err, invite) =>
expect(invite).to.deep.equal @fakeInvite
done()
it 'should call ProjectInvite.findOne', (done) ->
@call (err) =>
@ProjectInvite.findOne.callCount.should.equal 1
@ProjectInvite.findOne.calledWith({projectId: @projectId, token: @token}).should.equal true
done()
describe 'when findOne produces an error', ->
beforeEach ->
@ProjectInvite.findOne.callsArgWith(1, new Error('woops'))
it 'should produce an error', (done) ->
@call (err) =>
expect(err).to.be.instanceof Error
done()
describe 'when findOne does not find an invite', ->
beforeEach ->
@ProjectInvite.findOne.callsArgWith(1, null, null)
it 'should produce an error', (done) ->
@call (err) =>
expect(err).to.be.instanceof Error
done()
describe 'when the invite is expired', ->
beforeEach ->
@theDeepPast = new Date()
@theDeepPast.setYear(1977)
@fakeInvite.expiresAt = @theDeepPast
@ProjectInvite.findOne.callsArgWith(1, null, @fakeInvite)
it 'should not produce an error', (done) ->
@call (err) =>
expect(err).to.not.be.instanceof Error
expect(err).to.be.oneOf [null, undefined]
done()
it 'should not produce an invite object', (done) ->
@call (err, invite) =>
expect(invite).to.be.oneOf [null, undefined]
done()