@@ -8,6 +8,7 @@ import * as sinon from 'sinon'
88import { stderr } from 'stdout-stderr'
99
1010import { Command as CommandBase } from '../src/command.js'
11+ import { setCredentialManagerProvider } from '../src/credential-manager.js'
1112import { RequestId , requestIdHeader } from '../src/request-id.js'
1213import { restoreCredentialManagerStub , stubCredentialManager } from './helpers/credential-manager-stub.js'
1314
@@ -28,6 +29,7 @@ const test = fancy
2829
2930describe ( 'api_client' , ( ) => {
3031 beforeEach ( function ( ) {
32+ nock . cleanAll ( )
3133 process . env = { }
3234 debug . disable ( )
3335 api = nock ( 'https://api.heroku.com' )
@@ -40,6 +42,56 @@ describe('api_client', () => {
4042 restoreCredentialManagerStub ( )
4143 } )
4244
45+ describe ( 'getAuth' , ( ) => {
46+ test
47+ . it ( 'returns token from credential manager' , async ctx => {
48+ stubCredentialManager ( 'token-from-store' )
49+ const cmd = new Command ( [ ] , ctx . config )
50+ cmd . config = ctx . config
51+ expect ( await cmd . heroku . getAuth ( ) ) . to . equal ( 'token-from-store' )
52+ } )
53+
54+ test
55+ . it ( 'returns cached auth when _auth is already set' , async ctx => {
56+ stubCredentialManager ( 'ignored-after-cache' )
57+ const cmd = new Command ( [ ] , ctx . config )
58+ cmd . config = ctx . config
59+ cmd . heroku . auth = 'cached-only'
60+ expect ( await cmd . heroku . getAuth ( ) ) . to . equal ( 'cached-only' )
61+ } )
62+ } )
63+
64+ describe ( 'logout' , ( ) => {
65+ const removeAuthCalls : { account : string | undefined ; hosts : string [ ] } [ ] = [ ]
66+
67+ beforeEach ( ( ) => {
68+ removeAuthCalls . length = 0
69+ setCredentialManagerProvider ( {
70+ async getAuth ( ) {
71+ return 'logout-test-token'
72+ } ,
73+ async removeAuth ( account : string | undefined , hosts : string [ ] ) {
74+ removeAuthCalls . push ( { account, hosts} )
75+ } ,
76+ async saveAuth ( ) { } ,
77+ } )
78+ api . delete ( '/oauth/sessions/~' ) . reply ( 200 , { } )
79+ api . get ( '/oauth/authorizations' ) . reply ( 200 , [ ] )
80+ api . get ( '/oauth/authorizations/~' ) . reply ( 200 , { } )
81+ } )
82+
83+ test
84+ . it ( 'calls removeAuth with api and git hosts after revoking session' , async ctx => {
85+ const cmd = new Command ( [ ] , ctx . config )
86+ cmd . config = ctx . config
87+ await cmd . heroku . logout ( )
88+ expect ( removeAuthCalls ) . to . have . length ( 1 )
89+ expect ( removeAuthCalls [ 0 ] . account ) . to . be . undefined
90+ expect ( removeAuthCalls [ 0 ] . hosts ) . to . deep . equal ( [ 'api.heroku.com' , 'git.heroku.com' ] )
91+ expect ( cmd . heroku . auth ) . to . be . undefined
92+ } )
93+ } )
94+
4395 test
4496 . it ( 'makes an HTTP request' , async ctx => {
4597 api = nock ( 'https://api.heroku.com' , {
0 commit comments