From bdc20a59026942b3b055b84a3106fd335dcbb927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Tue, 22 Apr 2025 12:29:52 +0200 Subject: [PATCH] fix test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .../analyzers/GithubLocationAnalyzer.test.ts | 101 ++++++++---------- .../src/analyzers/GithubLocationAnalyzer.ts | 4 +- 2 files changed, 46 insertions(+), 59 deletions(-) diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts index 951e67284b..9fccfd57eb 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts @@ -38,13 +38,12 @@ import { mockServices, } from '@backstage/backend-test-utils'; import { setupServer } from 'msw/node'; -import { http, HttpResponse } from 'msw'; import { catalogServiceMock } from '@backstage/plugin-catalog-node/testUtils'; const server = setupServer(); describe('GithubLocationAnalyzer', () => { - const mockAuthService = mockServices.auth.mock({ + const auth = mockServices.auth.mock({ getPluginRequestToken: async () => ({ token: 'abc123' }), }); const config = mockServices.rootConfig({ @@ -54,49 +53,48 @@ describe('GithubLocationAnalyzer', () => { }, }, }); + const catalog = catalogServiceMock.mock({ + addLocation: jest.fn(async location => ({ + location: { + id: 'test', + target: location.target, + type: location.type ?? 'url', + }, + exists: false, + entities: [ + { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Location', + metadata: { + name: 'test-entity', + }, + spec: { + type: 'url', + target: 'whatever', + }, + }, + { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + title: 'Test Entity', + name: 'test-entity-2', + description: 'The expected description 2', + }, + spec: { + type: 'some-type', + lifecycle: 'experimental', + owner: 'someone', + }, + }, + ], + })), + }); registerMswTestHooks(server); beforeEach(() => { - server.use( - http.post('http://localhost:7007/locations', () => - HttpResponse.json( - { - location: 'test', - exists: false, - entities: [ - { - apiVersion: 'backstage.io/v1alpha1', - kind: 'Location', - metadata: { - name: 'test-entity', - }, - spec: { - type: 'url', - target: 'whatever', - }, - }, - { - apiVersion: 'backstage.io/v1alpha1', - kind: 'Component', - metadata: { - title: 'Test Entity', - name: 'test-entity-2', - description: 'The expected description 2', - }, - spec: { - type: 'some-type', - lifecycle: 'experimental', - owner: 'someone', - }, - }, - ], - }, - { status: 201 }, - ), - ), - ); - + jest.clearAllMocks(); octokit.repos.get.mockResolvedValue({ data: { default_branch: 'my_default_branch' }, }); @@ -112,11 +110,8 @@ describe('GithubLocationAnalyzer', () => { return Promise.reject(); }); - const analyzer = new GithubLocationAnalyzer({ - catalog: catalogServiceMock.mock(), - auth: mockAuthService, - config, - }); + const analyzer = new GithubLocationAnalyzer({ catalog, auth, config }); + const result = await analyzer.analyze({ url: 'https://github.com/foo/bar', }); @@ -139,11 +134,8 @@ describe('GithubLocationAnalyzer', () => { return Promise.reject(); }); - const analyzer = new GithubLocationAnalyzer({ - catalog: catalogServiceMock.mock(), - auth: mockAuthService, - config, - }); + const analyzer = new GithubLocationAnalyzer({ catalog, auth, config }); + const result = await analyzer.analyze({ url: 'https://github.com/foo/bar', catalogFilename: 'anvil.yaml', @@ -165,11 +157,8 @@ describe('GithubLocationAnalyzer', () => { return Promise.reject(); }); - const analyzer = new GithubLocationAnalyzer({ - catalog: catalogServiceMock.mock(), - auth: mockAuthService, - config, - }); + const analyzer = new GithubLocationAnalyzer({ catalog, auth, config }); + const result = await analyzer.analyze({ url: 'https://github.com/foo/bar', catalogFilename: '.gitignore', diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts index 6719d60cd2..6a073394ad 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts @@ -104,8 +104,6 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { }); const defaultBranch = repoInformation.data.default_branch; - const credentials = await this.auth.getOwnServiceCredentials(); - const result = await Promise.all( searchResult.data.items .map(i => `${trimEnd(url, '/')}/blob/${defaultBranch}/${i.path}`) @@ -116,7 +114,7 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { target, dryRun: true, }, - { credentials }, + { credentials: await this.auth.getOwnServiceCredentials() }, ); return addLocationResult.entities.map(e => ({ location: { type: 'url', target },