Skip to content

Commit

Permalink
refactor: migrate apps:join to oclif/core (#2689)
Browse files Browse the repository at this point in the history
* refactor: migrate apps:join command to oclif/core

* refactor: initial test conversion

* refactor: migrate stubs/post and update apps:join tests

* refactor: remove v5 apps:join files and tests

* refactor: update apps:join test structure

* refactor: update types and test helper function

* refactor: remove unnecessary params from test helper function

* refactor: replace param in test helper function
  • Loading branch information
k80bowman committed Mar 12, 2024
1 parent c4dfe48 commit a3f1e14
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 79 deletions.
25 changes: 25 additions & 0 deletions packages/cli/src/commands/apps/join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {ux} from '@oclif/core'
import * as Heroku from '@heroku-cli/schema'

export default class AppsJoin extends Command {
static topic = 'apps';
static description = 'add yourself to a team app';
static aliases = ['join']
static flags = {
app: flags.app({required: true}),
remote: flags.remote({char: 'r'}),
};

public async run(): Promise<void> {
const {flags} = await this.parse(AppsJoin)
const {app} = flags
ux.action.start(`Joining ${color.cyan(app)}`)
const {body: user} = await this.heroku.get<Heroku.Account>('/account')
await this.heroku.post<Heroku.TeamAppCollaborator>(`/teams/apps/${app}/collaborators`, {
body: {user: user.email},
})
ux.action.stop()
}
}
2 changes: 1 addition & 1 deletion packages/cli/test/helpers/stubs/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function collaborators() {
}).reply(200)
}

export function teamAppCollaborators(email = 'raulb@heroku.com', permissions: string[] = [], response: {code?: number, description?: string} = {}) {
export function teamAppCollaborators(email = 'raulb@heroku.com', permissions: string[] = [], response: {code?: number, description?: Record<string, unknown>} = {}) {
const body: {user: string, permissions?: string[]} = {user: email}
if (permissions.length > 0) {
body.permissions = permissions
Expand Down
46 changes: 46 additions & 0 deletions packages/cli/test/unit/commands/apps/join.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {stdout, stderr} from 'stdout-stderr'
import * as nock from 'nock'
import {expect} from 'chai'
import Cmd from '../../../../src/commands/apps/join'
import runCommand from '../../../helpers/runCommand'
import expectOutput from '../../../helpers/utils/expectOutput'
import {userAccount} from '../../../helpers/stubs/get'
import {teamAppCollaborators} from '../../../helpers/stubs/post'

describe('heroku apps:join', () => {
let apiGetUserAccount: nock.Scope
let apiPostCollaborators: nock.Scope
beforeEach(() => {
apiGetUserAccount = userAccount('raulb@heroku.com')
})
afterEach(() => nock.cleanAll())
it('joins the app', async () => {
apiPostCollaborators = teamAppCollaborators('raulb@heroku.com')
await runCommand(Cmd, [
'--app',
'myapp',
])
expectOutput(stdout.output, '')
expectOutput(stderr.output, 'Joining myapp...\nJoining myapp... done\n')
apiGetUserAccount.done()
apiPostCollaborators.done()
})
it('is forbidden from joining the app', async () => {
const response = {
code: 403, description: {id: 'forbidden', error: 'You do not have access to the team heroku-tools.'},
}
apiPostCollaborators = teamAppCollaborators('raulb@heroku.com', [], response)
let thrown = false
await runCommand(Cmd, [
'--app',
'myapp',
])
.catch(function (error) {
thrown = true
expect(error.body.error).to.eq('You do not have access to the team heroku-tools.')
})
expect(thrown).to.eq(true)
apiGetUserAccount.done()
apiPostCollaborators.done()
})
})
26 changes: 0 additions & 26 deletions packages/orgs-v5/commands/apps/join.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/orgs-v5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ exports.topics = [
]

exports.commands = flatten([
require('./commands/apps/join'),
require('./commands/apps/leave'),
require('./commands/apps/lock'),
require('./commands/apps/transfer'),
Expand Down
49 changes: 0 additions & 49 deletions packages/orgs-v5/test/unit/commands/apps/join.unit.test.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/orgs-v5/test/unit/stub/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function collaborators() {
}).reply(200)
}

function teamAppCollaborators(email = 'raulb@heroku.com', permissions = [], response = {}) {
function teamAppCollaborators(email = 'raulb@heroku.com', permissions = []) {
let body = {user: email}
if (permissions.length > 0) {
body.permissions = permissions
Expand All @@ -18,7 +18,7 @@ function teamAppCollaborators(email = 'raulb@heroku.com', permissions = [], resp
return nock('https://api.heroku.com:443', {
reqheaders: {Accept: 'application/vnd.heroku+json; version=3'},
})
.post('/teams/apps/myapp/collaborators', body).reply(response.code || 200, response.description)
.post('/teams/apps/myapp/collaborators', body).reply(200)
}

function personalToPersonal() {
Expand Down

0 comments on commit a3f1e14

Please sign in to comment.