From a01c9c3dfd3082b922fa88339a535f3057b2c795 Mon Sep 17 00:00:00 2001 From: Radoslaw Krzemien Date: Mon, 27 Mar 2023 12:35:11 +0200 Subject: [PATCH] Add tests Added tests for `verifySignedCommits` workflow See: https://github.com/Expensify/App/issues/13604 --- .github/workflows/verifySignedCommits.yml | 3 +- .../verifySignedCommitsAssertions.js | 27 +++++ .../mocks/verifySignedCommitsMocks.js | 17 +++ workflow_tests/verifySignedCommits.test.js | 106 ++++++++++++++++++ 4 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 workflow_tests/assertions/verifySignedCommitsAssertions.js create mode 100644 workflow_tests/mocks/verifySignedCommitsMocks.js create mode 100644 workflow_tests/verifySignedCommits.test.js diff --git a/.github/workflows/verifySignedCommits.yml b/.github/workflows/verifySignedCommits.yml index e1068e71e041..ee1b0c4c78da 100644 --- a/.github/workflows/verifySignedCommits.yml +++ b/.github/workflows/verifySignedCommits.yml @@ -9,6 +9,7 @@ jobs: verifySignedCommits: runs-on: ubuntu-latest steps: - - uses: Expensify/App/.github/actions/javascript/verifySignedCommits@main + - name: Verify signed commits + uses: Expensify/App/.github/actions/javascript/verifySignedCommits@main with: GITHUB_TOKEN: ${{ github.token }} diff --git a/workflow_tests/assertions/verifySignedCommitsAssertions.js b/workflow_tests/assertions/verifySignedCommitsAssertions.js new file mode 100644 index 000000000000..3ea2b03e47c7 --- /dev/null +++ b/workflow_tests/assertions/verifySignedCommitsAssertions.js @@ -0,0 +1,27 @@ +const utils = require('../utils/utils'); + +const assertVerifySignedCommitsJobExecuted = (workflowResult, didExecute = true) => { + const steps = [ + utils.getStepAssertion( + 'Verify signed commits', + true, + null, + 'VERIFYSIGNEDCOMMITS', + 'Verify signed commits', + [{key: 'GITHUB_TOKEN', value: '***'}], + [], + ), + ]; + + for (const expectedStep of steps) { + if (didExecute) { + expect(workflowResult).toEqual(expect.arrayContaining([expectedStep])); + } else { + expect(workflowResult).not.toEqual(expect.arrayContaining([expectedStep])); + } + } +}; + +module.exports = { + assertVerifySignedCommitsJobExecuted, +}; diff --git a/workflow_tests/mocks/verifySignedCommitsMocks.js b/workflow_tests/mocks/verifySignedCommitsMocks.js new file mode 100644 index 000000000000..e01d89b2b18f --- /dev/null +++ b/workflow_tests/mocks/verifySignedCommitsMocks.js @@ -0,0 +1,17 @@ +const utils = require('../utils/utils'); + +// verifysignedcommits +const VERIFYSIGNEDCOMMITS__VERIFYSIGNEDCOMMITS__VERIFY_SIGNED_COMMITS__STEP_MOCK = utils.getMockStep( + 'Verify signed commits', + 'Verify signed commits', + 'VERIFYSIGNEDCOMMITS', + ['GITHUB_TOKEN'], + [], +); +const VERIFYSIGNEDCOMMITS__VERIFYSIGNEDCOMMITS__STEP_MOCKS = [ + VERIFYSIGNEDCOMMITS__VERIFYSIGNEDCOMMITS__VERIFY_SIGNED_COMMITS__STEP_MOCK, +]; + +module.exports = { + VERIFYSIGNEDCOMMITS__VERIFYSIGNEDCOMMITS__STEP_MOCKS, +}; diff --git a/workflow_tests/verifySignedCommits.test.js b/workflow_tests/verifySignedCommits.test.js new file mode 100644 index 000000000000..ff8ecb7f6785 --- /dev/null +++ b/workflow_tests/verifySignedCommits.test.js @@ -0,0 +1,106 @@ +const path = require('path'); +const kieMockGithub = require('@kie/mock-github'); +const utils = require('./utils/utils'); +const assertions = require('./assertions/verifySignedCommitsAssertions'); +const mocks = require('./mocks/verifySignedCommitsMocks'); +const eAct = require('./utils/ExtendedAct'); + +jest.setTimeout(60 * 1000); +let mockGithub; +const FILES_TO_COPY_INTO_TEST_REPO = [ + { + src: path.resolve(__dirname, '..', '.github', 'actions'), + dest: '.github/actions', + }, + { + src: path.resolve(__dirname, '..', '.github', 'libs'), + dest: '.github/libs', + }, + { + src: path.resolve(__dirname, '..', '.github', 'scripts'), + dest: '.github/scripts', + }, + { + src: path.resolve(__dirname, '..', '.github', 'workflows', 'verifySignedCommits.yml'), + dest: '.github/workflows/verifySignedCommits.yml', + }, +]; + +describe('test workflow verifySignedCommits', () => { + const githubToken = 'dummy_github_token'; + const actor = 'Dummy Actor'; + beforeEach(async () => { + // create a local repository and copy required files + mockGithub = new kieMockGithub.MockGithub({ + repo: { + testVerifySignedCommitsWorkflowRepo: { + files: FILES_TO_COPY_INTO_TEST_REPO, + }, + }, + }); + + await mockGithub.setup(); + }); + + afterEach(async () => { + await mockGithub.teardown(); + }); + describe('pull request opened', () => { + const event = 'pull_request'; + const eventOptions = { + action: 'opened', + }; + it('test stub', async () => { + const repoPath = mockGithub.repo.getPath('testVerifySignedCommitsWorkflowRepo') || ''; + const workflowPath = path.join(repoPath, '.github', 'workflows', 'verifySignedCommits.yml'); + let act = new eAct.ExtendedAct(repoPath, workflowPath); + act = utils.setUpActParams( + act, + event, + eventOptions, + {}, + githubToken, + ); + const testMockSteps = { + verifySignedCommits: mocks.VERIFYSIGNEDCOMMITS__VERIFYSIGNEDCOMMITS__STEP_MOCKS, + }; + const result = await act + .runEvent(event, { + workflowFile: path.join(repoPath, '.github', 'workflows'), + mockSteps: testMockSteps, + actor, + }); + + assertions.assertVerifySignedCommitsJobExecuted(result); + }); + }); + describe('pull request synchronized', () => { + const event = 'pull_request'; + const eventOptions = { + action: 'synchronize', + }; + it('test stub', async () => { + const repoPath = mockGithub.repo.getPath('testVerifySignedCommitsWorkflowRepo') || ''; + const workflowPath = path.join(repoPath, '.github', 'workflows', 'verifySignedCommits.yml'); + let act = new eAct.ExtendedAct(repoPath, workflowPath); + act = utils.setUpActParams( + act, + event, + eventOptions, + {}, + githubToken, + ); + const testMockSteps = { + verifySignedCommits: mocks.VERIFYSIGNEDCOMMITS__VERIFYSIGNEDCOMMITS__STEP_MOCKS, + }; + const result = await act + .runEvent(event, { + workflowFile: path.join(repoPath, '.github', 'workflows'), + mockSteps: testMockSteps, + actor, + }); + + assertions.assertVerifySignedCommitsJobExecuted(result); + }); + }); +});