Skip to content

Commit

Permalink
Jas config api for client-js
Browse files Browse the repository at this point in the history
  • Loading branch information
barv-jfrog committed Sep 23, 2024
1 parent 978e3bb commit 538a20a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
23 changes: 4 additions & 19 deletions src/Xray/XrayJasConfigClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,19 @@ export class XrayJasConfigClient {
*
* Sends 'GET /configuration/js requests to Xray and waits for 200 response'.
* @returns the jas config
* @throws an exception if an unexpected response received from Xray or if checkCanceled threw an exception
* @throws an exception if an unexpected response received from Xray
*/
async getJasConfig(): Promise<IJasConfig> {
this.logger.debug(`Sending GET ${this.jasConfigurationEndpoint} request...`);
let receivedStatus: number | undefined;
const requestParams: IRequestParams = {
url: this.jasConfigurationEndpoint,
method: 'GET',
validateStatus: (status: number): boolean => {
receivedStatus = status;
return status === 200;
},
};
let message: string | undefined;
let response: IClientResponse | undefined;
try {
response = await this.httpClient.doAuthRequest(requestParams);
} catch (error) {
throw new Error(`Received unexpected response from Xray. ${String(error)}`);
}
this.logger.debug(`Received status '${receivedStatus}' from Xray.`);
if (receivedStatus === 200) {
return response?.data;
} else {
if (receivedStatus) {
throw new Error(`Received unexpected status '${receivedStatus}' from Xray: ${message}`);
}
throw new Error(`Received response from Xray: ${message}`);
}
return await (
await this.httpClient.doAuthRequest(requestParams)
).data;
}
}
29 changes: 21 additions & 8 deletions test/tests/Xray/XrayJasConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@ import faker from 'faker';
import nock from 'nock';
import { JfrogClient } from '../../../src';
import { TestUtils } from '../../TestUtils';

let jfrogClient: JfrogClient;

beforeAll(() => {
jfrogClient = new JfrogClient(TestUtils.getJfrogClientConfig());
});
import { IGraphRequestModel } from '../../../model';

Check warning on line 5 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

'IGraphRequestModel' is defined but never used

Check warning on line 5 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

'IGraphRequestModel' is defined but never used

Check warning on line 5 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest)

'IGraphRequestModel' is defined but never used

Check warning on line 5 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest)

'IGraphRequestModel' is defined but never used

Check warning on line 5 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (macOS-latest)

'IGraphRequestModel' is defined but never used

Check warning on line 5 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (macOS-latest)

'IGraphRequestModel' is defined but never used
import { IJasConfig } from '../../../model/Xray/JasConfig/JasConfig';

Check warning on line 6 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

'IJasConfig' is defined but never used

Check warning on line 6 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

'IJasConfig' is defined but never used

Check warning on line 6 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest)

'IJasConfig' is defined but never used

Check warning on line 6 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest)

'IJasConfig' is defined but never used

Check warning on line 6 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (macOS-latest)

'IJasConfig' is defined but never used

Check warning on line 6 in test/tests/Xray/XrayJasConfig.spec.ts

View workflow job for this annotation

GitHub Actions / test (macOS-latest)

'IJasConfig' is defined but never used

describe('Xray jas config tests', () => {
test('Get jas config', async () => {
const PLATFORM_URL: string = faker.internet.url();
const uri: string = `/xray/api/v1/configuration/jas`;
const expectedResource: string = '{"enable_token_validation_scanning": true}';
nock(PLATFORM_URL).get(uri).reply(200, expectedResource);
const res: any = await jfrogClient.xray().jasconfig().getJasConfig();
expect(res).toEqual(JSON.parse(expectedResource));
const client: JfrogClient = new JfrogClient({ platformUrl: PLATFORM_URL, logger: TestUtils.createTestLogger() });
const res: any = await client.xray().jasconfig().getJasConfig();
expect(res).toHaveProperty("enable_token_validation_scanning")
expect(res.enable_token_validation_scanning).toEqual(true)
});
});

describe('Xray jas config tests', () => {
test('Fail get jas config', async () => {
const PLATFORM_URL: string = faker.internet.url();
const uri: string = `/xray/api/v1/configuration/jas`;
nock(PLATFORM_URL).get(uri).reply(402, { message: 'error' }).persist();
const client: JfrogClient = new JfrogClient({ platformUrl: PLATFORM_URL, logger: TestUtils.createTestLogger() })
await expect(async () => {
await client
.xray()
.jasconfig()
.getJasConfig();
}).rejects.toThrow(`Request failed with status code 402`);
});
});

0 comments on commit 538a20a

Please sign in to comment.