diff --git a/packages/backend-test-utils/src/next/implementations/index.ts b/packages/backend-test-utils/src/next/implementations/index.ts new file mode 100644 index 0000000000..073e47f5e4 --- /dev/null +++ b/packages/backend-test-utils/src/next/implementations/index.ts @@ -0,0 +1,16 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export { mockTokenManagerFactory } from './mockTokenManagerService'; diff --git a/packages/backend-test-utils/src/next/implementations/mockTokenManagerService.ts b/packages/backend-test-utils/src/next/implementations/mockTokenManagerService.ts new file mode 100644 index 0000000000..f68e910e0c --- /dev/null +++ b/packages/backend-test-utils/src/next/implementations/mockTokenManagerService.ts @@ -0,0 +1,37 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { TokenManager } from '@backstage/backend-common'; +import { + coreServices, + createServiceFactory, +} from '@backstage/backend-plugin-api'; + +class TokenManagerMock implements TokenManager { + async getToken(): Promise<{ token: string }> { + return { token: 'mock-token' }; + } + async authenticate(): Promise {} +} + +export const mockTokenManagerFactory = createServiceFactory({ + service: coreServices.tokenManager, + deps: {}, + async factory() { + return async () => { + return new TokenManagerMock(); + }; + }, +}); diff --git a/packages/backend-test-utils/src/next/wiring/TestBackend.test.ts b/packages/backend-test-utils/src/next/wiring/TestBackend.test.ts index 5ea37e9106..9d71005e18 100644 --- a/packages/backend-test-utils/src/next/wiring/TestBackend.test.ts +++ b/packages/backend-test-utils/src/next/wiring/TestBackend.test.ts @@ -20,7 +20,9 @@ import { createServiceFactory, createServiceRef, coreServices, + createBackendPlugin, } from '@backstage/backend-plugin-api'; + import { startTestBackend } from './TestBackend'; // This bit makes sure that test backends are cleaned up properly @@ -156,4 +158,39 @@ describe('TestBackend', () => { await backend.stop(); expect(shutdownSpy).toHaveBeenCalled(); }); + + it('should provide a set of default services', async () => { + expect.assertions(2); + + const testPlugin = createBackendPlugin({ + id: 'test', + register(env) { + env.registerInit({ + deps: { + cache: coreServices.cache, + config: coreServices.config, + database: coreServices.database, + discovery: coreServices.discovery, + lifecycle: coreServices.lifecycle, + logger: coreServices.logger, + permissions: coreServices.permissions, + rootLifecycle: coreServices.rootLifecycle, + rootLogger: coreServices.rootLogger, + scheduler: coreServices.scheduler, + tokenManager: coreServices.tokenManager, + urlReader: coreServices.urlReader, + }, + async init(deps) { + expect(Object.keys(deps)).toHaveLength(12); + expect(Object.values(deps)).not.toContain(undefined); + }, + }); + }, + }); + + await startTestBackend({ + services: [], + features: [testPlugin()], + }).then(backend => backend.stop()); + }); }); diff --git a/packages/backend-test-utils/src/next/wiring/TestBackend.ts b/packages/backend-test-utils/src/next/wiring/TestBackend.ts index 0295332855..7b78ad3d95 100644 --- a/packages/backend-test-utils/src/next/wiring/TestBackend.ts +++ b/packages/backend-test-utils/src/next/wiring/TestBackend.ts @@ -21,7 +21,15 @@ import { rootLifecycleFactory, loggerFactory, rootLoggerFactory, + configFactory, + discoveryFactory, + cacheFactory, + databaseFactory, + permissionsFactory, + schedulerFactory, + urlReaderFactory, } from '@backstage/backend-app-api'; + import { ServiceFactory, ServiceRef, @@ -30,6 +38,8 @@ import { ExtensionPoint, } from '@backstage/backend-plugin-api'; +import { mockTokenManagerFactory } from '../implementations'; + /** @alpha */ export interface TestBackendOptions< TServices extends any[], @@ -55,10 +65,18 @@ export interface TestBackendOptions< } const defaultServiceFactories = [ - rootLoggerFactory(), - loggerFactory(), + cacheFactory(), + configFactory(), + databaseFactory(), + discoveryFactory(), lifecycleFactory(), + loggerFactory(), + permissionsFactory(), rootLifecycleFactory(), + rootLoggerFactory(), + schedulerFactory(), + mockTokenManagerFactory(), + urlReaderFactory(), ]; const backendInstancesToCleanUp = new Array();