Skip to content

Commit

Permalink
Merge pull request #54 from HubSpot/br-unit-tests-5
Browse files Browse the repository at this point in the history
Add some HTTP tests and make some fixes to axios request handling
  • Loading branch information
brandenrodgers authored Nov 2, 2023
2 parents e844ae4 + 1122b44 commit 34a4ea9
Show file tree
Hide file tree
Showing 26 changed files with 257 additions and 170 deletions.
6 changes: 3 additions & 3 deletions api/customObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function batchCreateObjects(
): Promise<void> {
http.post<void>(accountId, {
url: `${CUSTOM_OBJECTS_API_PATH}/${objectTypeId}/batch/create`,
body: objects,
data: objects,
});
}

Expand All @@ -21,7 +21,7 @@ export async function createObjectSchema(
): Promise<Schema> {
return http.post(accountId, {
url: SCHEMA_API_PATH,
body: schema,
data: schema,
});
}

Expand All @@ -32,7 +32,7 @@ export async function updateObjectSchema(
): Promise<Schema> {
return http.patch(accountId, {
url: `${SCHEMA_API_PATH}/${schemaObjectType}`,
body: schema,
data: schema,
});
}

Expand Down
2 changes: 1 addition & 1 deletion api/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function buildPackage(
headers: {
Accept: 'text/plain',
},
body: {
data: {
folderPath,
},
});
Expand Down
8 changes: 4 additions & 4 deletions api/hubdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function createTable(
): Promise<Table> {
return http.post(accountId, {
url: `${HUBDB_API_PATH}/tables`,
body: schema,
data: schema,
});
}

Expand All @@ -36,7 +36,7 @@ export async function updateTable(
) {
return http.patch(accountId, {
url: `${HUBDB_API_PATH}/tables/${tableId}/draft`,
body: schema,
data: schema,
});
}

Expand Down Expand Up @@ -65,7 +65,7 @@ export async function createRows(
): Promise<CreateRowsResponse> {
return http.post(accountId, {
url: `${HUBDB_API_PATH}/tables/${tableId}/rows/draft/batch/create`,
body: { inputs: rows },
data: { inputs: rows },
});
}

Expand All @@ -87,6 +87,6 @@ export async function deleteRows(
): Promise<void> {
return http.post(accountId, {
url: `${HUBDB_API_PATH}/tables/${tableId}/rows/draft/batch/purge`,
body: { inputs: rowIds },
data: { inputs: rowIds },
});
}
6 changes: 3 additions & 3 deletions api/lighthouseScore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import http from '../http';
import { Body, QueryParams } from '../types/Http';
import { Data, QueryParams } from '../types/Http';
import {
GetLighthouseScoreResponse,
RequestLighthouseScoreResponse,
Expand All @@ -9,11 +9,11 @@ const LIGHTHOUSE_SCORE_API_BASE = 'quality-engine/v1/lighthouse';

export async function requestLighthouseScore(
accountId: number,
body: Body = {}
data: Data = {}
): Promise<RequestLighthouseScoreResponse> {
return http.post(accountId, {
url: `${LIGHTHOUSE_SCORE_API_BASE}/request`,
body,
data,
});
}

Expand Down
3 changes: 1 addition & 2 deletions api/localDevAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ export async function fetchAccessToken(
const axiosConfig = getAxiosConfig({
env,
localHostOverride: true,

url: `${LOCALDEVAUTH_API_AUTH_PATH}/refresh`,
body: {
data: {
encodedOAuthRefreshToken: personalAccessKey,
},
params: query,
Expand Down
6 changes: 3 additions & 3 deletions api/marketplaceValidation.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import http from '../http';
import { Body, QueryParams } from '../types/Http';
import { Data, QueryParams } from '../types/Http';
import { GetValidationResultsResponse } from '../types/MarketplaceValidation';

const VALIDATION_API_BASE = 'quality-engine/v1/validation';

export function requestValidation(
accountId: number,
body: Body = {}
data: Data = {}
): Promise<number> {
return http.post(accountId, {
url: `${VALIDATION_API_BASE}/request`,
body,
data,
});
}

Expand Down
4 changes: 2 additions & 2 deletions api/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function createProject(
): Promise<Project> {
return http.post(accountId, {
url: PROJECTS_API_PATH,
body: {
data: {
name,
},
});
Expand Down Expand Up @@ -147,7 +147,7 @@ export async function deployProject(
): Promise<ProjectDeployResponse> {
return http.post(accountId, {
url: `${PROJECTS_DEPLOY_API_PATH}/deploys/queue/async`,
body: {
data: {
projectName,
buildId,
},
Expand Down
2 changes: 1 addition & 1 deletion api/sandboxHubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function createSandbox(
type: string
): Promise<SandboxResponse> {
return http.post(accountId, {
body: { name, type, generatePersonalAccessKey: true }, // For CLI, generatePersonalAccessKey will always be true since we'll be saving the entry to the config
data: { name, type, generatePersonalAccessKey: true }, // For CLI, generatePersonalAccessKey will always be true since we'll be saving the entry to the config
timeout: SANDBOX_TIMEOUT,
url: SANDBOX_API_PATH_V2, // Create uses v2 for sandbox type and PAK generation support
});
Expand Down
2 changes: 1 addition & 1 deletion api/sandboxSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function initiateSync(
sandboxHubId: number
): Promise<InitiateSyncResponse> {
return http.post(fromHubId, {
body: {
data: {
command: 'SYNC',
fromHubId,
toHubId,
Expand Down
4 changes: 2 additions & 2 deletions api/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function addSecret(
): Promise<void> {
return http.post(accountId, {
url: SECRETS_API_PATH,
body: {
data: {
key,
secret: value,
},
Expand All @@ -27,7 +27,7 @@ export async function updateSecret(
): Promise<void> {
return http.put(accountId, {
url: SECRETS_API_PATH,
body: {
data: {
key,
secret: value,
},
Expand Down
2 changes: 1 addition & 1 deletion api/validateHubl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function validateHubl(
): Promise<Validation> {
return http.post(accountId, {
url: HUBL_VALIDATE_API_PATH,
body: {
data: {
template_source: sourceCode,
...hublValidationOptions,
},
Expand Down
40 changes: 18 additions & 22 deletions errors/__tests__/apiErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const newError = (overrides = {}): BaseError => {
return {
name: 'Error',
message: 'An error ocurred',
statusCode: 200,
status: 200,
errors: [],
...overrides,
};
Expand All @@ -32,7 +32,7 @@ export const newStatutsCodeError = (overrides = {}): GenericError => {
},
body: {},
headers: {},
statusCode: 200,
status: 200,
},
...overrides,
};
Expand All @@ -41,17 +41,17 @@ export const newStatutsCodeError = (overrides = {}): GenericError => {
describe('apiErrors', () => {
describe('isApiStatusCodeError', () => {
it('returns true for api status code errors', () => {
const error1 = newError({ statusCode: 100 });
const error2 = newError({ statusCode: 599 });
const error3 = newStatutsCodeError({ statusCode: 99 });
const error1 = newError({ status: 100 });
const error2 = newError({ status: 599 });
const error3 = newStatutsCodeError({ status: 99 });
expect(isApiStatusCodeError(error1)).toBe(true);
expect(isApiStatusCodeError(error2)).toBe(true);
expect(isApiStatusCodeError(error3)).toBe(true);
});

it('returns false for non api status code errors', () => {
const error1 = newError({ statusCode: 99 });
const error2 = newError({ statusCode: 600 });
const error1 = newError({ status: 99 });
const error2 = newError({ status: 600 });
expect(isApiStatusCodeError(error1)).toBe(false);
expect(isApiStatusCodeError(error2)).toBe(false);
});
Expand All @@ -60,15 +60,15 @@ describe('apiErrors', () => {
describe('isMissingScopeError', () => {
it('returns true for missing scope errors', () => {
const error1 = newStatutsCodeError({
statusCode: 403,
status: 403,
error: { category: 'MISSING_SCOPES' },
});
expect(isMissingScopeError(error1)).toBe(true);
});

it('returns false for non missing scope errors', () => {
const error1 = newStatutsCodeError({
statusCode: 400,
status: 400,
error: { category: 'MISSING_SCOPES' },
});
const error2 = newStatutsCodeError({ name: 'NonStatusCodeError' });
Expand All @@ -80,15 +80,15 @@ describe('apiErrors', () => {
describe('isGatingError', () => {
it('returns true for gating errors', () => {
const error1 = newStatutsCodeError({
statusCode: 403,
status: 403,
error: { category: 'GATED' },
});
expect(isGatingError(error1)).toBe(true);
});

it('returns false for non gating errors', () => {
const error1 = newStatutsCodeError({
statusCode: 400,
status: 400,
error: { category: 'GATED' },
});
const error2 = newStatutsCodeError({ name: 'NonStatusCodeError' });
Expand All @@ -100,11 +100,11 @@ describe('apiErrors', () => {
describe('isApiUploadValidationError', () => {
it('returns true for api upload validation errors', () => {
const error1 = newStatutsCodeError({
statusCode: 400,
status: 400,
response: { body: { message: 'upload validation error' } },
});
const error2 = newStatutsCodeError({
statusCode: 400,
status: 400,
response: { body: { errors: [] } },
});
expect(isApiUploadValidationError(error1)).toBe(true);
Expand All @@ -113,7 +113,7 @@ describe('apiErrors', () => {

it('returns false for non api upload validation errors', () => {
const error1 = newStatutsCodeError({
statusCode: 400,
status: 400,
response: { body: null },
});
const error2 = newStatutsCodeError({ name: 'NonStatusCodeError' });
Expand All @@ -124,17 +124,13 @@ describe('apiErrors', () => {

describe('isSpecifiedHubSpotAuthError', () => {
it('returns true for matching HubSpot auth errors', () => {
const error1 = newError({ name: 'HubSpotAuthError', statusCode: 123 });
expect(isSpecifiedHubSpotAuthError(error1, { statusCode: 123 })).toBe(
true
);
const error1 = newError({ name: 'HubSpotAuthError', status: 123 });
expect(isSpecifiedHubSpotAuthError(error1, { status: 123 })).toBe(true);
});

it('returns false for non matching HubSpot auth errors', () => {
const error1 = newError({ name: 'HubSpotAuthError', statusCode: 123 });
expect(isSpecifiedHubSpotAuthError(error1, { statusCode: 124 })).toBe(
false
);
const error1 = newError({ name: 'HubSpotAuthError', status: 123 });
expect(isSpecifiedHubSpotAuthError(error1, { status: 124 })).toBe(false);
});
});

Expand Down
24 changes: 12 additions & 12 deletions errors/apiErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { HubSpotAuthError } from '../models/HubSpotAuthError';
export function isApiStatusCodeError(err: GenericError): boolean {
return (
err.name === 'StatusCodeError' ||
(!!err.statusCode && err.statusCode >= 100 && err.statusCode < 600)
(!!err.status && err.status >= 100 && err.status < 600)
);
}

export function isMissingScopeError(err: GenericError): boolean {
return (
isApiStatusCodeError(err) &&
err.statusCode === 403 &&
err.status === 403 &&
!!err.error &&
err.error.category === 'MISSING_SCOPES'
);
Expand All @@ -27,7 +27,7 @@ export function isMissingScopeError(err: GenericError): boolean {
export function isGatingError(err: GenericError): boolean {
return (
isApiStatusCodeError(err) &&
err.statusCode === 403 &&
err.status === 403 &&
!!err.error &&
err.error.category === 'GATED'
);
Expand All @@ -36,7 +36,7 @@ export function isGatingError(err: GenericError): boolean {
export function isApiUploadValidationError(err: GenericError): boolean {
return (
isApiStatusCodeError(err) &&
err.statusCode === 400 &&
err.status === 400 &&
!!err.response &&
!!err.response.body &&
!!(err.response.body.message || !!err.response.body.errors)
Expand All @@ -45,9 +45,9 @@ export function isApiUploadValidationError(err: GenericError): boolean {

export function isSpecifiedHubSpotAuthError(
err: GenericError,
{ statusCode, category, subCategory }: Partial<HubSpotAuthError>
{ status, category, subCategory }: Partial<HubSpotAuthError>
): boolean {
const statusCodeErr = !statusCode || err.statusCode === statusCode;
const statusCodeErr = !status || err.status === status;
const categoryErr = !category || err.category === category;
const subCategoryErr = !subCategory || err.subCategory === subCategory;
return Boolean(
Expand Down Expand Up @@ -101,9 +101,9 @@ export function throwStatusCodeError(
error: StatusCodeError,
context: StatusCodeErrorContext = {}
): never {
const { statusCode, message, response } = error;
const { status, message, response } = error;
const errorData = JSON.stringify({
statusCode,
status,
message,
url: response ? response.request.href : null,
method: response ? response.request.method : null,
Expand All @@ -122,7 +122,7 @@ export function throwApiStatusCodeError(
context: StatusCodeErrorContext = {}
): never {
const i18nKey = 'errors.errorTypes.api';
const { statusCode } = error;
const { status } = error;
const { method } = error.options || {};
const { projectName } = context;

Expand Down Expand Up @@ -152,7 +152,7 @@ export function throwApiStatusCodeError(
}
const isProjectMissingScopeError = isMissingScopeError(error) && projectName;
const isProjectGatingError = isGatingError(error) && projectName;
switch (statusCode) {
switch (status) {
case 400:
errorMessage.push(i18n(`${i18nKey}.codes.400`, { messageDetail }));
break;
Expand Down Expand Up @@ -196,11 +196,11 @@ export function throwApiStatusCodeError(
errorMessage.push(i18n(`${i18nKey}.codes.503`, { messageDetail }));
break;
default:
if (statusCode && statusCode >= 500 && statusCode < 600) {
if (status && status >= 500 && status < 600) {
errorMessage.push(
i18n(`${i18nKey}.codes.500Generic`, { messageDetail })
);
} else if (statusCode && statusCode >= 400 && statusCode < 500) {
} else if (status && status >= 400 && status < 500) {
errorMessage.push(
i18n(`${i18nKey}.codes.400Generic`, { messageDetail })
);
Expand Down
Loading

0 comments on commit 34a4ea9

Please sign in to comment.