Skip to content

[fix] Fixed test environment pollution in TestAdminMedia #405#447

Merged
nemesifier merged 3 commits intoopenwisp:masterfrom
shivsubh:issues/405-improve-test-admin-media
Apr 18, 2026
Merged

[fix] Fixed test environment pollution in TestAdminMedia #405#447
nemesifier merged 3 commits intoopenwisp:masterfrom
shivsubh:issues/405-improve-test-admin-media

Conversation

@shivsubh
Copy link
Copy Markdown
Contributor

@shivsubh shivsubh commented Mar 25, 2026

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

  • I have read the OpenWISP Contributing Guidelines.
  • I have manually tested the changes proposed in this pull request.
  • I have written new test cases for new code and/or updated existing tests for changes to existing code.
  • I have updated the documentation.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 25, 2026

Warning

Rate limit exceeded

@nemesifier has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 44 minutes and 58 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 44 minutes and 58 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3a95b616-898a-4015-ab88-ec42f7fd727f

📥 Commits

Reviewing files that changed from the base of the PR and between 9531226 and e881ecd.

📒 Files selected for processing (1)
  • openwisp_notifications/tests/test_admin.py
📝 Walkthrough

Walkthrough

The tests were updated to avoid polluting test state and to simplify script execution. In test_admin, test_object_notification_setting_configured now snapshots any existing UserAdmin.Media (deep-copying js/css when present), runs through multiple UserAdmin.Media scenarios (including the MediaOrderConflictWarning path), and restores the original Media object or its attributes in a finally block; direct import/assignment of Media() was removed. In test_selenium, a minor invocation change consolidates the injected JavaScript into a single execute_script(...) call without altering the script logic.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes


Caution

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

  • Ignore

❌ Failed checks (1 error)

Check name Status Explanation Resolution
Bug Fixes ❌ Error Pull request addresses test infrastructure pollution rather than core user-facing functionality bug, and implementation has critical defects in cleanup logic. Clarify scope applicability; fix line 316 to use identity checking; add explicit attribute deletion in finally block when attributes weren't originally present; add regression test.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title correctly uses the [fix] prefix format and clearly describes the issue being resolved: fixing test environment pollution in TestAdminMedia.
Description check ✅ Passed The description follows the template with all key sections completed: checklist partially filled, issue reference provided, and clear change description.
Linked Issues check ✅ Passed The PR addresses issue #405 by adding a try...finally block to restore UserAdmin.Media to its original state, preventing test environment pollution.
Out of Scope Changes check ✅ Passed All changes are within scope: test restoration logic in test_admin.py and formatting fix in test_selenium.py are both related to improving test reliability.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between d5b5bb9 and 55d5946.

📒 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: copy for deep copying mutable js/css attributes, and MediaOrderConflictWarning for asserting the expected warning when the widget is added multiple times.


316-322: LGTM - state preservation logic is correct.

Deep copying the mutable js and css attributes 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 finally block properly restores the original state:

  • When Media existed originally: restores the class reference and its js/css attributes from deep copies
  • When Media didn't exist: removes the Media class that was added during the test
  • Correctly handles cases where js/css attributes didn't exist originally by deleting them

This effectively prevents test environment pollution as intended by the PR.

Comment thread openwisp_notifications/tests/test_admin.py
@shivsubh shivsubh force-pushed the issues/405-improve-test-admin-media branch 2 times, most recently from f644384 to 10d4374 Compare March 25, 2026 06:25
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 55d5946 and f644384.

📒 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: Unused response variables should either be asserted or discarded.

The response assignments 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 copy import is appropriate for deep copying the js and css attributes.


359-372: LGTM!

The try...finally pattern correctly ensures UserAdmin.Media is restored regardless of test outcome. The restoration logic properly handles all edge cases:

  • Original Media with/without js/css attributes
  • Original Media not existing at all
  • Media being replaced entirely by _add_object_notification_widget()

This addresses the test pollution issue described in #405.

Comment thread openwisp_notifications/tests/test_admin.py Outdated
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
@shivsubh shivsubh force-pushed the issues/405-improve-test-admin-media branch from 10d4374 to 3687a8a Compare March 25, 2026 07:40
@nemesifier nemesifier changed the title [fix] fix test environment pollution in TestAdminMedia #405 [fix] Fixed test environment pollution in TestAdminMedia #405 Apr 18, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 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 315-320: The check for original_media uses truthiness; change it
to an identity None check to match the finally block: in the test where
original_media is obtained from getattr(UserAdmin, "Media", None), replace the
conditional "if original_media:" with "if original_media is not None:" so
assignments to original_js and original_css (via copy.deepcopy of
getattr(original_media, 'js', None) and getattr(original_media, 'css', None))
only run when original_media is not None, keeping behavior symmetric with the
finally block that uses "is not None" and preventing surprises if Media defines
a custom __bool__.
- Around line 361-369: The finally block must remove any js/css attributes that
were added to the original Media class when those attributes did not exist
originally: after restoring UserAdmin.Media = original_media (i.e., referencing
original_media, original_js, original_css), check if original_js is None and
hasattr(UserAdmin.Media, "js") and if so delattr(UserAdmin.Media, "js"); do the
same for original_css: if original_css is None and hasattr(UserAdmin.Media,
"css") then delattr(UserAdmin.Media, "css"). This ensures UserAdmin.Media is
returned to the exact pre-test shape even when original Media lacked js/css.
🪄 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: ee4754d4-bacd-4290-b08f-16f253a3f019

📥 Commits

Reviewing files that changed from the base of the PR and between 3687a8a and 9531226.

📒 Files selected for processing (1)
  • openwisp_notifications/tests/test_admin.py

Comment thread openwisp_notifications/tests/test_admin.py
Comment thread openwisp_notifications/tests/test_admin.py
@nemesifier
Copy link
Copy Markdown
Member

Thanks @shivsubh @pandafy

@nemesifier nemesifier merged commit 1d6998b into openwisp:master Apr 18, 2026
18 checks passed
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.

[chores] Improve TestAdminMedia: test_object_notification_setting_configured pollutes test environment

2 participants