From 93be506ca9991cd0afde26af72d293db20bddf63 Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Fri, 22 Dec 2023 10:03:59 +0100 Subject: [PATCH] scaffolder-backend: add extra permission rule tests Signed-off-by: Vincenzo Scamporlino --- .../src/service/rules.test.ts | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/plugins/scaffolder-backend/src/service/rules.test.ts b/plugins/scaffolder-backend/src/service/rules.test.ts index e6032dde15..8e24cacb32 100644 --- a/plugins/scaffolder-backend/src/service/rules.test.ts +++ b/plugins/scaffolder-backend/src/service/rules.test.ts @@ -23,6 +23,9 @@ import { hasStringProperty, hasTag, } from './rules'; +import { createConditionAuthorizer } from '@backstage/plugin-permission-node'; +import { RESOURCE_TYPE_SCAFFOLDER_ACTION } from '@backstage/plugin-scaffolder-common/alpha'; +import { AuthorizeResult } from '@backstage/plugin-permission-common'; describe('hasTag', () => { describe('apply', () => { @@ -206,6 +209,84 @@ describe('hasProperty', () => { ).toEqual(true); }, ); + + it('should throw if params are invalid', () => { + const isActionAuthorized = createConditionAuthorizer([hasProperty]); + + expect(() => + isActionAuthorized( + { + resourceType: RESOURCE_TYPE_SCAFFOLDER_ACTION, + pluginId: 'scaffolder', + result: AuthorizeResult.CONDITIONAL, + conditions: { + resourceType: RESOURCE_TYPE_SCAFFOLDER_ACTION, + rule: 'HAS_PROPERTY', + params: { + key: 1, + }, + }, + }, + { action: 'an-action', input: {} }, + ), + ).toThrow(); + expect(() => + isActionAuthorized( + { + resourceType: RESOURCE_TYPE_SCAFFOLDER_ACTION, + pluginId: 'scaffolder', + result: AuthorizeResult.CONDITIONAL, + conditions: { + resourceType: RESOURCE_TYPE_SCAFFOLDER_ACTION, + rule: 'HAS_PROPERTY', + params: {}, + }, + }, + { action: 'an-action', input: {} }, + ), + ).toThrow(); + }); + + it('should not throw if params are valid', () => { + const isActionAuthorized = createConditionAuthorizer([hasProperty]); + + expect(() => + isActionAuthorized( + { + resourceType: RESOURCE_TYPE_SCAFFOLDER_ACTION, + pluginId: 'scaffolder', + result: AuthorizeResult.CONDITIONAL, + conditions: { + resourceType: RESOURCE_TYPE_SCAFFOLDER_ACTION, + rule: 'HAS_PROPERTY', + params: { + key: 'key', + }, + }, + }, + { action: 'an-action', input: {} }, + ), + ).not.toThrow(); + + expect(() => + isActionAuthorized( + { + resourceType: RESOURCE_TYPE_SCAFFOLDER_ACTION, + pluginId: 'scaffolder', + result: AuthorizeResult.CONDITIONAL, + conditions: { + resourceType: RESOURCE_TYPE_SCAFFOLDER_ACTION, + rule: 'HAS_PROPERTY', + params: { + key: 'key', + value: 'value', + }, + }, + }, + { action: 'an-action', input: {} }, + ), + ).not.toThrow(); + }); }); });