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
This commit is contained in:
mubbsharanwar
2021-04-20 14:38:59 +05:00
parent b0d41f6ebb
commit 709e31a2aa
4 changed files with 49 additions and 17 deletions

View File

@@ -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

View File

@@ -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'),
]

View File

@@ -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

View File

@@ -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