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

Ensure new_url is absolute in Redirects #391

Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Unreleased

### Changed
- Handling of `new_url` on `RedirectObjectType` to always return absolute URLs ([#391](https://github.com/torchbox/wagtail-grapple/pull/391)) @JakubMastalerz

## [0.25.1] - 2024-04-21

### Changed
Expand Down
2 changes: 1 addition & 1 deletion grapple/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def user_settings(self):

def __getattr__(self, attr):
if attr not in self.defaults:
raise AttributeError("Invalid Grapple setting: '%s'" % attr)
raise AttributeError(f"Invalid Grapple setting: '{attr}'")

try:
# Check if present in user settings
Expand Down
28 changes: 22 additions & 6 deletions grapple/types/redirects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy

from typing import List, Optional
from urllib.parse import urlparse

import graphene

Expand All @@ -25,24 +26,39 @@ class RedirectObjectType(graphene.ObjectType):
class Meta:
name = "Redirect"

def resolve_old_url(self, info, **kwargs) -> str:
def resolve_old_url(self: Redirect, info, **kwargs) -> str:
"""
Resolve the value of `old_url` using the `root_url` of the associated
site and `old_path`.
"""

return self.site.root_url + self.old_path

def resolve_new_url(self, info, **kwargs) -> Optional[str]:
def resolve_new_url(self: Redirect, info, **kwargs) -> Optional[str]:
"""
Resolve the value of `new_url`. If `redirect_page` is specified then its
URL is prioritised.
Resolve the value of `new_url`. If `redirect_page` is specified then
`link` is used. Otherwise, ensure that the redirect link is absolute.
"""
if self.redirect_page:
return self.link # Handled by the `Redirect` model

elif self.redirect_link:
parsed_url = urlparse(self.redirect_link)

if not parsed_url.scheme: # url without scheme is not absolute
return (
self.site.root_url.rstrip("/")
+ "/"
+ self.redirect_link.lstrip("/")
)
else:
return self.redirect_link

return self.link
else:
return None
Comment on lines +42 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change will have the effect of causing newUrl to always be absolute. Redirect.link uses Page.url in the case of redirect_page existing which will return a relative URL on single-site instances, or an absolute URL on multi-site instances. The reason your test is passing is that there are multiple sites. If we want to always return an absolute URL we should use Page.full_url.

Given the behaviour of Page.url (which seems like it shouldn't be problematic) I'm scratching my head a little as to why we're doing this. Unfortunately our ticket and my notes are a little vague as to the motivator. Is it worth making a change just for consistency of the format of the returned URL? Any thoughts @zerolab ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just got time to look at this properly.

Redirect.redirect_link is an URLField and will always have a URL scheme and netloc (or at least it should), so this change doesn't improve the behaviour whatsoever.

Looking at #384, the thought process was that newUrl should always be a full URL to reduce ambiguity in a multi-site setup. As far as I can unpick the results from #384 the relative URL comes from the fact that the redirect applies to all sites.

Now, if we change to return full URLs, we should document it properly in the changelog and release notes. I am not opposed to always returning the full URL, but definitely want to have proper test coverage for redirect that apply to all sites

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saying that, I do wonder if we're adding maintenance burden as we'll need to replicate https://github.com/wagtail/wagtail/blob/a09bba67cd58f519f3ae5bff32575e7ce9244031/wagtail/contrib/redirects/models.py#L68-L81

Copy link
Contributor

@jams2 jams2 May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell, newUrl being relative will only occur in single site setups, as we call into Page.url which returns relative URLs for single site, or an absolute URL if there are multiple sites. I'm having a hard time coming up with ways that this would be problematic. I think the original issue is erroneous.


# Return the page that's being redirected to, if at all.
def resolve_page(self, info, **kwargs) -> Optional[Page]:
def resolve_page(self: Redirect, info, **kwargs) -> Optional[Page]:
if self.redirect_page is not None:
return self.redirect_page.specific

Expand Down
21 changes: 9 additions & 12 deletions tests/test_image_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,15 @@ def test_renditions(self):
def test_renditions_with_allowed_image_filters_restrictions(self):
def get_query(**kwargs):
params = ",".join([f"{key}: {value}" for key, value in kwargs.items()])
return (
"""
query ($id: ID!) {
image(id: $id) {
rendition(%s) {
url
}
}
}
"""
% params
)
return f"""
query ($id: ID!) {{
image(id: $id) {{
rendition({params}) {{
url
}}
}}
}}
"""

results = self.client.execute(
get_query(width=100), variables={"id": self.example_image.id}
Expand Down
39 changes: 39 additions & 0 deletions tests/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,42 @@ def test_query_efficiency(self):
# There should be one SELECT query for Redirects and one for Sites.
with self.assertNumQueries(2):
self.client.execute(query)

def test_new_url_is_absolute(self):
"""Test that the `new url` is always an absolute url."""

# Create a redirect with just `redirect_page`.
RedirectFactory(
redirect_link="",
redirect_page=self.page,
)
# Create a redirect with absolute url in `redirect_link`.
RedirectFactory(
redirect_link="http://test.com/",
redirect_page=None,
)

# Create a redirect with relative url in `redirect_link`.
RedirectFactory(
redirect_link="/test",
redirect_page=None,
site=SiteFactory(
hostname="test-site",
port=81,
),
)

query = """
{
redirects {
newUrl
}
}
"""

result = self.client.execute(query)["data"]["redirects"]

# assert all urls are absolute
self.assertEqual(result[0]["newUrl"], "http://localhost/test-page-url/")
self.assertEqual(result[1]["newUrl"], "http://test.com/")
self.assertEqual(result[2]["newUrl"], "http://test-site:81/test")
Loading