Skip to content

fix: freeze time to avoid jwt expiration#38492

Merged
wgu-taylor-payne merged 1 commit intoopenedx:masterfrom
WGU-Open-edX:tpayne/fix-flaky-jwt-tests
Apr 30, 2026
Merged

fix: freeze time to avoid jwt expiration#38492
wgu-taylor-payne merged 1 commit intoopenedx:masterfrom
WGU-Open-edX:tpayne/fix-flaky-jwt-tests

Conversation

@wgu-taylor-payne
Copy link
Copy Markdown
Contributor

@wgu-taylor-payne wgu-taylor-payne commented Apr 30, 2026

Description

Fixes flaky ExpiredSignatureError failures in openedx/core/lib/tests/test_jwt.py by freezing time during test execution.

Root cause

test_now = int(time()) is evaluated once at module import time. Tokens are created with
exp = test_now + 1000. When unpack_and_verify calls jwt.decode, PyJWT checks exp against
the real wall clock via time.time(). If the test runs more than 1000 seconds after the module
was imported (due to test ordering or slow CI), the token is already expired.

Fix

Wrap both test classes with @freeze_time at the test_now timestamp so that time.time()
returns a consistent value during test execution. This ensures PyJWT's internal exp check
sees the same clock that was used to create the token.

Also converts assertions to pytest style (assert ==, pytest.raises) and removes the
corresponding # noqa suppressions.

Also removes unused variables (expected_token_with_claims) in a couple of methods.

No production code changes.

Supporting information

Testing instructions

  1. Run the test file:
    pytest openedx/core/lib/tests/test_jwt.py --ds=lms.envs.test --no-migrations -x -v
    
  2. All 10 tests should pass.

Deadline

None

Other information

  • No dependencies on other changes.
  • freezegun is already in test requirements (requirements/edx/testing.in).
  • Used Kiro (AI assistant) to help develop this change. Kiro wrote the base implementation, ran tests/linting. I reviewed the approach, made some edits (comments, naming), directed the assertion style conversion to pytest, chose the decorator ordering, and verified the fix didn't drop any meaningful test coverage.

@openedx-webhooks openedx-webhooks added open-source-contribution PR author is not from Axim or 2U core contributor PR author is a Core Contributor (who may or may not have write access to this repo). labels Apr 30, 2026
@openedx-webhooks
Copy link
Copy Markdown

Thanks for the pull request, @wgu-taylor-payne!

This repository is currently maintained by @openedx/wg-maintenance-openedx-platform.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@github-project-automation github-project-automation Bot moved this to Needs Triage in Contributions Apr 30, 2026
@wgu-taylor-payne wgu-taylor-payne requested review from a team, feanil and robrap April 30, 2026 20:22
Copy link
Copy Markdown
Contributor

@robrap robrap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@wgu-taylor-payne wgu-taylor-payne merged commit 33f33ef into openedx:master Apr 30, 2026
42 checks passed
@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in Contributions Apr 30, 2026
wgu-taylor-payne added a commit to WGU-Open-edX/openedx-platform that referenced this pull request May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core contributor PR author is a Core Contributor (who may or may not have write access to this repo). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants