Skip to content

[FIX] Handle missing test in restart_test and stop_test - #1249

Open
youknowme19 wants to merge 1 commit into
CCExtractor:masterfrom
youknowme19:fix/handle-missing-test-in-restart-and-stop
Open

youknowme19 wants to merge 1 commit into
CCExtractor:masterfrom
youknowme19:fix/handle-missing-test-in-restart-and-stop

Conversation

@youknowme19

Copy link
Copy Markdown

In raising this pull request, I confirm the following (please check boxes):

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • I have considered, and confirmed that this submission will be valuable to others.
  • I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  • I give this submission freely, and claim no ownership to its content.

My familiarity with the project is as follows (check one):

  • I have never used the project.
  • I have used the project briefly.
  • I have used the project extensively, but have not contributed previously.
  • I am an active contributor to the project.

Description

This pull request resolves an issue where requesting /test/restart_test/<test_id> or /test/stop_test/<test_id> with a non-existent test_id results in an unhandled AttributeError (HTTP 500) for administrators or an improper HTTP 403 Forbidden for non-administrators, rather than the expected HTTP 404 Not Found error page.

Fixes #1248

Root Cause

In mod_test/controllers.py:

  • Both restart_test(test_id) and stop_test(test_id) queried test = Test.query.filter(Test.id == test_id).first() but omitted a test is None check.
  • For administrator accounts (g.user.is_admin == True), code execution proceeded directly to inspect test.id (for Celery task revocation / logging), which crashed with AttributeError: 'NoneType' object has no attribute 'id' and caused Flask to return an unhandled HTTP 500 Internal Server Error.
  • For non-admin accounts (Role.tester), the test_fork query returned None, which triggered the restricted endpoint check not g.user.is_admin and test_fork is None and aborted with HTTP 403 Forbidden instead of notifying the user that the test does not exist.

Solution

  • Added explicit if test is None: checks in both restart_test and stop_test.
  • Logged an error (g.log.error(f"test with id: {test_id} not found!")) and raised TestNotFoundException(f"Test with id {test_id} does not exist").
  • This integrates cleanly with the existing @mod_test.errorhandler(TestNotFoundException) which renders test/test_not_found.html with status code 404, matching standard behavior in by_id, download_build_log_file, ccextractor_version, and by_commit.

Verification & Testing

  • Added 4 unit regression tests in tests/test_test/test_controllers.py:
    • test_restart_nonexistent_test_as_admin
    • test_restart_nonexistent_test_as_tester
    • test_stop_nonexistent_test_as_admin
    • test_stop_nonexistent_test_as_tester
  • Executed full test suite with TESTING=true ./venv/bin/nose2: All 811 tests passed in 65.3s.
  • Executed all linting and type-checking suites:
    • dodgy -i venv: Passed (0 warnings)
    • isort . --check-only: Passed
    • pydocstyle --config=./.pydocstylerc: Passed
    • mypy .: Passed (157 source files, no issues)
    • pycodestyle ./ --config=./.pycodestylerc: Passed

When restart_test or stop_test is invoked with a nonexistent test_id, the controller queried Test.query.filter(Test.id == test_id).first() without verifying if test is None.

For admin users, execution continued to test.id, causing an unhandled AttributeError ('NoneType' object has no attribute 'id') and returning HTTP 500. For non-admin users, the endpoint erroneously returned HTTP 403 Forbidden instead of HTTP 404 Not Found.

Explicitly verify if test is None and raise TestNotFoundException, logging the error and rendering the standard 404 test_not_found page, matching by_id and REST API runs endpoints.

Fixes CCExtractor#1248
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] restart_test and stop_test raise 500 / 403 when test_id does not exist

1 participant