2023-01-31 10:34:51 +08:00
|
|
|
/**
|
|
|
|
* Copyright 2023 actions-toolkit authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-01-30 09:31:09 +08:00
|
|
|
import {describe, expect, jest, it, beforeEach, afterEach} from '@jest/globals';
|
2023-01-30 10:08:12 +08:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2023-02-03 10:33:22 +08:00
|
|
|
import * as core from '@actions/core';
|
2023-01-30 09:31:09 +08:00
|
|
|
|
2023-02-01 20:06:20 +08:00
|
|
|
import {GitHub} from '../src/github';
|
|
|
|
import {GitHubRepo} from '../src/types/github';
|
2023-01-30 09:31:09 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2023-02-04 10:03:11 +08:00
|
|
|
import repoFixture from './fixtures/github-repo.json';
|
2023-01-30 09:31:09 +08:00
|
|
|
jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRepo> => {
|
|
|
|
return <Promise<GitHubRepo>>(repoFixture as unknown);
|
|
|
|
});
|
|
|
|
|
2023-02-01 08:42:02 +08:00
|
|
|
describe('repoData', () => {
|
|
|
|
it('returns GitHub repository', async () => {
|
|
|
|
const github = new GitHub();
|
|
|
|
expect((await github.repoData()).name).toEqual('Hello-World');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-30 09:31:09 +08:00
|
|
|
describe('context', () => {
|
|
|
|
it('returns repository name from payload', async () => {
|
2023-02-01 08:42:02 +08:00
|
|
|
expect(GitHub.context.payload.repository?.name).toEqual('test-docker-action');
|
2023-01-30 09:31:09 +08:00
|
|
|
});
|
|
|
|
it('is repository private', async () => {
|
2023-02-01 08:42:02 +08:00
|
|
|
expect(GitHub.context.payload.repository?.private).toEqual(true);
|
2023-01-30 09:31:09 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('serverURL', () => {
|
|
|
|
const originalEnv = process.env;
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetModules();
|
|
|
|
process.env = {
|
|
|
|
...originalEnv,
|
|
|
|
GITHUB_SERVER_URL: 'https://foo.github.com'
|
|
|
|
};
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
process.env = originalEnv;
|
|
|
|
});
|
|
|
|
it('returns default', async () => {
|
|
|
|
process.env.GITHUB_SERVER_URL = '';
|
2023-02-01 08:42:02 +08:00
|
|
|
expect(GitHub.serverURL).toEqual('https://github.com');
|
2023-01-30 09:31:09 +08:00
|
|
|
});
|
|
|
|
it('returns from env', async () => {
|
2023-02-01 08:42:02 +08:00
|
|
|
expect(GitHub.serverURL).toEqual('https://foo.github.com');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('apiURL', () => {
|
|
|
|
const originalEnv = process.env;
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetModules();
|
|
|
|
process.env = {
|
|
|
|
...originalEnv,
|
|
|
|
GITHUB_API_URL: 'https://bar.github.com'
|
|
|
|
};
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
process.env = originalEnv;
|
|
|
|
});
|
|
|
|
it('returns default', async () => {
|
|
|
|
process.env.GITHUB_API_URL = '';
|
|
|
|
expect(GitHub.apiURL).toEqual('https://api.github.com');
|
|
|
|
});
|
|
|
|
it('returns from env', async () => {
|
|
|
|
expect(GitHub.apiURL).toEqual('https://bar.github.com');
|
2023-01-30 09:31:09 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-30 10:08:12 +08:00
|
|
|
describe('actionsRuntimeToken', () => {
|
|
|
|
const originalEnv = process.env;
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetModules();
|
|
|
|
process.env = {
|
|
|
|
...originalEnv
|
|
|
|
};
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
process.env = originalEnv;
|
|
|
|
});
|
|
|
|
it('empty', async () => {
|
|
|
|
process.env.ACTIONS_RUNTIME_TOKEN = '';
|
2023-02-03 11:37:26 +08:00
|
|
|
expect(GitHub.actionsRuntimeToken).toBeUndefined();
|
|
|
|
});
|
|
|
|
it('malformed', async () => {
|
|
|
|
process.env.ACTIONS_RUNTIME_TOKEN = 'foo';
|
|
|
|
expect(() => {
|
|
|
|
GitHub.actionsRuntimeToken;
|
2023-03-27 02:58:13 +08:00
|
|
|
}).toThrow();
|
2023-01-30 10:08:12 +08:00
|
|
|
});
|
|
|
|
it('fixture', async () => {
|
|
|
|
process.env.ACTIONS_RUNTIME_TOKEN = fs.readFileSync(path.join(__dirname, 'fixtures', 'runtimeToken.txt')).toString().trim();
|
2023-02-01 08:42:02 +08:00
|
|
|
const runtimeToken = GitHub.actionsRuntimeToken;
|
2023-02-03 11:09:04 +08:00
|
|
|
expect(runtimeToken?.ac).toEqual('[{"Scope":"refs/heads/master","Permission":3}]');
|
|
|
|
expect(runtimeToken?.iss).toEqual('vstoken.actions.githubusercontent.com');
|
2023-01-30 10:08:12 +08:00
|
|
|
});
|
|
|
|
});
|
2023-02-03 10:33:22 +08:00
|
|
|
|
2023-02-03 11:09:04 +08:00
|
|
|
describe('printActionsRuntimeTokenACs', () => {
|
2023-02-03 10:33:22 +08:00
|
|
|
const originalEnv = process.env;
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetModules();
|
|
|
|
process.env = {
|
|
|
|
...originalEnv
|
|
|
|
};
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
process.env = originalEnv;
|
|
|
|
});
|
|
|
|
it('empty', async () => {
|
|
|
|
process.env.ACTIONS_RUNTIME_TOKEN = '';
|
2023-03-27 02:58:13 +08:00
|
|
|
await expect(GitHub.printActionsRuntimeTokenACs()).rejects.toThrow(new Error('ACTIONS_RUNTIME_TOKEN not set'));
|
2023-02-03 11:37:26 +08:00
|
|
|
});
|
|
|
|
it('malformed', async () => {
|
|
|
|
process.env.ACTIONS_RUNTIME_TOKEN = 'foo';
|
2023-03-27 02:58:13 +08:00
|
|
|
await expect(GitHub.printActionsRuntimeTokenACs()).rejects.toThrow(new Error("Cannot parse GitHub Actions Runtime Token: Invalid token specified: Cannot read properties of undefined (reading 'replace')"));
|
2023-02-03 10:33:22 +08:00
|
|
|
});
|
2023-02-03 11:09:04 +08:00
|
|
|
it('refs/heads/master', async () => {
|
2023-02-03 11:37:26 +08:00
|
|
|
const infoSpy = jest.spyOn(core, 'info');
|
2023-02-03 10:33:22 +08:00
|
|
|
process.env.ACTIONS_RUNTIME_TOKEN = fs.readFileSync(path.join(__dirname, 'fixtures', 'runtimeToken.txt')).toString().trim();
|
2023-02-03 11:09:04 +08:00
|
|
|
await GitHub.printActionsRuntimeTokenACs();
|
2023-02-03 11:37:26 +08:00
|
|
|
expect(infoSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(infoSpy).toHaveBeenCalledWith(`refs/heads/master: read/write`);
|
2023-02-03 10:33:22 +08:00
|
|
|
});
|
|
|
|
});
|