From 709e31a2aa12755a219187dc6efcad8638f3f33b Mon Sep 17 00:00:00 2001 From: mubbsharanwar Date: Tue, 20 Apr 2021 14:38:59 +0500 Subject: [PATCH] Add country code in MFEAppContextView add new api MFEAppContextView to handle country code add unit test MFEAppContextViewTest this newly created api will be marged in TPAContextViewTest when margeing code in master branch VAN-366 --- .../user_authn/api/tests/test_views.py | 26 +++++++++++++++---- .../core/djangoapps/user_authn/api/urls.py | 7 +++-- .../core/djangoapps/user_authn/api/views.py | 17 ++++++------ .../core/djangoapps/user_authn/views/utils.py | 16 ++++++++++++ 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/openedx/core/djangoapps/user_authn/api/tests/test_views.py b/openedx/core/djangoapps/user_authn/api/tests/test_views.py index f270398ba7..8edef4ee01 100644 --- a/openedx/core/djangoapps/user_authn/api/tests/test_views.py +++ b/openedx/core/djangoapps/user_authn/api/tests/test_views.py @@ -3,6 +3,7 @@ Logistration API View Tests """ from unittest.mock import patch from urllib.parse import urlencode +import socket import ddt from django.conf import settings from django.urls import reverse @@ -11,13 +12,14 @@ from rest_framework.test import APITestCase from openedx.core.djangolib.testing.utils import skip_unless_lms from common.djangoapps.third_party_auth import pipeline from common.djangoapps.third_party_auth.tests.testutil import ThirdPartyAuthTestMixin, simulate_running_pipeline +from openedx.core.djangoapps.geoinfo.api import country_code_from_ip @skip_unless_lms @ddt.ddt -class TPAContextViewTest(ThirdPartyAuthTestMixin, APITestCase): +class MFEContextViewTest(ThirdPartyAuthTestMixin, APITestCase): """ - Third party auth context tests + MFE context tests """ def setUp(self): # pylint: disable=arguments-differ @@ -26,9 +28,13 @@ class TPAContextViewTest(ThirdPartyAuthTestMixin, APITestCase): """ super().setUp() - self.url = reverse('third_party_auth_context') + self.url = reverse('mfe_context') self.query_params = {'next': '/dashboard'} + hostname = socket.gethostname() + ip_address = socket.gethostbyname(hostname) + self.country_code = country_code_from_ip(ip_address) + # Several third party auth providers are created for these tests: self.configure_google_provider(enabled=True, visible=True) self.configure_facebook_provider(enabled=True, visible=True) @@ -75,7 +81,7 @@ class TPAContextViewTest(ThirdPartyAuthTestMixin, APITestCase): def get_context(self, params=None, current_provider=None, backend_name=None, add_user_details=False): """ - Returns the third party auth context + Returns the MFE context """ return { 'currentProvider': current_provider, @@ -86,7 +92,8 @@ class TPAContextViewTest(ThirdPartyAuthTestMixin, APITestCase): 'errorMessage': None, 'registerFormSubmitButtonText': 'Create Account', 'syncLearnerProfileData': False, - 'pipeline_user_details': {'email': 'test@test.com'} if add_user_details else {} + 'pipeline_user_details': {'email': 'test@test.com'} if add_user_details else {}, + 'countryCode': self.country_code } @patch.dict(settings.FEATURES, {'ENABLE_THIRD_PARTY_AUTH': False}) @@ -159,3 +166,12 @@ class TPAContextViewTest(ThirdPartyAuthTestMixin, APITestCase): response = self.client.get(self.url, self.query_params) assert response.data['providers'] == provider_data + + def test_user_country_code(self): + """ + Test api that returns country code of user + """ + response = self.client.get(self.url, self.query_params) + + assert response.status_code == 200 + assert response.data['countryCode'] == self.country_code diff --git a/openedx/core/djangoapps/user_authn/api/urls.py b/openedx/core/djangoapps/user_authn/api/urls.py index 8e5d9c208f..2e9d4993f0 100644 --- a/openedx/core/djangoapps/user_authn/api/urls.py +++ b/openedx/core/djangoapps/user_authn/api/urls.py @@ -4,10 +4,9 @@ Authn API urls from django.conf.urls import url -from openedx.core.djangoapps.user_authn.api.views import TPAContextView +from openedx.core.djangoapps.user_authn.api.views import MFEContextView urlpatterns = [ - url( - r'^third_party_auth_context$', TPAContextView.as_view(), name='third_party_auth_context' - ), + url(r'^third_party_auth_context$', MFEContextView.as_view(), name='third_party_auth_context'), + url(r'^mfe_context$', MFEContextView.as_view(), name='mfe_context'), ] diff --git a/openedx/core/djangoapps/user_authn/api/views.py b/openedx/core/djangoapps/user_authn/api/views.py index b62d4a1949..08a631e843 100644 --- a/openedx/core/djangoapps/user_authn/api/views.py +++ b/openedx/core/djangoapps/user_authn/api/views.py @@ -9,25 +9,26 @@ from rest_framework.throttling import AnonRateThrottle from rest_framework.views import APIView from common.djangoapps.student.helpers import get_next_url_for_login_page -from openedx.core.djangoapps.user_authn.views.utils import third_party_auth_context +from openedx.core.djangoapps.user_authn.views.utils import get_mfe_context -class ThirdPartyAuthContextThrottle(AnonRateThrottle): +class MFEContextThrottle(AnonRateThrottle): """ - Setting rate limit for ThirdPartyAuthContext API + Setting rate limit for MFEContextView API """ rate = settings.LOGISTRATION_API_RATELIMIT -class TPAContextView(APIView): +class MFEContextView(APIView): """ - API to get third party auth providers and the currently running pipeline. + API to get third party auth providers, user country code and the currently running pipeline. """ - throttle_classes = [ThirdPartyAuthContextThrottle] + throttle_classes = [MFEContextThrottle] def get(self, request, **kwargs): # lint-amnesty, pylint: disable=unused-argument """ - Returns the context for third party auth providers and the currently running pipeline. + Returns the context for third party auth providers, user country code + and the currently running pipeline. Arguments: request (HttpRequest): The request, used to determine if a pipeline @@ -39,7 +40,7 @@ class TPAContextView(APIView): redirect_to = get_next_url_for_login_page(request) third_party_auth_hint = request_params.get('tpa_hint') - context = third_party_auth_context(request, redirect_to, third_party_auth_hint) + context = get_mfe_context(request, redirect_to, third_party_auth_hint) return Response( status=status.HTTP_200_OK, data=context diff --git a/openedx/core/djangoapps/user_authn/views/utils.py b/openedx/core/djangoapps/user_authn/views/utils.py index 6809dfab0d..12432b3a1f 100644 --- a/openedx/core/djangoapps/user_authn/views/utils.py +++ b/openedx/core/djangoapps/user_authn/views/utils.py @@ -4,10 +4,12 @@ User Auth Views Utils from django.conf import settings from django.contrib import messages from django.utils.translation import ugettext as _ +from ipware.ip import get_client_ip from common.djangoapps import third_party_auth from common.djangoapps.third_party_auth import pipeline from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers +from openedx.core.djangoapps.geoinfo.api import country_code_from_ip UUID4_REGEX = '[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}' @@ -87,3 +89,17 @@ def third_party_auth_context(request, redirect_to, tpa_hint=None): break return context + + +def get_mfe_context(request, redirect_to, tpa_hint=None): + """ + Returns Authn MFE context. + """ + + ip_address = get_client_ip(request)[0] + country_code = country_code_from_ip(ip_address) + context = third_party_auth_context(request, redirect_to, tpa_hint) + context.update({ + 'countryCode': country_code, + }) + return context