From 5707bbdc90073b4145587e59133f2038247109ae Mon Sep 17 00:00:00 2001
From: Ahtisham Shahid <ahtisham300@gmail.com>
Date: Mon, 22 Jun 2020 17:10:41 +0500
Subject: [PATCH] updated confirm_email field type (#24205)

* updated confirm_email field type and removed confirm email form v1
---
 openedx/core/djangoapps/user_authn/utils.py           |  9 +++++++++
 openedx/core/djangoapps/user_authn/views/register.py  |  6 +++++-
 .../djangoapps/user_authn/views/registration_form.py  | 11 +++++++----
 .../user_authn/views/tests/test_register.py           |  2 +-
 4 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/openedx/core/djangoapps/user_authn/utils.py b/openedx/core/djangoapps/user_authn/utils.py
index e9fddab58f4..2ed90062f13 100644
--- a/openedx/core/djangoapps/user_authn/utils.py
+++ b/openedx/core/djangoapps/user_authn/utils.py
@@ -58,3 +58,12 @@ def generate_password(length=12, chars=string.ascii_letters + string.digits):
     password += choice(string.ascii_letters)
     password += ''.join([choice(chars) for _i in range(length - 2)])
     return password
+
+
+def is_registration_api_v1(request):
+    """
+    Checks if registration api is v1
+    :param request:
+    :return: Bool
+    """
+    return 'v1' in request.get_full_path() and 'register' not in request.get_full_path()
diff --git a/openedx/core/djangoapps/user_authn/views/register.py b/openedx/core/djangoapps/user_authn/views/register.py
index 965f4c9baeb..d9105f4702d 100644
--- a/openedx/core/djangoapps/user_authn/views/register.py
+++ b/openedx/core/djangoapps/user_authn/views/register.py
@@ -48,7 +48,7 @@ from openedx.core.djangoapps.user_api.accounts.api import (
     get_username_existence_validation_error,
     get_username_validation_error
 )
-from openedx.core.djangoapps.user_authn.utils import generate_password
+from openedx.core.djangoapps.user_authn.utils import generate_password, is_registration_api_v1
 from openedx.core.djangoapps.user_api.preferences import api as preferences_api
 from openedx.core.djangoapps.user_authn.cookies import set_logged_in_cookies
 from openedx.core.djangoapps.user_authn.views.registration_form import (
@@ -157,6 +157,10 @@ def create_account_with_params(request, params):
         'REGISTRATION_EXTRA_FIELDS',
         getattr(settings, 'REGISTRATION_EXTRA_FIELDS', {})
     )
+    if is_registration_api_v1(request):
+        if 'confirm_email' in extra_fields:
+            del extra_fields['confirm_email']
+
     # registration via third party (Google, Facebook) using mobile application
     # doesn't use social auth pipeline (no redirect uri(s) etc involved).
     # In this case all related info (required for account linking)
diff --git a/openedx/core/djangoapps/user_authn/views/registration_form.py b/openedx/core/djangoapps/user_authn/views/registration_form.py
index b3ce0e4b330..fbc7ce53448 100644
--- a/openedx/core/djangoapps/user_authn/views/registration_form.py
+++ b/openedx/core/djangoapps/user_authn/views/registration_form.py
@@ -23,6 +23,7 @@ from edxmako.shortcuts import marketing_link
 from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
 from openedx.core.djangoapps.user_api import accounts
 from openedx.core.djangoapps.user_api.helpers import FormDescription
+from openedx.core.djangoapps.user_authn.utils import is_registration_api_v1 as is_api_v1
 from openedx.core.djangolib.markup import HTML, Text
 from openedx.features.enterprise_support.api import enterprise_customer_for_request
 from student.models import (
@@ -373,7 +374,7 @@ class RegistrationFormFactory(object):
         Returns:
             HttpResponse
         """
-        form_desc = FormDescription("post", reverse("user_api_registration"))
+        form_desc = FormDescription("post", self._get_registration_submit_url(request))
         self._apply_third_party_auth_overrides(request, form_desc)
 
         # Custom form fields can be added via the form set in settings.REGISTRATION_EXTENSION_FORM
@@ -429,16 +430,17 @@ class RegistrationFormFactory(object):
                         form_desc,
                         required=self._is_field_required(field_name)
                     )
-
         # remove confirm_email form v1 registration form
-        if 'v1' in request.get_full_path():
+        if is_api_v1(request):
             for index, field in enumerate(form_desc.fields):
                 if field['name'] == 'confirm_email':
                     del form_desc.fields[index]
                     break
-
         return form_desc
 
+    def _get_registration_submit_url(self, request):
+        return reverse("user_api_registration") if is_api_v1(request) else reverse("user_api_registration_v2")
+
     def _add_email_field(self, form_desc, required=True):
         """Add an email field to a form description.
         Arguments:
@@ -481,6 +483,7 @@ class RegistrationFormFactory(object):
 
         form_desc.add_field(
             "confirm_email",
+            field_type="email",
             label=email_label,
             required=required,
             error_messages={
diff --git a/openedx/core/djangoapps/user_authn/views/tests/test_register.py b/openedx/core/djangoapps/user_authn/views/tests/test_register.py
index d3e83c810ee..4a0d7f7a54f 100644
--- a/openedx/core/djangoapps/user_authn/views/tests/test_register.py
+++ b/openedx/core/djangoapps/user_authn/views/tests/test_register.py
@@ -1894,7 +1894,7 @@ class RegistrationViewTestV2(RegistrationViewTestV1):
             {"confirm_email": "required"},
             {
                 "name": "confirm_email",
-                "type": "text",
+                "type": "email",
                 "required": True,
                 "label": "Confirm Email",
                 "errorMessages": {
-- 
GitLab