Skip to content

Commit

Permalink
chore: Update verification tag retrieval tests to enhance error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kseghair committed Jan 16, 2025
1 parent e091e37 commit fcac012
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { HttpClientError } from "mktg-with-google-common";
import { runRetrievalOfVerificationTag } from "./verification-tag-retriever";
import * as Sentry from '@sentry/browser';

jest.mock('@sentry/browser');

describe('runRetrievalOfVerificationTag', () => {
beforeEach(() => {
jest.clearAllMocks(); // Clear any previous mock calls
});

it('runs the whole process', async () => {
// prepare
const fetchOnboardingMock = jest.fn();
Expand Down Expand Up @@ -84,4 +91,55 @@ describe('runRetrievalOfVerificationTag', () => {
expect(fetchOnboardingMock).toBeCalledTimes(1);
expect(fetchShopMock).toBeCalledTimes(0);
});
});

it('handles 403 Forbidden error gracefully', async () => {
// prepare
const fetchOnboardingMock = jest.fn();
const fetchShopMock = jest.fn();
fetchOnboardingMock.mockImplementationOnce(() => {
throw new HttpClientError("subscription not found", 403);
});

// act
await runRetrievalOfVerificationTag(fetchOnboardingMock, fetchShopMock);

// assert
expect(fetchOnboardingMock).toBeCalledTimes(1);
expect(fetchShopMock).toBeCalledTimes(0);
expect(Sentry.captureException).not.toHaveBeenCalled(); // Ensure Sentry is not called
});

it('handles 404 Not Found error', async () => {
// prepare
const fetchOnboardingMock = jest.fn();
const fetchShopMock = jest.fn();
fetchOnboardingMock.mockImplementationOnce(() => {
throw new HttpClientError("oh no", 404);
});

// act
await runRetrievalOfVerificationTag(fetchOnboardingMock, fetchShopMock);

// assert
expect(fetchOnboardingMock).toBeCalledTimes(1);
expect(fetchShopMock).toBeCalledTimes(0);
expect(Sentry.captureException).toHaveBeenCalled(); // Ensure Sentry is called
});

it('stops when token retrieval fails', async () => {
// prepare
const fetchOnboardingMock = jest.fn();
const fetchShopMock = jest.fn();
fetchOnboardingMock.mockImplementationOnce(() => {
throw new HttpClientError("Token retrieval failed", 500);
});

// act
await runRetrievalOfVerificationTag(fetchOnboardingMock, fetchShopMock);

// assert
expect(fetchOnboardingMock).toBeCalledTimes(1);
expect(fetchShopMock).toBeCalledTimes(0);
expect(Sentry.captureException).toHaveBeenCalled(); // Ensure Sentry is called
});
});
11 changes: 7 additions & 4 deletions _dev/apps/verification-tag/src/verification-tag-retriever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const runRetrievalOfVerificationTag = async (
onResponse: responseHandler,
},
)).json();

// 2- Store token in shop
await fetchShop("setWebsiteVerificationMeta", {
websiteVerificationMeta: token,
Expand All @@ -48,14 +48,18 @@ export const runRetrievalOfVerificationTag = async (
onResponse: responseHandler,
},
);

console.info('Marketing with Google - Google Verification tag has been refreshed.');
analytics?.track('[GGL] Re-verification & claiming Succeeded');
} catch (e) {
console.error('Marketing with Google - Google Verification tag refresh failed.', e);
analytics?.track('[GGL] Re-verification & claiming Failed');
scope.setTag('correlationId', correlationId);
Sentry.captureException(e, scope);

// Send error to Sentry if it's not a 403 Forbidden error
if (e.code !== 403) {
Sentry.captureException(e, scope);
}
}
};

Expand All @@ -82,4 +86,3 @@ const responseHandler = async (response: Response) => {
}
return response;
};

0 comments on commit fcac012

Please sign in to comment.