Skip to content

Commit f334b8e

Browse files
Adi Eyalmilafrerichs
authored andcommitted
Improved the 404 not found error message on profile-by-url
1 parent b4cdda3 commit f334b8e

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

tests/profile/test_views.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from tests.profile.factories import ProfileFactory, IndicatorCategoryFactory, IndicatorSubcategoryFactory, ProfileIndicatorFactory
77
from tests.datasets.factories import DatasetFactory, IndicatorFactory, IndicatorDataFactory, GroupFactory
88

9+
from wazimap_ng.profile.views import ProfileByUrl
910

1011
@pytest.mark.django_db
1112
class TestProfileGeographyData(APITestCase):
@@ -114,3 +115,27 @@ def test_profile_geography_data_indicator_default_ordering_is_correct(self):
114115

115116
assert len(indicators) == 2
116117
assert indicator_list[0][0] == 'Indicator'
118+
119+
120+
@pytest.fixture
121+
def profile():
122+
_profile = ProfileFactory()
123+
_profile.configuration = {
124+
"urls": ["some_domain.com"]
125+
}
126+
127+
_profile.save()
128+
129+
return _profile
130+
131+
@pytest.mark.django_db
132+
class TestProfileByUrl:
133+
def test_missing_url(self, profile, rf):
134+
request = rf.get("profile-by-url")
135+
response = ProfileByUrl.as_view()(request)
136+
assert response.status_code == 404
137+
138+
def test_matched_url(self, profile, rf):
139+
request = rf.get("profile-by-url", HTTP_WM_HOSTNAME="some_domain.com")
140+
response = ProfileByUrl.as_view()(request)
141+
assert response.status_code == 200

wazimap_ng/profile/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from rest_framework import generics
1313
from rest_framework.response import Response
1414
from rest_framework.decorators import api_view
15+
from rest_framework.exceptions import NotFound
1516

1617

1718
from . import models
@@ -51,7 +52,7 @@ def retrieve(self, request, *args, **kwargs):
5152
qs = qs.filter(configuration__urls__contains=[hostname])
5253
if qs.count() == 0:
5354
logger.warning(f"Can't find a profile for {hostname} - returning 404 ")
54-
raise Http404
55+
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.")
5556

5657
instance = qs.first()
5758
serializer = self.get_serializer(instance)

0 commit comments

Comments
 (0)