From 995d0249e9e202bce35a05c4c2218fab2933640f Mon Sep 17 00:00:00 2001 From: Kevin Snyder Date: Mon, 7 Apr 2025 11:56:16 -0700 Subject: [PATCH] Add tests Signed-off-by: Kevin Snyder --- .../src/actions/github.test.ts | 22 ++++++++--- .../src/actions/githubActionsDispatch.test.ts | 22 ++++++++--- .../src/actions/githubAutolinks.test.ts | 37 ++++++++++++++++--- .../actions/githubBranchProtection.test.ts | 24 +++++++++--- .../src/actions/githubDeployKey.test.ts | 23 +++++++++--- .../src/actions/githubEnvironment.test.ts | 24 +++++++++--- .../src/actions/githubIssuesLabel.test.ts | 22 ++++++++--- .../src/actions/githubPagesEnable.test.ts | 23 +++++++++--- .../src/actions/githubRepoCreate.test.ts | 22 ++++++++--- .../src/actions/githubRepoPush.test.ts | 23 +++++++++--- .../src/actions/githubWebhook.test.ts | 22 ++++++++--- 11 files changed, 208 insertions(+), 56 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts index 30945d92f2..97867226c6 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.test.ts @@ -54,6 +54,9 @@ const initRepoAndPushMocked = initRepoAndPush as jest.Mock< Promise<{ commitHash: string }> >; +import { Octokit } from 'octokit'; + +const octokitMock = Octokit as unknown as jest.Mock; const mockOctokit = { rest: { users: { @@ -81,11 +84,7 @@ const mockOctokit = { request: jest.fn(), }; jest.mock('octokit', () => ({ - Octokit: class { - constructor() { - return mockOctokit; - } - }, + Octokit: jest.fn(), })); describe('publish:github', () => { @@ -114,6 +113,7 @@ describe('publish:github', () => { }); beforeEach(() => { + octokitMock.mockImplementation(() => mockOctokit); initRepoAndPushMocked.mockResolvedValue({ commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6', }); @@ -139,6 +139,18 @@ describe('publish:github', () => { afterEach(jest.resetAllMocks); + it('should pass context logger to Octokit client', async () => { + try { + await action.handler(mockContext); + } catch (e) { + // no-op + } + + expect(octokitMock).toHaveBeenCalledWith( + expect.objectContaining({ log: mockContext.logger }), + ); + }); + it('should fail to create if the team is not found in the org', async () => { mockOctokit.rest.users.getByUsername.mockResolvedValue({ data: { type: 'Organization' }, diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.test.ts index 7f0d9b0490..6924d909d9 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.test.ts @@ -24,6 +24,9 @@ import { TemplateAction } from '@backstage/plugin-scaffolder-node'; import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; import { createGithubActionsDispatchAction } from './githubActionsDispatch'; +import { Octokit } from 'octokit'; + +const octokitMock = Octokit as unknown as jest.Mock; const mockOctokit = { rest: { actions: { @@ -32,11 +35,7 @@ const mockOctokit = { }, }; jest.mock('octokit', () => ({ - Octokit: class { - constructor() { - return mockOctokit; - } - }, + Octokit: jest.fn(), })); describe('github:actions:dispatch', () => { @@ -63,6 +62,7 @@ describe('github:actions:dispatch', () => { beforeEach(() => { jest.resetAllMocks(); + octokitMock.mockImplementation(() => mockOctokit); githubCredentialsProvider = DefaultGithubCredentialsProvider.fromIntegrations(integrations); action = createGithubActionsDispatchAction({ @@ -71,6 +71,18 @@ describe('github:actions:dispatch', () => { }); }); + it('should pass context logger to Octokit client', async () => { + try { + await action.handler(mockContext); + } catch (e) { + // no-op + } + + expect(octokitMock).toHaveBeenCalledWith( + expect.objectContaining({ log: mockContext.logger }), + ); + }); + it('should call the githubApis for creating WorkflowDispatch without an input object', async () => { mockOctokit.rest.actions.createWorkflowDispatch.mockResolvedValue({ data: { diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.test.ts index 56115c156c..0c63376a15 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.test.ts @@ -25,6 +25,9 @@ import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test- import { TemplateAction } from '@backstage/plugin-scaffolder-node'; import { createGithubAutolinksAction } from './githubAutolinks'; +import { Octokit } from 'octokit'; + +const octokitMock = Octokit as unknown as jest.Mock; const mockOctokit = { rest: { repos: { @@ -33,11 +36,7 @@ const mockOctokit = { }, }; jest.mock('octokit', () => ({ - Octokit: class { - constructor() { - return mockOctokit; - } - }, + Octokit: jest.fn(), })); describe('github:autolinks:create', () => { @@ -55,6 +54,34 @@ describe('github:autolinks:create', () => { let action: TemplateAction; const workspacePath = createMockDirectory().resolve('workspace'); + it('should pass context logger to Octokit client', async () => { + githubCredentialsProvider = + DefaultGithubCredentialsProvider.fromIntegrations(integrations); + action = createGithubAutolinksAction({ + integrations, + githubCredentialsProvider, + }); + + octokitMock.mockImplementation(() => mockOctokit); + + const mockContext = createMockActionContext({ + input: { + repoUrl: 'github.com?repo=repo&owner=owner', + }, + workspacePath, + }); + + try { + await action.handler(mockContext); + } catch (e) { + // no-op + } + + expect(octokitMock).toHaveBeenCalledWith( + expect.objectContaining({ log: mockContext.logger }), + ); + }); + it('should call the githubApis for creating alphanumeric autolink reference', async () => { githubCredentialsProvider = DefaultGithubCredentialsProvider.fromIntegrations(integrations); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.test.ts index 8e672af725..5f26b9d298 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.test.ts @@ -20,6 +20,9 @@ import { TemplateAction } from '@backstage/plugin-scaffolder-node'; import { ConfigReader } from '@backstage/config'; import { ScmIntegrations } from '@backstage/integration'; +import { Octokit } from 'octokit'; + +const octokitMock = Octokit as unknown as jest.Mock; const mockOctokit = { rest: { repos: { @@ -29,13 +32,8 @@ const mockOctokit = { }, }, }; - jest.mock('octokit', () => ({ - Octokit: class { - constructor() { - return mockOctokit; - } - }, + Octokit: jest.fn(), })); describe('github:branch-protection:create', () => { @@ -59,6 +57,8 @@ describe('github:branch-protection:create', () => { }); beforeEach(() => { + octokitMock.mockImplementation(() => mockOctokit); + mockOctokit.rest.repos.get.mockResolvedValue({ data: { default_branch: 'master', @@ -72,6 +72,18 @@ describe('github:branch-protection:create', () => { afterEach(jest.resetAllMocks); + it('should pass context logger to Octokit client', async () => { + try { + await action.handler(mockContext); + } catch (e) { + // no-op + } + + expect(octokitMock).toHaveBeenCalledWith( + expect.objectContaining({ log: mockContext.logger }), + ); + }); + it('should work with default params', async () => { await action.handler(mockContext); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.test.ts index 798aa7705e..e1e7885a15 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.test.ts @@ -20,6 +20,9 @@ import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test- import { ConfigReader } from '@backstage/config'; import { ScmIntegrations } from '@backstage/integration'; +import { Octokit } from 'octokit'; + +const octokitMock = Octokit as unknown as jest.Mock; const mockOctokit = { rest: { actions: { @@ -32,11 +35,7 @@ const mockOctokit = { }, }; jest.mock('octokit', () => ({ - Octokit: class { - constructor() { - return mockOctokit; - } - }, + Octokit: jest.fn(), })); const publicKey = '2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvvcCU='; @@ -66,11 +65,25 @@ describe('github:deployKey:create', () => { beforeEach(() => { jest.resetAllMocks(); + octokitMock.mockImplementation(() => mockOctokit); + action = createGithubDeployKeyAction({ integrations, }); }); + it('should pass context logger to Octokit client', async () => { + try { + await action.handler(mockContext); + } catch (e) { + // no-op + } + + expect(octokitMock).toHaveBeenCalledWith( + expect.objectContaining({ log: mockContext.logger }), + ); + }); + it('should work happy path', async () => { mockOctokit.rest.actions.getRepoPublicKey.mockResolvedValue({ data: { diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.test.ts index 1a4ffa5ea4..ebef47a06b 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.test.ts @@ -22,6 +22,10 @@ import { ScmIntegrations } from '@backstage/integration'; import { CatalogApi } from '@backstage/catalog-client'; import { mockCredentials, mockServices } from '@backstage/backend-test-utils'; +import { Octokit } from 'octokit'; + +const octokitMock = Octokit as unknown as jest.Mock; + const mockOctokit = { rest: { actions: { @@ -48,11 +52,7 @@ const mockCatalogClient: Partial = { }; jest.mock('octokit', () => ({ - Octokit: class { - constructor() { - return mockOctokit; - } - }, + Octokit: jest.fn(), })); jest.mock('@backstage/catalog-client', () => ({ @@ -91,6 +91,8 @@ describe('github:environment:create', () => { }); beforeEach(() => { + octokitMock.mockImplementation(() => mockOctokit); + mockOctokit.rest.actions.getEnvironmentPublicKey.mockResolvedValue({ data: { key: publicKey, @@ -138,6 +140,18 @@ describe('github:environment:create', () => { afterEach(jest.resetAllMocks); + it('should pass context logger to Octokit client', async () => { + try { + await action.handler(mockContext); + } catch (e) { + // no-op + } + + expect(octokitMock).toHaveBeenCalledWith( + expect.objectContaining({ log: mockContext.logger }), + ); + }); + it('should work happy path', async () => { await action.handler(mockContext); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.test.ts index af1d7d552c..e792c7d91f 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.test.ts @@ -31,6 +31,9 @@ jest.mock('../util', () => { }; }); +import { Octokit } from 'octokit'; + +const octokitMock = Octokit as unknown as jest.Mock; const mockOctokit = { rest: { issues: { @@ -39,11 +42,7 @@ const mockOctokit = { }, }; jest.mock('octokit', () => ({ - Octokit: class { - constructor() { - return mockOctokit; - } - }, + Octokit: jest.fn(), })); describe('github:issues:label', () => { @@ -71,6 +70,7 @@ describe('github:issues:label', () => { beforeEach(() => { jest.resetAllMocks(); + octokitMock.mockImplementation(() => mockOctokit); githubCredentialsProvider = DefaultGithubCredentialsProvider.fromIntegrations(integrations); action = createGithubIssuesLabelAction({ @@ -79,6 +79,18 @@ describe('github:issues:label', () => { }); }); + it('should pass context logger to Octokit client', async () => { + try { + await action.handler(mockContext); + } catch (e) { + // no-op + } + + expect(octokitMock).toHaveBeenCalledWith( + expect.objectContaining({ log: mockContext.logger }), + ); + }); + it('should call the githubApi for adding labels', async () => { await action.handler(mockContext); expect(mockOctokit.rest.issues.addLabels).toHaveBeenCalledWith({ diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.test.ts index 203014b67b..46d9394606 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.test.ts @@ -24,16 +24,16 @@ import { } from '@backstage/integration'; import { createGithubPagesEnableAction } from './githubPagesEnable'; +import { Octokit } from 'octokit'; + +const octokitMock = Octokit as unknown as jest.Mock; + const mockOctokit = { request: jest.fn(), }; jest.mock('octokit', () => ({ - Octokit: class { - constructor() { - return mockOctokit; - } - }, + Octokit: jest.fn(), })); describe('github:pages', () => { @@ -61,6 +61,7 @@ describe('github:pages', () => { }); beforeEach(() => { + octokitMock.mockImplementation(() => mockOctokit); githubCredentialsProvider = DefaultGithubCredentialsProvider.fromIntegrations(integrations); action = createGithubPagesEnableAction({ @@ -71,6 +72,18 @@ describe('github:pages', () => { afterEach(jest.resetAllMocks); + it('should pass context logger to Octokit client', async () => { + try { + await action.handler(mockContext); + } catch (e) { + // no-op + } + + expect(octokitMock).toHaveBeenCalledWith( + expect.objectContaining({ log: mockContext.logger }), + ); + }); + it('should work happy path', async () => { await action.handler(mockContext); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts index bf340a485e..186d0daeea 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.test.ts @@ -35,6 +35,9 @@ import { entityRefToName } from './gitHelpers'; const publicKey = '2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvvcCU='; +import { Octokit } from 'octokit'; + +const octokitMock = Octokit as unknown as jest.Mock; const mockOctokit = { rest: { users: { @@ -62,11 +65,7 @@ const mockOctokit = { request: jest.fn(), }; jest.mock('octokit', () => ({ - Octokit: class { - constructor() { - return mockOctokit; - } - }, + Octokit: jest.fn(), })); describe('github:repo:create', () => { @@ -93,6 +92,7 @@ describe('github:repo:create', () => { }); beforeEach(() => { + octokitMock.mockImplementation(() => mockOctokit); githubCredentialsProvider = DefaultGithubCredentialsProvider.fromIntegrations(integrations); action = createGithubRepoCreateAction({ @@ -110,6 +110,18 @@ describe('github:repo:create', () => { afterEach(jest.resetAllMocks); + it('should pass context logger to Octokit client', async () => { + try { + await action.handler(mockContext); + } catch (e) { + // no-op + } + + expect(octokitMock).toHaveBeenCalledWith( + expect.objectContaining({ log: mockContext.logger }), + ); + }); + it('should call the githubApis with the correct values for createInOrg', async () => { mockOctokit.rest.users.getByUsername.mockResolvedValue({ data: { type: 'Organization' }, diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.test.ts index 7fdf2e1af6..e845d69954 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.test.ts @@ -52,6 +52,9 @@ const initRepoAndPushMocked = initRepoAndPush as jest.Mock< Promise<{ commitHash: string }> >; +import { Octokit } from 'octokit'; + +const octokitMock = Octokit as unknown as jest.Mock; const mockOctokit = { rest: { repos: { @@ -60,11 +63,7 @@ const mockOctokit = { }, }; jest.mock('octokit', () => ({ - Octokit: class { - constructor() { - return mockOctokit; - } - }, + Octokit: jest.fn(), })); describe('github:repo:push', () => { @@ -93,6 +92,8 @@ describe('github:repo:push', () => { beforeEach(() => { jest.resetAllMocks(); + octokitMock.mockImplementation(() => mockOctokit); + initRepoAndPushMocked.mockResolvedValue({ commitHash: 'test123' }); githubCredentialsProvider = @@ -104,6 +105,18 @@ describe('github:repo:push', () => { }); }); + it('should pass context logger to Octokit client', async () => { + try { + await action.handler(mockContext); + } catch (e) { + // no-op + } + + expect(octokitMock).toHaveBeenCalledWith( + expect.objectContaining({ log: mockContext.logger }), + ); + }); + it('should call initRepoAndPush with the correct values', async () => { mockOctokit.rest.repos.get.mockResolvedValue({ data: { diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts index 30bdcef20d..cc0fa28ae3 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.test.ts @@ -24,6 +24,9 @@ import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test- import { ConfigReader } from '@backstage/config'; import { TemplateAction } from '@backstage/plugin-scaffolder-node'; +import { Octokit } from 'octokit'; + +const octokitMock = Octokit as unknown as jest.Mock; const mockOctokit = { rest: { repos: { @@ -32,11 +35,7 @@ const mockOctokit = { }, }; jest.mock('octokit', () => ({ - Octokit: class { - constructor() { - return mockOctokit; - } - }, + Octokit: jest.fn(), })); describe('github:repository:webhook:create', () => { @@ -56,6 +55,7 @@ describe('github:repository:webhook:create', () => { beforeEach(() => { jest.resetAllMocks(); + octokitMock.mockImplementation(() => mockOctokit); githubCredentialsProvider = DefaultGithubCredentialsProvider.fromIntegrations(integrations); action = createGithubWebhookAction({ @@ -72,6 +72,18 @@ describe('github:repository:webhook:create', () => { }, }); + it('should pass context logger to Octokit client', async () => { + try { + await action.handler(mockContext); + } catch (e) { + // no-op + } + + expect(octokitMock).toHaveBeenCalledWith( + expect.objectContaining({ log: mockContext.logger }), + ); + }); + it('should call the githubApi for creating repository Webhook', async () => { const repoUrl = 'github.com?repo=repo&owner=owner'; const webhookUrl = 'https://example.com/payload';