Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat sub-items like items in @linkintegrity #1714

Merged
merged 19 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Breaking changes:
- Change the @linkintegrity endpoint to add `items_total`, the number of contained items which would be deleted. @davisagli, @danalvrz, @pgrunewald (#1636)
- The default branch was renamed from `master` to `main`. @tisto, @davisagli (#1695)
- Drop support for Python 3.7. Set python_requires to >= 3.8 @tisto (#1709)
- Expose the sources + their target instead of just some sources in "breaches" attribute in @linkintegrity endpoint response. [jaroel]


New features:
Expand Down Expand Up @@ -461,7 +462,7 @@ Bug fixes:


- Added url field to Actions (#817)
- Update statictime tests following changes to p.a.disucssion (see
- Update statictime tests following changes to p.a.disucssion (see
tisto marked this conversation as resolved.
Show resolved Hide resolved
https://github.com/plone/plone.app.discussion/pull/204) - [instification] (#1520)
tisto marked this conversation as resolved.
Show resolved Hide resolved
- Update @portrait endpoint to use sanitized user id [instification] (#1524)
tisto marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
27 changes: 18 additions & 9 deletions src/plone/restapi/services/linkintegrity/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,25 @@ def reply(self):
item = uuidToObject(uid)
item_path = "/".join(item.getPhysicalPath())
links_info = item.restrictedTraverse("@@delete_confirmation_info")
breaches = links_info.get_breaches()
data = getMultiAdapter((item, self.request), ISerializeToJsonSummary)()
data["breaches"] = []
for breach in breaches:
for source in breach.get("sources", []):
# remove unwanted data
source["@id"] = source["url"]
del source["url"]
del source["accessible"]
data["breaches"].append(source)
data["breaches"] = [
{
"target": {
"@id": result["target"]["url"],
"uid": result["target"]["uid"],
"title": result["target"]["title"],
},
"sources": [
{
"@id": source["url"],
"uid": source["uid"],
"title": source["title"],
}
for source in result["sources"]
],
}
for result in links_info.get_breaches()
]
# subtract one because we don't want to count item_path itself
data["items_total"] = len(catalog(path=item_path)) - 1
result.append(data)
Expand Down
15 changes: 12 additions & 3 deletions src/plone/restapi/tests/http-examples/linkintegrity_get.resp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ Content-Type: application/json
"@type": "Document",
"breaches": [
{
"@id": "http://localhost:55001/plone/doc-1",
"title": "First document",
"uid": "SomeUUID000000000000000000000001"
"sources": [
{
"@id": "http://localhost:55001/plone/doc-1",
"title": "First document",
"uid": "SomeUUID000000000000000000000001"
}
],
"target": {
"@id": "http://localhost:55001/plone/doc-2",
"title": "Second document",
"uid": "SomeUUID000000000000000000000002"
}
jaroel marked this conversation as resolved.
Show resolved Hide resolved
}
],
"description": "",
Expand Down
160 changes: 150 additions & 10 deletions src/plone/restapi/tests/test_services_linkintegrity.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from pkg_resources import get_distribution
from plone.app.testing import setRoles
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
Expand All @@ -18,9 +19,10 @@
import transaction
import unittest

linkintegrity_version = get_distribution("plone.app.linkintegrity").version

class TestLinkIntegrity(unittest.TestCase):

class TestLinkIntegrity(unittest.TestCase):
layer = PLONE_RESTAPI_BLOCKS_FUNCTIONAL_TESTING

def setUp(self):
Expand Down Expand Up @@ -66,15 +68,17 @@ def test_return_right_breaches_for_reference_field(self):
response = self.api_session.get(
"/@linkintegrity", params={"uids": [self.doc2.UID()]}
)

result = response.json()
self.assertEqual(response.status_code, 200)
self.assertEqual(len(result), 1)
self.assertEqual(result[0]["@id"], self.doc2.absolute_url())
self.assertEqual(len(result), 1)

breaches = result[0]["breaches"]
self.assertEqual(breaches[0]["sources"][0]["uid"], IUUID(self.doc1))
self.assertEqual(breaches[0]["sources"][0]["@id"], self.doc1.absolute_url())
self.assertEqual(len(breaches), 1)
self.assertEqual(breaches[0]["uid"], IUUID(self.doc1))
self.assertEqual(breaches[0]["@id"], self.doc1.absolute_url())
self.assertEqual(len(breaches[0]["sources"]), 1)

def test_do_not_return_breaches_if_check_is_disabled(self):
registry = getUtility(IRegistry)
Expand Down Expand Up @@ -135,13 +139,14 @@ def test_return_right_breaches_for_blocks(self):

result = response.json()
self.assertEqual(response.status_code, 200)
self.assertEqual(len(result), 1)
self.assertEqual(result[0]["@id"], self.doc2.absolute_url())
self.assertEqual(len(result), 1)

breaches = result[0]["breaches"]
self.assertEqual(breaches[0]["sources"][0]["uid"], IUUID(doc_with_rel))
self.assertEqual(breaches[0]["sources"][0]["@id"], doc_with_rel.absolute_url())
self.assertEqual(len(breaches), 1)
self.assertEqual(breaches[0]["uid"], IUUID(doc_with_rel))
self.assertEqual(breaches[0]["@id"], doc_with_rel.absolute_url())
self.assertEqual(len(breaches[0]["sources"]), 1)

def test_return_breaches_for_contents_in_subfolders(self):
# create a folder structure
Expand Down Expand Up @@ -183,13 +188,14 @@ def test_return_breaches_for_contents_in_subfolders(self):

result = response.json()
self.assertEqual(response.status_code, 200)
self.assertEqual(len(result), 1)
self.assertEqual(result[0]["@id"], self.doc2.absolute_url())
self.assertEqual(len(result), 1)

breaches = result[0]["breaches"]
self.assertEqual(breaches[0]["sources"][0]["uid"], IUUID(doc_in_folder))
self.assertEqual(breaches[0]["sources"][0]["@id"], doc_in_folder.absolute_url())
self.assertEqual(len(breaches), 1)
self.assertEqual(breaches[0]["uid"], IUUID(doc_in_folder))
self.assertEqual(breaches[0]["@id"], doc_in_folder.absolute_url())
self.assertEqual(len(breaches[0]["sources"]), 1)

def test_return_items_total_in_subfolders(self):
# create a folder structure
Expand All @@ -210,3 +216,137 @@ def test_return_items_total_in_subfolders(self):
self.assertEqual(result[0]["@id"], level1.absolute_url())
self.assertEqual(result[0]["breaches"], [])
self.assertEqual(result[0]["items_total"], 1)

@unittest.skipUnless(
linkintegrity_version > "4.0.2",
"Remove this skipUnless after release of p.a.linkintegrity.",
)
def test_tree_breaches_no_duplicates(self):
# /target_parent/target_child
target_parent = createContentInContainer(
self.portal, "Folder", id="target-parent"
)
target_child = createContentInContainer(
target_parent, "Document", id="target-child"
)
target_parent_uid = IUUID(target_parent)
target_child_uid = IUUID(target_child)

source_a = createContentInContainer(
self.portal,
"Document",
id="source_a",
blocks={
"block-uuid1": {
"@type": "text",
"text": {
"blocks": [{"text": "some link"}],
"entityMap": {
"0": {
"data": {
"href": f"../resolveuid/{target_parent_uid}",
"rel": "nofollow",
"url": f"../resolveuid/{target_parent_uid}",
},
"mutability": "MUTABLE",
"type": "LINK",
}
},
},
},
"block-uuid2": {
"@type": "text",
"text": {
"blocks": [{"text": "some other link"}],
"entityMap": {
"0": {
"data": {
"href": f"../resolveuid/{target_child_uid}",
"rel": "nofollow",
"url": f"../resolveuid/{target_child_uid}",
},
"mutability": "MUTABLE",
"type": "LINK",
}
},
},
},
},
)

source_b = createContentInContainer(
self.portal,
"Document",
id="source_b",
blocks={
"block-uuid3": {
"@type": "text",
"text": {
"blocks": [{"text": "some link"}],
"entityMap": {
"0": {
"data": {
"href": f"../resolveuid/{target_parent_uid}",
"rel": "nofollow",
"url": f"../resolveuid/{target_parent_uid}",
},
"mutability": "MUTABLE",
"type": "LINK",
}
},
},
}
},
)

source_c = createContentInContainer(
self.portal,
"Document",
id="source_c",
blocks={
"block-uuid4": {
"@type": "text",
"text": {
"blocks": [{"text": "some other link"}],
"entityMap": {
"0": {
"data": {
"href": f"../resolveuid/{target_child_uid}",
"rel": "nofollow",
"url": f"../resolveuid/{target_child_uid}",
},
"mutability": "MUTABLE",
"type": "LINK",
}
},
},
},
},
)

transaction.commit()

response = self.api_session.get(
"/@linkintegrity", params={"uids": [target_parent_uid]}
)

result = response.json()
self.assertEqual(response.status_code, 200)
self.assertEqual(len(result), 1)
self.assertEqual(result[0]["@id"], target_parent.absolute_url())

breaches = result[0]["breaches"]
self.assertEqual(breaches[0]["target"]["uid"], target_parent_uid)
self.assertEqual(
[source["uid"] for source in breaches[0]["sources"]],
[IUUID(source_a), IUUID(source_b)],
)
self.assertEqual(breaches[1]["target"]["uid"], target_child_uid)
self.assertEqual(
[source["uid"] for source in breaches[1]["sources"]],
[IUUID(source_a), IUUID(source_c)],
)
# target parent + target child
# p.a.linkintegrity > 4.0.2 deduplicates breaches, so if you see 3
# instead of 2, that's why.
self.assertEqual(len(breaches), 2)