Add some more test cases

Signed-off-by: Iain Billett <iain@roadie.io>
This commit is contained in:
Iain Billett
2021-11-02 18:26:36 +00:00
parent f091863f03
commit 23195a4494
2 changed files with 58 additions and 5 deletions
@@ -15,7 +15,7 @@
*/
import { entityFactRetriever } from './entityFactRetriever';
import { Entity } from '@backstage/catalog-model';
import { Entity, RELATION_OWNED_BY } from '@backstage/catalog-model';
import {
PluginEndpointDiscovery,
getVoidLogger,
@@ -49,7 +49,7 @@ const defaultEntityListResponse: CatalogListResponse<Entity> = {
},
relations: [
{
type: 'ownedBy',
type: RELATION_OWNED_BY,
target: { name: 'team-a', kind: 'group', namespace: 'default' },
},
],
@@ -63,6 +63,19 @@ const defaultEntityListResponse: CatalogListResponse<Entity> = {
spec: {
type: 'service',
lifecycle: 'test',
owner: '',
},
},
{
apiVersion: 'backstage.io/v1beta1',
metadata: {
name: 'service-with-user-owner',
},
kind: 'Component',
spec: {
type: 'service',
lifecycle: 'test',
owner: 'user:my-user',
},
},
],
@@ -108,4 +121,42 @@ describe('entityFactRetriever', () => {
});
});
});
describe('hasSpecWithGroupOwner', () => {
describe('where the entity has an group as owner in the spec', () => {
it('returns true for hasSpecWithGroupOwner', async () => {
const facts = await entityFactRetriever.handler(handlerContext);
expect(
facts.find(it => it.entity.name === 'service-with-owner'),
).toMatchObject({
facts: {
hasSpecWithGroupOwner: true,
},
});
});
});
describe('where the entity has a user as owner in the spec', () => {
it('returns false for hasSpecWithGroupOwner', async () => {
const facts = await entityFactRetriever.handler(handlerContext);
expect(
facts.find(it => it.entity.name === 'service-with-user-owner'),
).toMatchObject({
facts: {
hasSpecWithGroupOwner: false,
},
});
});
});
describe('where the entity does not have an owner in the spec', () => {
it('returns false for hasSpecWithGroupOwner', async () => {
const facts = await entityFactRetriever.handler(handlerContext);
expect(
facts.find(it => it.entity.name === 'service-without-owner'),
).toMatchObject({
facts: {
hasSpecWithGroupOwner: false,
},
});
});
});
});
});
@@ -16,7 +16,7 @@
import { FactRetrieverContext } from '@backstage/plugin-tech-insights-node';
import { CatalogClient } from '@backstage/catalog-client';
import { RELATION_OWNED_BY } from '@backstage/catalog-model';
import { Entity, RELATION_OWNED_BY } from '@backstage/catalog-model';
const applicableEntityKinds = [
'component',
@@ -64,7 +64,7 @@ export const entityFactRetriever = {
filter: entityKindFilter,
});
return entities.items.map(it => {
return entities.items.map((it: Entity) => {
return {
entity: {
namespace: it.metadata.namespace!!,
@@ -73,7 +73,9 @@ export const entityFactRetriever = {
},
facts: {
hasSpecWithOwner: Boolean(it.spec?.owner),
hasSpecWithGroupOwner: Boolean(it.spec?.owner && it.spec?.owner),
hasSpecWithGroupOwner: Boolean(
it.spec?.owner && (it.spec?.owner as string).startsWith('group:'),
),
hasOwnedByRelation: Boolean(
it.relations &&
it.relations.find(({ type }) => type === RELATION_OWNED_BY),