Skip to content

Commit

Permalink
Merge branch 'main' into port-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
camden11 committed Oct 31, 2023
2 parents 1d30147 + e844ae4 commit 91a3228
Show file tree
Hide file tree
Showing 22 changed files with 1,316 additions and 90 deletions.
4 changes: 3 additions & 1 deletion api/localDevAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ const LOCALDEVAUTH_API_AUTH_PATH = 'localdevauth/v1/auth';

type AccessTokenResponse = {
hubId: number;
userId: number;
oauthAccessToken: string;
expiresAtMillis: number;
enabledFeatures?: { [key: string]: number };
scopeGroups: Array<string>;
encodedOauthRefreshToken: string;
encodedOAuthRefreshToken: string;
};

export async function fetchAccessToken(
Expand Down
168 changes: 168 additions & 0 deletions errors/__tests__/apiErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import {
isApiStatusCodeError,
isMissingScopeError,
isGatingError,
isApiUploadValidationError,
isSpecifiedHubSpotAuthError,
throwStatusCodeError,
throwApiStatusCodeError,
throwApiError,
throwApiUploadError,
} from '../apiErrors';
import { BaseError, GenericError, StatusCodeError } from '../../types/Error';

export const newError = (overrides = {}): BaseError => {
return {
name: 'Error',
message: 'An error ocurred',
statusCode: 200,
errors: [],
...overrides,
};
};

export const newStatutsCodeError = (overrides = {}): GenericError => {
return {
...newError(),
name: 'StatusCodeError',
response: {
request: {
href: 'http://example.com/',
method: 'GET',
},
body: {},
headers: {},
statusCode: 200,
},
...overrides,
};
};

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 });
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 });
expect(isApiStatusCodeError(error1)).toBe(false);
expect(isApiStatusCodeError(error2)).toBe(false);
});
});

describe('isMissingScopeError', () => {
it('returns true for missing scope errors', () => {
const error1 = newStatutsCodeError({
statusCode: 403,
error: { category: 'MISSING_SCOPES' },
});
expect(isMissingScopeError(error1)).toBe(true);
});

it('returns false for non missing scope errors', () => {
const error1 = newStatutsCodeError({
statusCode: 400,
error: { category: 'MISSING_SCOPES' },
});
const error2 = newStatutsCodeError({ name: 'NonStatusCodeError' });
expect(isMissingScopeError(error1)).toBe(false);
expect(isMissingScopeError(error2)).toBe(false);
});
});

describe('isGatingError', () => {
it('returns true for gating errors', () => {
const error1 = newStatutsCodeError({
statusCode: 403,
error: { category: 'GATED' },
});
expect(isGatingError(error1)).toBe(true);
});

it('returns false for non gating errors', () => {
const error1 = newStatutsCodeError({
statusCode: 400,
error: { category: 'GATED' },
});
const error2 = newStatutsCodeError({ name: 'NonStatusCodeError' });
expect(isGatingError(error1)).toBe(false);
expect(isGatingError(error2)).toBe(false);
});
});

describe('isApiUploadValidationError', () => {
it('returns true for api upload validation errors', () => {
const error1 = newStatutsCodeError({
statusCode: 400,
response: { body: { message: 'upload validation error' } },
});
const error2 = newStatutsCodeError({
statusCode: 400,
response: { body: { errors: [] } },
});
expect(isApiUploadValidationError(error1)).toBe(true);
expect(isApiUploadValidationError(error2)).toBe(true);
});

it('returns false for non api upload validation errors', () => {
const error1 = newStatutsCodeError({
statusCode: 400,
response: { body: null },
});
const error2 = newStatutsCodeError({ name: 'NonStatusCodeError' });
expect(isApiUploadValidationError(error1)).toBe(false);
expect(isApiUploadValidationError(error2)).toBe(false);
});
});

describe('isSpecifiedHubSpotAuthError', () => {
it('returns true for matching HubSpot auth errors', () => {
const error1 = newError({ name: 'HubSpotAuthError', statusCode: 123 });
expect(isSpecifiedHubSpotAuthError(error1, { statusCode: 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
);
});
});

describe('throwStatusCodeError', () => {
it('throws status code error', () => {
const error = newStatutsCodeError() as StatusCodeError;
expect(() => throwStatusCodeError(error)).toThrow();
});
});

describe('throwApiStatusCodeError', () => {
it('throws api status code error', () => {
const error = newStatutsCodeError() as StatusCodeError;
expect(() => throwApiStatusCodeError(error)).toThrow();
});
});

describe('throwApiError', () => {
it('throws api error', () => {
const error = newStatutsCodeError() as StatusCodeError;
expect(() => throwApiError(error)).toThrow();
});
});

describe('throwApiUploadError', () => {
it('throws api upload error', () => {
const error = newStatutsCodeError() as StatusCodeError;
expect(() => throwApiUploadError(error)).toThrow();
});
});
});
81 changes: 81 additions & 0 deletions errors/__tests__/standardErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {
isSystemError,
isFatalError,
throwErrorWithMessage,
throwTypeErrorWithMessage,
throwAuthErrorWithMessage,
throwError,
} from '../standardErrors';
import { BaseError, StatusCodeError } from '../../types/Error';
import { HubSpotAuthError } from '../../models/HubSpotAuthError';

export const newError = (overrides = {}): BaseError => {
return {
name: 'Error',
message: 'An error ocurred',
errno: 1,
code: 'error_code',
syscall: 'error_syscall',
errors: [],
...overrides,
};
};

describe('standardErrors', () => {
describe('isSystemError', () => {
it('returns true for system errors', () => {
const error = newError();
expect(isSystemError(error)).toBe(true);
});

it('returns false for non system errors', () => {
const error1 = newError({ errno: null });
const error2 = newError({ code: null });
const error3 = newError({ syscall: null });
expect(isSystemError(error1)).toBe(false);
expect(isSystemError(error2)).toBe(false);
expect(isSystemError(error3)).toBe(false);
});
});

describe('isFatalError', () => {
it('returns true for fatal errors', () => {
const cause = newError() as StatusCodeError;
const error = new HubSpotAuthError('A fatal auth error', { cause });
expect(isFatalError(error)).toBe(true);
});

it('returns false for non fatal errors', () => {
const error = newError();
expect(isFatalError(error)).toBe(false);
});
});

describe('throwErrorWithMessage', () => {
it('throws error with message', () => {
const error = newError();
expect(() => throwErrorWithMessage('', {}, error)).toThrow();
});
});

describe('throwTypeErrorWithMessage', () => {
it('throws type error with message', () => {
const error = newError();
expect(() => throwTypeErrorWithMessage('', {}, error)).toThrow();
});
});

describe('throwAuthErrorWithMessage', () => {
it('throws auth error with message', () => {
const error = newError() as StatusCodeError;
expect(() => throwAuthErrorWithMessage('', {}, error)).toThrow();
});
});

describe('throwError', () => {
it('throws error', () => {
const error = newError();
expect(() => throwError(error)).toThrow();
});
});
});
Loading

0 comments on commit 91a3228

Please sign in to comment.