Skip to content

Commit

Permalink
feat: handle countries not recognized by geoip2 DB
Browse files Browse the repository at this point in the history
The `geoip2` library returns `None` when it only identifies the continent
related to the IP address.
  • Loading branch information
Agrendalath committed Jan 24, 2024
1 parent 41637a4 commit eb47194
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lms/djangoapps/learner_recommendations/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def test_successful_response(
assert segment_mock.call_args[0][1] == "edx.bi.user.recommendations.viewed"


@ddt.ddt
class TestCrossProductRecommendationsView(APITestCase):
"""Unit tests for the Cross Product Recommendations View"""

Expand Down Expand Up @@ -218,13 +219,12 @@ def _get_recommended_courses(self, num_of_courses_with_restriction=0):
@mock.patch("django.conf.settings.CROSS_PRODUCT_RECOMMENDATIONS_KEYS", mock_cross_product_recommendation_keys)
@mock.patch("lms.djangoapps.learner_recommendations.views.get_course_data")
@mock.patch("lms.djangoapps.learner_recommendations.views.country_code_from_ip")
def test_successful_response(
self, country_code_from_ip_mock, get_course_data_mock,
):
@ddt.data("za", "") # Ensure that the empty string is handled correctly.
def test_successful_response(self, country_code, country_code_from_ip_mock, get_course_data_mock):
"""
Verify 2 cross product course recommendations are returned.
"""
country_code_from_ip_mock.return_value = "za"
country_code_from_ip_mock.return_value = country_code
mock_course_data = self._get_recommended_courses()
get_course_data_mock.side_effect = [mock_course_data[0], mock_course_data[1]]

Expand Down
2 changes: 1 addition & 1 deletion openedx/core/djangoapps/geoinfo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def country_code_from_ip(ip_addr: str) -> str:
try:
response = reader.country(ip_addr)
# pylint: disable=no-member
country_code = response.country.iso_code
country_code = response.country.iso_code or ""
except geoip2.errors.AddressNotFoundError:
country_code = ""
reader.close()
Expand Down

0 comments on commit eb47194

Please sign in to comment.