1- import { expect } from 'chai' ;
1+ import { expect , use } from 'chai' ;
2+ import chaiAsPromised = require( 'chai-as-promised' ) ;
3+ use ( chaiAsPromised ) ;
4+
25import * as shell from 'shelljs' ;
36
47import execa = require( 'execa' ) ;
@@ -61,18 +64,22 @@ describe('ExecAuth', () => {
6164 stdout : JSON . stringify ( { status : { token : 'foo' } } ) ,
6265 } as execa . ExecaSyncReturnValue ;
6366 } ;
64-
65- const token = auth . getToken ( {
66- name : 'user' ,
67- authProvider : {
68- config : {
69- exec : {
70- command : 'echo' ,
67+ const opts = { } as request . Options ;
68+ opts . headers = [ ] ;
69+ auth . applyAuthentication (
70+ {
71+ name : 'user' ,
72+ authProvider : {
73+ config : {
74+ exec : {
75+ command : 'echo' ,
76+ } ,
7177 } ,
7278 } ,
7379 } ,
74- } ) ;
75- expect ( token ) . to . equal ( 'Bearer foo' ) ;
80+ opts ,
81+ ) ;
82+ expect ( opts . headers . Authorization ) . to . equal ( 'Bearer foo' ) ;
7683 } ) ;
7784
7885 it ( 'should correctly exec for certs' , async ( ) => {
@@ -98,11 +105,11 @@ describe('ExecAuth', () => {
98105 } ,
99106 } ,
100107 } ;
101- const token = auth . getToken ( user ) ;
102- expect ( token ) . to . be . null ;
103-
104108 const opts = { } as request . Options ;
109+ opts . headers = [ ] ;
110+
105111 auth . applyAuthentication ( user , opts ) ;
112+ expect ( opts . headers . Authorization ) . to . be . undefined ;
106113 expect ( opts . cert ) . to . equal ( 'foo' ) ;
107114 expect ( opts . key ) . to . equal ( 'bar' ) ;
108115 } ) ;
@@ -136,28 +143,35 @@ describe('ExecAuth', () => {
136143 } ,
137144 } ,
138145 } ;
139- var token = auth . getToken ( user ) ;
140- expect ( token ) . to . equal ( `Bearer ${ tokenValue } ` ) ;
146+
147+ const opts = { } as request . Options ;
148+ opts . headers = [ ] ;
149+
150+ await auth . applyAuthentication ( user , opts ) ;
151+ expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ tokenValue } ` ) ;
141152 expect ( execCount ) . to . equal ( 1 ) ;
142153
143154 // old token should be expired, set expiration for the new token for the future.
144155 expire = '29 Mar 2095 00:00:00 GMT' ;
145156 tokenValue = 'bar' ;
146- token = auth . getToken ( user ) ;
147- expect ( token ) . to . equal ( `Bearer ${ tokenValue } ` ) ;
157+ await auth . applyAuthentication ( user , opts ) ;
158+ expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ tokenValue } ` ) ;
148159 expect ( execCount ) . to . equal ( 2 ) ;
149160
150161 // Should use cached token, execCount should stay at two, token shouldn't change
151162 tokenValue = 'baz' ;
152- token = auth . getToken ( user ) ;
153- expect ( token ) . to . equal ( 'Bearer bar' ) ;
163+ await auth . applyAuthentication ( user , opts ) ;
164+ expect ( opts . headers . Authorization ) . to . equal ( 'Bearer bar' ) ;
154165 expect ( execCount ) . to . equal ( 2 ) ;
155166 } ) ;
156167
157- it ( 'should return null on no exec info' , ( ) => {
168+ it ( 'should return null on no exec info' , async ( ) => {
158169 const auth = new ExecAuth ( ) ;
159- const token = auth . getToken ( { } as User ) ;
160- expect ( token ) . to . be . null ;
170+ const opts = { } as request . Options ;
171+ opts . headers = [ ] ;
172+
173+ await auth . applyAuthentication ( { } as User , opts ) ;
174+ expect ( opts . headers . Authorization ) . to . be . undefined ;
161175 } ) ;
162176
163177 it ( 'should throw on exec errors' , ( ) => {
@@ -184,8 +198,11 @@ describe('ExecAuth', () => {
184198 } ,
185199 } ,
186200 } ;
201+ const opts = { } as request . Options ;
202+ opts . headers = [ ] ;
187203
188- expect ( ( ) => auth . getToken ( user ) ) . to . throw ( 'Some error!' ) ;
204+ const promise = auth . applyAuthentication ( user , opts ) ;
205+ return expect ( promise ) . to . eventually . be . rejected ;
189206 } ) ;
190207
191208 it ( 'should exec with env vars' , async ( ) => {
@@ -203,22 +220,28 @@ describe('ExecAuth', () => {
203220 } as execa . ExecaSyncReturnValue ;
204221 } ;
205222 process . env . BLABBLE = 'flubble' ;
206- const token = auth . getToken ( {
207- name : 'user' ,
208- authProvider : {
209- config : {
210- exec : {
211- command : 'echo' ,
212- env : [
213- {
214- name : 'foo' ,
215- value : 'bar' ,
216- } ,
217- ] ,
223+ const opts = { } as request . Options ;
224+ opts . headers = [ ] ;
225+
226+ await auth . applyAuthentication (
227+ {
228+ name : 'user' ,
229+ authProvider : {
230+ config : {
231+ exec : {
232+ command : 'echo' ,
233+ env : [
234+ {
235+ name : 'foo' ,
236+ value : 'bar' ,
237+ } ,
238+ ] ,
239+ } ,
218240 } ,
219241 } ,
220242 } ,
221- } ) ;
243+ opts ,
244+ ) ;
222245 expect ( optsOut . env . foo ) . to . equal ( 'bar' ) ;
223246 expect ( optsOut . env . PATH ) . to . equal ( process . env . PATH ) ;
224247 expect ( optsOut . env . BLABBLE ) . to . equal ( process . env . BLABBLE ) ;
0 commit comments