Skip to content

Commit

Permalink
Improved the 404 not found error message on profile-by-url
Browse files Browse the repository at this point in the history
  • Loading branch information
Adi Eyal authored and milafrerichs committed Jan 11, 2021
1 parent b4cdda3 commit f334b8e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 25 additions & 0 deletions tests/profile/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from tests.profile.factories import ProfileFactory, IndicatorCategoryFactory, IndicatorSubcategoryFactory, ProfileIndicatorFactory
from tests.datasets.factories import DatasetFactory, IndicatorFactory, IndicatorDataFactory, GroupFactory

from wazimap_ng.profile.views import ProfileByUrl

@pytest.mark.django_db
class TestProfileGeographyData(APITestCase):
Expand Down Expand Up @@ -114,3 +115,27 @@ def test_profile_geography_data_indicator_default_ordering_is_correct(self):

assert len(indicators) == 2
assert indicator_list[0][0] == 'Indicator'


@pytest.fixture
def profile():
_profile = ProfileFactory()
_profile.configuration = {
"urls": ["some_domain.com"]
}

_profile.save()

return _profile

@pytest.mark.django_db
class TestProfileByUrl:
def test_missing_url(self, profile, rf):
request = rf.get("profile-by-url")
response = ProfileByUrl.as_view()(request)
assert response.status_code == 404

def test_matched_url(self, profile, rf):
request = rf.get("profile-by-url", HTTP_WM_HOSTNAME="some_domain.com")
response = ProfileByUrl.as_view()(request)
assert response.status_code == 200
3 changes: 2 additions & 1 deletion wazimap_ng/profile/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework.exceptions import NotFound


from . import models
Expand Down Expand Up @@ -51,7 +52,7 @@ def retrieve(self, request, *args, **kwargs):
qs = qs.filter(configuration__urls__contains=[hostname])
if qs.count() == 0:
logger.warning(f"Can't find a profile for {hostname} - returning 404 ")
raise Http404
raise NotFound(detail=f"Could not find matching profile with hostname: {hostname}. Check your profile configuration to ensure that it contains {hostname} in the urls array.")

instance = qs.first()
serializer = self.get_serializer(instance)
Expand Down

0 comments on commit f334b8e

Please sign in to comment.