build: Moved user and group management commands and unit tests to edx-django-utils

- Removed manage_user and manage_group commands and their unit tests from edx-platform and added then to edx-django-utils.
- Modified User.post_save signal to ensure the user profile is created when manage_user management command is run to create a user.
- Added edx-django-utils to INSTALLED_APPS for LMS and Studio.
- Moved generate_password from openedx.core.djangoapps.user_authn.utils to edx_django_utils.user along with its unit test.
This commit is contained in:
Usama Sadiq
2021-08-26 14:34:39 +05:00
committed by Saad Ali
parent 2ed89f5805
commit cde050618e
14 changed files with 27 additions and 713 deletions

View File

@@ -6,7 +6,7 @@ Django forms for accounts
from django import forms
from django.core.exceptions import ValidationError
from openedx.core.djangoapps.user_authn.utils import generate_password
from edx_django_utils.user import generate_password
class RetirementQueueDeletionForm(forms.Form):

View File

@@ -10,7 +10,8 @@ import logging
from django.core.management.base import BaseCommand, CommandError
from openedx.core.djangoapps.user_api.models import UserRetirementStatus
from openedx.core.djangoapps.user_authn.utils import generate_password
from edx_django_utils.user import generate_password
LOGGER = logging.getLogger(__name__)

View File

@@ -3,16 +3,13 @@
from collections import namedtuple
from urllib.parse import urlencode # pylint: disable=import-error
import pytest
import ddt
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from openedx.core.djangoapps.oauth_dispatch.tests.factories import ApplicationFactory
from openedx.core.djangoapps.user_authn.utils import (
generate_password, is_safe_login_or_logout_redirect
)
from openedx.core.djangoapps.user_authn.utils import is_safe_login_or_logout_redirect
@ddt.ddt
@@ -71,29 +68,3 @@ class TestRedirectUtils(TestCase):
req = self.request.get(f'/logout?{urlencode(params)}', HTTP_HOST=host)
actual_is_safe = self._is_safe_redirect(req, redirect_url)
assert actual_is_safe == expected_is_safe
class GeneratePasswordTest(TestCase):
"""Tests formation of randomly generated passwords."""
def test_default_args(self):
password = generate_password()
assert 12 == len(password)
assert any(c.isdigit for c in password)
assert any(c.isalpha for c in password)
def test_length(self):
length = 25
assert length == len(generate_password(length=length))
def test_chars(self):
char = '!'
password = generate_password(length=12, chars=(char,))
assert any(c.isdigit for c in password)
assert any(c.isalpha for c in password)
assert (char * 10) == password[2:]
def test_min_length(self):
with pytest.raises(ValueError):
generate_password(length=7)

View File

@@ -3,7 +3,6 @@ Utility functions used during user authentication.
"""
import random
import string
from urllib.parse import urlparse # pylint: disable=import-error
from uuid import uuid4 # lint-amnesty, pylint: disable=unused-import
@@ -49,20 +48,6 @@ def is_safe_login_or_logout_redirect(redirect_to, request_host, dot_client_id, r
return is_safe_url
def generate_password(length=12, chars=string.ascii_letters + string.digits):
"""Generate a valid random password"""
if length < 8:
raise ValueError("password must be at least 8 characters")
choice = random.SystemRandom().choice
password = ''
password += choice(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

View File

@@ -18,7 +18,6 @@ from opaque_keys.edx.locator import CourseLocator
from lms.djangoapps.verify_student.models import ManualVerification
from openedx.core.djangoapps.django_comment_common.models import assign_role
from openedx.core.djangoapps.user_authn.utils import generate_password
from openedx.core.djangoapps.user_authn.views.registration_form import AccountCreationForm
from openedx.features.course_experience import course_home_url_name
from common.djangoapps.student.helpers import (
@@ -37,6 +36,8 @@ from common.djangoapps.student.models import (
)
from common.djangoapps.util.json_request import JsonResponse
from edx_django_utils.user import generate_password
def auto_auth(request): # pylint: disable=too-many-statements
"""

View File

@@ -52,7 +52,7 @@ from openedx.core.djangoapps.user_api.accounts.api import (
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.utils import (
generate_password, generate_username_suggestions, is_registration_api_v1
generate_username_suggestions, is_registration_api_v1
)
from openedx.core.djangoapps.user_authn.views.registration_form import (
AccountCreationForm,
@@ -80,6 +80,8 @@ from common.djangoapps.track import segment
from common.djangoapps.util.db import outer_atomic
from common.djangoapps.util.json_request import JsonResponse
from edx_django_utils.user import generate_password
log = logging.getLogger("edx.student")
AUDIT_LOG = logging.getLogger("audit")