Add tests for create_user / create_random_users management commands
This commit is contained in:
@@ -34,7 +34,7 @@ class Command(TrackedCommand):
|
||||
parser.add_argument('-u', '--username',
|
||||
metavar='USERNAME',
|
||||
help='Username, defaults to "user" in the email')
|
||||
parser.add_argument('-n', '--name',
|
||||
parser.add_argument('-n', '--proper_name',
|
||||
metavar='NAME',
|
||||
help='Name, defaults to "user" in the email')
|
||||
parser.add_argument('-p', '--password',
|
||||
@@ -54,7 +54,7 @@ class Command(TrackedCommand):
|
||||
|
||||
def handle(self, *args, **options):
|
||||
username = options['username'] if options['username'] else options['email'].split('@')[0]
|
||||
name = options['name'] if options['name'] else options['email'].split('@')[0]
|
||||
name = options['proper_name'] if options['proper_name'] else options['email'].split('@')[0]
|
||||
|
||||
# parse out the course into a coursekey
|
||||
course = CourseKey.from_string(options['course']) if options['course'] else None
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
"""
|
||||
Test the create_random_users command line script
|
||||
"""
|
||||
|
||||
from six import text_type
|
||||
|
||||
import pytest
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.management import call_command
|
||||
from opaque_keys import InvalidKeyError
|
||||
from xmodule.modulestore.tests.factories import CourseFactory
|
||||
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
|
||||
|
||||
from student.models import CourseEnrollment
|
||||
|
||||
|
||||
class CreateRandomUserTests(SharedModuleStoreTestCase):
|
||||
"""
|
||||
Test creating random users via command line, with various options
|
||||
"""
|
||||
def setUp(self):
|
||||
super(CreateRandomUserTests, self).setUp()
|
||||
self.course = CourseFactory.create()
|
||||
self.user_model = get_user_model()
|
||||
self.num_users_start = len(self.user_model.objects.all())
|
||||
|
||||
def test_create_users(self):
|
||||
"""
|
||||
The command should create users_to_create number of random users
|
||||
"""
|
||||
users_to_create = 5
|
||||
call_command('create_random_users', text_type(users_to_create))
|
||||
|
||||
# Verify correct number of users are now in the database
|
||||
self.assertEqual(self.num_users_start + users_to_create, len(self.user_model.objects.all()))
|
||||
|
||||
def test_create_users_with_course(self):
|
||||
"""
|
||||
The command should create users_to_create number of random users and add them to self.course
|
||||
"""
|
||||
users_to_create = 3
|
||||
call_command('create_random_users', text_type(users_to_create), text_type(self.course.id))
|
||||
|
||||
# Verify correct number of users are now in the database
|
||||
self.assertEqual(self.num_users_start + users_to_create, len(self.user_model.objects.all()))
|
||||
|
||||
# Verify that the users are enrolled in our course
|
||||
self.assertEqual(len(CourseEnrollment.objects.filter(course__id=self.course.id)), users_to_create)
|
||||
|
||||
def test_create_users_with_bad_course(self):
|
||||
"""
|
||||
The test passes in a bad course id, no users or CourseEnrollments should be created
|
||||
"""
|
||||
users_to_create = 3
|
||||
|
||||
with pytest.raises(InvalidKeyError):
|
||||
call_command('create_random_users', text_type(users_to_create), u'invalid_course_id')
|
||||
|
||||
# Verify correct number of users are now in the database
|
||||
self.assertEqual(self.num_users_start, len(self.user_model.objects.all()))
|
||||
|
||||
# Verify that the users are enrolled in our course
|
||||
self.assertEqual(len(CourseEnrollment.objects.filter(course__id=self.course.id)), 0)
|
||||
118
common/djangoapps/student/management/tests/test_create_user.py
Normal file
118
common/djangoapps/student/management/tests/test_create_user.py
Normal file
@@ -0,0 +1,118 @@
|
||||
"""
|
||||
Test the create_user command line script
|
||||
"""
|
||||
|
||||
from six import text_type
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.management import call_command
|
||||
from xmodule.modulestore.tests.factories import CourseFactory
|
||||
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
|
||||
|
||||
from course_modes.models import CourseMode
|
||||
from student.models import CourseEnrollment, UserProfile
|
||||
|
||||
|
||||
class CreateUserMgmtTests(SharedModuleStoreTestCase):
|
||||
"""
|
||||
Test creating users via command line, exercising various options
|
||||
"""
|
||||
def setUp(self):
|
||||
super(CreateUserMgmtTests, self).setUp()
|
||||
self.course = CourseFactory.create()
|
||||
self.user_model = get_user_model()
|
||||
self.default_email = 'testuser555@test.edx.org'
|
||||
self.default_password = 'testuser@555@password'
|
||||
|
||||
# This is the default mode that the create_user commands gives a user enrollment
|
||||
self.default_course_mode = CourseMode.HONOR
|
||||
|
||||
def _do_successful_create_and_check(self, **kwargs):
|
||||
"""
|
||||
Performs a create_user that is expected to succeed, passing in kwargs as given
|
||||
"""
|
||||
|
||||
# Do arg munging to work around required option issues in Django see:
|
||||
# https://stackoverflow.com/questions/32036562/call-command-argument-is-required
|
||||
email = kwargs.pop('email', self.default_email)
|
||||
password = kwargs.pop('password', self.default_password)
|
||||
|
||||
should_be_enrolled = 'course' in kwargs
|
||||
should_be_staff = 'staff' in kwargs
|
||||
|
||||
mode = kwargs.get('mode', self.default_course_mode)
|
||||
|
||||
# For right now this method only handles creating users for the default course
|
||||
if 'course' in kwargs:
|
||||
self.assertEqual(kwargs['course'], text_type(self.course.id))
|
||||
|
||||
self.assertFalse(self.user_model.objects.filter(email=email).exists())
|
||||
|
||||
call_command('create_user',
|
||||
'--email={}'.format(email),
|
||||
'--password={}'.format(password),
|
||||
**kwargs
|
||||
)
|
||||
|
||||
self.assertTrue(self.user_model.objects.filter(email=email).exists())
|
||||
|
||||
user = self.user_model.objects.get(email=email)
|
||||
|
||||
self.assertEqual(user.is_staff, should_be_staff)
|
||||
|
||||
# create_user should activate their registration and set them active on success
|
||||
self.assertTrue(user.is_active)
|
||||
|
||||
# Confirm the user is enrolled, or not, as expected
|
||||
self.assertEqual(
|
||||
CourseEnrollment.objects.filter(
|
||||
course__id=self.course.id,
|
||||
user__email=email,
|
||||
mode=mode).exists(),
|
||||
should_be_enrolled
|
||||
)
|
||||
|
||||
def test_create_user(self):
|
||||
"""
|
||||
Run create_user with all defaults
|
||||
"""
|
||||
self._do_successful_create_and_check()
|
||||
|
||||
def test_create_user_with_course(self):
|
||||
"""
|
||||
Run create_user with a course and confirm enrollment
|
||||
"""
|
||||
self._do_successful_create_and_check(course=text_type(self.course.id))
|
||||
|
||||
def test_create_user_as_staff(self):
|
||||
"""
|
||||
Test the functionality of creating the user with the staff flag
|
||||
"""
|
||||
self._do_successful_create_and_check(staff=True)
|
||||
|
||||
def test_create_user_with_overrides(self):
|
||||
"""
|
||||
Test the results of passing in overrides for all optional parameters
|
||||
"""
|
||||
params = {
|
||||
'mode': CourseMode.AUDIT,
|
||||
'username': 'test_username',
|
||||
'proper_name': 'test_name',
|
||||
'password': 'test_password',
|
||||
'email': 'rushfan2112@test.edx.org',
|
||||
'course': text_type(self.course.id),
|
||||
'staff': True
|
||||
}
|
||||
|
||||
self._do_successful_create_and_check(**params)
|
||||
|
||||
user = self.user_model.objects.get(email=params['email'])
|
||||
profile = UserProfile.objects.get(user=user)
|
||||
|
||||
# staff, course, and mode are checked in _do_successful_create_and_check
|
||||
self.assertEqual(params['username'], user.username)
|
||||
self.assertEqual(params['proper_name'], profile.name)
|
||||
self.assertEqual(params['email'], user.email)
|
||||
|
||||
# Check that the password was handled correctly and that the user can log in
|
||||
self.assertTrue(self.client.login(username=params['username'], password=params['password']))
|
||||
Reference in New Issue
Block a user