diff --git a/cms/djangoapps/contentstore/views/tests/test_entrance_exam.py b/cms/djangoapps/contentstore/views/tests/test_entrance_exam.py index 1fb7d8acfdd3c117a7897c8ea4803c30dcef0a3b..6aea2d92df86d8da36c4308fea8603a9eec25367 100644 --- a/cms/djangoapps/contentstore/views/tests/test_entrance_exam.py +++ b/cms/djangoapps/contentstore/views/tests/test_entrance_exam.py @@ -7,7 +7,6 @@ import json from unittest.mock import patch from django.conf import settings -from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user from django.test.client import RequestFactory from milestones.tests.utils import MilestonesTestCaseMixin from opaque_keys.edx.keys import UsageKey @@ -173,7 +172,7 @@ class EntranceExamHandlerTests(CourseTestCase, MilestonesTestCaseMixin): resp = self.client.get(self.exam_url) self.assertEqual(resp.status_code, 404) - user = User.objects.create( + user = UserFactory.create( username='test_user', email='test_user@edx.org', is_active=True, @@ -287,7 +286,7 @@ class EntranceExamHandlerTests(CourseTestCase, MilestonesTestCaseMixin): """ Unit Test: test_contentstore_views_entrance_exam_get_invalid_user """ - user = User.objects.create( + user = UserFactory.create( username='test_user', email='test_user@edx.org', is_active=True, diff --git a/common/djangoapps/student/management/tests/test_change_enterprise_user_username.py b/common/djangoapps/student/management/tests/test_change_enterprise_user_username.py index 9f6f60f5b40abcde03bac79cc367b0855d709721..a1354feab5552421276786c62c898b0067dc58c5 100644 --- a/common/djangoapps/student/management/tests/test_change_enterprise_user_username.py +++ b/common/djangoapps/student/management/tests/test_change_enterprise_user_username.py @@ -12,6 +12,8 @@ from django.test import TestCase from enterprise.models import EnterpriseCustomer, EnterpriseCustomerUser from pytest import mark +from common.djangoapps.student.tests.factories import UserFactory + @mark.django_db class ChangeEnterpriseUserUsernameCommandTests(TestCase): @@ -25,7 +27,7 @@ class ChangeEnterpriseUserUsernameCommandTests(TestCase): """ Test that the command does not update a user's username if it is not linked to an Enterprise. """ - user = User.objects.create(is_active=True, username='old_username', email='test@example.com') + user = UserFactory.create(is_active=True, username='old_username', email='test@example.com') new_username = 'new_username' post_save_handler = mock.MagicMock() @@ -41,7 +43,7 @@ class ChangeEnterpriseUserUsernameCommandTests(TestCase): """ Test that the command updates the user's username when the user is linked to an Enterprise. """ - user = User.objects.create(is_active=True, username='old_username', email='test@example.com') + user = UserFactory.create(is_active=True, username='old_username', email='test@example.com') site, _ = Site.objects.get_or_create(domain='example.com') enterprise_customer = EnterpriseCustomer.objects.create( name='Test EnterpriseCustomer', diff --git a/common/djangoapps/student/tests/tests.py b/common/djangoapps/student/tests/tests.py index 73d2cc5721a3ff4301d670337eddd806cf614dce..e82036593df95a64d4e2866c28177e3c00689c41 100644 --- a/common/djangoapps/student/tests/tests.py +++ b/common/djangoapps/student/tests/tests.py @@ -763,7 +763,7 @@ class EnrollInCourseTest(EnrollmentEventTestMixin, CacheIsolationTestCase): def test_enrollment_non_existent_user(self): # Testing enrollment of newly unsaved user (i.e. no database entry) - user = User(username="rusty", email="rusty@fake.edx.org") + user = UserFactory(username="rusty", email="rusty@fake.edx.org") course_id = CourseLocator("edX", "Test101", "2013") course = CourseOverviewFactory.create(id=course_id) @@ -781,7 +781,7 @@ class EnrollInCourseTest(EnrollmentEventTestMixin, CacheIsolationTestCase): @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') def test_enrollment_by_email(self): - user = User.objects.create(username="jack", email="jack@fake.edx.org") + user = UserFactory.create(username="jack", email="jack@fake.edx.org") course_id = CourseLocator("edX", "Test101", "2013") course = CourseOverviewFactory.create(id=course_id) @@ -818,7 +818,7 @@ class EnrollInCourseTest(EnrollmentEventTestMixin, CacheIsolationTestCase): @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') def test_enrollment_multiple_classes(self): - user = User(username="rusty", email="rusty@fake.edx.org") + user = UserFactory(username="rusty", email="rusty@fake.edx.org") course_id1 = CourseLocator("edX", "Test101", "2013") course_id2 = CourseLocator("MITx", "6.003z", "2012") course1 = CourseOverviewFactory.create(id=course_id1) @@ -843,7 +843,7 @@ class EnrollInCourseTest(EnrollmentEventTestMixin, CacheIsolationTestCase): @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') def test_activation(self): - user = User.objects.create(username="jack", email="jack@fake.edx.org") + user = UserFactory.create(username="jack", email="jack@fake.edx.org") course_id = CourseLocator("edX", "Test101", "2013") course = CourseOverviewFactory.create(id=course_id) assert not CourseEnrollment.is_enrolled(user, course_id) @@ -881,7 +881,7 @@ class EnrollInCourseTest(EnrollmentEventTestMixin, CacheIsolationTestCase): self.assert_enrollment_event_was_emitted(user, course_id, course, enrollment) def test_change_enrollment_modes(self): - user = User.objects.create(username="justin", email="jh@fake.edx.org") + user = UserFactory.create(username="justin", email="jh@fake.edx.org") course_id = CourseLocator("edX", "Test101", "2013") course = CourseOverviewFactory.create(id=course_id) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py index 38912a4354ed6859b02f00f353dbdfed89c155bf..908ad97baa61d4e95749e7b9da5957120f900dd6 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py @@ -11,7 +11,7 @@ from enum import Enum from unittest.mock import patch from django.conf import settings -from django.contrib.auth.models import AnonymousUser, User # lint-amnesty, pylint: disable=imported-auth-user +from django.contrib.auth.models import AnonymousUser from django.db import connections from django.test import TestCase from django.test.utils import override_settings @@ -525,7 +525,7 @@ class ModuleStoreTestCase( if self.CREATE_USER: # Create the user so we can log them in. - self.user = User.objects.create_user(uname, email, self.user_password) + self.user = UserFactory.create(username=uname, email=email, password=self.user_password) # Note that we do not actually need to do anything # for registration if we directly mark them active. @@ -542,7 +542,7 @@ class ModuleStoreTestCase( """ uname = 'teststudent' password = 'foo' - nonstaff_user = User.objects.create_user(uname, 'test+student@edx.org', password) + nonstaff_user = UserFactory.create(username=uname, email='test+student@edx.org', password=password) # Note that we do not actually need to do anything # for registration if we directly mark them active. diff --git a/lms/djangoapps/grades/management/commands/tests/test_compute_grades.py b/lms/djangoapps/grades/management/commands/tests/test_compute_grades.py index bb4d6b9220ff79b86e11886db25e77ff822f4651..c51144ad240db497f4d50fcac874c15f03f0005b 100644 --- a/lms/djangoapps/grades/management/commands/tests/test_compute_grades.py +++ b/lms/djangoapps/grades/management/commands/tests/test_compute_grades.py @@ -7,9 +7,9 @@ from unittest.mock import ANY, patch import ddt import pytest -from django.contrib.auth import get_user_model from django.core.management import CommandError, call_command +from common.djangoapps.student.tests.factories import UserFactory from common.djangoapps.student.models import CourseEnrollment from lms.djangoapps.grades.config.models import ComputeGradesSetting from lms.djangoapps.grades.management.commands import compute_grades @@ -32,7 +32,7 @@ class TestComputeGrades(SharedModuleStoreTestCase): cls.courses = [CourseFactory.create() for _ in range(cls.num_courses)] cls.course_keys = [str(course.id) for course in cls.courses] - cls.users = [get_user_model().objects.create(username=f'user{idx}') for idx in range(cls.num_users)] + cls.users = [UserFactory.create(username=f'user{idx}') for idx in range(cls.num_users)] for user in cls.users: for course in cls.courses: diff --git a/openedx/core/djangoapps/credit/tests/test_services.py b/openedx/core/djangoapps/credit/tests/test_services.py index b300d59b05655cc149572a90cf26d41890868065..70fd22aa8aa0ad120151d3fe5951461575d2c1b4 100644 --- a/openedx/core/djangoapps/credit/tests/test_services.py +++ b/openedx/core/djangoapps/credit/tests/test_services.py @@ -26,7 +26,8 @@ class CreditServiceTests(ModuleStoreTestCase): self.service = CreditService() self.course = CourseFactory.create(org='edX', number='DemoX', display_name='Demo_Course') self.credit_course = CreditCourse.objects.create(course_key=self.course.id, enabled=True) - self.profile = UserProfile.objects.create(user_id=self.user.id, name='Foo Bar') + self.user.profile.name = 'Foo Bar' + self.user.profile.save() def enroll(self, course_id=None, mode=CourseMode.VERIFIED): """ diff --git a/openedx/core/djangoapps/user_authn/views/tests/test_login.py b/openedx/core/djangoapps/user_authn/views/tests/test_login.py index aa4c3eead6aa6375441c4d9ef6216a3801e13d5f..50827e65404da699575e6cb677eff40157af87b3 100644 --- a/openedx/core/djangoapps/user_authn/views/tests/test_login.py +++ b/openedx/core/djangoapps/user_authn/views/tests/test_login.py @@ -70,7 +70,7 @@ class LoginTest(SiteMixin, CacheIsolationTestCase): self.url = reverse('login_api') def _create_user(self, username, user_email): - user = UserFactory.build(username=username, email=user_email) + user = UserFactory.create(username=username, email=user_email) user.set_password(self.password) user.save() return user