Skip to content

Commit ef72d00

Browse files
Tests: patch unique url for scale in old or new way.
This is only in serializer tests for images. uuid4 is only used until plone.scale 6.0.0a4. plone/plone.scale#57 introduces a new way. We also patch the _modified_since method to always return True. Otherwise you may get info from a different scale back, precisely because we give all scales the same "unique" id, which then is of course no longer unique, making the logic unstable. This is needed for the newer plone.scale versions, but should be perfectly fine for the older ones.
1 parent 08a2fbe commit ef72d00

4 files changed

Lines changed: 45 additions & 14 deletions

File tree

news/57.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Tests: patch unique url for scale in old or new way.
2+
This is only in serializer tests for images.
3+
[maurits]

src/plone/restapi/tests/helpers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from contextlib import contextmanager
2+
from plone.scale import storage
13
from Products.CMFCore.utils import getToolByName
4+
from unittest.mock import patch
25
from urllib.parse import urlparse
36

47
import quopri
@@ -43,3 +46,30 @@ def ascii_token(text):
4346
bytestring that is safe to use in term tokens.
4447
"""
4548
return quopri.encodestring(text.encode("utf-8"))
49+
50+
51+
@contextmanager
52+
def patch_scale_uuid(value):
53+
"""Patch plone.scale to use a hard coded value as unique id.
54+
55+
Until plone.scale 4.0.0a3 (2022-05-09) this goes via the uuid4 function.
56+
For later versions we need to patch the new hash_key method.
57+
58+
We also patch the _modified_since method to always return True.
59+
Otherwise you may get info from a different scale back,
60+
precisely because we give all scales the same "unique" id,
61+
which then is of course no longer unique, making the logic unstable.
62+
This is needed for the newer plone.scale versions,
63+
but should be perfectly fine for the older ones.
64+
"""
65+
if hasattr(storage.AnnotationStorage, "hash_key"):
66+
to_patch = storage.AnnotationStorage
67+
name = "hash_key"
68+
else:
69+
to_patch = storage
70+
name = "uuid4"
71+
with patch.object(to_patch, name, return_value=value):
72+
with patch.object(
73+
storage.AnnotationStorage, "_modified_since", return_value=True
74+
):
75+
yield

src/plone/restapi/tests/test_dxfield_serializer.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
from plone.restapi.interfaces import IFieldSerializer
1414
from plone.restapi.serializer.dxfields import DefaultFieldSerializer
1515
from plone.restapi.testing import PLONE_RESTAPI_DX_INTEGRATION_TESTING
16-
from plone.scale import storage
16+
from plone.restapi.tests.helpers import patch_scale_uuid
1717
from plone.uuid.interfaces import IUUID
1818
from unittest import TestCase
19-
from unittest.mock import patch
2019
from z3c.form.interfaces import IDataManager
2120
from zope.component import getMultiAdapter
2221
from zope.interface.verify import verifyClass
@@ -387,7 +386,8 @@ def test_namedimage_field_serialization_returns_dict_with_original_scale(self):
387386
with open(image_file, "rb") as f:
388387
image_data = f.read()
389388
fn = "test_namedimage_field"
390-
with patch.object(storage, "uuid4", return_value="uuid_1"):
389+
scale_url_uuid = "uuid_1"
390+
with patch_scale_uuid(scale_url_uuid):
391391
value = self.serialize(
392392
fn,
393393
NamedImage(
@@ -396,7 +396,6 @@ def test_namedimage_field_serialization_returns_dict_with_original_scale(self):
396396
)
397397
self.assertTrue(isinstance(value, dict), "Not a <dict>")
398398

399-
scale_url_uuid = "uuid_1"
400399
obj_url = self.doc1.absolute_url()
401400

402401
# Original image is still a "scale"
@@ -483,7 +482,8 @@ def test_namedimage_field_serialization_doesnt_choke_on_corrupt_image(self):
483482
is returned as is and we need to check it, but the scales should be empty"""
484483
image_data = b"INVALID IMAGE DATA"
485484
fn = "test_namedimage_field"
486-
with patch.object(storage, "uuid4", return_value="uuid_1"):
485+
scale_url_uuid = "uuid_1"
486+
with patch_scale_uuid(scale_url_uuid):
487487
value = self.serialize(
488488
fn,
489489
NamedImage(
@@ -492,7 +492,6 @@ def test_namedimage_field_serialization_doesnt_choke_on_corrupt_image(self):
492492
)
493493

494494
obj_url = self.doc1.absolute_url()
495-
scale_url_uuid = "uuid_1"
496495
self.assertEqual(
497496
{
498497
"content-type": "image/gif",
@@ -514,7 +513,8 @@ def test_namedblobimage_field_serialization_returns_dict_with_original_scale(sel
514513
with open(image_file, "rb") as f:
515514
image_data = f.read()
516515
fn = "test_namedblobimage_field"
517-
with patch.object(storage, "uuid4", return_value="uuid_1"):
516+
scale_url_uuid = "uuid_1"
517+
with patch_scale_uuid(scale_url_uuid):
518518
value = self.serialize(
519519
fn,
520520
NamedBlobImage(
@@ -523,7 +523,6 @@ def test_namedblobimage_field_serialization_returns_dict_with_original_scale(sel
523523
)
524524
self.assertTrue(isinstance(value, dict), "Not a <dict>")
525525

526-
scale_url_uuid = "uuid_1"
527526
obj_url = self.doc1.absolute_url()
528527

529528
# Original image is still a "scale"
@@ -610,7 +609,8 @@ def test_namedblobimage_field_serialization_doesnt_choke_on_corrupt_image(self):
610609
is returned as is and we need to check it, but the scales should be empty"""
611610
image_data = b"INVALID IMAGE DATA"
612611
fn = "test_namedblobimage_field"
613-
with patch.object(storage, "uuid4", return_value="uuid_1"):
612+
scale_url_uuid = "uuid_1"
613+
with patch_scale_uuid(scale_url_uuid):
614614
value = self.serialize(
615615
fn,
616616
NamedBlobImage(
@@ -619,7 +619,6 @@ def test_namedblobimage_field_serialization_doesnt_choke_on_corrupt_image(self):
619619
)
620620

621621
obj_url = self.doc1.absolute_url()
622-
scale_url_uuid = "uuid_1"
623622
self.assertEqual(
624623
{
625624
"content-type": "image/gif",

src/plone/restapi/tests/test_serializer.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
from plone.namedfile.file import NamedFile
88
from plone.restapi.interfaces import ISerializeToJson
99
from plone.restapi.testing import PLONE_RESTAPI_DX_INTEGRATION_TESTING
10-
from plone.scale import storage
10+
from plone.restapi.tests.helpers import patch_scale_uuid
1111
from Products.CMFCore.utils import getToolByName
12-
from unittest.mock import patch
1312
from zope.component import getMultiAdapter
1413

1514
import json
@@ -261,9 +260,9 @@ def test_serialize_image(self):
261260

262261
self.maxDiff = 99999
263262

264-
with patch.object(storage, "uuid4", return_value="uuid_1"):
263+
scale_url_uuid = "uuid_1"
264+
with patch_scale_uuid(scale_url_uuid):
265265
obj_url = self.portal.image1.absolute_url()
266-
scale_url_uuid = "uuid_1"
267266
download_url = f"{obj_url}/@@images/{scale_url_uuid}.png"
268267
scales = {
269268
"listing": {"download": download_url, "width": 16, "height": 4},

0 commit comments

Comments
 (0)