Skip to content

Commit

Permalink
refactor: refactor members:add tests and test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
k80bowman committed Mar 3, 2024
1 parent e3b7a8b commit 5a3a36f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/commands/members/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Args} from '@oclif/core'
import * as Heroku from '@heroku-cli/schema'
const {RoleCompletion} = require('@heroku-cli/command/lib/completions')
import {addMemberToTeam, inviteMemberToTeam} from '../../lib/members/util'
export default class Add extends Command {
export default class MembersAdd extends Command {
static topic = 'members';
static description = 'adds a user to a team';
static flags = {
Expand All @@ -16,7 +16,7 @@ export default class Add extends Command {
};

public async run(): Promise<void> {
const {flags, args} = await this.parse(Add)
const {flags, args} = await this.parse(MembersAdd)
const {team, role} = flags
const {body: teamInfo} = await this.heroku.get<Heroku.Team>(`/teams/${team}`)
const email = args.email
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/test/helpers/stubs/put.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as nock from 'nock'

export function sendInvite(email = 'raulb@heroku.com', role = 'admin') {
return nock('https://api.heroku.com:443')
.put('/teams/myteam/invitations', {email, role})
.reply(200)
}

export function updateMemberRole(email = 'raulb@heroku.com', role = 'admin') {
return nock('https://api.heroku.com:443')
.put('/teams/myteam/members', {email, role})
.reply(200)
}
63 changes: 63 additions & 0 deletions packages/cli/test/unit/commands/members/add.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {stdout, stderr} from 'stdout-stderr'
import {expect} from 'chai'
import * as nock from 'nock'
import Cmd from '../../../../src/commands/members/add'
import runCommand from '../../../helpers/runCommand'
import {sendInvite, updateMemberRole} from '../../../helpers/stubs/put'
import {
teamFeatures,
teamInfo,
variableSizeTeamInvites,
variableSizeTeamMembers,
} from '../../../helpers/stubs/get'

describe('heroku members:add', () => {
let apiUpdateMemberRole: nock.Scope
afterEach(() => nock.cleanAll())

context('without the feature flag team-invite-acceptance', () => {
beforeEach(() => {
teamFeatures([])
})
context('and group is an enterprise org', () => {
beforeEach(() => {
teamInfo('enterprise')
variableSizeTeamMembers(1)
})
it('adds a member to an org', () => {
apiUpdateMemberRole = updateMemberRole('foo@foo.com', 'admin')
return runCommand(Cmd, [
'--team',
'myteam',
'--role',
'admin',
'foo@foo.com',
])
.then(() => expect('').to.eq(stdout.output))
.then(() => expect('Adding foo@foo.com to myteam as admin...\nAdding foo@foo.com to myteam as admin... done\n').to.eq(stderr.output))
.then(() => apiUpdateMemberRole.done())
})
})
})
context('with the feature flag team-invite-acceptance for a team', () => {
beforeEach(() => {
teamFeatures([{name: 'team-invite-acceptance', enabled: true}])
teamInfo('team')
})
it('sends an invite when adding a new user to the team', () => {
const apiSendInvite = sendInvite('foo@foo.com', 'admin')
variableSizeTeamMembers(1)
variableSizeTeamInvites(0)
return runCommand(Cmd, [
'--role',
'admin',
'--team',
'myteam',
'foo@foo.com',
])
.then(() => expect('').to.eq(stdout.output))
.then(() => expect('Inviting foo@foo.com to myteam as admin...\nInviting foo@foo.com to myteam as admin... done\n').to.eq(stderr.output))
.then(() => apiSendInvite.done())
})
})
})

0 comments on commit 5a3a36f

Please sign in to comment.