chore: provide all core services to the TestBackend

Signed-off-by: blam <ben@blam.sh>

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2023-01-03 15:27:02 +01:00
parent ac65652aa9
commit bdd4b973e8
4 changed files with 110 additions and 2 deletions
@@ -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';
@@ -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<void> {}
}
export const mockTokenManagerFactory = createServiceFactory({
service: coreServices.tokenManager,
deps: {},
async factory() {
return async () => {
return new TokenManagerMock();
};
},
});
@@ -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());
});
});
@@ -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<Backend>();