From a370ecab04a59c4a50ba6e92b9d321532a53e240 Mon Sep 17 00:00:00 2001 From: Yogesh Lonkar Date: Thu, 15 Jul 2021 13:17:05 +0200 Subject: [PATCH] fix unit test Signed-off-by: Yogesh Lonkar Signed-off-by: Andrea Falzetti --- .../builtin/ci/githubActionsDispatch.test.ts | 157 +++++++++++------- .../builtin/ci/githubActionsDispatch.ts | 3 +- 2 files changed, 97 insertions(+), 63 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/ci/githubActionsDispatch.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/ci/githubActionsDispatch.test.ts index 606881e47a..6564671664 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/ci/githubActionsDispatch.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/ci/githubActionsDispatch.test.ts @@ -13,16 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +// @ts-nocheck -jest.mock('@octokit/rest'); jest.mock('@backstage/integration'); +jest.mock('@octokit/rest', () => ({ Octokit: jest.fn() })); import { createGithubActionsDispatchAction } from './githubActionsDispatch'; -import { ScmIntegrations } from '@backstage/integration'; +import { + ScmIntegrations, + GithubCredentialsProvider, +} from '@backstage/integration'; import { ConfigReader } from '@backstage/config'; import { getVoidLogger } from '@backstage/backend-common'; import { PassThrough } from 'stream'; -// import { when } from 'jest-when'; + +import { Octokit } from '@octokit/rest'; describe('ci:github-actions-dispatch', () => { const config = new ConfigReader({ @@ -34,9 +39,6 @@ describe('ci:github-actions-dispatch', () => { }, }); - const integrations = ScmIntegrations.fromConfig(config); - const action = createGithubActionsDispatchAction({ integrations, config }); - const mockContext = { input: { owner: 'a-owner', @@ -52,67 +54,101 @@ describe('ci:github-actions-dispatch', () => { createTemporaryDirectory: jest.fn(), }; - const { mockGithubClient } = require('@octokit/rest'); - mockGithubClient.rest = { - actions: { - createWorkflowDispatch: jest.fn(), - }, - }; - beforeEach(() => { jest.resetAllMocks(); }); - // it('should throw if there is no integration config provided', async () => { - // const actionWithNoIntegrations = createGithubActionsDispatchAction({ - // integrations: { - // ...integrations, - // github: { - // list: () => [], - // byHost: jest.fn(), - // byUrl: jest.fn(), - // }, - // }, - // config, - // }); - // await expect(actionWithNoIntegrations.handler(mockContext)).rejects.toThrow( - // /No matching integration configuration/, - // ); - // }); + it('should throw if there is no integration config provided', async () => { + const integrations = { + github: { + list: () => [], + byHost: jest.fn(), + byUrl: jest.fn(), + }, + } as ScmIntegrations; + const actionWithNoIntegrations = createGithubActionsDispatchAction({ + integrations, + config, + }); + await expect(actionWithNoIntegrations.handler(mockContext)).rejects.toThrow( + /No matching integration configuration/, + ); + }); - // it('should throw if there is no token in the integration config that is returned', async () => { - // const mockedProvider = jest.fn(); - // const mockedIntegrationConfig = jest.fn(); - // GithubCredentialsProvider.create.mockReturnOnce(mockedProvider); - // const mockedArray = [ - // { - // config: { - // host: 'github.com', - // }, - // }, - // ] as GitHubIntegration[]; - // const integrations1 = { - // github: { - // list: () => mockedArray, - // byHost: mockedIntegrationConfig, - // byUrl: jest.fn(), - // }, - // } as ScmIntegrationRegistry; - // const actionWithNoIntegrations = createGithubActionsDispatchAction({ - // integrations: integrations1, - // config, - // }); - // mockedProvider.mockResolvedValue(undefined); - - // await expect(actionWithNoIntegrations.handler(mockContext)).rejects.toThrow( - // /No token available for host/, - // ); - // }); + it('should throw if no token is provided', async () => { + const integrations = { + github: { + list: () => [ + { + config: { + host: 'github.com', + }, + }, + ], + byHost: () => ({ + config: { + apiBaseUrl: 'https://api.github.com', + }, + }), + byUrl: jest.fn(), + }, + } as ScmIntegrations; + const mockedCredentialsProvider = { + getCredentials: jest.fn(), + }; + mockedCredentialsProvider.getCredentials.mockResolvedValue({}); + GithubCredentialsProvider.create.mockReturnValueOnce( + mockedCredentialsProvider, + ); + const actionWithNoIntegrations = createGithubActionsDispatchAction({ + integrations, + config, + }); + await expect(actionWithNoIntegrations.handler(mockContext)).rejects.toThrow( + /No token available for host/, + ); + }); it('should call the githubApis for creating WorkflowDispatch', async () => { - mockGithubClient.rest.actions.createWorkflowDispatch.mockResolvedValue({ + const integrations = { + github: { + list: () => [ + { + config: { + host: 'github.com', + }, + }, + ], + byHost: () => ({ + config: { + apiBaseUrl: 'https://api.github.com', + }, + }), + byUrl: jest.fn(), + }, + } as ScmIntegrations; + const mockedCredentialsProvider = { + getCredentials: jest.fn(), + }; + mockedCredentialsProvider.getCredentials.mockResolvedValue({ + token: 'test-token', + }); + GithubCredentialsProvider.create.mockReturnValueOnce( + mockedCredentialsProvider, + ); + const mockedCreateWorkflowDispatch = jest.fn(); + mockedCreateWorkflowDispatch.mockResolvedValue({ message: 'Success', }); + Octokit.mockImplementation(() => { + return { + rest: { + actions: { + createWorkflowDispatch: mockedCreateWorkflowDispatch, + }, + }, + }; + }); const owner = 'dx'; const repoName = 'test1'; const workflowId = 'dispatch_workflow'; @@ -120,11 +156,10 @@ describe('ci:github-actions-dispatch', () => { const ctx = Object.assign({}, mockContext, { input: { owner, repoName, workflowId, branchOrTagName }, }); + const action = createGithubActionsDispatchAction({ integrations, config }); await action.handler(ctx); - expect( - mockGithubClient.rest.actions.createWorkflowDispatch, - ).toHaveBeenCalledWith({ + expect(mockedCreateWorkflowDispatch).toHaveBeenCalledWith({ owner, repo: repoName, workflow_id: workflowId, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/ci/githubActionsDispatch.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/ci/githubActionsDispatch.ts index fbeeb4c9ac..e720695b13 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/ci/githubActionsDispatch.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/ci/githubActionsDispatch.ts @@ -104,7 +104,6 @@ export function createGithubActionsDispatchAction(options: { `No token available for host: ${host}, with owner ${owner}, and repo ${repoName}`, ); } - const client = new Octokit({ auth: ctx.token ?? token, baseUrl: integrationConfig.config.apiBaseUrl, @@ -113,7 +112,7 @@ export function createGithubActionsDispatchAction(options: { await client.rest.actions.createWorkflowDispatch({ owner, - repo, + repo: repoName, workflow_id: workflowId, ref: branchOrTagName, });