-
Notifications
You must be signed in to change notification settings - Fork 0
Fix: don't redirect to /signin on callback session failure #59
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
Merged
Jose-Gael-Cruz-Lopez
merged 2 commits into
main
from
claude/sapling-codebase-analysis-quypQ
Apr 15, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /** | ||
| * Tests for app/signin/callback/page.tsx | ||
| * | ||
| * Covers the new error branches added to the OAuth callback handler: | ||
| * - Successful session → redirect to /dashboard | ||
| * - 403 from session API → redirect to /pending | ||
| * - Other failures from session API → inline error | ||
| * - Network errors → inline error | ||
| * - Missing user_id/name params → inline error | ||
| * - Missing is_approved param → inline error (not a /pending redirect) | ||
| * - Explicit is_approved=false → /pending redirect | ||
| */ | ||
| import React from 'react'; | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
|
|
||
| const replace = jest.fn(); | ||
| const setActiveUser = jest.fn(); | ||
| const confirmApproved = jest.fn(); | ||
| let searchParams = new URLSearchParams(); | ||
|
|
||
| jest.mock('next/navigation', () => ({ | ||
| useRouter: () => ({ replace }), | ||
| useSearchParams: () => searchParams, | ||
| })); | ||
|
|
||
| jest.mock('@/context/UserContext', () => ({ | ||
| useUser: () => ({ setActiveUser, confirmApproved }), | ||
| })); | ||
|
|
||
| import CallbackPage from '@/app/signin/callback/page'; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| searchParams = new URLSearchParams(); | ||
| global.fetch = jest.fn(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // @ts-expect-error allow cleanup | ||
| delete global.fetch; | ||
| }); | ||
|
|
||
| function setParams(params: Record<string, string>) { | ||
| searchParams = new URLSearchParams(params); | ||
| } | ||
|
|
||
| describe('signin/callback page', () => { | ||
| it('redirects to /dashboard on a successful session', async () => { | ||
| setParams({ user_id: 'u1', name: 'Ada', is_approved: 'true' }); | ||
| (global.fetch as jest.Mock).mockResolvedValue({ ok: true, status: 200 }); | ||
|
|
||
| render(<CallbackPage />); | ||
|
|
||
| await waitFor(() => expect(replace).toHaveBeenCalledWith('/dashboard')); | ||
| expect(setActiveUser).toHaveBeenCalledWith('u1', 'Ada', ''); | ||
| expect(confirmApproved).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('redirects to /pending when session API returns 403', async () => { | ||
| setParams({ user_id: 'u1', name: 'Ada', is_approved: 'true' }); | ||
| (global.fetch as jest.Mock).mockResolvedValue({ ok: false, status: 403 }); | ||
|
|
||
| render(<CallbackPage />); | ||
|
|
||
| await waitFor(() => expect(replace).toHaveBeenCalledWith('/pending')); | ||
| expect(confirmApproved).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('shows an inline error when session API returns a non-403 failure', async () => { | ||
| setParams({ user_id: 'u1', name: 'Ada', is_approved: 'true' }); | ||
| (global.fetch as jest.Mock).mockResolvedValue({ ok: false, status: 500 }); | ||
|
|
||
| render(<CallbackPage />); | ||
|
|
||
| expect(await screen.findByText(/unable to complete sign-in/i)).toBeInTheDocument(); | ||
| expect(replace).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('shows an inline error when the session fetch rejects', async () => { | ||
| setParams({ user_id: 'u1', name: 'Ada', is_approved: 'true' }); | ||
| (global.fetch as jest.Mock).mockRejectedValue(new Error('network down')); | ||
|
|
||
| render(<CallbackPage />); | ||
|
|
||
| expect(await screen.findByText(/unable to reach the server/i)).toBeInTheDocument(); | ||
| expect(replace).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('shows an inline error when user_id is missing', async () => { | ||
| setParams({ name: 'Ada', is_approved: 'true' }); | ||
|
|
||
| render(<CallbackPage />); | ||
|
|
||
| expect(await screen.findByText(/sign-in failed/i)).toBeInTheDocument(); | ||
| expect(global.fetch).not.toHaveBeenCalled(); | ||
| expect(replace).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('shows an inline error when is_approved is missing entirely', async () => { | ||
| setParams({ user_id: 'u1', name: 'Ada' }); | ||
|
|
||
| render(<CallbackPage />); | ||
|
|
||
| expect(await screen.findByText(/sign-in failed/i)).toBeInTheDocument(); | ||
| expect(replace).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('redirects to /pending when is_approved is explicitly "false"', async () => { | ||
| setParams({ user_id: 'u1', name: 'Ada', is_approved: 'false' }); | ||
|
|
||
| render(<CallbackPage />); | ||
|
|
||
| await waitFor(() => expect(replace).toHaveBeenCalledWith('/pending')); | ||
| }); | ||
|
|
||
| it('redirects to /pending when error=not_approved is set', async () => { | ||
| setParams({ error: 'not_approved' }); | ||
|
|
||
| render(<CallbackPage />); | ||
|
|
||
| await waitFor(() => expect(replace).toHaveBeenCalledWith('/pending')); | ||
| }); | ||
| }); | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a regression test for missing
namequery param.The callback logic requires both
user_idandname, but this suite currently only asserts the missing-user_idbranch. Please add a missing-namecase to lock the behavior.Based on learnings: "If you change how a feature works (not just its interface), update the tests that cover it."
🤖 Prompt for AI Agents