Mercurial > p > roundup > code
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 8590:db48c0bb4f1c | 8591:501eb8088ea3 |
|---|---|
| 217 classes = '|'.join(self.db.classes.keys()) | 217 classes = '|'.join(self.db.classes.keys()) |
| 218 self.FV_SPECIAL = re.compile(FormParser.FV_LABELS%classes, | 218 self.FV_SPECIAL = re.compile(FormParser.FV_LABELS%classes, |
| 219 re.VERBOSE) | 219 re.VERBOSE) |
| 220 | 220 |
| 221 @pytest.fixture(autouse=True) | 221 @pytest.fixture(autouse=True) |
| 222 def inject_fixtures(self, caplog): | 222 def inject_fixtures(self, caplog, monkeypatch): |
| 223 self._caplog = caplog | 223 self._caplog = caplog |
| 224 self._monkeypatch = monkeypatch | |
| 224 | 225 |
| 225 # | 226 # |
| 226 # form label extraction | 227 # form label extraction |
| 227 # | 228 # |
| 228 def tl(self, s, c, i, a, p): | 229 def tl(self, s, c, i, a, p): |
| 1055 cl.instance.templates.load = load_template | 1056 cl.instance.templates.load = load_template |
| 1056 cl.selectTemplate = MockNull() | 1057 cl.selectTemplate = MockNull() |
| 1057 cl.determine_context = MockNull () | 1058 cl.determine_context = MockNull () |
| 1058 def hasPermission(s, p, classname=None, d=None, e=None, **kw): | 1059 def hasPermission(s, p, classname=None, d=None, e=None, **kw): |
| 1059 return True | 1060 return True |
| 1060 actions.Action.hasPermission = hasPermission | 1061 self._monkeypatch.setattr(actions.Action, "hasPermission", |
| 1061 orig_HTMLItem_is_edit_ok = _HTMLItem.is_edit_ok | 1062 hasPermission) |
| 1062 e1 = _HTMLItem.is_edit_ok | 1063 self._monkeypatch.setattr(_HTMLItem, "is_edit_ok",lambda x : True) |
| 1063 _HTMLItem.is_edit_ok = lambda x : True | 1064 self._monkeypatch.setattr(HTMLProperty, "is_edit_ok",lambda x : True) |
| 1064 e2 = HTMLProperty.is_edit_ok | |
| 1065 orig_HTMLProperty_is_edit_ok = HTMLProperty.is_edit_ok | |
| 1066 HTMLProperty.is_edit_ok = lambda x : True | |
| 1067 | 1065 |
| 1068 # If Result is not "Unable to authorize request", the CSRF check | 1066 # If Result is not "Unable to authorize request", the CSRF check |
| 1069 # passed. Since we are using a form that specified the edit action, | 1067 # passed. Since we are using a form that specified the edit action, |
| 1070 # GET, HEAD, OPTIONS should all fail with invalid request as edit | 1068 # GET, HEAD, OPTIONS should all fail with invalid request as edit |
| 1071 # action requires POST. | 1069 # action requires POST. |
| 1249 | 1247 |
| 1250 # clean up from email log | 1248 # clean up from email log |
| 1251 if os.path.exists(SENDMAILDEBUG): | 1249 if os.path.exists(SENDMAILDEBUG): |
| 1252 os.remove(SENDMAILDEBUG) | 1250 os.remove(SENDMAILDEBUG) |
| 1253 | 1251 |
| 1254 # Undo monkey patching | |
| 1255 _HTMLItem.is_edit_ok = orig_HTMLItem_is_edit_ok | |
| 1256 HTMLProperty.is_edit_ok = orig_HTMLProperty_is_edit_ok | |
| 1257 | |
| 1258 def testCsrfProtectionHtml(self): | 1252 def testCsrfProtectionHtml(self): |
| 1259 # need to set SENDMAILDEBUG to prevent | 1253 # need to set SENDMAILDEBUG to prevent |
| 1260 # downstream issue when email is sent on successful | 1254 # downstream issue when email is sent on successful |
| 1261 # issue creation. Also delete the file afterwards | 1255 # issue creation. Also delete the file afterwards |
| 1262 # just to make sure that some other test looking for | 1256 # just to make sure that some other test looking for |
