From 1ffaad9c3bbe50df3a530ac857f5e28ba4dfa662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Wed, 22 Sep 2021 21:50:40 +0200 Subject: [PATCH] add test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- ...faultCatalogProcessingOrchestrator.test.ts | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 plugins/catalog-backend/src/next/processing/DefaultCatalogProcessingOrchestrator.test.ts diff --git a/plugins/catalog-backend/src/next/processing/DefaultCatalogProcessingOrchestrator.test.ts b/plugins/catalog-backend/src/next/processing/DefaultCatalogProcessingOrchestrator.test.ts new file mode 100644 index 0000000000..1051557a0d --- /dev/null +++ b/plugins/catalog-backend/src/next/processing/DefaultCatalogProcessingOrchestrator.test.ts @@ -0,0 +1,86 @@ +/* + * Copyright 2021 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 { getVoidLogger } from '@backstage/backend-common'; +import { + EntityPolicy, + LocationEntity, + LOCATION_ANNOTATION, + ORIGIN_LOCATION_ANNOTATION, +} from '@backstage/catalog-model'; +import { ScmIntegrationRegistry } from '@backstage/integration'; +import { + CatalogProcessor, + CatalogProcessorParser, + results, +} from '../../ingestion'; +import { CatalogRulesEnforcer } from '../../ingestion/CatalogRules'; +import { DefaultCatalogProcessingOrchestrator } from './DefaultCatalogProcessingOrchestrator'; + +describe('DefaultCatalogProcessingOrchestrator', () => { + it('enforces catalog rules', async () => { + const entity: LocationEntity = { + apiVersion: 'backstage.io/v1beta1', + kind: 'Location', + metadata: { + name: 'l', + annotations: { + [ORIGIN_LOCATION_ANNOTATION]: 'url:https://example.com/origin.yaml', + [LOCATION_ANNOTATION]: 'url:https://example.com/origin.yaml', + }, + }, + spec: { + type: 'url', + target: 'http://example.com/entity.yaml', + }, + }; + + const processor: jest.Mocked = { + validateEntityKind: jest.fn(async () => true), + readLocation: jest.fn(async (_l, _o, emit) => { + emit(results.entity({ type: 't', target: 't' }, entity)); + return true; + }), + }; + const integrations: jest.Mocked = {} as any; + const parser: CatalogProcessorParser = jest.fn(); + const policy: jest.Mocked = { + enforce: jest.fn(async x => x), + }; + const rulesEnforcer: jest.Mocked = { + isAllowed: jest.fn(), + } as any; + + const orchestrator = new DefaultCatalogProcessingOrchestrator({ + processors: [processor], + integrations, + logger: getVoidLogger(), + parser, + policy, + rulesEnforcer, + }); + + rulesEnforcer.isAllowed.mockReturnValueOnce(true); + await expect( + orchestrator.process({ entity, state: new Map() }), + ).resolves.toEqual(expect.objectContaining({ ok: true })); + + rulesEnforcer.isAllowed.mockReturnValueOnce(false); + await expect( + orchestrator.process({ entity, state: new Map() }), + ).resolves.toEqual(expect.objectContaining({ ok: false })); + }); +});