refct(events,catalog/github): add tests to helper functions

Signed-off-by: Rogerio Angeliski <angeliski@hotmail.com>
This commit is contained in:
Rogerio Angeliski
2022-11-30 16:18:02 +00:00
parent 427d8f4411
commit 9451e8f20d
2 changed files with 256 additions and 0 deletions
@@ -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']);
});
});
});
@@ -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 };