Skip to content

Commit 8c40c25

Browse files
authored
Fix tests (jfrog#260)
1 parent 0615a66 commit 8c40c25

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

lib/utils.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class Utils {
312312
}
313313
const configCmd = [Utils.getServerIdForConfig(), '--url', url, '--interactive=false', '--overwrite=true'];
314314
// OIDC auth
315-
if (oidcProviderName) {
315+
if (this.isCLIVersionOidcSupported() && !!oidcProviderName) {
316316
configCmd.push(`--oidc-provider-name=${oidcProviderName}`);
317317
configCmd.push('--oidc-provider-type=Github');
318318
configCmd.push(`--oidc-token-id=${oidcTokenId}`);
@@ -565,6 +565,14 @@ class Utils {
565565
const version = core.getInput(Utils.CLI_VERSION_ARG);
566566
return version === Utils.LATEST_CLI_VERSION || (0, semver_1.gte)(version, Utils.MIN_CLI_VERSION_JOB_SUMMARY);
567567
}
568+
static isCLIVersionOidcSupported() {
569+
const version = core.getInput(Utils.CLI_VERSION_ARG) || '';
570+
if (version === '') {
571+
// No input meaning default version which is supported
572+
return true;
573+
}
574+
return version === Utils.LATEST_CLI_VERSION || (0, semver_1.gte)(version, Utils.MIN_CLI_OIDC_VERSION);
575+
}
568576
/**
569577
* Generates GitHub workflow unified Summary report.
570578
* This function runs as part of post-workflow cleanup function,
@@ -828,10 +836,9 @@ class Utils {
828836
if (!jfrogCredentials.jfrogUrl) {
829837
throw new Error(`JF_URL must be provided when oidc-provider-name is specified`);
830838
}
831-
const version = core.getInput(Utils.CLI_VERSION_ARG);
832839
// Version should be more than min version
833840
// If CLI_REMOTE_ARG specified, we have to fetch token before we can download the CLI.
834-
if ((version === Utils.LATEST_CLI_VERSION || (0, semver_1.gte)(version, Utils.MIN_CLI_OIDC_VERSION)) && !core.getInput(this.CLI_REMOTE_ARG)) {
841+
if (this.isCLIVersionOidcSupported() && !core.getInput(this.CLI_REMOTE_ARG)) {
835842
core.debug('Using CLI Config OIDC Auth Method..');
836843
jfrogCredentials.oidcTokenId = yield Utils.getIdToken(jfrogCredentials.oidcAudience);
837844
return jfrogCredentials;

src/utils.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ export class Utils {
383383

384384
const configCmd: string[] = [Utils.getServerIdForConfig(), '--url', url, '--interactive=false', '--overwrite=true'];
385385
// OIDC auth
386-
if (oidcProviderName) {
386+
if (this.isCLIVersionOidcSupported() && !!oidcProviderName) {
387387
configCmd.push(`--oidc-provider-name=${oidcProviderName}`);
388388
configCmd.push('--oidc-provider-type=Github');
389389
configCmd.push(`--oidc-token-id=${oidcTokenId}`);
@@ -660,6 +660,15 @@ export class Utils {
660660
return version === Utils.LATEST_CLI_VERSION || gte(version, Utils.MIN_CLI_VERSION_JOB_SUMMARY);
661661
}
662662

663+
public static isCLIVersionOidcSupported(): boolean {
664+
const version: string = core.getInput(Utils.CLI_VERSION_ARG) || '';
665+
if (version === '') {
666+
// No input meaning default version which is supported
667+
return true;
668+
}
669+
return version === Utils.LATEST_CLI_VERSION || gte(version, Utils.MIN_CLI_OIDC_VERSION);
670+
}
671+
663672
/**
664673
* Generates GitHub workflow unified Summary report.
665674
* This function runs as part of post-workflow cleanup function,
@@ -927,11 +936,9 @@ export class Utils {
927936
if (!jfrogCredentials.jfrogUrl) {
928937
throw new Error(`JF_URL must be provided when oidc-provider-name is specified`);
929938
}
930-
931-
const version: string = core.getInput(Utils.CLI_VERSION_ARG);
932939
// Version should be more than min version
933940
// If CLI_REMOTE_ARG specified, we have to fetch token before we can download the CLI.
934-
if ((version === Utils.LATEST_CLI_VERSION || gte(version, Utils.MIN_CLI_OIDC_VERSION)) && !core.getInput(this.CLI_REMOTE_ARG)) {
941+
if (this.isCLIVersionOidcSupported() && !core.getInput(this.CLI_REMOTE_ARG)) {
935942
core.debug('Using CLI Config OIDC Auth Method..');
936943
jfrogCredentials.oidcTokenId = await Utils.getIdToken(jfrogCredentials.oidcAudience);
937944
return jfrogCredentials;

test/main.spec.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import semver = require('semver/preload');
99

1010
jest.mock('os');
1111
jest.mock('@actions/core');
12-
jest.mock('semver');
1312
jest.mock('@actions/core');
1413
jest.mock('fs', () => ({
1514
promises: {
@@ -183,7 +182,12 @@ describe('JFrog CLI Configuration', () => {
183182

184183
// Expect the custom server ID to be used.
185184
let customServerId: string = 'custom-server-id';
186-
jest.spyOn(core, 'getInput').mockReturnValue(customServerId);
185+
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
186+
if (name === customServerId) {
187+
return 'custom-server-id'; // Return this value for the specific argument
188+
}
189+
return ''; // Default return value for other arguments
190+
});
187191
testConfigCommand(customServerId);
188192

189193
// Expect the servers env var to include both servers.
@@ -409,7 +413,6 @@ describe('Job Summaries', () => {
409413
});
410414

411415
describe('isJobSummarySupported', () => {
412-
const MIN_CLI_VERSION_JOB_SUMMARY: string = '2.66.0';
413416
const LATEST_CLI_VERSION: string = 'latest';
414417

415418
beforeEach(() => {
@@ -424,17 +427,13 @@ describe('isJobSummarySupported', () => {
424427
it('should return true if the version is greater than or equal to the minimum supported version', () => {
425428
const version: string = '2.66.0';
426429
jest.spyOn(core, 'getInput').mockReturnValue(version);
427-
(semver.gte as jest.Mock).mockReturnValue(true);
428430
expect(Utils.isJobSummarySupported()).toBe(true);
429-
expect(semver.gte).toHaveBeenCalledWith(version, MIN_CLI_VERSION_JOB_SUMMARY);
430431
});
431432

432433
it('should return false if the version is less than the minimum supported version', () => {
433434
const version: string = '2.65.0';
434435
jest.spyOn(core, 'getInput').mockReturnValue(version);
435-
(semver.gte as jest.Mock).mockReturnValue(false);
436436
expect(Utils.isJobSummarySupported()).toBe(false);
437-
expect(semver.gte).toHaveBeenCalledWith(version, MIN_CLI_VERSION_JOB_SUMMARY);
438437
});
439438
});
440439

@@ -740,4 +739,43 @@ describe('handleOidcAuth', () => {
740739
const result: JfrogCredentials = await (Utils as any).handleOidcAuth(credentials);
741740
expect(result.accessToken).toBe('forced-manual-token');
742741
});
742+
it('should include OIDC flags only for supported versions', () => {
743+
const creds: JfrogCredentials = {
744+
jfrogUrl: 'https://example.jfrog.io',
745+
oidcProviderName: 'setup-jfrog-cli',
746+
oidcTokenId: 'abc-123',
747+
oidcAudience: 'jfrog-github',
748+
} as JfrogCredentials;
749+
750+
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
751+
if (name === Utils.CLI_VERSION_ARG) return '2.75.0'; // Supported version
752+
return '';
753+
});
754+
755+
const args: string[] | undefined = Utils.getSeparateEnvConfigArgs(creds);
756+
expect(args).toContain('--oidc-provider-name=setup-jfrog-cli');
757+
expect(args).toContain('--oidc-provider-type=Github');
758+
expect(args).toContain('--oidc-token-id=abc-123');
759+
expect(args).toContain('--oidc-audience=jfrog-github');
760+
});
761+
762+
it('should not include OIDC flags for unsupported versions', () => {
763+
const creds: JfrogCredentials = {
764+
jfrogUrl: 'https://example.jfrog.io',
765+
oidcProviderName: 'setup-jfrog-cli',
766+
oidcTokenId: 'abc-123',
767+
oidcAudience: 'jfrog-github',
768+
} as JfrogCredentials;
769+
770+
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
771+
if (name === Utils.CLI_VERSION_ARG) return '2.74.0'; // Unsupported version
772+
return '';
773+
});
774+
775+
const args: string[] | undefined = Utils.getSeparateEnvConfigArgs(creds);
776+
expect(args).not.toContain('--oidc-provider-name=setup-jfrog-cli');
777+
expect(args).not.toContain('--oidc-provider-type=Github');
778+
expect(args).not.toContain('--oidc-token-id=abc-123');
779+
expect(args).not.toContain('--oidc-audience=jfrog-github');
780+
});
743781
});

0 commit comments

Comments
 (0)