Skip to content

Commit

Permalink
Merge pull request #765 from QuentinPace/main
Browse files Browse the repository at this point in the history
Added proper error handling when trying to return false templates
  • Loading branch information
geekygirlsarah authored Jan 29, 2025
2 parents a4c9a44 + 6dfc7c2 commit fe62247
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
28 changes: 28 additions & 0 deletions web/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,31 @@ def test_robots_txt_post_disallowed(self):
response = self.client.post("/robots.txt")

self.assertEqual(HTTPStatus.METHOD_NOT_ALLOWED, response.status_code)

def test_api_not_found(self):
"""Test if an invalid API call returns a 404 error"""
url = "http://localhost:8000/api/shared/config/config.env/"
response = self.client.get(url)

self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)

def test_single_concept_view_valid_language_version(self):
"""
Test if the API response contains 'meta' and 'concepts' keys. And doesnt return error.
"""
url = '/api/data_types/javascript/ECMAScript%202009/'

# Send GET request to the URL
response = self.client.get(url)

# Check the response status
self.assertEqual(response.status_code, HTTPStatus.OK)

# Parse the response JSON
response_data = response.json()

# Assert that 'meta' and 'concepts' keys exist
self.assertIn('meta', response_data)
self.assertIn('concepts', response_data)


7 changes: 6 additions & 1 deletion web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,16 @@ def api_reference(request, structure_key, lang, version):
store_url_info(request)

lang = Language(lang, "")
response = lang.load_filled_concepts(structure_key, version)

try:
response = lang.load_filled_concepts(structure_key, version)
except Exception as e:
return error_handler_404_not_found(request, e)

if response is False:
return HttpResponseNotFound()


return HttpResponse(response, content_type="application/json")

def api_compare(request, structure_key, lang1, version1, lang2, version2):
Expand Down

0 comments on commit fe62247

Please sign in to comment.