Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davisagli committed Jun 12, 2024
1 parent 228b8ec commit 79f5bf6
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/plone/restapi/serializer/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,22 @@ def _process_data(self, data, field=None):
data["overwrite"] = True
return data

if data.get("overwrite"):
# Editor decided to overwrite a data
return data

if isinstance(value, str):
url = value
value = [{"@id": url}]
else:
url = value[0].get("@id", "")
brain = url_to_brain(url)

if brain is not None:
serialized_brain = getMultiAdapter(
(brain, self.request), ISerializeToJsonSummary
)()

# Fields from the teaser-schema need to be overwritten
for key in ["title", "description", "head_title"]:
if key in serialized_brain:
data[key] = serialized_brain[key]
if not data.get("overwrite"):
# Update fields at the top level of the block data
for key in ["title", "description", "head_title"]:
if key in serialized_brain:
data[key] = serialized_brain[key]

# We return the serialized brain.
value[0].update(serialized_brain)
Expand Down
94 changes: 94 additions & 0 deletions src/plone/restapi/tests/test_blocks_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,97 @@ def test_image_scales_serializer_is_json_compatible(self):
blocks={"123": {"@type": "image", "url": f"../resolveuid/{image_uid}"}},
)
self.assertIs(type(res["123"]["image_scales"]), dict)

def test_teaser_block_serializer_dynamic(self):
doc = self.portal["doc1"]
doc_uid = doc.UID()
resolve_uid_link = f"../resolveuid/{doc_uid}"
value = self.serialize(
context=self.portal.doc1,
blocks={
"1": {
"@type": "teaser",
"href": resolve_uid_link,
"overwrite": False,
}
},
)

block = value["1"]
self.assertEqual(block["title"], doc.title)
self.assertEqual(block["description"], doc.description)
href = block["href"][0]
self.assertEqual(href["@id"], doc.absolute_url())

def test_teaser_block_serializer_dynamic_nested(self):
doc = self.portal["doc1"]
doc_uid = doc.UID()
resolve_uid_link = f"../resolveuid/{doc_uid}"
value = self.serialize(
context=self.portal.doc1,
blocks={
"grid": {
"@type": "gridBlock",
"blocks": {
"1": {
"@type": "teaser",
"href": resolve_uid_link,
"overwrite": False,
},
},
"blocks_layout": {"items": ["1"]},
}
},
)

block = value["grid"]["blocks"]["1"]
self.assertEqual(block["title"], doc.title)
self.assertEqual(block["description"], doc.description)
href = block["href"][0]
self.assertEqual(href["@id"], doc.absolute_url())

def test_teaser_block_serializer_with_overwrite(self):
doc = self.portal["doc1"]
doc_uid = doc.UID()
resolve_uid_link = f"../resolveuid/{doc_uid}"
value = self.serialize(
context=self.portal.doc1,
blocks={
"1": {
"@type": "teaser",
"href": resolve_uid_link,
"overwrite": True,
"title": "Custom title",
"description": "Custom description",
}
},
)

block = value["1"]
self.assertEqual(block["title"], "Custom title")
self.assertEqual(block["description"], "Custom description")
href = block["href"][0]
self.assertEqual(href["@id"], doc.absolute_url())

def test_teaser_block_serializer_legacy(self):
# no "overwrite" key -> default to True
doc = self.portal["doc1"]
doc_uid = doc.UID()
resolve_uid_link = f"../resolveuid/{doc_uid}"
value = self.serialize(
context=self.portal.doc1,
blocks={
"1": {
"@type": "teaser",
"href": [{"@id": resolve_uid_link}],
"title": "Custom title",
"description": "Custom description",
}
},
)

block = value["1"]
self.assertEqual(block["title"], "Custom title")
self.assertEqual(block["description"], "Custom description")
href = block["href"][0]
self.assertEqual(href["@id"], doc.absolute_url())

0 comments on commit 79f5bf6

Please sign in to comment.