Skip to content

Commit

Permalink
fix(authorizations): surface api warnings in temporary fix (#2804)
Browse files Browse the repository at this point in the history
* Surface api warnings for authorizations:create

* Surface remaining api warnings for authorizations command

* Add api warning header test for authorizations:create

* Update response type in authorizations:info
  • Loading branch information
zwhitfield3 authored Apr 15, 2024
1 parent 381575f commit 33f8aac
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
8 changes: 7 additions & 1 deletion packages/cli/src/commands/authorizations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class AuthorizationsCreate extends Command {

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

const {body: auth} = await this.heroku.post<Heroku.OAuthAuthorization>('/oauth/authorizations', {
const {body: auth, headers} = await this.heroku.post<Heroku.OAuthAuthorization>('/oauth/authorizations', {
body: {
description: flags.description,
scope: flags.scope ? flags.scope.split(',') : undefined,
Expand All @@ -35,6 +35,12 @@ export default class AuthorizationsCreate extends Command {

ux.action.stop()

const apiWarnings = headers['warning-message'] as string || ''

if (apiWarnings) {
ux.warn(apiWarnings)
}

if (flags.short) {
ux.log(auth.access_token && auth.access_token.token)
} else if (flags.json) {
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/commands/authorizations/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ export default class AuthorizationsInfo extends Command {
async run() {
const {args, flags} = await this.parse(AuthorizationsInfo)

const {body: authentication} = await this.heroku.get<Heroku.OAuthClient>(
const {body: authentication, headers} = await this.heroku.get<Heroku.OAuthAuthorization>(
`/oauth/authorizations/${args.id}`,
)

const apiWarnings = headers['warning-message'] as string || ''

if (apiWarnings) {
ux.warn(apiWarnings)
}

if (flags.json) {
ux.styledJSON(authentication)
} else {
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/commands/authorizations/revoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ export default class AuthorizationsRevoke extends Command {

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

const {body: auth} = await this.heroku.delete<Heroku.OAuthAuthorization>(
const {body: auth, headers} = await this.heroku.delete<Heroku.OAuthAuthorization>(
`/oauth/authorizations/${encodeURIComponent(args.id)}`,
)

const apiWarnings = headers['warning-message'] as string || ''

if (apiWarnings) {
ux.warn(apiWarnings)
}

ux.action.stop(`done, revoked authorization from ${color.cyan(auth.description)}`)
}
}
8 changes: 7 additions & 1 deletion packages/cli/src/commands/authorizations/rotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ export default class AuthorizationsRotate extends Command {

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

const {body: authorization} = await this.heroku.post<Heroku.OAuthAuthorization>(
const {body: authorization, headers} = await this.heroku.post<Heroku.OAuthAuthorization>(
`/oauth/authorizations/${encodeURIComponent(args.id)}/actions/regenerate-tokens`,
)

const apiWarnings = headers['warning-message'] as string || ''

if (apiWarnings) {
ux.warn(apiWarnings)
}

ux.action.stop()

display(authorization)
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/commands/authorizations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class AuthorizationsUpdate extends Command {
}
}

const {body: authentication} = await this.heroku.patch<Heroku.OAuthAuthorization>(
const {body: authentication, headers} = await this.heroku.patch<Heroku.OAuthAuthorization>(
`/oauth/authorizations/${args.id}`,
{
body: {
Expand All @@ -40,6 +40,12 @@ export default class AuthorizationsUpdate extends Command {
},
)

const apiWarnings = headers['warning-message'] as string || ''

if (apiWarnings) {
ux.warn(apiWarnings)
}

ux.action.stop()

display(authentication)
Expand Down
15 changes: 15 additions & 0 deletions packages/cli/test/unit/commands/authorizations/create.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,19 @@ describe('authorizations:create', () => {
expect(json.scope).to.contain('global')
})
})

context('API warning headers', () => {
test
.stderr()
.stdout()
.nock('https://api.heroku.com:443', api => {
api
.post('/oauth/authorizations', {description: 'awesome'})
.reply(201, {scope: ['global'], access_token: {token: 'secrettoken'}}, {'warning-message': 'this is an API warning message example'})
})
.command(['authorizations:create', '--description', 'awesome'])
.it('outputs API warning message', ctx => {
expect(ctx.stderr).contains('this is an API warning message example')
})
})
})

0 comments on commit 33f8aac

Please sign in to comment.