1
0
Fork 0

comments resolved

pull/887/head
Sourav Chanduka 2021-08-10 15:36:13 +05:30
parent aa1968c9e9
commit 5d9c674092
3 changed files with 37 additions and 5 deletions

View File

@ -232,7 +232,6 @@ async function getIDTokenAction(): Promise<void> {
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)

View File

@ -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")
})
})

View File

@ -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
}