Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/commands/authorizations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {hux} from '@heroku/heroku-cli-util'
import * as color from '@heroku/heroku-cli-util/color'
import {ux} from '@oclif/core/ux'

import {SDK_HEADER} from '../../lib/api.js'
import {display} from '../../lib/authorizations/authorizations.js'

export default class AuthorizationsCreate extends Command {
Expand All @@ -18,19 +19,25 @@ export default class AuthorizationsCreate extends Command {
json: flags.boolean({char: 'j', description: 'output in json format'}),
scope: flags.string({char: 's', completion: ScopeCompletion, description: 'set custom OAuth scopes'}),
short: flags.boolean({char: 'S', description: 'only output token'}),
team: flags.team(),
}

async run() {
const {flags} = await this.parse(AuthorizationsCreate)

ux.action.start('Creating OAuth Authorization')

const {body: auth} = await this.heroku.post<Heroku.OAuthAuthorization>('/oauth/authorizations', {
const endpoint = flags.team
? `/teams/${encodeURIComponent(flags.team)}/oauth/authorizations`
: '/oauth/authorizations'

const {body: auth} = await this.heroku.post<Heroku.OAuthAuthorization>(endpoint, {
body: {
description: flags.description,
expires_in: flags['expires-in'],
scope: flags.scope ? flags.scope.split(',') : undefined,
},
...(flags.team ? {headers: {Accept: SDK_HEADER}} : {}),
})

ux.action.stop()
Expand Down
15 changes: 15 additions & 0 deletions test/unit/commands/authorizations/create.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,19 @@ describe('authorizations:create', function () {
expect(json.scope).to.contain('global')
})
})

context('with team flag', function () {
it('creates the authorization for the team', async function () {
api
.matchHeader('accept', 'application/vnd.heroku+json; version=3.sdk')
.post('/teams/my-team/oauth/authorizations', {description: 'awesome'})
.reply(201, {access_token: {token: 'secrettoken'}, scope: ['global']})

const {stdout} = await runCommand(AuthorizationsCreate, ['--description', 'awesome', '--team', 'my-team'])

expect(stdout).to.contain('Client: <none>\n')
expect(stdout).to.contain('Scope: global\n')
expect(stdout).to.contain('Token: secrettoken\n')
})
})
})
Loading