[fix] fix test environment pollution in TestAdminMedia #405#447
[fix] fix test environment pollution in TestAdminMedia #405#447shivsubh wants to merge 1 commit intoopenwisp:masterfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
📜 Recent review details🔇 Additional comments (5)
📝 WalkthroughWalkthroughThe test Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@openwisp_notifications/tests/test_admin.py`:
- Around line 343-358: The test assigns responses from self.client.get when
calling reverse for f"admin:{self.users_app_label}_user_change" after toggling
UserAdmin.Media but never asserts them; either replace the unused local name
with _ to show intentional discard or add explicit assertions (e.g., check
response.status_code == 200 and presence/absence of the widget JS/CSS in
response.content) after the calls that invoke _add_object_notification_widget
and self.client.get, referencing the UserAdmin.Media mutation,
_add_object_notification_widget(), reverse(...), self.client.get(...) and
self.admin.pk to locate the code to change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 35571361-ce5f-4601-82c3-00ac24765e05
📒 Files selected for processing (1)
openwisp_notifications/tests/test_admin.py
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
- GitHub Check: Python==3.12 | django~=4.2.0
- GitHub Check: Python==3.11 | django~=5.2.0
- GitHub Check: Python==3.13 | django~=5.2.0
- GitHub Check: Python==3.12 | django~=5.0.0
- GitHub Check: Python==3.12 | django~=5.1.0
- GitHub Check: Python==3.13 | django~=5.1.0
- GitHub Check: Python==3.11 | django~=5.1.0
- GitHub Check: Python==3.12 | django~=5.2.0
- GitHub Check: Python==3.10 | django~=5.2.0
- GitHub Check: Python==3.10 | django~=4.2.0
- GitHub Check: Python==3.10 | django~=5.0.0
- GitHub Check: Python==3.10 | django~=5.1.0
- GitHub Check: Python==3.11 | django~=5.0.0
- GitHub Check: Python==3.11 | django~=4.2.0
🔇 Additional comments (3)
openwisp_notifications/tests/test_admin.py (3)
1-8: LGTM!The new imports are necessary and correctly used:
copyfor deep copying mutablejs/cssattributes, andMediaOrderConflictWarningfor asserting the expected warning when the widget is added multiple times.
316-322: LGTM - state preservation logic is correct.Deep copying the mutable
jsandcssattributes ensures they can be properly restored after in-place mutations by_add_object_notification_widget().Minor: For consistency with line 360's
if original_media is not None:, consider using the same pattern here on line 317.
359-372: LGTM - restoration logic correctly handles all cases.The
finallyblock properly restores the original state:
- When
Mediaexisted originally: restores the class reference and itsjs/cssattributes from deep copies- When
Mediadidn't exist: removes theMediaclass that was added during the test- Correctly handles cases where
js/cssattributes didn't exist originally by deleting themThis effectively prevents test environment pollution as intended by the PR.
f644384 to
10d4374
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@openwisp_notifications/tests/test_admin.py`:
- Around line 316-322: The test fails to preserve the Media.extend attribute
when patching UserAdmin.Media; capture the original extend value before
modifying by doing original_extend = getattr(original_media, "extend", None)
(alongside original_js/original_css) and in the finally block restore it with
setattr(UserAdmin, "Media", original_media) or if restoring piecewise, set
getattr(UserAdmin, "Media", "extend") back to original_extend; reference the
UserAdmin.Media object and the IgnoreObjectNotificationWidgetMedia class to
ensure Media.extend is preserved and restored to avoid test pollution.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 577a9354-086d-4803-89e2-77e86209b465
📒 Files selected for processing (1)
openwisp_notifications/tests/test_admin.py
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
- GitHub Check: Python==3.13 | django~=5.2.0
- GitHub Check: Python==3.10 | django~=5.1.0
- GitHub Check: Python==3.10 | django~=5.0.0
- GitHub Check: Python==3.11 | django~=5.2.0
- GitHub Check: Python==3.12 | django~=5.0.0
- GitHub Check: Python==3.11 | django~=4.2.0
- GitHub Check: Python==3.12 | django~=4.2.0
- GitHub Check: Python==3.13 | django~=5.1.0
- GitHub Check: Python==3.12 | django~=5.2.0
- GitHub Check: Python==3.11 | django~=5.1.0
- GitHub Check: Python==3.10 | django~=4.2.0
- GitHub Check: Python==3.11 | django~=5.0.0
- GitHub Check: Python==3.12 | django~=5.1.0
- GitHub Check: Python==3.10 | django~=5.2.0
🔇 Additional comments (3)
openwisp_notifications/tests/test_admin.py (3)
337-358: Unusedresponsevariables should either be asserted or discarded.The
responseassignments on lines 337-342, 346-350, and 354-358 are never asserted. If the intent is only to verify no exceptions are raised, consider using_to indicate intentional discard, or add explicit assertions to validate the expected behavior.
1-1: LGTM!The
copyimport is appropriate for deep copying thejsandcssattributes.
359-372: LGTM!The
try...finallypattern correctly ensuresUserAdmin.Mediais restored regardless of test outcome. The restoration logic properly handles all edge cases:
- Original Media with/without
js/cssattributes- Original Media not existing at all
- Media being replaced entirely by
_add_object_notification_widget()This addresses the test pollution issue described in
#405.
The test_object_notification_setting_configured test case was modifying UserAdmin.Media without proper restoration, causing pollution in the test environment. Added a try...finally block to ensure UserAdmin.Media is restored to its original state. Closes openwisp#405
10d4374 to
3687a8a
Compare
The test_object_notification_setting_configured test case was modifying UserAdmin.Media without proper restoration, causing pollution in the test environment. Added a try...finally block to ensure UserAdmin.Media is restored to its original state.
Closes #405
Checklist