From 05b7e918612ec41a151f2c5d4e91915d066c429b Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 14 Feb 2024 12:32:38 +0100 Subject: [PATCH] backend-test-utils: add httpAuth instance mock + discovery mock Signed-off-by: Patrik Oldsberg --- packages/backend-test-utils/api-report.md | 14 ++++++ .../src/next/services/mockServices.ts | 49 ++++++++++++++++--- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/packages/backend-test-utils/api-report.md b/packages/backend-test-utils/api-report.md index a29a782af0..cff8a8fda9 100644 --- a/packages/backend-test-utils/api-report.md +++ b/packages/backend-test-utils/api-report.md @@ -11,6 +11,7 @@ import { Backend } from '@backstage/backend-app-api'; import { BackendFeature } from '@backstage/backend-plugin-api'; import { CacheService } from '@backstage/backend-plugin-api'; import { DatabaseService } from '@backstage/backend-plugin-api'; +import { DiscoveryService } from '@backstage/backend-plugin-api'; import { ExtendedHttpServer } from '@backstage/backend-app-api'; import { ExtensionPoint } from '@backstage/backend-plugin-api'; import { HttpAuthService } from '@backstage/backend-plugin-api'; @@ -118,6 +119,19 @@ export namespace mockServices { ) => ServiceMock; } // (undocumented) + export function discovery(): DiscoveryService; + // (undocumented) + export namespace discovery { + const // (undocumented) + factory: () => ServiceFactory; + const // (undocumented) + mock: ( + partialImpl?: Partial | undefined, + ) => ServiceMock; + } + // (undocumented) + export function httpAuth(options?: { pluginId?: string }): HttpAuthService; + // (undocumented) export namespace httpAuth { const // (undocumented) factory: () => ServiceFactory; diff --git a/packages/backend-test-utils/src/next/services/mockServices.ts b/packages/backend-test-utils/src/next/services/mockServices.ts index 58a6f7c421..ba43aff008 100644 --- a/packages/backend-test-utils/src/next/services/mockServices.ts +++ b/packages/backend-test-utils/src/next/services/mockServices.ts @@ -24,6 +24,8 @@ import { ServiceRef, TokenManagerService, AuthService, + DiscoveryService, + HttpAuthService, } from '@backstage/backend-plugin-api'; import { cacheServiceFactory, @@ -37,12 +39,16 @@ import { schedulerServiceFactory, urlReaderServiceFactory, httpAuthServiceFactory, + discoveryServiceFactory, + HostDiscovery, } from '@backstage/backend-app-api'; import { ConfigReader } from '@backstage/config'; import { JsonObject } from '@backstage/types'; import { MockIdentityService } from './MockIdentityService'; import { MockRootLoggerService } from './MockRootLoggerService'; import { MockAuthService } from './MockAuthService'; +// eslint-disable-next-line @backstage/no-relative-monorepo-imports +import { DefaultHttpAuthService } from '../../../../backend-app-api/src/services/implementations/httpAuth/httpAuthServiceFactory'; /** @internal */ function simpleFactory< @@ -180,6 +186,41 @@ export namespace mockServices { })); } + export function discovery(): DiscoveryService { + return HostDiscovery.fromConfig( + new ConfigReader({ + backend: { + // Invalid port to make sure that requests are always mocked + baseUrl: 'http://localhost:0', + listen: { port: 0 }, + }, + }), + ); + } + export namespace discovery { + export const factory = discoveryServiceFactory; + export const mock = simpleMock(coreServices.discovery, () => ({ + getBaseUrl: jest.fn(), + getExternalBaseUrl: jest.fn(), + })); + } + + export function httpAuth(options?: { pluginId?: string }): HttpAuthService { + return new DefaultHttpAuthService( + auth(), + discovery(), + options?.pluginId ?? 'test', + ); + } + export namespace httpAuth { + export const factory = httpAuthServiceFactory; + export const mock = simpleMock(coreServices.httpAuth, () => ({ + credentials: jest.fn(), + issueUserCookie: jest.fn(), + requestHeaders: jest.fn(), + })); + } + // TODO(Rugvip): Not all core services have implementations available here yet. // some may need a bit more refactoring for it to be simpler to // re-implement functioning mock versions here. @@ -205,14 +246,6 @@ export namespace mockServices { addAuthPolicy: jest.fn(), })); } - export namespace httpAuth { - export const factory = httpAuthServiceFactory; - export const mock = simpleMock(coreServices.httpAuth, () => ({ - credentials: jest.fn(), - issueUserCookie: jest.fn(), - requestHeaders: jest.fn(), - })); - } export namespace rootHttpRouter { export const factory = rootHttpRouterServiceFactory; export const mock = simpleMock(coreServices.rootHttpRouter, () => ({