diff test/test_cgi.py @ 8591:501eb8088ea3

test: use monkeypatch to safely handle monekypatching Tests are unittest based, so pytest fixtures can not be used by adding them to the function signature. Augment the inject_fixtures to inject monkeypatch as self._monkeypatch. Use _monkeypatch to patch the three functions replacing the code that manually did the patch. Remove the code that rolls back the manual patching as monkeypatch rolls it back automatically when the test function exits.
author John Rouillard <rouilj@ieee.org>
date Mon, 20 Apr 2026 23:56:15 -0400
parents db48c0bb4f1c
children 363a6bb5a6ae
line wrap: on
line diff
--- a/test/test_cgi.py	Mon Apr 20 22:21:08 2026 -0400
+++ b/test/test_cgi.py	Mon Apr 20 23:56:15 2026 -0400
@@ -219,8 +219,9 @@
             re.VERBOSE)
 
     @pytest.fixture(autouse=True)
-    def inject_fixtures(self, caplog):
+    def inject_fixtures(self, caplog, monkeypatch):
         self._caplog = caplog
+        self._monkeypatch = monkeypatch
 
     #
     # form label extraction
@@ -1057,13 +1058,10 @@
         cl.determine_context = MockNull ()
         def hasPermission(s, p, classname=None, d=None, e=None, **kw):
             return True
-        actions.Action.hasPermission = hasPermission
-        orig_HTMLItem_is_edit_ok = _HTMLItem.is_edit_ok
-        e1 = _HTMLItem.is_edit_ok
-        _HTMLItem.is_edit_ok = lambda x : True
-        e2 = HTMLProperty.is_edit_ok
-        orig_HTMLProperty_is_edit_ok = HTMLProperty.is_edit_ok
-        HTMLProperty.is_edit_ok = lambda x : True
+        self._monkeypatch.setattr(actions.Action, "hasPermission",
+                                  hasPermission)
+        self._monkeypatch.setattr(_HTMLItem, "is_edit_ok",lambda x : True)
+        self._monkeypatch.setattr(HTMLProperty, "is_edit_ok",lambda x : True)
 
         # If Result is not "Unable to authorize request", the CSRF check
         # passed. Since we are using a form that specified the edit action,
@@ -1251,10 +1249,6 @@
         if os.path.exists(SENDMAILDEBUG):
             os.remove(SENDMAILDEBUG)
 
-        # Undo monkey patching
-        _HTMLItem.is_edit_ok = orig_HTMLItem_is_edit_ok
-        HTMLProperty.is_edit_ok = orig_HTMLProperty_is_edit_ok
-
     def testCsrfProtectionHtml(self):
         # need to set SENDMAILDEBUG to prevent
         # downstream issue when email is sent on successful

Roundup Issue Tracker: http://roundup-tracker.org/