Skip to content

Commit

Permalink
PP-13137 Refactor team-members controller test (#4381)
Browse files Browse the repository at this point in the history
PP-13137 Refactor team-members controller test
  • Loading branch information
james-peacock-gds authored Dec 12, 2024
1 parent 7ecab23 commit 13228c3
Showing 1 changed file with 100 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,104 +1,129 @@
const proxyquire = require('proxyquire')
const sinon = require('sinon')
const { expect } = require('chai')
const User = require('@models/User.class')
const userFixtures = require('@test/fixtures/user.fixtures')
const ControllerTestBuilder = require('@test/test-helpers/simplified-account/controllers/ControllerTestBuilder.class')

const ACCOUNT_TYPE = 'test'
const SERVICE_ID = 'service-id-123abc'

let req, res, responseStub, getServiceUsersStub, getInvitedUsersStub, teamMembersController

const getController = (stubs = {}) => {
return proxyquire('./team-members.controller', {
'@utils/response': { response: stubs.response },
'@services/user.service':
{ getServiceUsers: stubs.getServiceUsers, getInvitedUsers: stubs.getInvitedUsers }
})
}

const setupTest = (method, additionalReqProps = {}, additionalStubs = {}) => {
responseStub = sinon.spy()
const adminUser = new User(userFixtures.validUserResponse({
external_id: 'user-id-for-admin-user',
const adminUser = new User(userFixtures.validUserResponse({
external_id: 'user-id-for-admin-user',
service_roles: {
service: {
service: { external_id: SERVICE_ID },
role: { name: 'admin' }
}
}
}))
const viewOnlyUser = new User(userFixtures.validUserResponse(
{
external_id: 'user-id-for-view-only-user',
service_roles: {
service: {
service: { external_id: SERVICE_ID },
role: { name: 'admin' }
}
service:
{
service: { external_id: SERVICE_ID },
role: { name: 'view-only' }
}
}
}))
const viewOnlyUser = new User(userFixtures.validUserResponse(
{
external_id: 'user-id-for-view-only-user',
service_roles: {
service:
{
service: { external_id: SERVICE_ID },
role: { name: 'view-only' }
}
}
}))
const users = [adminUser, viewOnlyUser]
getServiceUsersStub = sinon.stub().resolves(users)

const invitedAdminUser = { email: '[email protected]', role: 'admin' }
const invitedViewOnlyUser = { email: '[email protected]', role: 'view-only' }
const invitedUsers = [invitedAdminUser, invitedViewOnlyUser]
getInvitedUsersStub = sinon.stub().resolves(invitedUsers)

teamMembersController = getController({
response: responseStub,
getServiceUsers: getServiceUsersStub,
getInvitedUsers: getInvitedUsersStub,
...additionalStubs
const users = [adminUser, viewOnlyUser]
const invitedUsers = [
{ email: '[email protected]', role: 'admin' },
{ email: '[email protected]', role: 'view-only' }
]

const mockResponse = sinon.spy()
const mockGetServiceUsers = sinon.stub().resolves(users)
const mockGetInvitedUsers = sinon.stub().resolves(invitedUsers)

const { res, nextRequest, call } = new ControllerTestBuilder('@controllers/simplified-account/settings/team-members/team-members.controller')
.withServiceExternalId(SERVICE_ID)
.withAccountType(ACCOUNT_TYPE)
.withStubs({
'@utils/response': { response: mockResponse },
'@services/user.service':
{ getServiceUsers: mockGetServiceUsers, getInvitedUsers: mockGetInvitedUsers }

})
res = {
redirect: sinon.spy()
}
req = {
user: adminUser,
service: {
externalId: SERVICE_ID
},
account: {
type: ACCOUNT_TYPE
},
...additionalReqProps
}
teamMembersController[method](req, res)
}
.build()

describe('Controller: settings/team-members', () => {
describe('get', () => {
before(() => setupTest('get'))
before(() => {
nextRequest({
user: adminUser
})
call('get')
})

it('should call the response method', () => {
expect(mockGetServiceUsers.called).to.be.true // eslint-disable-line
expect(mockResponse.called).to.be.true // eslint-disable-line
expect(mockResponse.called).to.be.true // eslint-disable-line
})

it('should pass req, res and template path to the response method', () => {
expect(mockResponse.args[0][0].user).to.deep.include(adminUser)
expect(mockResponse.args[0]).to.include(res)
expect(mockResponse.args[0]).to.include('simplified-account/settings/team-members/index')
})

it('should pass context data to the response method', () => {
expect(mockResponse.args[0][3]).to.have.property('teamMembers').to.have.property('admin').to.have.length(1)
expect(mockResponse.args[0][3].teamMembers.admin[0]).to.have.property('externalId').to.equal('user-id-for-admin-user')
expect(mockResponse.args[0][3].teamMembers).to.have.property('view-only').to.have.length(1)
expect(mockResponse.args[0][3].teamMembers['view-only'][0]).to.have.property('externalId').to.equal('user-id-for-view-only-user')

expect(mockResponse.args[0][3]).to.have.property('invitedTeamMembers').to.have.property('admin').to.have.length(1)
expect(mockResponse.args[0][3].invitedTeamMembers.admin[0]).to.have.property('email').to.equal('[email protected]')
expect(mockResponse.args[0][3].invitedTeamMembers).to.have.property('view-only').to.have.length(1)
expect(mockResponse.args[0][3].invitedTeamMembers['view-only'][0]).to.have.property('email').to.equal('[email protected]')

expect(mockResponse.args[0][3]).to.have.property('inviteTeamMemberLink')
.to.equal('/simplified/service/service-id-123abc/account/test/settings/team-members/invite')
expect(mockResponse.args[0][3]).to.have.property('numberInvitedMembers').to.equal(2)
expect(mockResponse.args[0][3]).to.have.property('isServiceAdmin').to.be.true // eslint-disable-line no-unused-expressions
expect(mockResponse.args[0][3]).to.have.property('roles').to.have.keys('admin', 'view-and-initiate-moto', 'view-and-refund', 'view-only', 'view-refund-and-initiate-moto')
})
})

describe('get', () => {
before(() => {
nextRequest({
user: adminUser
})
call('get')
})

it('should call the response method', () => {
expect(getServiceUsersStub.called).to.be.true // eslint-disable-line
expect(getInvitedUsersStub.called).to.be.true // eslint-disable-line
expect(responseStub.called).to.be.true // eslint-disable-line
expect(mockGetServiceUsers.called).to.be.true // eslint-disable-line
expect(mockResponse.called).to.be.true // eslint-disable-line
expect(mockResponse.called).to.be.true // eslint-disable-line
})

it('should pass req, res and template path to the response method', () => {
expect(responseStub.args[0]).to.include(req)
expect(responseStub.args[0]).to.include(res)
expect(responseStub.args[0]).to.include('simplified-account/settings/team-members/index')
expect(mockResponse.args[0][0].user).to.deep.include(adminUser)
expect(mockResponse.args[0]).to.include(res)
expect(mockResponse.args[0]).to.include('simplified-account/settings/team-members/index')
})

it('should pass context data to the response method', () => {
expect(responseStub.args[0][3]).to.have.property('teamMembers').to.have.property('admin').to.have.length(1)
expect(responseStub.args[0][3].teamMembers.admin[0]).to.have.property('externalId').to.equal('user-id-for-admin-user')
expect(responseStub.args[0][3].teamMembers).to.have.property('view-only').to.have.length(1)
expect(responseStub.args[0][3].teamMembers['view-only'][0]).to.have.property('externalId').to.equal('user-id-for-view-only-user')
expect(mockResponse.args[0][3]).to.have.property('teamMembers').to.have.property('admin').to.have.length(1)
expect(mockResponse.args[0][3].teamMembers.admin[0]).to.have.property('externalId').to.equal('user-id-for-admin-user')
expect(mockResponse.args[0][3].teamMembers).to.have.property('view-only').to.have.length(1)
expect(mockResponse.args[0][3].teamMembers['view-only'][0]).to.have.property('externalId').to.equal('user-id-for-view-only-user')

expect(responseStub.args[0][3]).to.have.property('invitedTeamMembers').to.have.property('admin').to.have.length(1)
expect(responseStub.args[0][3].invitedTeamMembers.admin[0]).to.have.property('email').to.equal('[email protected]')
expect(responseStub.args[0][3].invitedTeamMembers).to.have.property('view-only').to.have.length(1)
expect(responseStub.args[0][3].invitedTeamMembers['view-only'][0]).to.have.property('email').to.equal('[email protected]')
expect(mockResponse.args[0][3]).to.have.property('invitedTeamMembers').to.have.property('admin').to.have.length(1)
expect(mockResponse.args[0][3].invitedTeamMembers.admin[0]).to.have.property('email').to.equal('[email protected]')
expect(mockResponse.args[0][3].invitedTeamMembers).to.have.property('view-only').to.have.length(1)
expect(mockResponse.args[0][3].invitedTeamMembers['view-only'][0]).to.have.property('email').to.equal('[email protected]')

expect(responseStub.args[0][3]).to.have.property('inviteTeamMemberLink')
expect(mockResponse.args[0][3]).to.have.property('inviteTeamMemberLink')
.to.equal('/simplified/service/service-id-123abc/account/test/settings/team-members/invite')
expect(responseStub.args[0][3]).to.have.property('numberInvitedMembers').to.equal(2)
expect(mockResponse.args[0][3]).to.have.property('numberInvitedMembers').to.equal(2)
expect(mockResponse.args[0][3]).to.have.property('isServiceAdmin').to.be.true // eslint-disable-line no-unused-expressions
expect(mockResponse.args[0][3]).to.have.property('roles').to.have.keys('admin', 'view-and-initiate-moto', 'view-and-refund', 'view-only', 'view-refund-and-initiate-moto')
})
})
})

0 comments on commit 13228c3

Please sign in to comment.