fix(catalog-backend): let processors validate kinds (#3113)
This commit is contained in:
@@ -16,10 +16,10 @@
|
||||
|
||||
import {
|
||||
ApiEntityV1alpha1,
|
||||
apiEntityV1alpha1Policy as policy,
|
||||
apiEntityV1alpha1Validator as validator,
|
||||
} from './ApiEntityV1alpha1';
|
||||
|
||||
describe('ApiV1alpha1Policy', () => {
|
||||
describe('ApiV1alpha1Validator', () => {
|
||||
let entity: ApiEntityV1alpha1;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -75,81 +75,81 @@ components:
|
||||
});
|
||||
|
||||
it('happy path: accepts valid data', async () => {
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('silently accepts v1beta1 as well', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta1';
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('ignores unknown apiVersion', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta0';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('ignores unknown kind', async () => {
|
||||
(entity as any).kind = 'Wizard';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('rejects missing type', async () => {
|
||||
delete (entity as any).spec.type;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('rejects wrong type', async () => {
|
||||
(entity as any).spec.type = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('rejects empty type', async () => {
|
||||
(entity as any).spec.type = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('rejects missing lifecycle', async () => {
|
||||
delete (entity as any).spec.lifecycle;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/lifecycle/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/lifecycle/);
|
||||
});
|
||||
|
||||
it('rejects wrong lifecycle', async () => {
|
||||
(entity as any).spec.lifecycle = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/lifecycle/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/lifecycle/);
|
||||
});
|
||||
|
||||
it('rejects empty lifecycle', async () => {
|
||||
(entity as any).spec.lifecycle = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/lifecycle/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/lifecycle/);
|
||||
});
|
||||
|
||||
it('rejects missing owner', async () => {
|
||||
delete (entity as any).spec.owner;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/owner/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/owner/);
|
||||
});
|
||||
|
||||
it('rejects wrong owner', async () => {
|
||||
(entity as any).spec.owner = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/owner/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/owner/);
|
||||
});
|
||||
|
||||
it('rejects empty owner', async () => {
|
||||
(entity as any).spec.owner = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/owner/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/owner/);
|
||||
});
|
||||
|
||||
it('rejects missing definition', async () => {
|
||||
delete (entity as any).spec.definition;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/definition/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/definition/);
|
||||
});
|
||||
|
||||
it('rejects wrong definition', async () => {
|
||||
(entity as any).spec.definition = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/definition/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/definition/);
|
||||
});
|
||||
|
||||
it('rejects empty definition', async () => {
|
||||
(entity as any).spec.definition = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/definition/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/definition/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
import * as yup from 'yup';
|
||||
import type { Entity } from '../entity/Entity';
|
||||
import { schemaPolicy } from './util';
|
||||
import { schemaValidator } from './util';
|
||||
|
||||
const API_VERSION = ['backstage.io/v1alpha1', 'backstage.io/v1beta1'] as const;
|
||||
const KIND = 'API' as const;
|
||||
@@ -45,4 +45,8 @@ export interface ApiEntityV1alpha1 extends Entity {
|
||||
};
|
||||
}
|
||||
|
||||
export const apiEntityV1alpha1Policy = schemaPolicy(KIND, API_VERSION, schema);
|
||||
export const apiEntityV1alpha1Validator = schemaValidator(
|
||||
KIND,
|
||||
API_VERSION,
|
||||
schema,
|
||||
);
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
|
||||
import {
|
||||
ComponentEntityV1alpha1,
|
||||
componentEntityV1alpha1Policy as policy,
|
||||
componentEntityV1alpha1Validator as validator,
|
||||
} from './ComponentEntityV1alpha1';
|
||||
|
||||
describe('ComponentV1alpha1Policy', () => {
|
||||
describe('ComponentV1alpha1Validator', () => {
|
||||
let entity: ComponentEntityV1alpha1;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -39,86 +39,86 @@ describe('ComponentV1alpha1Policy', () => {
|
||||
});
|
||||
|
||||
it('happy path: accepts valid data', async () => {
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('silently accepts v1beta1 as well', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta1';
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('ignores unknown apiVersion', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta0';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('ignores unknown kind', async () => {
|
||||
(entity as any).kind = 'Wizard';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('rejects missing type', async () => {
|
||||
delete (entity as any).spec.type;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('rejects wrong type', async () => {
|
||||
(entity as any).spec.type = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('rejects empty type', async () => {
|
||||
(entity as any).spec.type = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('rejects missing lifecycle', async () => {
|
||||
delete (entity as any).spec.lifecycle;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/lifecycle/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/lifecycle/);
|
||||
});
|
||||
|
||||
it('rejects wrong lifecycle', async () => {
|
||||
(entity as any).spec.lifecycle = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/lifecycle/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/lifecycle/);
|
||||
});
|
||||
|
||||
it('rejects empty lifecycle', async () => {
|
||||
(entity as any).spec.lifecycle = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/lifecycle/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/lifecycle/);
|
||||
});
|
||||
|
||||
it('rejects missing owner', async () => {
|
||||
delete (entity as any).spec.owner;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/owner/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/owner/);
|
||||
});
|
||||
|
||||
it('rejects wrong owner', async () => {
|
||||
(entity as any).spec.owner = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/owner/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/owner/);
|
||||
});
|
||||
|
||||
it('rejects empty owner', async () => {
|
||||
(entity as any).spec.owner = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/owner/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/owner/);
|
||||
});
|
||||
|
||||
it('accepts missing implementsApis', async () => {
|
||||
delete (entity as any).spec.implementsApis;
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('rejects empty implementsApis', async () => {
|
||||
(entity as any).spec.implementsApis = [''];
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/implementsApis/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/implementsApis/);
|
||||
});
|
||||
|
||||
it('rejects undefined implementsApis', async () => {
|
||||
(entity as any).spec.implementsApis = [undefined];
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/implementsApis/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/implementsApis/);
|
||||
});
|
||||
|
||||
it('accepts no implementsApis', async () => {
|
||||
(entity as any).spec.implementsApis = [];
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
import * as yup from 'yup';
|
||||
import type { Entity } from '../entity/Entity';
|
||||
import { schemaPolicy } from './util';
|
||||
import { schemaValidator } from './util';
|
||||
|
||||
const API_VERSION = ['backstage.io/v1alpha1', 'backstage.io/v1beta1'] as const;
|
||||
const KIND = 'Component' as const;
|
||||
@@ -45,7 +45,7 @@ export interface ComponentEntityV1alpha1 extends Entity {
|
||||
};
|
||||
}
|
||||
|
||||
export const componentEntityV1alpha1Policy = schemaPolicy(
|
||||
export const componentEntityV1alpha1Validator = schemaValidator(
|
||||
KIND,
|
||||
API_VERSION,
|
||||
schema,
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
|
||||
import {
|
||||
GroupEntityV1alpha1,
|
||||
groupEntityV1alpha1Policy as policy,
|
||||
groupEntityV1alpha1Validator as validator,
|
||||
} from './GroupEntityV1alpha1';
|
||||
|
||||
describe('GroupV1alpha1Policy', () => {
|
||||
describe('GroupV1alpha1Validator', () => {
|
||||
let entity: GroupEntityV1alpha1;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -42,106 +42,106 @@ describe('GroupV1alpha1Policy', () => {
|
||||
});
|
||||
|
||||
it('happy path: accepts valid data', async () => {
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('silently accepts v1beta1 as well', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta1';
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('ignores unknown apiVersion', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta0';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('ignores unknown kind', async () => {
|
||||
(entity as any).kind = 'Wizard';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('rejects missing type', async () => {
|
||||
delete (entity as any).spec.type;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('rejects wrong type', async () => {
|
||||
(entity as any).spec.type = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('rejects empty type', async () => {
|
||||
(entity as any).spec.type = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('accepts missing parent', async () => {
|
||||
delete (entity as any).spec.parent;
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('rejects empty parent', async () => {
|
||||
(entity as any).spec.parent = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/parent/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/parent/);
|
||||
});
|
||||
|
||||
it('rejects missing ancestors', async () => {
|
||||
delete (entity as any).spec.ancestors;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/ancestor/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/ancestor/);
|
||||
});
|
||||
|
||||
it('rejects empty ancestors', async () => {
|
||||
(entity as any).spec.ancestors = [''];
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/ancestor/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/ancestor/);
|
||||
});
|
||||
|
||||
it('rejects undefined ancestors', async () => {
|
||||
(entity as any).spec.ancestors = [undefined];
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/ancestor/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/ancestor/);
|
||||
});
|
||||
|
||||
it('accepts no ancestors', async () => {
|
||||
(entity as any).spec.ancestors = [];
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('rejects missing children', async () => {
|
||||
delete (entity as any).spec.children;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/children/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/children/);
|
||||
});
|
||||
|
||||
it('rejects empty children', async () => {
|
||||
(entity as any).spec.children = [''];
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/children/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/children/);
|
||||
});
|
||||
|
||||
it('rejects undefined children', async () => {
|
||||
(entity as any).spec.children = [undefined];
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/children/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/children/);
|
||||
});
|
||||
|
||||
it('accepts no children', async () => {
|
||||
(entity as any).spec.children = [];
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('rejects missing descendants', async () => {
|
||||
delete (entity as any).spec.descendants;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/descendants/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/descendants/);
|
||||
});
|
||||
|
||||
it('rejects empty descendants', async () => {
|
||||
(entity as any).spec.descendants = [''];
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/descendants/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/descendants/);
|
||||
});
|
||||
|
||||
it('rejects undefined descendants', async () => {
|
||||
(entity as any).spec.descendants = [undefined];
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/descendants/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/descendants/);
|
||||
});
|
||||
|
||||
it('accepts no descendants', async () => {
|
||||
(entity as any).spec.descendants = [];
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
import * as yup from 'yup';
|
||||
import type { Entity } from '../entity/Entity';
|
||||
import { schemaPolicy } from './util';
|
||||
import { schemaValidator } from './util';
|
||||
|
||||
const API_VERSION = ['backstage.io/v1alpha1', 'backstage.io/v1beta1'] as const;
|
||||
const KIND = 'Group' as const;
|
||||
@@ -63,7 +63,7 @@ export interface GroupEntityV1alpha1 extends Entity {
|
||||
};
|
||||
}
|
||||
|
||||
export const groupEntityV1alpha1Policy = schemaPolicy(
|
||||
export const groupEntityV1alpha1Validator = schemaValidator(
|
||||
KIND,
|
||||
API_VERSION,
|
||||
schema,
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
|
||||
import {
|
||||
LocationEntityV1alpha1,
|
||||
locationEntityV1alpha1Policy as policy,
|
||||
locationEntityV1alpha1Validator as validator,
|
||||
} from './LocationEntityV1alpha1';
|
||||
|
||||
describe('LocationV1alpha1Policy', () => {
|
||||
describe('LocationV1alpha1Validator', () => {
|
||||
let entity: LocationEntityV1alpha1;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -36,53 +36,53 @@ describe('LocationV1alpha1Policy', () => {
|
||||
});
|
||||
|
||||
it('happy path: accepts valid data', async () => {
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('silently accepts v1beta1 as well', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta1';
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('ignores unknown apiVersion', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta0';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('ignores unknown kind', async () => {
|
||||
(entity as any).kind = 'Wizard';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('rejects missing type', async () => {
|
||||
delete (entity as any).spec.type;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('rejects wrong type', async () => {
|
||||
(entity as any).spec.type = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('rejects empty type', async () => {
|
||||
(entity as any).spec.type = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('accepts good target', async () => {
|
||||
(entity as any).spec.target =
|
||||
'https://github.com/spotify/backstage/blob/master/plugins/catalog-backend/examples/artist-lookup-component.yaml';
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('rejects wrong target', async () => {
|
||||
(entity as any).spec.target = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/target/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/target/);
|
||||
});
|
||||
|
||||
it('rejects empty target', async () => {
|
||||
(entity as any).spec.target = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/target/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/target/);
|
||||
});
|
||||
|
||||
it('accepts good targets', async () => {
|
||||
@@ -90,16 +90,16 @@ describe('LocationV1alpha1Policy', () => {
|
||||
'https://github.com/spotify/backstage/blob/master/plugins/catalog-backend/examples/artist-lookup-component.yaml',
|
||||
'https://github.com/spotify/backstage/blob/master/plugins/catalog-backend/examples/playback-order-component.yaml',
|
||||
];
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('accepts empty targets', async () => {
|
||||
(entity as any).spec.targets = [];
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('rejects wrong targets', async () => {
|
||||
(entity as any).spec.targets = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/targets/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/targets/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
import * as yup from 'yup';
|
||||
import type { Entity } from '../entity/Entity';
|
||||
import { schemaPolicy } from './util';
|
||||
import { schemaValidator } from './util';
|
||||
|
||||
const API_VERSION = ['backstage.io/v1alpha1', 'backstage.io/v1beta1'] as const;
|
||||
const KIND = 'Location' as const;
|
||||
@@ -43,7 +43,7 @@ export interface LocationEntityV1alpha1 extends Entity {
|
||||
};
|
||||
}
|
||||
|
||||
export const locationEntityV1alpha1Policy = schemaPolicy(
|
||||
export const locationEntityV1alpha1Validator = schemaValidator(
|
||||
KIND,
|
||||
API_VERSION,
|
||||
schema,
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
|
||||
import {
|
||||
TemplateEntityV1alpha1,
|
||||
templateEntityV1alpha1Policy as policy,
|
||||
templateEntityV1alpha1Validator as validator,
|
||||
} from './TemplateEntityV1alpha1';
|
||||
|
||||
describe('templateEntityV1alpha1', () => {
|
||||
describe('templateEntityV1alpha1Validator', () => {
|
||||
let entity: TemplateEntityV1alpha1;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -53,41 +53,41 @@ describe('templateEntityV1alpha1', () => {
|
||||
});
|
||||
|
||||
it('happy path: accepts valid data', async () => {
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('silently accepts v1beta1 as well', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta1';
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('ignores unknown apiVersion', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta0';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('ignores unknown kind', async () => {
|
||||
(entity as any).kind = 'Wizard';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('rejects missing type', async () => {
|
||||
delete (entity as any).spec.type;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('accepts any other type', async () => {
|
||||
(entity as any).spec.type = 'hallo';
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('rejects empty type', async () => {
|
||||
(entity as any).spec.type = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/type/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/type/);
|
||||
});
|
||||
|
||||
it('rejects missing templater', async () => {
|
||||
(entity as any).spec.templater = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/templater/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/templater/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import * as yup from 'yup';
|
||||
import type { Entity } from '../entity/Entity';
|
||||
import type { JSONSchema } from '../types';
|
||||
import { schemaPolicy } from './util';
|
||||
import { schemaValidator } from './util';
|
||||
|
||||
const API_VERSION = ['backstage.io/v1alpha1', 'backstage.io/v1beta1'] as const;
|
||||
const KIND = 'Template' as const;
|
||||
@@ -46,7 +46,7 @@ export interface TemplateEntityV1alpha1 extends Entity {
|
||||
};
|
||||
}
|
||||
|
||||
export const templateEntityV1alpha1Policy = schemaPolicy(
|
||||
export const templateEntityV1alpha1Validator = schemaValidator(
|
||||
KIND,
|
||||
API_VERSION,
|
||||
schema,
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
|
||||
import {
|
||||
UserEntityV1alpha1,
|
||||
userEntityV1alpha1Policy as policy,
|
||||
userEntityV1alpha1Validator as validator,
|
||||
} from './UserEntityV1alpha1';
|
||||
|
||||
describe('userEntityV1alpha1Policy', () => {
|
||||
describe('userEntityV1alpha1Validator', () => {
|
||||
let entity: UserEntityV1alpha1;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -41,117 +41,117 @@ describe('userEntityV1alpha1Policy', () => {
|
||||
});
|
||||
|
||||
it('happy path: accepts valid data', async () => {
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
// root
|
||||
|
||||
it('silently accepts v1beta1 as well', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta1';
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('ignores unknown apiVersion', async () => {
|
||||
(entity as any).apiVersion = 'backstage.io/v1beta0';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('ignores unknown kind', async () => {
|
||||
(entity as any).kind = 'Wizard';
|
||||
await expect(policy.enforce(entity)).resolves.toBeUndefined();
|
||||
await expect(validator.check(entity)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('spec accepts unknown additional fields', async () => {
|
||||
(entity as any).spec.foo = 'data';
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
// profile
|
||||
|
||||
it('accepts missing profile', async () => {
|
||||
delete (entity as any).spec.profile;
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('rejects wrong profile', async () => {
|
||||
(entity as any).spec.profile = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/profile/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/profile/);
|
||||
});
|
||||
|
||||
it('profile accepts missing displayName', async () => {
|
||||
delete (entity as any).spec.profile.displayName;
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('profile rejects wrong displayName', async () => {
|
||||
(entity as any).spec.profile.displayName = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/displayName/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/displayName/);
|
||||
});
|
||||
|
||||
it('profile rejects empty displayName', async () => {
|
||||
(entity as any).spec.profile.displayName = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/displayName/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/displayName/);
|
||||
});
|
||||
|
||||
it('profile accepts missing email', async () => {
|
||||
delete (entity as any).spec.profile.email;
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('profile rejects wrong email', async () => {
|
||||
(entity as any).spec.profile.email = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/email/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/email/);
|
||||
});
|
||||
|
||||
it('profile rejects empty email', async () => {
|
||||
(entity as any).spec.profile.email = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/email/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/email/);
|
||||
});
|
||||
|
||||
it('profile accepts missing picture', async () => {
|
||||
delete (entity as any).spec.profile.picture;
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('profile rejects wrong picture', async () => {
|
||||
(entity as any).spec.profile.picture = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/picture/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/picture/);
|
||||
});
|
||||
|
||||
it('profile rejects empty picture', async () => {
|
||||
(entity as any).spec.profile.picture = '';
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/picture/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/picture/);
|
||||
});
|
||||
|
||||
it('profile accepts unknown additional fields', async () => {
|
||||
(entity as any).spec.profile.foo = 'data';
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
// memberOf
|
||||
|
||||
it('rejects missing memberOf', async () => {
|
||||
delete (entity as any).spec.memberOf;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/memberOf/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/memberOf/);
|
||||
});
|
||||
|
||||
it('rejects wrong memberOf', async () => {
|
||||
(entity as any).spec.memberOf = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/memberOf/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/memberOf/);
|
||||
});
|
||||
|
||||
it('rejects wrong memberOf item', async () => {
|
||||
(entity as any).spec.memberOf[0] = 7;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/memberOf/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/memberOf/);
|
||||
});
|
||||
|
||||
it('accepts empty memberOf', async () => {
|
||||
(entity as any).spec.memberOf = [];
|
||||
await expect(policy.enforce(entity)).resolves.toBe(entity);
|
||||
await expect(validator.check(entity)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('rejects null memberOf', async () => {
|
||||
(entity as any).spec.memberOf = null;
|
||||
await expect(policy.enforce(entity)).rejects.toThrow(/memberOf/);
|
||||
await expect(validator.check(entity)).rejects.toThrow(/memberOf/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
import * as yup from 'yup';
|
||||
import type { Entity } from '../entity/Entity';
|
||||
import { schemaPolicy } from './util';
|
||||
import { schemaValidator } from './util';
|
||||
|
||||
const API_VERSION = ['backstage.io/v1alpha1', 'backstage.io/v1beta1'] as const;
|
||||
const KIND = 'User' as const;
|
||||
@@ -59,4 +59,8 @@ export interface UserEntityV1alpha1 extends Entity {
|
||||
};
|
||||
}
|
||||
|
||||
export const userEntityV1alpha1Policy = schemaPolicy(KIND, API_VERSION, schema);
|
||||
export const userEntityV1alpha1Validator = schemaValidator(
|
||||
KIND,
|
||||
API_VERSION,
|
||||
schema,
|
||||
);
|
||||
|
||||
@@ -14,34 +14,35 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { apiEntityV1alpha1Policy } from './ApiEntityV1alpha1';
|
||||
export { apiEntityV1alpha1Validator } from './ApiEntityV1alpha1';
|
||||
export type {
|
||||
ApiEntityV1alpha1 as ApiEntity,
|
||||
ApiEntityV1alpha1,
|
||||
} from './ApiEntityV1alpha1';
|
||||
export { componentEntityV1alpha1Policy } from './ComponentEntityV1alpha1';
|
||||
export { componentEntityV1alpha1Validator } from './ComponentEntityV1alpha1';
|
||||
export type {
|
||||
ComponentEntityV1alpha1 as ComponentEntity,
|
||||
ComponentEntityV1alpha1,
|
||||
} from './ComponentEntityV1alpha1';
|
||||
export { groupEntityV1alpha1Policy } from './GroupEntityV1alpha1';
|
||||
export { groupEntityV1alpha1Validator } from './GroupEntityV1alpha1';
|
||||
export type {
|
||||
GroupEntityV1alpha1 as GroupEntity,
|
||||
GroupEntityV1alpha1,
|
||||
} from './GroupEntityV1alpha1';
|
||||
export { locationEntityV1alpha1Policy } from './LocationEntityV1alpha1';
|
||||
export { locationEntityV1alpha1Validator } from './LocationEntityV1alpha1';
|
||||
export type {
|
||||
LocationEntityV1alpha1 as LocationEntity,
|
||||
LocationEntityV1alpha1,
|
||||
} from './LocationEntityV1alpha1';
|
||||
export { templateEntityV1alpha1Policy } from './TemplateEntityV1alpha1';
|
||||
export * from './relations';
|
||||
export { templateEntityV1alpha1Validator } from './TemplateEntityV1alpha1';
|
||||
export type {
|
||||
TemplateEntityV1alpha1 as TemplateEntity,
|
||||
TemplateEntityV1alpha1,
|
||||
} from './TemplateEntityV1alpha1';
|
||||
export { userEntityV1alpha1Policy } from './UserEntityV1alpha1';
|
||||
export type { KindValidator } from './types';
|
||||
export { userEntityV1alpha1Validator } from './UserEntityV1alpha1';
|
||||
export type {
|
||||
UserEntityV1alpha1 as UserEntity,
|
||||
UserEntityV1alpha1,
|
||||
} from './UserEntityV1alpha1';
|
||||
export * from './relations';
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* 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 { Entity } from '../entity';
|
||||
|
||||
/**
|
||||
* Validates entities of a certain kind.
|
||||
*/
|
||||
export type KindValidator = {
|
||||
/**
|
||||
* Validates the entity as a known entity kind.
|
||||
*
|
||||
* @param entity The entity to validate
|
||||
* @returns Resolves to true, if the entity was of a kind that was known and
|
||||
* handled by this validator, and was found to be valid. Resolves to false,
|
||||
* if the entity was not of a kind that was known by this validator.
|
||||
* Rejects to an Error describing the problem, if the entity was of a kind
|
||||
* that was known by this validator and was not valid.
|
||||
*/
|
||||
check(entity: Entity): Promise<boolean>;
|
||||
};
|
||||
@@ -15,23 +15,23 @@
|
||||
*/
|
||||
|
||||
import * as yup from 'yup';
|
||||
import { Entity } from '../entity';
|
||||
import { EntityPolicy } from '../types';
|
||||
import { KindValidator } from './types';
|
||||
|
||||
export function schemaPolicy(
|
||||
export function schemaValidator(
|
||||
kind: string,
|
||||
apiVersion: readonly string[],
|
||||
schema: yup.Schema<any>,
|
||||
): EntityPolicy {
|
||||
): KindValidator {
|
||||
return {
|
||||
async enforce(envelope: Entity): Promise<Entity | undefined> {
|
||||
async check(envelope) {
|
||||
if (
|
||||
kind !== envelope.kind ||
|
||||
!apiVersion.includes(envelope.apiVersion as any)
|
||||
) {
|
||||
return undefined;
|
||||
return false;
|
||||
}
|
||||
return await schema.validate(envelope, { strict: true });
|
||||
await schema.validate(envelope, { strict: true });
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user