backend-test-utils: add httpAuth instance mock + discovery mock

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-02-14 12:32:38 +01:00
parent bfc63fdf37
commit 05b7e91861
2 changed files with 55 additions and 8 deletions
+14
View File
@@ -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<DatabaseService>;
}
// (undocumented)
export function discovery(): DiscoveryService;
// (undocumented)
export namespace discovery {
const // (undocumented)
factory: () => ServiceFactory<DiscoveryService, 'plugin'>;
const // (undocumented)
mock: (
partialImpl?: Partial<DiscoveryService> | undefined,
) => ServiceMock<DiscoveryService>;
}
// (undocumented)
export function httpAuth(options?: { pluginId?: string }): HttpAuthService;
// (undocumented)
export namespace httpAuth {
const // (undocumented)
factory: () => ServiceFactory<HttpAuthService, 'plugin'>;
@@ -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, () => ({