-
-
Notifications
You must be signed in to change notification settings - Fork 71
fix: sanitize username parameter by stripping leading @ symbol to prevent search API 422 errors #164
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
Open
AbiramiR-27
wants to merge
2
commits into
AOSSIE-Org:main
Choose a base branch
from
AbiramiR-27:fix/contributor-profile-422
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+151
−13
Open
fix: sanitize username parameter by stripping leading @ symbol to prevent search API 422 errors #164
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import React from 'react' | ||
| import { render, screen, waitFor } from '@testing-library/react' | ||
| import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest' | ||
| import ContributorProfilePage from './ContributorProfilePage' | ||
|
|
||
| global.ResizeObserver = class ResizeObserver { | ||
| observe() {} | ||
| unobserve() {} | ||
| disconnect() {} | ||
| } | ||
|
|
||
| vi.mock('react-router-dom', () => ({ | ||
| useParams: () => ({ username: 'rohan-pandeyy' }), | ||
| useNavigate: () => vi.fn() | ||
| })) | ||
|
|
||
| const mockUseApp = { | ||
| orgs: [{ login: 'AOSSIE-Org' }], | ||
| pat: 'mock-pat', | ||
| pullsData: {}, | ||
| isComplete: true, | ||
| loading: false, | ||
| runFullExplore: vi.fn() | ||
| } | ||
|
|
||
| vi.mock('../context/AppContext', () => ({ | ||
| useApp: () => mockUseApp | ||
| })) | ||
|
|
||
| describe('ContributorProfilePage fetch retry logic', () => { | ||
| let fetchMock | ||
|
|
||
| beforeEach(() => { | ||
| fetchMock = vi.spyOn(global, 'fetch') | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| it('retries with public unauthenticated headers when authenticated search fails with HTTP 422', async () => { | ||
| const todayIso = new Date().toISOString().slice(0, 10) | ||
| // Mock sequence: | ||
| // 1 & 2: Authenticated fetches fail with 422 | ||
| // 3 & 4: Unauthenticated retries succeed | ||
| fetchMock | ||
| .mockResolvedValueOnce({ | ||
| status: 422, | ||
| ok: false, | ||
| headers: new Headers() | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| status: 422, | ||
| ok: false, | ||
| headers: new Headers() | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| status: 200, | ||
| ok: true, | ||
| headers: new Headers(), | ||
| json: async () => ({ items: [{ id: 1, title: 'Mock PR', number: 42, created_at: todayIso, html_url: 'http://url', pull_request: {} }] }) | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| status: 200, | ||
| ok: true, | ||
| headers: new Headers(), | ||
| json: async () => ({ items: [] }) | ||
| }) | ||
|
|
||
| render(<ContributorProfilePage />) | ||
|
|
||
| // Wait for the mock issues title to appear | ||
| await waitFor(() => { | ||
| expect(screen.getByText('Mock PR')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| // Verify fetch was called 4 times in total (2 authenticated + 2 public retries) | ||
| expect(fetchMock).toHaveBeenCalledTimes(4) | ||
|
|
||
| // Verify first fetch calls included the Authorization header | ||
| const firstCallHeaders = fetchMock.mock.calls[0][1].headers | ||
| expect(firstCallHeaders.Authorization).toBe('token mock-pat') | ||
|
|
||
| // Verify fallback fetch calls did NOT include the Authorization header | ||
| const fallbackCallHeaders = fetchMock.mock.calls[2][1].headers | ||
| expect(fallbackCallHeaders.Authorization).toBeUndefined() | ||
| }) | ||
|
|
||
| it('does not retry and propagates error for non-422 failures', async () => { | ||
| // Mock authenticated fetch failing with 500 Internal Server Error | ||
| fetchMock.mockResolvedValue({ | ||
| status: 500, | ||
| ok: false, | ||
| headers: new Headers() | ||
| }) | ||
|
|
||
| render(<ContributorProfilePage />) | ||
|
|
||
| // Wait for error card to render | ||
| await waitFor(() => { | ||
| expect(screen.getByText('Failed to fetch contributor details: HTTP_500')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| // Verify fetch was called only twice (1 for main search, 1 for merged search running in parallel) | ||
| // and no unauthenticated retries were executed | ||
| expect(fetchMock).toHaveBeenCalledTimes(2) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.