From f86328fee63654be639bae316f842833d1d22804 Mon Sep 17 00:00:00 2001 From: Anton Ganhammar Date: Thu, 31 Aug 2023 15:10:40 +0200 Subject: [PATCH] feat: add token related tests to report and history endpoints Signed-off-by: Anton Ganhammar --- .../src/service/router.test.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/plugins/code-coverage-backend/src/service/router.test.ts b/plugins/code-coverage-backend/src/service/router.test.ts index 7d1825c25f..4f1a9ee2e5 100644 --- a/plugins/code-coverage-backend/src/service/router.test.ts +++ b/plugins/code-coverage-backend/src/service/router.test.ts @@ -25,6 +25,35 @@ import { } from '@backstage/backend-common'; import { ConfigReader } from '@backstage/config'; import { createRouter } from './router'; +import { CatalogRequestOptions } from '@backstage/catalog-client'; + +jest.mock('./CodeCoverageDatabase'); + +import { CodeCoverageDatabase } from './CodeCoverageDatabase'; + +CodeCoverageDatabase.create = jest.fn( + async () => + ({ + getCodeCoverage: async () => ({ + files: [], + metadata: { + generationTime: 1, + }, + }), + getHistory: async () => ({}), + } as any), +); + +let catalogRequestOptions: CatalogRequestOptions; + +jest.mock('@backstage/catalog-client', () => ({ + CatalogClient: jest.fn().mockImplementation(() => ({ + getEntityByRef: async (_: string, options: CatalogRequestOptions) => { + catalogRequestOptions = options; + return {}; + }, + })), +})); function createDatabase(): PluginDatabaseManager { return DatabaseManager.fromConfig( @@ -76,4 +105,28 @@ describe('createRouter', () => { expect(response.body).toEqual({ status: 'ok' }); }); }); + + [ + '/report?entity=component:default/mycomponent', + '/history?entity=component:default/mycomponent', + ].forEach(uri => { + describe(`GET ${uri}`, () => { + it('does not send token when calling catalog api and request is unauthenticated', async () => { + const response = await request(app).get(uri); + + expect(response.status).toEqual(200); + expect(catalogRequestOptions.token).toBeUndefined(); + }); + + it('includes auth token when calling catalog api', async () => { + const token = 'my-auth-token'; + const response = await request(app) + .get(uri) + .set('Authorization', `Bearer ${token}`); + + expect(response.status).toEqual(200); + expect(catalogRequestOptions.token).toEqual(token); + }); + }); + }); });