Skip to content

Commit

Permalink
Merge pull request #17 from cabinetoffice/feature/NTRNL-288-fix-mappi…
Browse files Browse the repository at this point in the history
…ng-error-and-remove-unused-fields

Feature/ntrnl 288 fix mapping error and remove unused fields
  • Loading branch information
Mouhajer-CO authored Jan 10, 2024
2 parents fb9478f + 8f35486 commit 15d4a60
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/api-sdk/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class Github {

if (response.error) {
resource.errors = [response.error];
} else if (response.status >= 400) {
resource.errors = [response.body];
} else {
resource.resource = mappingFunction(response.body);
}
Expand Down
7 changes: 2 additions & 5 deletions src/api-sdk/github/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export const membersMapping = (body: any[]): GitHubMembers[] => {
return body.map((obj) => ({
login: obj.login,
url: obj.url,
html_url: obj.html_url,
repos_url: obj.repos_url
html_url: obj.html_url
}));
};

Expand All @@ -27,9 +26,7 @@ export const teamsMapping = (body: any[]): GitHubTeams[] => {
name: obj.name,
description: obj.description,
url: obj.url,
html_url: obj.html_url,
repositories_url: obj.repositories_url,
members_url: obj.members_url
html_url: obj.html_url
}));
};

Expand Down
3 changes: 0 additions & 3 deletions src/api-sdk/github/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ export interface GitHubTeams {
description: string;
url: string;
html_url: string;
repositories_url: string;
members_url: string;
}

export interface GitHubMembers {
login: string;
url: string;
html_url: string;
repos_url: string;
}

export interface GitHubMembersPerTeam {
Expand Down
11 changes: 9 additions & 2 deletions test/mock/data.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const MOCK_MEMBERS = [

export const MOCK_MEMBER_FETCH_RESPONSE = {
httpStatusCode: 200,
resource: MOCK_MEMBERS
resource: MOCK_MEMBERS.map(({ repos_url, ...rest }) => rest)
};

export const MOCK_TEAMS = [
Expand All @@ -100,7 +100,7 @@ export const MOCK_TEAMS = [

export const MOCK_TEAM_FETCH_RESPONSE = {
httpStatusCode: 200,
resource: MOCK_TEAMS
resource: MOCK_TEAMS.map(({ members_url, repositories_url, ...rest }) => rest)
};

export const MOCK_MEMBERS_PER_TEAM = [
Expand Down Expand Up @@ -139,3 +139,10 @@ export const MOCK_ERROR_RESPONSE = {
httpStatusCode: 500,
errors: [MOCK_ERROR]
};

export const MOCK_403_ERROR_MSG = { "message": "Must have admin rights to Repository." };

export const MOCK_403_ERROR_RESPONSE = {
httpStatusCode: 403,
errors: [MOCK_403_ERROR_MSG]
};
14 changes: 12 additions & 2 deletions test/unit/api-sdk/github/github.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
MOCK_MEMBERS_PER_TEAM,
MOCK_MEMBERS_PER_TEAM_RESPONSE,
MOCK_REPOS_PER_TEAM,
MOCK_REPOS_PER_TEAM_RESPONSE
MOCK_REPOS_PER_TEAM_RESPONSE,
MOCK_403_ERROR_MSG,
MOCK_403_ERROR_RESPONSE
} from '../../../mock/data.mock';
import { HttpResponse } from '../../../../src/http-request/type';

Expand Down Expand Up @@ -90,7 +92,7 @@ describe('Github sdk module test suites', () => {
test('Should return an object with an error property', async () => {
httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse(MOCK_TEAMS, 500, MOCK_ERROR));

const url = 'https://api.github.com/users/test/repos';
const url = 'https://api.github.com/users/test/teams';
const result = await github.getTeams(url);
expect(result).toEqual(MOCK_ERROR_RESPONSE);
});
Expand All @@ -103,4 +105,12 @@ describe('Github sdk module test suites', () => {

expect(result).toEqual(MOCK_REPO_FETCH_RESPONSE);
});

test('Should return an object with an error property when status code is 403 with no error response', async () => {
httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse(MOCK_403_ERROR_MSG, 403));

const url = 'https://api.github.com/users/test/repos';
const result = await github.getRepos(url);
expect(result).toEqual(MOCK_403_ERROR_RESPONSE);
});
});

0 comments on commit 15d4a60

Please sign in to comment.