Skip to content

Commit

Permalink
do not depend jest-circus directly
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Sep 22, 2023
1 parent e66bbd2 commit 1f728f5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 63 deletions.
10 changes: 1 addition & 9 deletions action/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid",
"parser": "typescript"
"printWidth": 120
}
90 changes: 46 additions & 44 deletions action/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,87 @@
import * as os from 'os';
import * as fs from 'fs';
import * as path from 'path';
import * as exec from '@actions/exec';
import * as io from '@actions/io';
import * as child_process from 'child_process';
import * as index from '../src/index';
import * as os from "os";
import * as fs from "fs";
import * as path from "path";
import * as exec from "@actions/exec";
import * as io from "@actions/io";
import * as child_process from "child_process";
import * as index from "../src/index";

const sep = path.sep;

jest.mock("@actions/core");

// extension of executable files
const binExt = os.platform() === 'win32' ? '.exe' : '';
const binExt = os.platform() === "win32" ? ".exe" : "";

process.env.GITHUB_REPOSITORY = 'shogo82148/actions-aws-assume-role';
process.env.GITHUB_WORKFLOW = 'test';
process.env.GITHUB_RUN_ID = '1234567890';
process.env.GITHUB_ACTOR = 'shogo82148';
process.env.GITHUB_SHA = 'e3a45c6c16c1464826b36a598ff39e6cc98c4da4';
process.env.GITHUB_REF = 'ref/heads/main';
process.env.GITHUB_REPOSITORY = "shogo82148/actions-aws-assume-role";
process.env.GITHUB_WORKFLOW = "test";
process.env.GITHUB_RUN_ID = "1234567890";
process.env.GITHUB_ACTOR = "shogo82148";
process.env.GITHUB_SHA = "e3a45c6c16c1464826b36a598ff39e6cc98c4da4";
process.env.GITHUB_REF = "ref/heads/main";

describe('tests', () => {
let tmpdir = '';
describe("tests", () => {
let tmpdir = "";
let subprocess: child_process.ChildProcess;
beforeAll(async () => {
tmpdir = await mkdtemp();
const bin = `${tmpdir}${sep}dummy${binExt}`;

console.log('compiling dummy server');
console.log("compiling dummy server");
await exec.exec(
'go',
['build', '-o', bin, 'github.com/fuller-inc/actions-aws-assume-role/provider/assume-role/cmd/dummy'],
"go",
["build", "-o", bin, "github.com/fuller-inc/actions-aws-assume-role/provider/assume-role/cmd/dummy"],
{
cwd: `..${sep}provider${sep}assume-role`
}
cwd: `..${sep}provider${sep}assume-role`,
},
);

console.log('starting dummy server');
console.log("starting dummy server");
subprocess = child_process.spawn(bin, [], {
detached: true,
stdio: 'ignore'
stdio: "ignore",
});
await sleep(1); // wait for starting process
}, 5 * 60000);

afterAll(async () => {
console.log('killing dummy server');
subprocess?.kill('SIGTERM');
console.log("killing dummy server");
subprocess?.kill("SIGTERM");
await sleep(1); // wait for stopping process
await io.rmRF(tmpdir);
});

it('succeed', async () => {
it("succeed", async () => {
await index.assumeRole({
githubToken: 'ghs_dummyGitHubToken',
awsRegion: 'us-east-1',
roleToAssume: 'arn:aws:iam::123456789012:role/assume-role-test',
githubToken: "ghs_dummyGitHubToken",
awsRegion: "us-east-1",
roleToAssume: "arn:aws:iam::123456789012:role/assume-role-test",
roleDurationSeconds: 900,
roleSessionName: 'GitHubActions',
roleSessionName: "GitHubActions",
roleSessionTagging: true,
providerEndpoint: 'http://localhost:8080',
providerEndpoint: "http://localhost:8080",
useNodeId: false,
obfuscateRepository: ''
obfuscateRepository: "",
});
expect(process.env.AWS_ACCESS_KEY_ID).toBe('AKIAIOSFODNN7EXAMPLE');
expect(process.env.AWS_SECRET_ACCESS_KEY).toBe('wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY');
expect(process.env.AWS_SESSION_TOKEN).toBe('session-token');
expect(process.env.AWS_DEFAULT_REGION).toBe('us-east-1');
expect(process.env.AWS_REGION).toBe('us-east-1');
expect(process.env.AWS_ACCESS_KEY_ID).toBe("AKIAIOSFODNN7EXAMPLE");
expect(process.env.AWS_SECRET_ACCESS_KEY).toBe("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY");
expect(process.env.AWS_SESSION_TOKEN).toBe("session-token");
expect(process.env.AWS_DEFAULT_REGION).toBe("us-east-1");
expect(process.env.AWS_REGION).toBe("us-east-1");
});

it('invalid GitHub Token', async () => {
it("invalid GitHub Token", async () => {
await expect(async () => {
await index.assumeRole({
githubToken: 'ghp_dummyPersonalGitHubToken',
awsRegion: 'us-east-1',
roleToAssume: 'arn:aws:iam::123456789012:role/assume-role-test',
githubToken: "ghp_dummyPersonalGitHubToken",
awsRegion: "us-east-1",
roleToAssume: "arn:aws:iam::123456789012:role/assume-role-test",
roleDurationSeconds: 900,
roleSessionName: 'GitHubActions',
roleSessionName: "GitHubActions",
roleSessionTagging: true,
providerEndpoint: 'http://localhost:8080',
providerEndpoint: "http://localhost:8080",
useNodeId: false,
obfuscateRepository: ''
obfuscateRepository: "",
});
}).rejects.toThrow();
});
Expand Down
13 changes: 6 additions & 7 deletions action/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
moduleFileExtensions: ["js", "ts"],
testEnvironment: "node",
testMatch: ["**/*.test.ts"],
transform: {
'^.+\\.ts$': 'ts-jest'
"^.+\\.ts$": "ts-jest",
},
verbose: true
}
verbose: true,
};
1 change: 0 additions & 1 deletion action/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@types/jest": "^29.5.5",
"@types/node": "^20.5.9",
"jest": "^29.7.0",
"jest-circus": "^29.6.0",
"prettier": "^3.0.3",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
Expand Down
5 changes: 4 additions & 1 deletion provider/assume-role/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ func (c *githubClientDummy) GetUser(ctx context.Context, nextIDFormat bool, toke
}

func (c *githubClientDummy) ParseIDToken(ctx context.Context, idToken string) (*github.ActionsIDToken, error) {
return nil, errors.New("invalid jwt")
if idToken != "dummyGitHubIDToken" {
return nil, errors.New("invalid id token")
}
return &github.ActionsIDToken{}, nil
}

func (c *githubClientDummy) ValidateAPIURL(url string) error {
Expand Down

0 comments on commit 1f728f5

Please sign in to comment.