Skip to content

Commit 44f5170

Browse files
authored
Merge pull request #790 from davidgamero/azure_auth_tests_update
Azure auth tests update
2 parents b54ace6 + 2e5cacf commit 44f5170

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

src/azure_auth_test.ts

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
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';
34
import { join } from 'path';
45

56
import { User, Cluster } from './config_types';
67
import { AzureAuth } from './azure_auth';
78
import { KubeConfig } from './config';
9+
import { OutgoingHttpHeaders } from 'http2';
10+
import { HttpMethod, RequestContext } from '.';
11+
12+
use(chaiAsPromised);
813

914
describe('AzureAuth', () => {
15+
16+
const testUrl1 = 'https://test1.com';
1017
var auth: AzureAuth;
1118
beforeEach(() => {
1219
auth = new AzureAuth();
@@ -53,20 +60,18 @@ describe('AzureAuth', () => {
5360
},
5461
} as User,
5562
);
56-
const opts = {} as requestlib.Options;
63+
let requestContext = new RequestContext(testUrl1, HttpMethod.GET)
5764

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}`);
6772
});
6873

69-
it('should populate from auth provider without expirty', async () => {
74+
it('should populate from auth provider without expiry', async () => {
7075
const config = new KubeConfig();
7176
const token = 'token';
7277
config.loadFromClusterAndUser(
@@ -80,13 +85,11 @@ describe('AzureAuth', () => {
8085
},
8186
} as User,
8287
);
83-
const opts = {} as requestlib.Options;
88+
let requestContext = new RequestContext(testUrl1, HttpMethod.GET)
8489

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}`);
9093
});
9194

9295
it('should populate rejectUnauthorized=false when skipTLSVerify is set', async () => {
@@ -103,10 +106,11 @@ describe('AzureAuth', () => {
103106
},
104107
} as User,
105108
);
106-
const opts = {} as requestlib.Options;
109+
let requestContext = new RequestContext(testUrl1, HttpMethod.GET)
107110

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);
110114
});
111115

112116
it('should not set rejectUnauthorized if skipTLSVerify is not set', async () => {
@@ -125,10 +129,11 @@ describe('AzureAuth', () => {
125129
},
126130
} as User,
127131
);
128-
const opts = {} as requestlib.Options;
132+
let requestContext = new RequestContext(testUrl1, HttpMethod.GET)
129133

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);
132137
});
133138

134139
it('should throw with expired token and no cmd', () => {
@@ -144,9 +149,9 @@ describe('AzureAuth', () => {
144149
},
145150
} as User,
146151
);
147-
const opts = {} as requestlib.Options;
152+
let requestContext = new RequestContext(testUrl1, HttpMethod.GET)
148153

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!');
150155
});
151156

152157
it('should throw with bad command', () => {
@@ -164,8 +169,9 @@ describe('AzureAuth', () => {
164169
},
165170
} as User,
166171
);
167-
const opts = {} as requestlib.Options;
168-
return expect(config.applyToRequest(opts)).to.eventually.be.rejectedWith(/Failed to refresh token/);
172+
let requestContext = new RequestContext(testUrl1, HttpMethod.GET)
173+
174+
return expect(config.applySecurityAuthentication(requestContext)).to.eventually.be.rejectedWith(/Failed to refresh token/);
169175
});
170176

171177
it('should exec when no cmd and token is not expired', async () => {
@@ -183,8 +189,8 @@ describe('AzureAuth', () => {
183189
},
184190
} as User,
185191
);
186-
const opts = {} as requestlib.Options;
187-
await config.applyToRequest(opts);
192+
let requestContext = new RequestContext(testUrl1, HttpMethod.GET)
193+
await config.applySecurityAuthentication(requestContext);
188194
});
189195

190196
it('should exec with expired token', async () => {
@@ -210,12 +216,12 @@ describe('AzureAuth', () => {
210216
},
211217
} as User,
212218
);
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}`);
219225
});
220226
it('should exec without access-token', async () => {
221227
// TODO: fix this test for Windows
@@ -239,12 +245,11 @@ describe('AzureAuth', () => {
239245
},
240246
} as User,
241247
);
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}`);
248253
});
249254
it('should exec without access-token', async () => {
250255
// TODO: fix this test for Windows
@@ -268,20 +273,19 @@ describe('AzureAuth', () => {
268273
},
269274
} as User,
270275
);
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}`);
277281
});
278282
it('should exec succesfully with spaces in cmd', async () => {
279283
// TODO: fix this test for Windows
280284
if (process.platform === 'win32') {
281285
return;
282286
}
283287
const config = new KubeConfig();
284-
const token = 'token';
288+
const token = 'test-token';
285289
const responseStr = `{"token":{"accessToken":"${token}"}}`;
286290
config.loadFromClusterAndUser(
287291
{ skipTLSVerify: false } as Cluster,
@@ -297,11 +301,10 @@ describe('AzureAuth', () => {
297301
},
298302
} as User,
299303
);
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}`);
306309
});
307310
});

0 commit comments

Comments
 (0)