Repository: wagtail/wagtail
File: wagtail/contrib/redirects/tests/test_redirects_api.py (Python)

Code before:
            old_path="/hello-world",
            site=self.example_site,
            redirect_link="https://www.example.com/hello-world/",
        )

        Redirect.objects.create(
            old_path="/good-work",
            site=self.example_site,
            redirect_link="https://www.example.com/hello-world/",
        )

        Redirect.add_redirect(
            old_path="/hello-world", redirect_to="https://www.example.net/new-world/"
        )

        Redirect.add_redirect(
            old_path="/old-example", redirect_to=self.example_home, is_permanent=False
        )

        Redirect.add_redirect(
            old_path="/old-example?bar=foo&foo=bar",
            redirect_to=self.example_page,
            is_permanent=False,
        )

    def get_response(self, html_path):
        url = reverse("wagtailapi_v2:redirects:find")

        # Add the html_path to the URL
        url += f"?html_path={html_path}"

        return self.client.get(url)

    def test_redirect(self):
        """Returns a matching (not site specific) redirect"""
        html_path = "/hello-world"
        response = self.get_response(html_path)

        # Check for a redirect status code
        self.assertEqual(response.status_code, 302)

        # Follow the redirect to get the final response
        response = self.client.get(response.url)
        self.assertEqual(response.status_code, 200)
        response_id = response.json()["id"]

        expected_dict = {
            "id": response_id,
            "meta": {
                "detail_url": f"http://localhost/api/main/redirects/{response_id}/",
                "type": "wagtailredirects.Redirect",

Review comment (refactor):
Normally we avoid too much abstraction in unit tests early, I would recommend putting this code (copy/paste) into the individual tests.

It feels a bit odd but often in unit tests a bit of duplication can help make the tests easier to understand.

When it gets excessive (e.g. maybe 5+ copy/paste) then we try to abstract common cases.

Code after:
            old_path="/hello-world",
            site=self.example_site,
            redirect_link="https://www.example.com/hello-world/",
        )

        Redirect.objects.create(
            old_path="/good-work",
            site=self.example_site,
            redirect_link="https://www.example.com/hello-world/",
        )

        Redirect.add_redirect(
            old_path="/hello-world", redirect_to="https://www.example.net/new-world/"
        )

        Redirect.add_redirect(
            old_path="/old-example", redirect_to=self.example_home, is_permanent=False
        )

        Redirect.add_redirect(
            old_path="/old-example?bar=foo&foo=bar",
            redirect_to=self.example_page,
            is_permanent=False,
        )

    def test_redirects_listing(self):
        """Returns a list of all redirects"""

        url = reverse("wagtailapi_v2:redirects:listing")

        response = self.client.get(url)

        self.assertEqual(response.status_code, 200)

        self.assertEqual(5, len(response.json()["items"]))

        item = response.json()["items"][0]

        self.assertEqual("https://www.example.com/hello-world/", item["location"])
        self.assertEqual("/hello-world", item["old_path"])

    def test_redirect(self):
        """Returns a matching (not site specific) redirect"""

        url = reverse("wagtailapi_v2:redirects:find")

        html_path = "/hello-world"

        # Add the html_path to the URL
        url += f"?html_path={html_path}"