From 1d4f4ac0b054849960ff8ea2a0bac8aa55afba94 Mon Sep 17 00:00:00 2001 From: Mouhajer-CO Date: Wed, 10 Jan 2024 15:23:27 +0000 Subject: [PATCH 1/3] Fix bug related to equal and greater than response status code --- src/api-sdk/github/github.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/api-sdk/github/github.ts b/src/api-sdk/github/github.ts index 15cd689..d8dab5c 100644 --- a/src/api-sdk/github/github.ts +++ b/src/api-sdk/github/github.ts @@ -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); } From 2aeec9f52e65a2845be773a77f8dd7e161600813 Mon Sep 17 00:00:00 2001 From: Mouhajer-CO Date: Wed, 10 Jan 2024 16:00:41 +0000 Subject: [PATCH 2/3] Update mapping and fix related unit tests --- src/api-sdk/github/mapping.ts | 7 ++----- src/api-sdk/github/type.ts | 3 --- test/mock/data.mock.ts | 4 ++-- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/api-sdk/github/mapping.ts b/src/api-sdk/github/mapping.ts index 4d9491a..a64f289 100644 --- a/src/api-sdk/github/mapping.ts +++ b/src/api-sdk/github/mapping.ts @@ -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 })); }; @@ -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 })); }; diff --git a/src/api-sdk/github/type.ts b/src/api-sdk/github/type.ts index 724c787..0ad6539 100644 --- a/src/api-sdk/github/type.ts +++ b/src/api-sdk/github/type.ts @@ -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 { diff --git a/test/mock/data.mock.ts b/test/mock/data.mock.ts index 430349e..a15999f 100644 --- a/test/mock/data.mock.ts +++ b/test/mock/data.mock.ts @@ -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 = [ @@ -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 = [ From 8f3548685d3273658a58a5d64c2349467ee6fbde Mon Sep 17 00:00:00 2001 From: Mouhajer-CO Date: Wed, 10 Jan 2024 16:19:03 +0000 Subject: [PATCH 3/3] Add unit test for correct response object when status code is over 400 with no error response --- test/mock/data.mock.ts | 7 +++++++ test/unit/api-sdk/github/github.spec.ts | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/test/mock/data.mock.ts b/test/mock/data.mock.ts index a15999f..1b9e275 100644 --- a/test/mock/data.mock.ts +++ b/test/mock/data.mock.ts @@ -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] +}; diff --git a/test/unit/api-sdk/github/github.spec.ts b/test/unit/api-sdk/github/github.spec.ts index 4ddc982..4ee36fd 100644 --- a/test/unit/api-sdk/github/github.spec.ts +++ b/test/unit/api-sdk/github/github.spec.ts @@ -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'; @@ -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); }); @@ -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); + }); });