diff --git a/packages/core/README.md b/packages/core/README.md index 08ec495f..d42c2fc3 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -232,7 +232,6 @@ async function getIDTokenAction(): Promise { if (audience !== undefined) aud = `${audience}` const id_token = await core.getIDToken(aud) - const val = `ID token is ${id_token}` core.setSecret(id_token) core.setOutput('id_token', id_token) diff --git a/packages/core/__tests__/core.test.ts b/packages/core/__tests__/core.test.ts index 20ebbccf..5341383f 100644 --- a/packages/core/__tests__/core.test.ts +++ b/packages/core/__tests__/core.test.ts @@ -2,6 +2,7 @@ import * as fs from 'fs' import * as os from 'os' import * as path from 'path' import * as core from '../src/core' +import * as oidcUtil from '../src/oidc-utils' var httpclient = require('@actions/http-client') /* eslint-disable @typescript-eslint/unbound-method */ @@ -394,6 +395,19 @@ function getTokenEndPoint() { } describe('oidc-client-tests', () => { + + const OLD_ENV = process.env + const oidcClient = new oidcUtil.OidcClient() + + beforeEach(() => { + jest.resetModules() + process.env = { ...OLD_ENV }; + }); + + afterAll(() => { + process.env = OLD_ENV; + }); + it('Get Http Client', async () => { const http = new httpclient.HttpClient('actions/oidc-client') expect(http).toBeDefined() @@ -404,4 +418,25 @@ describe('oidc-client-tests', () => { const res = await http.get(getTokenEndPoint()) expect(res.message.statusCode).toBe(200) }) + + it('check if success status return true, if succeeded', () => { + expect(oidcClient.isSuccessStatusCode(200)).toBeTruthy() + }) + + it('check if success status return false, if failed', () => { + expect(oidcClient.isSuccessStatusCode(400)).toBeFalsy() + }) + + it('check if we get correct ID Token Request url with correct api version', () => { + process.env.ACTIONS_ID_TOKEN_REQUEST_URL = "https://www.example.com/" + expect(oidcClient.getIDTokenUrl()).toBe("https://www.example.com/?api-version=" + oidcClient.getApiVersion()) + }) + + it('check if invalid json throws error', () => { + expect(() => oidcClient.parseJson("{}")).toThrow() + }) + + it('check if invalid json throws error', () => { + expect(oidcClient.parseJson('{"value" : "abc" }')).toBe("abc") + }) }) \ No newline at end of file diff --git a/packages/core/src/oidc-utils.ts b/packages/core/src/oidc-utils.ts index f9b95e6c..ebabf946 100644 --- a/packages/core/src/oidc-utils.ts +++ b/packages/core/src/oidc-utils.ts @@ -68,8 +68,6 @@ export class OidcClient implements IOidcClient { throw new Error(`Failed to get Httpclient `) } - debug(`Httpclient created ${httpclient} `) // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true - let additionalHeaders: IHeaders = {} additionalHeaders[actions_http_client.Headers.ContentType] = actions_http_client.MediaTypes.ApplicationJson additionalHeaders[actions_http_client.Headers.Accept] = actions_http_client.MediaTypes.ApplicationJson @@ -78,13 +76,13 @@ export class OidcClient implements IOidcClient { const data: string = audience !== null ? JSON.stringify({aud: audience}) : '' const response = await httpclient.post(id_token_url, data, additionalHeaders) + const body: string = await response.readBody() if (!this.isSuccessStatusCode(response.message.statusCode)) { throw new Error( - `Failed to get ID Token. Error Code : ${response.message.statusCode} Error message : ${response.message.statusMessage}` + `Failed to get ID Token. \n Error Code : ${response.message.statusCode} Error message : ${response.message.statusMessage} \n Response body: ${body}` ) } - let body: string = await response.readBody() return body }