1
- import { expect } from 'chai' ;
2
- import * as requestlib from 'request' ;
1
+ import { use , expect } from 'chai' ;
2
+ import chaiAsPromised from 'chai-as-promised' ;
3
+ import { RequestOptions } from 'https' ;
3
4
import { join } from 'path' ;
4
5
5
6
import { User , Cluster } from './config_types' ;
6
7
import { AzureAuth } from './azure_auth' ;
7
8
import { KubeConfig } from './config' ;
9
+ import { OutgoingHttpHeaders } from 'http2' ;
10
+ import { HttpMethod , RequestContext } from '.' ;
11
+
12
+ use ( chaiAsPromised ) ;
8
13
9
14
describe ( 'AzureAuth' , ( ) => {
15
+
16
+ const testUrl1 = 'https://test1.com' ;
10
17
var auth : AzureAuth ;
11
18
beforeEach ( ( ) => {
12
19
auth = new AzureAuth ( ) ;
@@ -53,20 +60,18 @@ describe('AzureAuth', () => {
53
60
} ,
54
61
} as User ,
55
62
) ;
56
- const opts = { } as requestlib . Options ;
63
+ let requestContext = new RequestContext ( testUrl1 , HttpMethod . GET )
57
64
58
- await config . applyToRequest ( opts ) ;
59
- expect ( opts . headers ) . to . not . be . undefined ;
60
- if ( opts . headers ) {
61
- expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
62
- }
63
- opts . headers = [ ] ;
64
- opts . headers . Host = 'foo.com' ;
65
- await config . applyToRequest ( opts ) ;
66
- expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
65
+ await config . applySecurityAuthentication ( requestContext )
66
+ expect ( requestContext . getHeaders ( ) ) . to . not . be . undefined ;
67
+ expect ( requestContext . getHeaders ( ) [ 'Authorization' ] ) . to . equal ( `Bearer ${ token } ` ) ;
68
+
69
+ requestContext . setHeaderParam ( 'Host' , 'foo.com' ) ;
70
+ await config . applySecurityAuthentication ( requestContext )
71
+ expect ( requestContext . getHeaders ( ) . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
67
72
} ) ;
68
73
69
- it ( 'should populate from auth provider without expirty ' , async ( ) => {
74
+ it ( 'should populate from auth provider without expiry ' , async ( ) => {
70
75
const config = new KubeConfig ( ) ;
71
76
const token = 'token' ;
72
77
config . loadFromClusterAndUser (
@@ -80,13 +85,11 @@ describe('AzureAuth', () => {
80
85
} ,
81
86
} as User ,
82
87
) ;
83
- const opts = { } as requestlib . Options ;
88
+ let requestContext = new RequestContext ( testUrl1 , HttpMethod . GET )
84
89
85
- await config . applyToRequest ( opts ) ;
86
- expect ( opts . headers ) . to . not . be . undefined ;
87
- if ( opts . headers ) {
88
- expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
89
- }
90
+ await config . applySecurityAuthentication ( requestContext )
91
+ expect ( requestContext . getHeaders ( ) ) . to . not . be . undefined ;
92
+ expect ( requestContext . getHeaders ( ) [ 'Authorization' ] ) . to . equal ( `Bearer ${ token } ` ) ;
90
93
} ) ;
91
94
92
95
it ( 'should populate rejectUnauthorized=false when skipTLSVerify is set' , async ( ) => {
@@ -103,10 +106,11 @@ describe('AzureAuth', () => {
103
106
} ,
104
107
} as User ,
105
108
) ;
106
- const opts = { } as requestlib . Options ;
109
+ let requestContext = new RequestContext ( testUrl1 , HttpMethod . GET )
107
110
108
- await config . applyToRequest ( opts ) ;
109
- expect ( opts . rejectUnauthorized ) . to . equal ( false ) ;
111
+ await config . applySecurityAuthentication ( requestContext )
112
+ // @ts -ignore
113
+ expect ( requestContext . getAgent ( ) . options . rejectUnauthorized ) . to . equal ( false ) ;
110
114
} ) ;
111
115
112
116
it ( 'should not set rejectUnauthorized if skipTLSVerify is not set' , async ( ) => {
@@ -125,10 +129,11 @@ describe('AzureAuth', () => {
125
129
} ,
126
130
} as User ,
127
131
) ;
128
- const opts = { } as requestlib . Options ;
132
+ let requestContext = new RequestContext ( testUrl1 , HttpMethod . GET )
129
133
130
- await config . applyToRequest ( opts ) ;
131
- expect ( opts . rejectUnauthorized ) . to . equal ( undefined ) ;
134
+ await config . applySecurityAuthentication ( requestContext )
135
+ // @ts -ignore
136
+ expect ( requestContext . getAgent ( ) . options . rejectUnauthorized ) . to . equal ( undefined ) ;
132
137
} ) ;
133
138
134
139
it ( 'should throw with expired token and no cmd' , ( ) => {
@@ -144,9 +149,9 @@ describe('AzureAuth', () => {
144
149
} ,
145
150
} as User ,
146
151
) ;
147
- const opts = { } as requestlib . Options ;
152
+ let requestContext = new RequestContext ( testUrl1 , HttpMethod . GET )
148
153
149
- return expect ( config . applyToRequest ( opts ) ) . to . eventually . be . rejectedWith ( 'Token is expired!' ) ;
154
+ return expect ( config . applySecurityAuthentication ( requestContext ) ) . to . eventually . be . rejectedWith ( 'Token is expired!' ) ;
150
155
} ) ;
151
156
152
157
it ( 'should throw with bad command' , ( ) => {
@@ -164,8 +169,9 @@ describe('AzureAuth', () => {
164
169
} ,
165
170
} as User ,
166
171
) ;
167
- const opts = { } as requestlib . Options ;
168
- return expect ( config . applyToRequest ( opts ) ) . to . eventually . be . rejectedWith ( / F a i l e d t o r e f r e s h t o k e n / ) ;
172
+ let requestContext = new RequestContext ( testUrl1 , HttpMethod . GET )
173
+
174
+ return expect ( config . applySecurityAuthentication ( requestContext ) ) . to . eventually . be . rejectedWith ( / F a i l e d t o r e f r e s h t o k e n / ) ;
169
175
} ) ;
170
176
171
177
it ( 'should exec when no cmd and token is not expired' , async ( ) => {
@@ -183,8 +189,8 @@ describe('AzureAuth', () => {
183
189
} ,
184
190
} as User ,
185
191
) ;
186
- const opts = { } as requestlib . Options ;
187
- await config . applyToRequest ( opts ) ;
192
+ let requestContext = new RequestContext ( testUrl1 , HttpMethod . GET )
193
+ await config . applySecurityAuthentication ( requestContext ) ;
188
194
} ) ;
189
195
190
196
it ( 'should exec with expired token' , async ( ) => {
@@ -210,12 +216,12 @@ describe('AzureAuth', () => {
210
216
} ,
211
217
} as User ,
212
218
) ;
213
- const opts = { } as requestlib . Options ;
214
- await config . applyToRequest ( opts ) ;
215
- expect ( opts . headers ) . to . not . be . undefined ;
216
- if ( opts . headers ) {
217
- expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
218
- }
219
+
220
+ let requestContext = new RequestContext ( testUrl1 , HttpMethod . GET )
221
+ await config . applySecurityAuthentication ( requestContext )
222
+
223
+ expect ( requestContext . getHeaders ( ) ) . to . not . be . undefined ;
224
+ expect ( requestContext . getHeaders ( ) [ 'Authorization' ] ) . to . equal ( `Bearer ${ token } ` ) ;
219
225
} ) ;
220
226
it ( 'should exec without access-token' , async ( ) => {
221
227
// TODO: fix this test for Windows
@@ -239,12 +245,11 @@ describe('AzureAuth', () => {
239
245
} ,
240
246
} as User ,
241
247
) ;
242
- const opts = { } as requestlib . Options ;
243
- await config . applyToRequest ( opts ) ;
244
- expect ( opts . headers ) . to . not . be . undefined ;
245
- if ( opts . headers ) {
246
- expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
247
- }
248
+ let requestContext = new RequestContext ( testUrl1 , HttpMethod . GET )
249
+ await config . applySecurityAuthentication ( requestContext )
250
+
251
+ expect ( requestContext . getHeaders ( ) ) . to . not . be . undefined ;
252
+ expect ( requestContext . getHeaders ( ) [ 'Authorization' ] ) . to . equal ( `Bearer ${ token } ` ) ;
248
253
} ) ;
249
254
it ( 'should exec without access-token' , async ( ) => {
250
255
// TODO: fix this test for Windows
@@ -268,20 +273,19 @@ describe('AzureAuth', () => {
268
273
} ,
269
274
} as User ,
270
275
) ;
271
- const opts = { } as requestlib . Options ;
272
- await config . applyToRequest ( opts ) ;
273
- expect ( opts . headers ) . to . not . be . undefined ;
274
- if ( opts . headers ) {
275
- expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
276
- }
276
+ let requestContext = new RequestContext ( testUrl1 , HttpMethod . GET )
277
+ await config . applySecurityAuthentication ( requestContext )
278
+
279
+ expect ( requestContext . getHeaders ( ) ) . to . not . be . undefined ;
280
+ expect ( requestContext . getHeaders ( ) [ 'Authorization' ] ) . to . equal ( `Bearer ${ token } ` ) ;
277
281
} ) ;
278
282
it ( 'should exec succesfully with spaces in cmd' , async ( ) => {
279
283
// TODO: fix this test for Windows
280
284
if ( process . platform === 'win32' ) {
281
285
return ;
282
286
}
283
287
const config = new KubeConfig ( ) ;
284
- const token = 'token' ;
288
+ const token = 'test- token' ;
285
289
const responseStr = `{"token":{"accessToken":"${ token } "}}` ;
286
290
config . loadFromClusterAndUser (
287
291
{ skipTLSVerify : false } as Cluster ,
@@ -297,11 +301,10 @@ describe('AzureAuth', () => {
297
301
} ,
298
302
} as User ,
299
303
) ;
300
- const opts = { } as requestlib . Options ;
301
- await config . applyToRequest ( opts ) ;
302
- expect ( opts . headers ) . to . not . be . undefined ;
303
- if ( opts . headers ) {
304
- expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
305
- }
304
+ let requestContext = new RequestContext ( testUrl1 , HttpMethod . GET )
305
+ await config . applySecurityAuthentication ( requestContext )
306
+
307
+ expect ( requestContext . getHeaders ( ) ) . to . not . be . undefined ;
308
+ expect ( requestContext . getHeaders ( ) [ 'Authorization' ] ) . to . equal ( `Bearer ${ token } ` ) ;
306
309
} ) ;
307
310
} ) ;
0 commit comments