Skip to content

Commit

Permalink
Added Site to RedirectObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubMastalerz committed Dec 19, 2023
1 parent 2822f36 commit a003b5a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
6 changes: 5 additions & 1 deletion grapple/types/redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from wagtail.contrib.redirects.models import Redirect
from wagtail.models import Page, Site

from grapple.types.sites import SiteObjectType

from .pages import get_page_interface


Expand All @@ -15,6 +17,7 @@ class RedirectObjectType(graphene.ObjectType):
old_url = graphene.String(required=True)
new_url = graphene.String(required=False)
page = graphene.Field(get_page_interface())
site = graphene.Field(SiteObjectType, required=False)
is_permanent = graphene.Boolean(required=True)

def resolve_old_url(self, info, **kwargs) -> str:
Expand All @@ -23,8 +26,9 @@ def resolve_old_url(self, info, **kwargs) -> str:
Otherwise, return a JSON string representing a list of all site urls.
"""
if self.site:
return f"http://{self.site.hostname}:{self.site.port}/{self.old_path}"
return f"{self.site.root_url}/{self.old_path}"

# TODO: Remove after making site required
if self.site is None:
sites_QS = Site.objects.all()
old_url_list = []
Expand Down
72 changes: 65 additions & 7 deletions tests/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@


class TestRedirectQueries(BaseGrappleTest):
def setUp(self) -> None:
super().setUp()
self.page = BlogPage(title="Test page", slug="test-page-url", date="2020-01-01")
self.home.add_child(instance=self.page)
@classmethod
def setUpTestData(cls) -> None:
super().setUpTestData()
cls.page = BlogPage(title="Test page", slug="test-page-url", date="2020-01-01")
cls.home.add_child(instance=cls.page)

def test_basic_query(self):
"""
Expand Down Expand Up @@ -47,6 +48,47 @@ def test_basic_query(self):
self.assertEqual(result_data["newUrl"], "http://localhost:8000/test/new")
self.assertEqual(result_data["isPermanent"], True)

def test_sub_type_query(self):
"""
Test that `Page` and `Site` fields on Redirects can be queried through graphql.
"""

self.redirect = RedirectFactory(
redirect_page=self.page,
site=SiteFactory(
hostname="test-site",
port=81,
),
)

query = """
{
redirects {
page {
title
url
}
site {
hostname
port
}
}
}
"""

result = self.client.execute(query)

self.assertEqual(type(result["data"]["redirects"][0]), dict)
self.assertEqual(type(result["data"]["redirects"][0]["page"]), dict)
self.assertEqual(type(result["data"]["redirects"][0]["site"]), dict)

result_data = result["data"]["redirects"][0]

self.assertEqual(result_data["page"]["title"], "Test page")
self.assertEqual(result_data["page"]["url"], "http://localhost/test-page-url/")
self.assertEqual(result_data["site"]["hostname"], "test-site")
self.assertEqual(result_data["site"]["port"], 81)

def test_new_url_sources(self):
"""
Test that when redirects are queried, the right source for `newUrl` is chosen.
Expand Down Expand Up @@ -111,7 +153,21 @@ def test_specified_site_url(self):
old_path="old-path",
site=SiteFactory(
hostname="test-site",
port=8000,
port=81,
),
)
self.redirect = RedirectFactory(
old_path="old-path",
site=SiteFactory(
hostname="test-site-default-port",
port=80,
),
)
self.redirect = RedirectFactory(
old_path="old-path",
site=SiteFactory(
hostname="test-site-secure",
port=443,
),
)

Expand All @@ -123,9 +179,11 @@ def test_specified_site_url(self):
}
"""

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

self.assertEqual(result, "http://test-site:8000/old-path")
self.assertEqual(result[0]["oldUrl"], "http://test-site:81/old-path")
self.assertEqual(result[1]["oldUrl"], "http://test-site-default-port/old-path")
self.assertEqual(result[2]["oldUrl"], "https://test-site-secure/old-path")

def test_all_sites_url(self):
"""
Expand Down

0 comments on commit a003b5a

Please sign in to comment.