Skip to content

Commit

Permalink
PP-13296 handle user already in team error (#4357)
Browse files Browse the repository at this point in the history
* PP-13296 handle user already in team error
  • Loading branch information
james-peacock-gds authored Nov 27, 2024
1 parent eafb06b commit 56be438
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,11 @@ async function post (req, res, next) {
const invitedUserRole = req.body.invitedUserRole
const teamMembersIndexPath = formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.teamMembers.index, externalServiceId, accountType)

const validations = [
body('invitedUserRole')
.not().isEmpty().withMessage('Select a permission level'),
body('invitedUserEmail')
.isEmail().withMessage('Enter a valid email address')
]
await Promise.all(validations.map(validation => validation.run(req)))
const errors = validationResult(req)

if (!errors.isEmpty()) {
const formattedErrors = formatValidationErrors(errors)

const responseWithErrors = (errors) => {
return response(req, res, 'simplified-account/settings/team-members/invite', {
errors: {
summary: formattedErrors.errorSummary,
formErrors: formattedErrors.formErrors
summary: errors.errorSummary,
formErrors: errors.formErrors
},
invitedUserEmail,
checkedRole: invitedUserRole,
Expand All @@ -48,12 +37,31 @@ async function post (req, res, next) {
})
}

const validations = [
body('invitedUserRole')
.not().isEmpty().withMessage('Select a permission level'),
body('invitedUserEmail')
.isEmail().withMessage('Enter a valid email address')
]
await Promise.all(validations.map(validation => validation.run(req)))
const validationErrors = validationResult(req)
if (!validationErrors.isEmpty()) {
const formattedValidationErrors = formatValidationErrors(validationErrors)
return responseWithErrors(formattedValidationErrors)
}

try {
await userService.createInviteToJoinService(invitedUserEmail, adminUserExternalId, externalServiceId, invitedUserRole)
req.flash('messages', { state: 'success', icon: '✓', heading: 'Team member invitation sent to ' + req.body.invitedUserEmail })
req.flash('messages', { state: 'success', icon: '✓', heading: `Team member invitation sent to ${req.body.invitedUserEmail}` })
res.redirect(teamMembersIndexPath)
} catch (err) {
// TODO - handle 412 (user already invited or already a team member)
if (err.errorCode === 412) {
const personAlreadyInvitedError = {
errorSummary: [{ text: 'This person has already been invited', href: '#invited-user-email' }],
formErrors: { invitedUserEmail: `You cannot send an invitation to ${req.body.invitedUserEmail} because they have received one already, or may be an existing team member` }
}
return responseWithErrors(personAlreadyInvitedError)
}
next(err)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const proxyquire = require('proxyquire')
const User = require('@models/User.class')
const userFixtures = require('@test/fixtures/user.fixtures')
const paths = require('@root/paths')
const { RESTClientError } = require('@govuk-pay/pay-js-commons/lib/utils/axios-base-client/errors')

const ACCOUNT_TYPE = 'test'
const SERVICE_ID = 'service-id-123abc'
Expand All @@ -13,8 +14,7 @@ let req, res, responseStub, createInviteToJoinServiceStub, inviteController
const getController = (stubs = {}) => {
return proxyquire('./invite.controller', {
'@utils/response': { response: stubs.response, renderErrorView: stubs.renderErrorView },
'@services/user.service':
{ createInviteToJoinService: stubs.createInviteToJoinService }
'@services/user.service': { createInviteToJoinService: stubs.createInviteToJoinService }
})
}

Expand All @@ -29,9 +29,12 @@ const adminUser = new User(userFixtures.validUserResponse({
}
}))

const setupTest = (method, additionalReqProps = {}) => {
const setupTest = (method, adminusers412Response, additionalReqProps = {}) => {
responseStub = sinon.spy()
createInviteToJoinServiceStub = sinon.stub()
if (adminusers412Response) {
createInviteToJoinServiceStub.rejects(new RESTClientError(null, 'adminusers', 412))
}

inviteController = getController({
response: responseStub,
Expand Down Expand Up @@ -75,7 +78,7 @@ describe('Controller: settings/team-members/invite', () => {

describe('post', () => {
describe('success', () => {
before(() => setupTest('post',
before(() => setupTest('post', false,
{
body: { invitedUserEmail: '[email protected]', invitedUserRole: 'view-only' },
flash: sinon.stub()
Expand All @@ -96,5 +99,26 @@ describe('Controller: settings/team-members/invite', () => {
expect(res.redirect.args[0][0]).to.include(paths.simplifiedAccount.settings.teamMembers.index)
})
})

describe('failure - user already in service', () => {
before(() => setupTest('post', true,
{
body: { invitedUserEmail: '[email protected]', invitedUserRole: 'view-only' },
flash: sinon.stub()
}
))

it('should respond with error message', () => {
expect(createInviteToJoinServiceStub.calledWith('[email protected]', adminUser.externalId, SERVICE_ID, 'view-only')).to.be.true // eslint-disable-line
expect(responseStub.calledOnce).to.be.true // eslint-disable-line
expect(responseStub.args[0][0]).to.deep.equal(req)
expect(responseStub.args[0][1]).to.deep.equal(res)
expect(responseStub.args[0][2]).to.equal('simplified-account/settings/team-members/invite')
expect(responseStub.args[0][3]).to.have.property('errors').to.deep.equal({
summary: [{ text: 'This person has already been invited', href: '#invited-user-email' }],
formErrors: { invitedUserEmail: `You cannot send an invitation to ${req.body.invitedUserEmail} because they have received one already, or may be an existing team member` }
})
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
text: "Email address",
classes: "govuk-label--s"
},
id: "invitedUserEmail",
id: "invited-user-email",
name: "invitedUserEmail",
type: "email",
value: invitedUserEmail,
Expand Down
Empty file.

0 comments on commit 56be438

Please sign in to comment.