Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update verification tag retrieval tests to enhance error handling #1842

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- run: composer install

- name: Run PHP-CS-Fixer
run: vendor/bin/php-cs-fixer fix --dry-run
uses: prestashopcorp/github-action-php-cs-fixer@master

phpstan:
name: PHPStan
Expand Down
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;
};

Loading