Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(auth): update warning message for auth:token #2750

Merged
merged 8 commits into from
Apr 9, 2024
Merged
3 changes: 2 additions & 1 deletion packages/cli/src/commands/auth/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ By default, the CLI auth token is only valid for 1 year. To generate a long-live
try {
const {body: tokens} = await this.heroku.get<Heroku.OAuthAuthorization>('/oauth/authorizations', {retryAuth: false})
const token = tokens.find((t: any) => t.access_token && t.access_token.token === this.heroku.auth)
const isInternal = token ? token.user.email.includes('@heroku.com') : false
zwhitfield3 marked this conversation as resolved.
Show resolved Hide resolved
if (token && token.access_token.expires_in) {
const d = new Date()
d.setSeconds(d.getSeconds() + token.access_token.expires_in)
this.warn(`token will expire ${formatRelative(d, new Date())}\nUse ${color.cmd('heroku authorizations:create')} to generate a long-term token`)
this.warn(`token will expire ${formatRelative(d, new Date())}\n${isInternal ? 'All tokens expire one year after we generate it.' : `To generate a long-lived token, use ${color.cmd('heroku authorizations:create')}.`}`)
}
} catch (error: any) {
this.warn(error)
Expand Down
40 changes: 39 additions & 1 deletion packages/cli/test/unit/commands/auth/token.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('auth:token', () => {
.get('/oauth/authorizations')
.reply(200, [
{access_token: {token: 'somethingelse'}},
{access_token: {token: 'foobar', expires_in: 60}},
{access_token: {token: 'foobar', expires_in: 60}, user: {email: 'foo@bar.com'}},
{},
]),
)
Expand All @@ -18,4 +18,42 @@ describe('auth:token', () => {
expect(ctx.stdout).to.equal('foobar\n')
expect(ctx.stderr).to.contain('Warning: token will expire today')
})

test
.env({HEROKU_API_KEY: 'foobar'})
.nock('https://api.heroku.com', api => api
.get('/oauth/authorizations')
.reply(200, [
{access_token: {token: 'somethingelse'}},
{access_token: {token: 'foobar', expires_in: 60}, user: {email: 'foo@bar.com'}},
{},
]),
)
.stdout()
.stderr()
.command(['auth:token'])
.it('shows "long-term" token generation warning for non-internal users', ctx => {
expect(ctx.stdout).to.equal('foobar\n')
expect(ctx.stderr).to.contain('To generate a long-lived token, use heroku authorizations:create.')
expect(ctx.stderr).to.not.contain('All tokens expire one year after we generate it.')
})

test
.env({HEROKU_API_KEY: 'foobar'})
.nock('https://api.heroku.com', api => api
.get('/oauth/authorizations')
.reply(200, [
{access_token: {token: 'somethingelse'}},
{access_token: {token: 'foobar', expires_in: 60}, user: {email: 'foo@heroku.com'}},
{},
]),
)
.stdout()
.stderr()
.command(['auth:token'])
.it('shows AT2 token generation warning for internal users', ctx => {
expect(ctx.stdout).to.equal('foobar\n')
expect(ctx.stderr).to.contain('All tokens expire one year after we generate it.')
expect(ctx.stderr).to.not.contain('To generate a long-lived token, use heroku authorizations:create.')
})
})