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

Add X-Robots-Tag header to response for items marked as no_index #17

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/kitconcept/seo/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
<include file="dependencies.zcml" />
<include file="profiles.zcml" />

<subscriber handler=".subscribers.add_x_robots_noindex_header" />

</configure>
16 changes: 16 additions & 0 deletions src/kitconcept/seo/subscribers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from kitconcept.seo.behaviors.seo import ISeo
from plone.dexterity.interfaces import IDexterityContent
from zope.component import adapter
from ZPublisher.interfaces import IPubAfterTraversal
from ZPublisher.pubevents import PubAfterTraversal


@adapter(IPubAfterTraversal)
def add_x_robots_noindex_header(event: PubAfterTraversal):
"""Add X-Robots-Tag to prevent indexing items with seo_noindex."""
request = event.request
for obj in request["PARENTS"]:
if IDexterityContent.providedBy(obj):
if ISeo.providedBy(obj) and obj.seo_noindex:
request.response.setHeader("X-Robots-Tag", "noindex")
break
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
@pytest.fixture
def manager_plone_client(functional):
portal = functional["portal"]
api_session = RelativeSession(f"{portal.absolute_url()}/++api++")
api_session = RelativeSession(f"{portal.absolute_url()}")
api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)
return api_session
28 changes: 26 additions & 2 deletions tests/test_behavior.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
def test_seo_behavior_fields(manager_plone_client):
# Enable behavior for pages
manager_plone_client.patch(
"/@controlpanels/dexterity-types/Document", json={"kitconcept.seo": True}
"++api++/@controlpanels/dexterity-types/Document", json={"kitconcept.seo": True}
)

# Check schema
schema = manager_plone_client.get("/@types/Document").json()
schema = manager_plone_client.get("++api++/@types/Document").json()
assert schema["fieldsets"][-1] == {
"behavior": "plone",
"description": "",
Expand All @@ -21,3 +21,27 @@ def test_seo_behavior_fields(manager_plone_client):
"id": "seo",
"title": "SEO",
}


def test_noindex_sets_response_header(manager_plone_client):
# Enable behavior for pages
manager_plone_client.patch(
"++api++/@controlpanels/dexterity-types/Document", json={"kitconcept.seo": True}
)

# Add page
resp = manager_plone_client.post(
"++api++/",
json={"@type": "Document", "title": "Test page", "seo_noindex": True},
)
assert resp.status_code == 201

# Confirm the page is served with X-Robots-Tag header
resp = manager_plone_client.get("/test-page")
assert resp.status_code == 200
assert resp.headers["X-Robots-Tag"] == "noindex"

# Confirm views of the page are served with X-Robots-Tag header
resp = manager_plone_client.get("/test-page/@@view")
assert resp.status_code == 200
assert resp.headers["X-Robots-Tag"] == "noindex"