Skip to content

CallbackClient.test.tsx's role-redirect tests mock user as already-resolved, so they can't catch the stale-closure bug in the real AuthContext timing #418

Closed

Description

@abayomicornelius

Problem

src/app/auth/callback/CallbackClient.test.tsx's "role-based redirect (issue #77)" tests mock useAuth() to return an already-resolved user from the component's very first render:

it("redirects maintainer to /dashboard/maintainer", async () => {
  mockedUseAuth.mockReturnValue({
    login: jest.fn().mockResolvedValue(undefined),
    user: makeUser(["maintainer"]),   // <- populated from the start
  });
  render(<CallbackClient />);
  await waitFor(() => {
    expect(mockReplace).toHaveBeenCalledWith("/dashboard/maintainer");
  });
});

This does not reproduce how AuthContext actually behaves in the running app. In production, user starts null (nobody is signed in yet when they land on /auth/callback mid-OAuth-flow), and only becomes the real, role-bearing profile after login()'s internal refresh() call resolves and calls setUser(...) — asynchronously, after CallbackClient's effect has already created its .then() closure over the pre-login user value. See the companion issue in this batch ("CallbackClient's redirectForRoles(user?.roles) reads a stale pre-login closure value") for the resulting bug.

Because this test's mock hands CallbackClient an already-correct user synchronously, on the very first (and, in this mocked scenario, only) render, the stale-closure timing gap that the real AuthContext creates never has a chance to manifest — the test can't fail even if redirectForRoles is reading a stale value, because in this mock there's no "stale" version of user to read in the first place.

Impact

These tests give false confidence: they're titled and framed as verifying #77's fix ("role-based redirect"), and they pass, but they don't actually exercise the timing behavior that makes #77's fix (as currently implemented) not work correctly in the real app. A genuine regression test for this needs user to transition from null to a resolved profile after login() resolves, mirroring AuthContext's real login() → refresh() → setUser() sequence — which is exactly the scenario these tests don't construct.

Suggested fix

Add a test where the mocked useAuth() starts with user: null, and login is a mock whose resolution triggers a re-render with user now populated (e.g. by having the mock's login implementation call a rerender()-triggering state update, or by using a stateful mock hook rather than a single static mockReturnValue) — the way AuthContext.test.tsx itself models asynchronous state transitions elsewhere in this codebase — so this test suite can actually catch the stale-closure bug described in the companion issue.

Activity

  1. added
    testing
    bug
    on Sep 17, 2026
  2. Ranjeet2063 commented on Sep 20, 2026

    @Ranjeet2063

    Hi maintainers,

    I would like to take on and implement this issue: CallbackClient.test.tsx's role-redirect tests mock user as already-resolved, so they can't catch the stale-closure bug in the real AuthContext timing.

    Technical Implementation Roadmap:

    1. Threat Model & Root Cause Mitigation: Enforce strict parameterized inputs, defensive validation barriers, and eliminate any injection or reentrancy vectors.
    2. Host Invocation & Access Control Matrix: Implement multi-role authorization enforcement and host invocation validation checks.
    3. Adversarial Security Test Cases: Write targeted regression tests simulating malicious payloads, unauthorized callers, and boundary exploit scenarios.
    4. Zero-Regression Guarantee: Verify complete backward compatibility with existing contract interfaces and passing CI workflows.

    I am ready to begin implementation immediately upon assignment and will deliver a high-quality, fully-tested PR. Please assign this issue to me!

  3. added
    Stellar Wave
    on Sep 23, 2026
  4. Oprimecloud commented on Sep 23, 2026

    @Oprimecloud

    @Oprimecloud has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    Hi team

    I can help you rewrite the CallbackClient.test.tsx suite to properly catch this stale-closure bug. As a React/TypeScript developer, I have dealt extensively with asynchronous state transitions and Jest testing, and I know exactly how to model this.

    I will implement a stateful mock for useAuth() that accurately simulates production behavior: starting with user: null and dynamically updating the mock state when login() resolves. This will ensure the test genuinely exercises the AuthContext timing gap and prevents false confidence.

    A quick professional suggestion:
    Since this test exposes a real bug in CallbackClient (mentioned in the companion issue), I suggest we update the login() function to return the resolved user object directly so the component does not have to rely on a useEffect closure or dependency array to trigger the redirect. I would be happy to fix both the test suite and the actual component logic in a single PR for you.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Oprimecloud to this issue.

  5. priyankbhikadiya commented on Sep 23, 2026

    @priyankbhikadiya

    @priyankbhikadiya has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    I'd like to work on this issue because it's a great opportunity to contribute to the project while improving the user experience. I have experience with JavaScript, Node.js, Python, and frontend development, and I'm comfortable working with Git/GitHub and following existing project standards to deliver a clean implementation.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @priyankbhikadiya to this issue.

  6. davidjhonsonmate commented on Sep 23, 2026

    @davidjhonsonmate
    Contributor

    @davidjhonsonmate has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    please assign this issue to me

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @davidjhonsonmate to this issue.

  7. drips-wave commented on Sep 23, 2026

    @drips-wave

    Congratulations, @davidjhonsonmate! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on September 30, 2026.

    🧑‍💻 @davidjhonsonmate: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

    Warning

    When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

    🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

    🌊 Happy Wave 🌊

  8. added 2 commits that reference this issue on Sep 25, 2026
  9. drips-wave commented on Sep 25, 2026

    @drips-wave

    This issue has been completed by @davidjhonsonmate as part of the Stellar Wave Program's 9th Wave 🥳

    😎 @davidjhonsonmate: You earned 200 Points for completing this issue! After the current Wave ends, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here. You can also Leave a review to share your experience working on this issue.

    🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

  10. removed
    Stellar Wave
    on Oct 15, 2026
  11. added
    Stellar Wave
    on Oct 15, 2026
  12. removed
    Stellar Wave
    on Oct 15, 2026
  13. added
    Stellar Wave
    on Oct 15, 2026
  14. removed
    Stellar Wave
    on Oct 15, 2026
  15. added
    Stellar Wave
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Stellar Wavebugtesting

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions