Skip to content

fix(upload): resolve login_required test TODOs & patch Exception TypeError#1044

Open
MrButtCode wants to merge 3 commits intoCCExtractor:masterfrom
MrButtCode:test/fix-login-required-mock
Open

fix(upload): resolve login_required test TODOs & patch Exception TypeError#1044
MrButtCode wants to merge 3 commits intoCCExtractor:masterfrom
MrButtCode:test/fix-login-required-mock

Conversation

@MrButtCode
Copy link

@MrButtCode MrButtCode commented Mar 5, 2026

TL;DR

  1. Fixed a live production bug: QueuedSampleNotFoundException was raising a TypeError due to a missing mandatory message argument across four call sites.
  2. Resolved Technical Debt: Fixed and restored two abandoned tests in test_controllers.py by resolving import-time decorator mocking failures.

1. Production Bug Fix (exceptions.py)
The Problem: The QueuedSampleNotFoundException class was updated at some point to require a mandatory message argument in its __init__. However, four call sites in mod_upload/controllers.py were never updated. If triggered in production, this would crash the application with a TypeError rather than raising the catchable exception.
The Fix: Added a default value (message="Queued sample not found") to the exception's constructor. This is backwards-compatible: existing call sites without arguments now work safely, while custom messages can still be passed.

2. Test Restoration (test_controllers.py)
The Problem: test_link_id_confirm_invalid and test_link_id_confirm_valid were commented out. The original author attempted to patch login_required using side_effect=mock_decorator (which wasn't defined) and fundamentally misunderstood that Python evaluates decorators at import time, making standard runtime mock.patch ineffective.
The Fix: I implemented two distinct testing strategies to handle the decorator:

  • For test_link_id_confirm_invalid: Restored the test by defining a module-level mock_decorator passthrough and using a reload() strategy. By patching the decorator at its source in mod_auth.controllers and reloading mod_upload.controllers, the decorators are correctly re-applied under the patch.
  • For test_link_id_confirm_valid: Rewrote this as a true integration test using the Flask HTTP test client. To satisfy the controller's ownership verification without throwing a 404, I seeded the test database with the complete required relational chain (User -> Upload -> Sample -> QueuedSample).

…Exception

- Defined missing mock_decorator helper used by login_required patch
- Fixed import-time decorator issue by patching at source and reloading module
- Rewrote test_link_id_confirm_valid to use HTTP test client with real session
- Added default message param to QueuedSampleNotFoundException.__init__()
  to restore backwards compatibility with all four call sites
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 7, 2026

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.

2 participants