diff options
Diffstat (limited to 'pydis_site/apps/home')
| -rw-r--r-- | pydis_site/apps/home/tests/test_repodata_helpers.py | 19 | ||||
| -rw-r--r-- | pydis_site/apps/home/views.py | 15 | 
2 files changed, 32 insertions, 2 deletions
diff --git a/pydis_site/apps/home/tests/test_repodata_helpers.py b/pydis_site/apps/home/tests/test_repodata_helpers.py index acf4a817..6715444b 100644 --- a/pydis_site/apps/home/tests/test_repodata_helpers.py +++ b/pydis_site/apps/home/tests/test_repodata_helpers.py @@ -5,6 +5,7 @@ from unittest import mock  from django.test import TestCase  from django.utils import timezone +from httpx import HTTPStatusError  from pydis_site.apps.home.models import RepositoryMetadata  from pydis_site.apps.home.views import HomeView @@ -20,6 +21,16 @@ def mocked_requests_get(*args, **kwargs) -> "MockResponse":  # noqa: F821          def json(self):              return self.json_data +        def raise_for_status(self): +            if not 200 >= self.status_code < 400: +                raise HTTPStatusError( +                    # NOTE: We only consume the response status code when working with this helper. +                    # If we ever need the request, this shim needs to be updated. +                    f"Received non-200/300 status code when performing request: HTTP {self.status_code}", +                    request=None, +                    response=self +                ) +      if args[0] == HomeView.github_api:          json_path = Path(__file__).resolve().parent / "mock_github_api_response.json"          with open(json_path) as json_file: @@ -98,6 +109,14 @@ class TestRepositoryMetadataHelpers(TestCase):          self.assertIsNotNone(success_data.json_data)          self.assertIsNone(fail_data.json_data) +    @mock.patch('httpx.get', side_effect=mocked_requests_get) +    def test_mocked_requests_raise_status(self, mock_get: mock.MagicMock): +        """Tests if our mocked_requests_get raises an exception for bad statuses.""" +        fail_data = mock_get("failtest") + +        with self.assertRaises(HTTPStatusError): +            fail_data.raise_for_status() +      @mock.patch('httpx.get')      def test_falls_back_to_database_on_error(self, mock_get: mock.MagicMock):          """Tests that fallback to the database is performed when we get garbage back.""" diff --git a/pydis_site/apps/home/views.py b/pydis_site/apps/home/views.py index 71f95293..0df84478 100644 --- a/pydis_site/apps/home/views.py +++ b/pydis_site/apps/home/views.py @@ -1,4 +1,5 @@  import logging +from json.decoder import JSONDecodeError  import httpx  from django.core.handlers.wsgi import WSGIRequest @@ -53,14 +54,24 @@ class HomeView(View):          repo_dict = {}          try:              # Fetch the data from the GitHub API -            api_data: list[dict] = httpx.get( +            resp = httpx.get(                  self.github_api,                  headers=self.headers,                  timeout=settings.TIMEOUT_PERIOD -            ).json() +            ) + +            resp.raise_for_status() + +            api_data: list[dict] = resp.json()          except httpx.TimeoutException:              log.error("Request to fetch GitHub repository metadata for timed out!")              return repo_dict +        except httpx.HTTPStatusError as ex: +            log.error(f"Received HTTP {ex.response.status_code} from GitHub repository metadata request!") +            return repo_dict +        except JSONDecodeError: +            log.error("GitHub returned invalid JSON for repository metadata!") +            return repo_dict          # Process the API data into our dict          for repo in api_data:  |