Skip to content

Commit dbc4766

Browse files
authored
Merge pull request #1433 from plone/maurits-fix-tests-pre-scale
Tests: patch unique url for scale in old or new way.
2 parents 08a2fbe + ef660fa commit dbc4766

6 files changed

Lines changed: 54 additions & 22 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_documentation.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from base64 import b64encode
22
from datetime import datetime
3-
from unittest.mock import patch
43
from pkg_resources import resource_filename
54
from plone import api
65
from plone.app.discussion.interfaces import IConversation
@@ -25,8 +24,8 @@
2524
from plone.restapi.testing import PLONE_RESTAPI_ITERATE_FUNCTIONAL_TESTING
2625
from plone.restapi.testing import register_static_uuid_utility
2726
from plone.restapi.testing import RelativeSession
27+
from plone.restapi.tests.helpers import patch_scale_uuid
2828
from plone.restapi.tests.statictime import StaticTime
29-
from plone.scale import storage
3029
from plone.testing.z2 import Browser
3130
from zope.component import createObject
3231
from zope.component import getUtility
@@ -301,7 +300,8 @@ def test_documentation_news_item(self):
301300
self.portal.newsitem.image_caption = "This is an image caption."
302301
transaction.commit()
303302

304-
with patch.object(storage, "uuid4", return_value="uuid1"):
303+
scale_url_uuid = "uuid1"
304+
with patch_scale_uuid(scale_url_uuid):
305305
response = self.api_session.get(self.portal.newsitem.absolute_url())
306306
save_request_and_response_for_docs("newsitem", response)
307307

@@ -349,7 +349,8 @@ def test_documentation_image(self):
349349
data=image_data, contentType="image/png", filename="image.png"
350350
)
351351
transaction.commit()
352-
with patch.object(storage, "uuid4", return_value="uuid1"):
352+
scale_url_uuid = "uuid1"
353+
with patch_scale_uuid(scale_url_uuid):
353354
response = self.api_session.get(self.portal.image.absolute_url())
354355
save_request_and_response_for_docs("image", response)
355356

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},

src/plone/restapi/tests/test_services.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from unittest.mock import patch
21
from plone.app.testing import setRoles
32
from plone.app.testing import SITE_OWNER_NAME
43
from plone.app.testing import SITE_OWNER_PASSWORD
@@ -8,7 +7,7 @@
87
from plone.namedfile.file import NamedBlobImage
98
from plone.restapi.testing import PLONE_RESTAPI_DX_FUNCTIONAL_TESTING
109
from plone.restapi.testing import RelativeSession
11-
from plone.scale import storage
10+
from plone.restapi.tests.helpers import patch_scale_uuid
1211
from z3c.relationfield import RelationValue
1312
from zope.component import getUtility
1413
from zope.intid.interfaces import IIntIds
@@ -87,7 +86,8 @@ def test_get_news_item(self):
8786
self.portal.news1.image_caption = "This is an image caption."
8887
transaction.commit()
8988

90-
with patch.object(storage, "uuid4", return_value="uuid1"):
89+
scale_url_uuid = "uuid1"
90+
with patch_scale_uuid(scale_url_uuid):
9191
response = self.api_session.get(self.portal.news1.absolute_url())
9292

9393
self.assertEqual(response.status_code, 200)
@@ -112,7 +112,7 @@ def test_get_news_item(self):
112112
"This is an image caption.", response.json()["image_caption"]
113113
)
114114
self.assertDictContainsSubset(
115-
{"download": self.portal_url + "/news1/@@images/uuid1.png"}, # noqa
115+
{"download": self.portal_url + f"/news1/@@images/{scale_url_uuid}.png"},
116116
response.json()["image"],
117117
)
118118

0 commit comments

Comments
 (0)