From 8dbacc222833e8c3abe08a0babde9e430b21c8fd Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Wed, 1 Feb 2023 14:56:05 +0100 Subject: [PATCH] scaffolder-backend: test hasTag rule Signed-off-by: Vincenzo Scamporlino --- .../src/service/rules.test.ts | 81 +++++++++++++++++++ .../scaffolder-backend/src/service/rules.ts | 6 +- 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 plugins/scaffolder-backend/src/service/rules.test.ts diff --git a/plugins/scaffolder-backend/src/service/rules.test.ts b/plugins/scaffolder-backend/src/service/rules.test.ts new file mode 100644 index 0000000000..0f6f4851b4 --- /dev/null +++ b/plugins/scaffolder-backend/src/service/rules.test.ts @@ -0,0 +1,81 @@ +/* + * 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 { hasTag } from './rules'; + +describe('hasTag', () => { + describe('apply', () => { + it('returns false when the tag is not present', () => { + expect( + hasTag.apply( + { + 'backstage:accessControl': { + tags: ['foo', 'bar'], + }, + }, + { + tag: 'baz', + }, + ), + ).toEqual(false); + }); + + it('returns false when accessControl is missing', () => { + expect( + hasTag.apply( + {}, + { + tag: 'baz', + }, + ), + ).toEqual(false); + }); + + it('returns false when tags is an empty array', () => { + expect( + hasTag.apply( + { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + 'backstage:accessControl': { + tags: [], + }, + }, + }, + { + tag: 'baz', + }, + ), + ).toEqual(false); + }); + + it('returns true when the tag is present', () => { + expect( + hasTag.apply( + { + 'backstage:accessControl': { + tags: ['foo', 'bar'], + }, + }, + { + tag: 'bar', + }, + ), + ).toEqual(true); + }); + }); +}); diff --git a/plugins/scaffolder-backend/src/service/rules.ts b/plugins/scaffolder-backend/src/service/rules.ts index 85bcca3e05..2dcf82dc09 100644 --- a/plugins/scaffolder-backend/src/service/rules.ts +++ b/plugins/scaffolder-backend/src/service/rules.ts @@ -22,16 +22,16 @@ import { } from '@backstage/plugin-scaffolder-common'; import { z } from 'zod'; -export const createScaffolderStepPermissionRule = makeCreatePermissionRule< +export const createScaffolderPermissionRule = makeCreatePermissionRule< TemplateEntityStepV1beta3 | TemplateParameter, {}, typeof RESOURCE_TYPE_SCAFFOLDER_TEMPLATE >(); -const hasTag = createScaffolderStepPermissionRule({ +export const hasTag = createScaffolderPermissionRule({ name: 'HAS_TAG', resourceType: RESOURCE_TYPE_SCAFFOLDER_TEMPLATE, - description: 'Match a scaffolder step with the given tag', + description: `Match parameters or steps with the given tag`, paramsSchema: z.object({ tag: z.string().describe('Name of the tag to match on'), }),