From 9451e8f20dc116e39a58acd7a118584bc685c802 Mon Sep 17 00:00:00 2001 From: Rogerio Angeliski Date: Wed, 30 Nov 2022 16:18:02 +0000 Subject: [PATCH] refct(events,catalog/github): add tests to helper functions Signed-off-by: Rogerio Angeliski --- .../src/lib/github.test.ts | 240 ++++++++++++++++++ .../src/lib/github.ts | 16 ++ 2 files changed, 256 insertions(+) diff --git a/plugins/catalog-backend-module-github/src/lib/github.test.ts b/plugins/catalog-backend-module-github/src/lib/github.test.ts index fa79c03903..0c0038f6d7 100644 --- a/plugins/catalog-backend-module-github/src/lib/github.test.ts +++ b/plugins/catalog-backend-module-github/src/lib/github.test.ts @@ -29,6 +29,11 @@ import { QueryResponse, GithubUser, GithubTeam, + createAddEntitiesOperation, + createRemoveEntitiesOperation, + createReplaceEntitiesOperation, + removeUserFromGroup, + addUserToGroup, } from './github'; import fetch from 'node-fetch'; @@ -567,4 +572,239 @@ describe('github', () => { ).resolves.toEqual(output); }); }); + + describe('createAddEntitiesOperation', () => { + it('create a function to add deferred entities to a delta operation', () => { + const operation = createAddEntitiesOperation('my-id', 'host'); + + const userEntity: UserEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'User', + metadata: { + name: 'githubuser', + annotations: { + 'backstage.io/managed-by-location': + 'url:https://github.com/githubuser', + 'backstage.io/managed-by-origin-location': + 'url:https://github.com/githubuser', + 'github.com/user-login': 'githubuser', + }, + }, + spec: { + memberOf: ['new-team'], + }, + }; + expect(operation('org', [userEntity])).toEqual({ + added: [ + { + locationKey: 'github-org-provider:my-id', + entity: userEntity, + }, + ], + removed: [], + }); + }); + }); + + describe('createRemoveEntitiesOperation', () => { + it('create a function to remove deferred entities to a delta operation', () => { + const operation = createRemoveEntitiesOperation('my-id', 'host'); + + const userEntity: UserEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'User', + metadata: { + name: 'githubuser', + annotations: { + 'backstage.io/managed-by-location': + 'url:https://github.com/githubuser', + 'backstage.io/managed-by-origin-location': + 'url:https://github.com/githubuser', + 'github.com/user-login': 'githubuser', + }, + }, + spec: { + memberOf: ['new-team'], + }, + }; + expect(operation('org', [userEntity])).toEqual({ + removed: [ + { + locationKey: 'github-org-provider:my-id', + entity: userEntity, + }, + ], + added: [], + }); + }); + }); + + describe('createReplaceEntitiesOperation', () => { + it('create a function to replace deferred entities to a delta operation', () => { + const operation = createReplaceEntitiesOperation('my-id', 'host'); + + const userEntity: UserEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'User', + metadata: { + name: 'githubuser', + annotations: { + 'backstage.io/managed-by-location': + 'url:https://github.com/githubuser', + 'backstage.io/managed-by-origin-location': + 'url:https://github.com/githubuser', + 'github.com/user-login': 'githubuser', + }, + }, + spec: { + memberOf: ['new-team'], + }, + }; + expect(operation('org', [userEntity])).toEqual({ + removed: [ + { + locationKey: 'github-org-provider:my-id', + entity: userEntity, + }, + ], + added: [ + { + locationKey: 'github-org-provider:my-id', + entity: userEntity, + }, + ], + }); + }); + }); + + describe('removeUserFromGroup', () => { + it('should remove a user from the group', () => { + const groupEntity: GroupEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Group', + metadata: { + name: 'new-team', + annotations: { + 'backstage.io/managed-by-location': + 'url:https://github.com/orgs/test-org/teams/new-team', + 'backstage.io/managed-by-origin-location': + 'url:https://github.com/orgs/test-org/teams/new-team', + }, + }, + spec: { + type: 'team', + children: [], + members: ['user-1', 'githubuser'], + }, + }; + + const userEntity: UserEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'User', + metadata: { + name: 'githubuser', + annotations: { + 'backstage.io/managed-by-location': + 'url:https://github.com/githubuser', + 'backstage.io/managed-by-origin-location': + 'url:https://github.com/githubuser', + 'github.com/user-login': 'githubuser', + }, + }, + spec: { + memberOf: ['new-team'], + }, + }; + + const { user, group } = removeUserFromGroup(userEntity, groupEntity); + + expect(user.spec.memberOf).toEqual([]); + expect(group.spec.members).toEqual(['user-1']); + }); + }); + + describe('addUserToGroup', () => { + it('should add a user to the group', () => { + const groupEntity: GroupEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Group', + metadata: { + name: 'new-team', + annotations: { + 'backstage.io/managed-by-location': + 'url:https://github.com/orgs/test-org/teams/new-team', + 'backstage.io/managed-by-origin-location': + 'url:https://github.com/orgs/test-org/teams/new-team', + }, + }, + spec: { + type: 'team', + children: [], + members: ['user-1'], + }, + }; + + const userEntity: UserEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'User', + metadata: { + name: 'githubuser', + annotations: { + 'backstage.io/managed-by-location': + 'url:https://github.com/githubuser', + 'backstage.io/managed-by-origin-location': + 'url:https://github.com/githubuser', + 'github.com/user-login': 'githubuser', + }, + }, + spec: { + memberOf: [], + }, + }; + + const { user, group } = addUserToGroup(userEntity, groupEntity); + expect(user.spec.memberOf).toEqual(['new-team']); + expect(group.spec.members).toEqual(['user-1', 'githubuser']); + }); + it('should add a user to the group when the fields are not defined', () => { + const groupEntity: GroupEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Group', + metadata: { + name: 'new-team', + annotations: { + 'backstage.io/managed-by-location': + 'url:https://github.com/orgs/test-org/teams/new-team', + 'backstage.io/managed-by-origin-location': + 'url:https://github.com/orgs/test-org/teams/new-team', + }, + }, + spec: { + type: 'team', + children: [], + }, + }; + + const userEntity: UserEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'User', + metadata: { + name: 'githubuser', + annotations: { + 'backstage.io/managed-by-location': + 'url:https://github.com/githubuser', + 'backstage.io/managed-by-origin-location': + 'url:https://github.com/githubuser', + 'github.com/user-login': 'githubuser', + }, + }, + spec: {}, + }; + + const { user, group } = addUserToGroup(userEntity, groupEntity); + + expect(user.spec.memberOf).toEqual(['new-team']); + expect(group.spec.members).toEqual(['githubuser']); + }); + }); }); diff --git a/plugins/catalog-backend-module-github/src/lib/github.ts b/plugins/catalog-backend-module-github/src/lib/github.ts index e3c492e1a8..b643075966 100644 --- a/plugins/catalog-backend-module-github/src/lib/github.ts +++ b/plugins/catalog-backend-module-github/src/lib/github.ts @@ -432,6 +432,7 @@ export const createAddEntitiesOperation = entity: withLocations(`https://${host}`, org, entity), })), }); + export const createRemoveEntitiesOperation = (id: string, host: string) => (org: string, entities: Entity[]) => ({ added: [], @@ -440,6 +441,7 @@ export const createRemoveEntitiesOperation = entity: withLocations(`https://${host}`, org, entity), })), }); + export const createReplaceEntitiesOperation = (id: string, host: string) => (org: string, entities: Entity[]) => { const entitiesToReplace = entities.map(entity => ({ @@ -470,6 +472,20 @@ export function removeUserFromGroup(user: UserEntity, group: GroupEntity) { } export function addUserToGroup(user: UserEntity, group: GroupEntity) { + if (!group.spec?.members) { + group.spec = { + ...group.spec, + members: [], + }; + } + + if (!user.spec?.memberOf) { + user.spec = { + ...user.spec, + memberOf: [], + }; + } + group.spec?.members?.push(user.metadata.name); user.spec?.memberOf?.push(group.metadata.name); return { group, user };