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

Code before:

        Redirect.objects.create(
            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": {

Review comment (suggestion):
We should add a test for just the index listing also, using `find` is not the only thing added with this new API endpoint

Code after:

        Redirect.objects.create(
            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