|
| 1 | +from importlib import import_module |
1 | 2 | from OFS.interfaces import IObjectWillBeAddedEvent |
2 | 3 | from plone.app.testing import login |
3 | 4 | from plone.app.testing import setRoles |
|
20 | 21 | import unittest |
21 | 22 |
|
22 | 23 |
|
| 24 | +HAS_PLONE_62 = getattr( |
| 25 | + import_module("Products.CMFPlone.factory"), "PLONE62MARKER", False |
| 26 | +) |
| 27 | + |
| 28 | + |
23 | 29 | class TestContentPatch(unittest.TestCase): |
24 | 30 |
|
25 | 31 | layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING |
@@ -197,3 +203,57 @@ def test_patch_document_with_apostrophe_dont_return_500(self): |
197 | 203 | self.assertEqual(204, response.status_code) |
198 | 204 | transaction.begin() |
199 | 205 | self.assertEqual("<p>example with '</p>", self.portal.doc1.text.raw) |
| 206 | + |
| 207 | + @unittest.skipUnless(HAS_PLONE_62, "Requires Plone 6.2+") |
| 208 | + def test_patch_image_redundant_no_event(self): |
| 209 | + image_data = "R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" |
| 210 | + response = requests.post( |
| 211 | + self.portal.absolute_url(), |
| 212 | + headers={"Accept": "application/json"}, |
| 213 | + auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD), |
| 214 | + json={ |
| 215 | + "@type": "Image", |
| 216 | + "image": { |
| 217 | + "data": image_data, |
| 218 | + "encoding": "base64", |
| 219 | + "content-type": "image/gif", |
| 220 | + "filename": "test.gif", |
| 221 | + }, |
| 222 | + }, |
| 223 | + ) |
| 224 | + self.assertEqual(201, response.status_code) |
| 225 | + image_url = response.json()["@id"] |
| 226 | + transaction.commit() |
| 227 | + |
| 228 | + # Track ObjectModifiedEvent |
| 229 | + sm = getGlobalSiteManager() |
| 230 | + fired_events = [] |
| 231 | + |
| 232 | + def record_event(event): |
| 233 | + fired_events.append(event) |
| 234 | + |
| 235 | + sm.registerHandler(record_event, (IObjectModifiedEvent,)) |
| 236 | + |
| 237 | + # Patch with same data |
| 238 | + response = requests.patch( |
| 239 | + image_url, |
| 240 | + headers={"Accept": "application/json"}, |
| 241 | + auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD), |
| 242 | + json={ |
| 243 | + "image": { |
| 244 | + "data": image_data, |
| 245 | + "encoding": "base64", |
| 246 | + "content-type": "image/gif", |
| 247 | + "filename": "test.gif", |
| 248 | + }, |
| 249 | + }, |
| 250 | + ) |
| 251 | + self.assertEqual(204, response.status_code) |
| 252 | + |
| 253 | + # In current Plone, this will be 1 because of identity mismatch |
| 254 | + # We want it to be 0 |
| 255 | + self.assertEqual( |
| 256 | + len(fired_events), 0, "ObjectModifiedEvent was fired for redundant PATCH" |
| 257 | + ) |
| 258 | + |
| 259 | + sm.unregisterHandler(record_event, (IObjectModifiedEvent,)) |
0 commit comments