-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathoidc-utils.spec.ts
More file actions
140 lines (113 loc) · 5.95 KB
/
oidc-utils.spec.ts
File metadata and controls
140 lines (113 loc) · 5.95 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { OidcUtils } from '../src/oidc-utils';
import * as core from '@actions/core';
import { JfrogCredentials } from '../src/types';
import { Utils } from '../src/utils';
import { HttpClient } from '@actions/http-client';
jest.mock('@actions/core');
jest.mock('@actions/exec');
describe('OidcUtils', (): void => {
afterEach((): void => {
jest.clearAllMocks();
jest.restoreAllMocks();
});
describe('exchangeOIDCTokenAndExportStepOutputs', (): void => {
const creds: JfrogCredentials = {
jfrogUrl: 'https://example.jfrog.io',
oidcProviderName: 'provider',
oidcTokenId: 'token-id',
oidcAudience: 'aud',
};
it('should export step outputs when CLI succeeds', async (): Promise<void> => {
const mockOutput: string = 'AccessToken: abc Username: tester';
const mockRunCli: any = jest.spyOn(Utils, 'runCliAndGetOutput').mockResolvedValueOnce(mockOutput);
const result: string | undefined = await OidcUtils.exchangeOIDCTokenAndExportStepOutputs(creds);
expect(result).toBe('abc');
expect(core.setOutput).toHaveBeenCalledWith('oidc-token', 'abc');
expect(core.setOutput).toHaveBeenCalledWith('oidc-user', 'tester');
mockRunCli.mockRestore();
});
it('should correctly set step outputs for CLI token exchange', async (): Promise<void> => {
const mockOutput: string = 'AccessToken: cli-token Username: cli-user';
const mockRunCli: any = jest.spyOn(Utils, 'runCliAndGetOutput').mockResolvedValueOnce(mockOutput);
const result: string | undefined = await OidcUtils.exchangeOIDCTokenAndExportStepOutputs(creds);
expect(result).toBe('cli-token');
expect(core.setOutput).toHaveBeenCalledWith('oidc-token', 'cli-token');
expect(core.setOutput).toHaveBeenCalledWith('oidc-user', 'cli-user');
mockRunCli.mockRestore();
});
it('should perform manual OIDC token exchange and set outputs', async () => {
// Arrange
const creds: JfrogCredentials = {
jfrogUrl: 'https://example.jfrog.io',
oidcProviderName: 'provider',
oidcTokenId: 'token-id',
oidcAudience: '',
};
const mockAccessToken: any = [
Buffer.from(JSON.stringify({ alg: 'none' })).toString('base64'), // Header
Buffer.from(JSON.stringify({ sub: 'jfrt@dummy-user' })).toString('base64'), // Payload
'', // No signature for testing
].join('.');
const mockResponse: any = {
readBody: jest.fn().mockResolvedValue(JSON.stringify({ access_token: mockAccessToken })),
};
const mockHttpClientPost: any = jest.spyOn(HttpClient.prototype, 'post').mockResolvedValue(mockResponse as any);
const mockGetApplicationKey: any = jest.spyOn(OidcUtils, 'getApplicationKey').mockResolvedValue('mock-application-key');
const mockOutputOidcTokenAndUsernameFromToken: any = jest.spyOn(OidcUtils, 'outputOidcTokenAndUsernameFromToken');
// Act
const result: any = await OidcUtils.manualExchangeOidc(creds);
// Assert
expect(result).toBe(mockAccessToken);
expect(mockOutputOidcTokenAndUsernameFromToken).toHaveBeenCalledWith(mockAccessToken);
// Cleanup
mockHttpClientPost.mockRestore();
mockGetApplicationKey.mockRestore();
mockOutputOidcTokenAndUsernameFromToken.mockRestore();
});
it('should throw if creds are missing required fields', async (): Promise<void> => {
const incompleteCreds: JfrogCredentials = {
jfrogUrl: 'https://example.jfrog.io',
oidcAudience: '',
// missing provider and token ID
};
await expect(OidcUtils.exchangeOIDCTokenAndExportStepOutputs(incompleteCreds)).rejects.toThrow(
'Missing one or more required fields: OIDC provider name, token ID, or JFrog Platform URL.',
);
});
});
describe('getAccessTokenFromCliOutput', (): void => {
it('should parse valid JSON', (): void => {
const input: string = '{"AccessToken":"abc","Username":"user"}';
const { accessToken, username }: { accessToken: string; username: string } = OidcUtils.extractValuesFromOIDCToken(input);
expect(accessToken).toBe('abc');
expect(username).toBe('user');
});
it('should fallback to regex parsing', (): void => {
const input: string = 'AccessToken: abc Username: user';
const { accessToken, username }: { accessToken: string; username: string } = OidcUtils.extractValuesFromOIDCToken(input);
expect(accessToken).toBe('abc');
expect(username).toBe('user');
});
it('should throw on invalid input', (): void => {
expect((): void => {
OidcUtils.extractValuesFromOIDCToken('Invalid');
}).toThrow();
});
});
describe('setOidcStepOutputs', (): void => {
it('should export user/token as step output and secret', (): void => {
OidcUtils.setOidcStepOutputs('foo', 'bar');
expect(core.setSecret).toHaveBeenCalledWith('bar');
expect(core.setSecret).toHaveBeenCalledWith('foo');
expect(core.setOutput).toHaveBeenCalledWith('oidc-token', 'bar');
expect(core.setOutput).toHaveBeenCalledWith('oidc-user', 'foo');
});
});
describe('trackOldOidcUsage', (): void => {
it('should export OIDC usage env vars', (): void => {
OidcUtils.trackOldOidcUsage();
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_CONFIG_OIDC', 'TRUE');
expect(core.exportVariable).toHaveBeenCalledWith('JFROG_CLI_USAGE_OIDC_USED', 'TRUE');
});
});
});