From a003b5a3396bd2a06471f8cd5de5c3887593676f Mon Sep 17 00:00:00 2001 From: Jakub Mastalerz Date: Tue, 19 Dec 2023 13:29:58 +0100 Subject: [PATCH] Added Site to RedirectObjectType --- grapple/types/redirects.py | 6 +++- tests/test_redirects.py | 72 ++++++++++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/grapple/types/redirects.py b/grapple/types/redirects.py index 3d201ad1..567d095f 100644 --- a/grapple/types/redirects.py +++ b/grapple/types/redirects.py @@ -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 @@ -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: @@ -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 = [] diff --git a/tests/test_redirects.py b/tests/test_redirects.py index bdf782c9..0b9eec99 100644 --- a/tests/test_redirects.py +++ b/tests/test_redirects.py @@ -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): """ @@ -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. @@ -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, ), ) @@ -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): """