From 039779492c5d62af958e725a8301636279fd9526 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Mon, 30 Jan 2023 03:08:12 +0100 Subject: [PATCH] test: githubActionsRuntimeToken Signed-off-by: CrazyMax --- __tests__/fixtures/runtimeToken.txt | 1 + __tests__/github.test.ts | 27 +++++++++++++++++++++++++++ src/github.ts | 7 ++++--- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 __tests__/fixtures/runtimeToken.txt diff --git a/__tests__/fixtures/runtimeToken.txt b/__tests__/fixtures/runtimeToken.txt new file mode 100644 index 0000000..6319103 --- /dev/null +++ b/__tests__/fixtures/runtimeToken.txt @@ -0,0 +1 @@ +eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImVCWl9jbjNzWFlBZDBjaDRUSEJLSElnT3dPRSJ9.eyJuYW1laWQiOiJkZGRkZGRkZC1kZGRkLWRkZGQtZGRkZC1kZGRkZGRkZGRkZGQiLCJzY3AiOiJBY3Rpb25zLkdlbmVyaWNSZWFkOjAwMDAwMDAwLTAwMDAtMDAwMC0wMDAwLTAwMDAwMDAwMDAwMCBBY3Rpb25zLlVwbG9hZEFydGlmYWN0czowMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAvMTpCdWlsZC9CdWlsZC85MDIgTG9jYXRpb25TZXJ2aWNlLkNvbm5lY3QgUmVhZEFuZFVwZGF0ZUJ1aWxkQnlVcmk6MDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwLzE6QnVpbGQvQnVpbGQvOTAyIiwiSWRlbnRpdHlUeXBlQ2xhaW0iOiJTeXN0ZW06U2VydmljZUlkZW50aXR5IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvc2lkIjoiREREREREREQtRERERC1ERERELUREREQtREREREREREREREREIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9wcmltYXJ5c2lkIjoiZGRkZGRkZGQtZGRkZC1kZGRkLWRkZGQtZGRkZGRkZGRkZGRkIiwiYXVpIjoiN2U0MGNlZWQtNWQxMi00OTRkLWI2M2QtOGVhMGQ2ZGRhMzRkIiwic2lkIjoiMmFlM2JkOTgtN2Q4NC00MGQxLWE1YjEtMWM5NTY1MmY5OTRjIiwiYWMiOiJbe1wiU2NvcGVcIjpcInJlZnMvaGVhZHMvbWFzdGVyXCIsXCJQZXJtaXNzaW9uXCI6M31dIiwiYWNzbCI6IjEwIiwib3JjaGlkIjoiNmViYTgwMjktZDliNy00ZGJkLWJkNzItM2I3ODFhZjYxNzkwLmJ1aWxkLl9fZGVmYXVsdCIsImlzcyI6InZzdG9rZW4uYWN0aW9ucy5naXRodWJ1c2VyY29udGVudC5jb20iLCJhdWQiOiJ2c3Rva2VuLmFjdGlvbnMuZ2l0aHVidXNlcmNvbnRlbnQuY29tfHZzbzo1YWQwMGYyMy05ZmEwLTRiZGEtYjQyNi1mMWIyNzg5MWM5MTkiLCJuYmYiOjE2NzUwNDE1NjcsImV4cCI6MTY3NTA2NDM2N30.lWJuHGC7wQ9pgHXs4dMMSsflUUi6zasOzH8rz4EFvZfIlzyMcP1sZQeySNGAZ9z91JkpREzRPHFOCTp0hM9MbN6jcA05zvhSXiEE5cZikm_z32g0c5um3F3LneHwOWZeM_Lw29FBMnswuCaljvCi9p50M4wRbtqJpoYfIwvm5-nLcBbSrQZcMfPEfVXHSBCUqdoiVAunXsUrnCsKURL5T_UJIBUYBGLUrZqf2m8YnO7Wg_Q11wSz33W0gDR4_PdnOiJY1hL0vAOiRbNG0RLPA0A2QCmvlwdJOWJKOS4GgWoRr-3_PQYNy5We70CpZTpVvHtx5PQ9l65qTASNmVeg4Q \ No newline at end of file diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index b8b32a0..e24ad32 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -1,4 +1,6 @@ import {describe, expect, jest, it, beforeEach, afterEach} from '@jest/globals'; +import * as fs from 'fs'; +import * as path from 'path'; import {GitHub, GitHubRepo} from '../src/github'; @@ -45,6 +47,31 @@ describe('serverURL', () => { }); }); +describe('actionsRuntimeToken', () => { + const originalEnv = process.env; + beforeEach(() => { + jest.resetModules(); + process.env = { + ...originalEnv + }; + }); + afterEach(() => { + process.env = originalEnv; + }); + it('empty', async () => { + process.env.ACTIONS_RUNTIME_TOKEN = ''; + const github = new GitHub(); + expect(github.actionsRuntimeToken).toEqual({}); + }); + it('fixture', async () => { + process.env.ACTIONS_RUNTIME_TOKEN = fs.readFileSync(path.join(__dirname, 'fixtures', 'runtimeToken.txt')).toString().trim(); + const github = new GitHub(); + const runtimeToken = github.actionsRuntimeToken; + expect(runtimeToken.ac).toEqual('[{"Scope":"refs/heads/master","Permission":3}]'); + expect(runtimeToken.iss).toEqual('vstoken.actions.githubusercontent.com'); + }); +}); + describe('repoData', () => { it('returns GitHub repository', async () => { const github = new GitHub(); diff --git a/src/github.ts b/src/github.ts index 569f683..c84857b 100644 --- a/src/github.ts +++ b/src/github.ts @@ -6,7 +6,7 @@ import {Context} from '@actions/github/lib/context'; export type GitHubRepo = OctoOpenApiTypes['schemas']['repository']; -export interface GitHubRuntimeToken extends JwtPayload { +export interface GitHubActionsRuntimeToken extends JwtPayload { ac?: string; } @@ -30,8 +30,9 @@ export class GitHub { return process.env.GITHUB_SERVER_URL || 'https://github.com'; } - get runtimeToken(): GitHubRuntimeToken { - return jwt_decode(`${process.env['ACTIONS_RUNTIME_TOKEN']}`); + get actionsRuntimeToken(): GitHubActionsRuntimeToken { + const token = process.env['ACTIONS_RUNTIME_TOKEN'] || ''; + return token ? jwt_decode(token) : {}; } public repoData(): Promise {