Add a getAllInvites api endpoint

This commit is contained in:
Shane Kilkelly
2016-07-25 11:17:47 +01:00
parent 8dea179b01
commit 73fed8b0bf
5 changed files with 109 additions and 0 deletions
@@ -7,6 +7,15 @@ logger = require('logger-sharelatex')
module.exports = CollaboratorsInviteController =
getAllInvites: (req, res, next) ->
projectId = req.params.Project_id
logger.log {projectId}, "getting all active invites for project"
CollaboratorsInviteHandler.getAllInvites projectId, (err, invites) ->
if err?
logger.err {projectId}, "error getting invites for project"
return next(err)
res.json({invites: invites})
inviteToProject: (req, res, next) ->
projectId = req.params.Project_id
email = req.body.email
@@ -11,6 +11,14 @@ Crypto = require 'crypto'
module.exports = CollaboratorsInviteHandler =
getAllInvites: (projectId, callback=(err, invites)->) ->
logger.log {projectId}, "fetching invites from mongo"
ProjectInvite.find {projectId: projectId}, (err, invites) ->
if err?
logger.err {err, projectId}, "error getting invites from mongo"
return callback(err)
callback(null, invites)
inviteToProject: (projectId, sendingUserId, email, privileges, callback=(err,invite)->) ->
logger.log {projectId, sendingUserId, email, privileges}, "adding invite"
Crypto.randomBytes 24, (err, buffer) ->
@@ -17,6 +17,12 @@ module.exports =
CollaboratorsInviteController.inviteToProject
)
webRouter.get(
'/project/:Project_id/invite',
AuthorizationMiddlewear.ensureUserCanAdminProject,
CollaboratorsInviteController.getAllInvites
)
webRouter.delete(
'/project/:Project_id/invite/:invite_id',
AuthorizationMiddlewear.ensureUserCanAdminProject,
@@ -25,6 +25,48 @@ describe "CollaboratorsInviteController", ->
@project_id = "project-id-123"
@callback = sinon.stub()
describe 'getAllInvites', ->
beforeEach ->
@fakeInvites = [
{_id: ObjectId(), one: 1},
{_id: ObjectId(), two: 2}
]
@req.params =
Project_id: @project_id
@res.json = sinon.stub()
@next = sinon.stub()
describe 'when all goes well', ->
beforeEach ->
@CollaboratorsInviteHandler.getAllInvites = sinon.stub().callsArgWith(1, null, @fakeInvites)
@CollaboratorsInviteController.getAllInvites @req, @res, @next
it 'should not produce an error', ->
@next.callCount.should.equal 0
it 'should produce a list of invite objects', ->
@res.json.callCount.should.equal 1
@res.json.calledWith({invites: @fakeInvites}).should.equal true
it 'should have called CollaboratorsInviteHandler.getAllInvites', ->
@CollaboratorsInviteHandler.getAllInvites.callCount.should.equal 1
@CollaboratorsInviteHandler.getAllInvites.calledWith(@project_id).should.equal true
describe 'when CollaboratorsInviteHandler.getAllInvites produces an error', ->
beforeEach ->
@CollaboratorsInviteHandler.getAllInvites = sinon.stub().callsArgWith(1, new Error('woops'))
@CollaboratorsInviteController.getAllInvites @req, @res, @next
it 'should produce an error', ->
@next.callCount.should.equal 1
@next.firstCall.args[0].should.be.instanceof Error
# # # #
describe 'inviteToProject', ->
beforeEach ->
@@ -18,6 +18,7 @@ describe "CollaboratorsInviteHandler", ->
this
save: sinon.stub()
@findOne: sinon.stub()
@find: sinon.stub()
@remove: sinon.stub()
@Project = class Project
constructor: () ->
@@ -54,6 +55,49 @@ describe "CollaboratorsInviteHandler", ->
privileges: @privileges
createdAt: new Date()
describe 'getAllInvites', ->
beforeEach ->
@fakeInvites = [
{_id: ObjectId(), one: 1},
{_id: ObjectId(), two: 2}
]
@ProjectInvite.find.callsArgWith(1, null, @fakeInvites)
@call = (callback) =>
@CollaboratorsInviteHandler.getAllInvites @projectId, callback
describe 'when all goes well', ->
beforeEach ->
it 'should not produce an error', (done) ->
@call (err, invites) =>
expect(err).to.not.be.instanceof Error
expect(err).to.be.oneOf [null, undefined]
done()
it 'should produce a list of invite objects', (done) ->
@call (err, invites) =>
expect(invites).to.not.be.oneOf [null, undefined]
expect(invites).to.deep.equal @fakeInvites
done()
it 'should have called ProjectInvite.find', (done) ->
@call (err, invites) =>
@ProjectInvite.find.callCount.should.equal 1
@ProjectInvite.find.calledWith({projectId: @projectId}).should.equal true
done()
describe 'when ProjectInvite.find produces an error', ->
beforeEach ->
@ProjectInvite.find.callsArgWith(1, new Error('woops'))
it 'should produce an error', (done) ->
@call (err, invites) =>
expect(err).to.be.instanceof Error
done()
describe 'inviteToProject', ->
beforeEach ->