forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_auth_test.ts
More file actions
89 lines (78 loc) · 2.58 KB
/
Copy pathfile_auth_test.ts
File metadata and controls
89 lines (78 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { expect } from 'chai';
import mockfs = require('mock-fs');
import request = require('request');
import { User } from './config_types';
import { FileAuth } from './file_auth';
describe('FileAuth', () => {
it('should refresh when null', async () => {
const auth = new FileAuth();
(auth as any).token = null;
const token = 'test';
mockfs({
'/path/to/fake/dir': {
'token.txt': token,
},
});
const user = {
authProvider: {
config: {
tokenFile: '/path/to/fake/dir/token.txt',
},
},
} as User;
const opts = {} as request.Options;
opts.headers = [];
await auth.applyAuthentication(user, opts);
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
mockfs.restore();
});
it('should refresh when expired', async () => {
const auth = new FileAuth();
(auth as any).token = 'other';
(auth as any).lastRead = new Date(1970, 1, 1);
const token = 'test';
mockfs({
'/path/to/fake/dir': {
'token.txt': token,
},
});
const user = {
authProvider: {
config: {
tokenFile: '/path/to/fake/dir/token.txt',
},
},
} as User;
const opts = {} as request.Options;
opts.headers = [];
await auth.applyAuthentication(user, opts);
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
mockfs.restore();
});
it('should claim correctly', async () => {
const auth = new FileAuth();
const token = 'test';
(auth as any).token = 'other';
mockfs({
'/path/to/fake/dir': {
'token.txt': token,
},
});
const user = {
authProvider: {
config: {
tokenFile: '/path/to/fake/dir/token.txt',
},
},
} as User;
const opts = {} as request.Options;
opts.headers = [];
await auth.applyAuthentication(user, opts);
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
// Set the file to non-existent, but shouldn't matter b/c token is cached.
user.authProvider.config.tokenFile = '/non/existent/file/token.txt';
await auth.applyAuthentication(user, opts);
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
mockfs.restore();
});
});