Skip to content

Commit a38f37b

Browse files
committed
Consolidate redundant PATCH test and add HAS_PLONE_62 guard
1 parent a2f75ca commit a38f37b

2 files changed

Lines changed: 60 additions & 74 deletions

File tree

src/plone/restapi/tests/test_content_patch.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from importlib import import_module
12
from OFS.interfaces import IObjectWillBeAddedEvent
23
from plone.app.testing import login
34
from plone.app.testing import setRoles
@@ -20,6 +21,11 @@
2021
import unittest
2122

2223

24+
HAS_PLONE_62 = getattr(
25+
import_module("Products.CMFPlone.factory"), "PLONE62MARKER", False
26+
)
27+
28+
2329
class TestContentPatch(unittest.TestCase):
2430

2531
layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING
@@ -197,3 +203,57 @@ def test_patch_document_with_apostrophe_dont_return_500(self):
197203
self.assertEqual(204, response.status_code)
198204
transaction.begin()
199205
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,))

src/plone/restapi/tests/test_redundant_patch.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)