Skip to content

Commit 2ee52ea

Browse files
refactor: replace some create_user with UserFactory to avoid non-existent profile errors
1 parent b7dfaa9 commit 2ee52ea

File tree

24 files changed

+142
-86
lines changed

24 files changed

+142
-86
lines changed

cms/djangoapps/cms_user_tasks/tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import ddt
1111
from boto.exception import NoAuthHandlerFound
1212
from django.conf import settings
13-
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
1413
from django.core import mail
1514
from django.test import override_settings
1615
from django.urls import reverse
@@ -20,6 +19,7 @@
2019
from user_tasks.serializers import ArtifactSerializer, StatusSerializer
2120

2221
from cms.djangoapps.contentstore.toggles import BYPASS_OLX_FAILURE
22+
from common.djangoapps.student.tests.factories import UserFactory
2323

2424
from .signals import user_task_stopped
2525

@@ -78,7 +78,7 @@ class TestUserTasks(APITestCase):
7878

7979
@classmethod
8080
def setUpTestData(cls): # lint-amnesty, pylint: disable=super-method-not-called
81-
cls.user = User.objects.create_user('test_user', '[email protected]', 'password')
81+
cls.user = UserFactory.create(username='test_user', email='[email protected]', password='password')
8282
cls.status = UserTaskStatus.objects.create(
8383
user=cls.user, task_id=str(uuid4()), task_class='test_rest_api.sample_task', name='SampleTask 2',
8484
total_steps=5)
@@ -152,7 +152,7 @@ class TestUserTaskStopped(APITestCase):
152152

153153
@classmethod
154154
def setUpTestData(cls): # lint-amnesty, pylint: disable=super-method-not-called
155-
cls.user = User.objects.create_user('test_user', '[email protected]', 'password')
155+
cls.user = UserFactory.create(username='test_user', email='[email protected]', password='password')
156156
cls.status = UserTaskStatus.objects.create(
157157
user=cls.user, task_id=str(uuid4()), task_class='test_rest_api.sample_task', name='SampleTask 2',
158158
total_steps=5)

cms/djangoapps/contentstore/tests/test_i18n.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import gettext
77
from unittest import mock, skip
88

9-
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
109
from django.utils import translation
1110
from django.utils.translation import get_language
1211

1312
from cms.djangoapps.contentstore.tests.utils import AjaxEnabledTestClient
1413
from cms.djangoapps.contentstore.views.preview import _preview_module_system
14+
from common.djangoapps.student.tests.factories import UserFactory
1515
from openedx.core.lib.edx_six import get_gettext
1616
from xmodule.modulestore.django import ModuleI18nService
1717
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
@@ -192,7 +192,7 @@ def setUp(self):
192192
self.password = 'foo'
193193

194194
# Create the use so we can log them in.
195-
self.user = User.objects.create_user(self.uname, self.email, self.password)
195+
self.user = UserFactory.create(username=self.uname, email=self.email, password=self.password)
196196

197197
# Note that we do not actually need to do anything
198198
# for registration if we directly mark them active.

cms/djangoapps/contentstore/tests/test_permissions.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55

66
import copy
77

8-
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
9-
108
from cms.djangoapps.contentstore.tests.utils import AjaxEnabledTestClient
119
from cms.djangoapps.contentstore.utils import reverse_course_url, reverse_url
1210
from common.djangoapps.student import auth
1311
from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole, OrgInstructorRole, OrgStaffRole
12+
from common.djangoapps.student.tests.factories import UserFactory
1413
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
1514

1615

@@ -52,7 +51,7 @@ def _create_users(self):
5251
for i in range(8):
5352
username = f"user{i}"
5453
email = f"test+user{i}@edx.org"
55-
user = User.objects.create_user(username, email, 'foo')
54+
user = UserFactory.create(username=username, email=email, password='foo')
5655
user.is_active = True
5756
user.save()
5857
users.append(user)

cms/djangoapps/contentstore/views/tests/test_access.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
"""
44

55

6-
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
76
from django.test import TestCase
87
from opaque_keys.edx.locator import CourseLocator
98

109
from common.djangoapps.student.auth import add_users
1110
from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole
12-
from common.djangoapps.student.tests.factories import AdminFactory
11+
from common.djangoapps.student.tests.factories import AdminFactory, UserFactory
1312

1413
from ..access import get_user_role
1514

@@ -23,8 +22,16 @@ def setUp(self):
2322
super().setUp()
2423

2524
self.global_admin = AdminFactory()
26-
self.instructor = User.objects.create_user('testinstructor', '[email protected]', 'foo')
27-
self.staff = User.objects.create_user('teststaff', '[email protected]', 'foo')
25+
self.instructor = UserFactory.create(
26+
username='testinstructor',
27+
28+
password='foo',
29+
)
30+
self.staff = UserFactory.create(
31+
username='teststaff',
32+
33+
password='foo',
34+
)
2835
self.course_key = CourseLocator('mitX', '101', 'test')
2936

3037
def test_get_user_role_instructor(self):

cms/djangoapps/contentstore/views/tests/test_user.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@
1212
from common.djangoapps.student import auth
1313
from common.djangoapps.student.models import CourseEnrollment
1414
from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole
15+
from common.djangoapps.student.tests.factories import UserFactory
1516

1617

1718
class UsersTestCase(CourseTestCase): # lint-amnesty, pylint: disable=missing-class-docstring
1819
def setUp(self):
1920
super().setUp()
20-
self.ext_user = User.objects.create_user(
21-
"joe", "[email protected]", "haha")
21+
self.ext_user = UserFactory.create(
22+
username="joe", email="[email protected]", password="haha",
23+
)
2224
self.ext_user.is_active = True
2325
self.ext_user.is_staff = False
2426
self.ext_user.save()
25-
self.inactive_user = User.objects.create_user(
26-
"carl", "[email protected]", "haha")
27+
self.inactive_user = UserFactory.create(
28+
username="carl", email="[email protected]", password="haha",
29+
)
2730
self.inactive_user.is_active = False
2831
self.inactive_user.is_staff = False
2932
self.inactive_user.save()

cms/djangoapps/course_creators/tests/test_admin.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from unittest import mock
77

88
from django.contrib.admin.sites import AdminSite
9-
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
109
from django.core import mail
1110
from django.http import HttpRequest
1211
from django.test import TestCase
@@ -15,6 +14,7 @@
1514
from cms.djangoapps.course_creators.models import CourseCreator
1615
from common.djangoapps.student import auth
1716
from common.djangoapps.student.roles import CourseCreatorRole
17+
from common.djangoapps.student.tests.factories import UserFactory
1818

1919

2020
def mock_render_to_string(template_name, context):
@@ -30,11 +30,19 @@ class CourseCreatorAdminTest(TestCase):
3030
def setUp(self):
3131
""" Test case setup """
3232
super().setUp()
33-
self.user = User.objects.create_user('test_user', '[email protected]', 'foo')
33+
self.user = UserFactory.create(
34+
username='test_user',
35+
36+
password='foo',
37+
)
3438
self.table_entry = CourseCreator(user=self.user)
3539
self.table_entry.save()
3640

37-
self.admin = User.objects.create_user('Mark', '[email protected]', 'foo')
41+
self.admin = UserFactory.create(
42+
username='Mark',
43+
44+
password='foo',
45+
)
3846
self.admin.is_staff = True
3947

4048
self.request = HttpRequest()

cms/djangoapps/course_creators/tests/test_views.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from unittest import mock
77

8-
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
98
from django.core.exceptions import PermissionDenied
109
from django.test import TestCase
1110
from django.urls import reverse
@@ -19,6 +18,7 @@
1918
)
2019
from common.djangoapps.student import auth
2120
from common.djangoapps.student.roles import CourseCreatorRole
21+
from common.djangoapps.student.tests.factories import UserFactory
2222

2323

2424
class CourseCreatorView(TestCase):
@@ -29,8 +29,16 @@ class CourseCreatorView(TestCase):
2929
def setUp(self):
3030
""" Test case setup """
3131
super().setUp()
32-
self.user = User.objects.create_user('test_user', '[email protected]', 'foo')
33-
self.admin = User.objects.create_user('Mark', '[email protected]', 'foo')
32+
self.user = UserFactory.create(
33+
username='test_user',
34+
35+
password='foo',
36+
)
37+
self.admin = UserFactory.create(
38+
username='Mark',
39+
40+
password='foo',
41+
)
3442
self.admin.is_staff = True
3543

3644
def test_staff_permission_required(self):

common/djangoapps/student/tests/test_authz.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import pytest
77

88
from ccx_keys.locator import CCXLocator
9-
from django.contrib.auth.models import AnonymousUser, User # lint-amnesty, pylint: disable=imported-auth-user
9+
from django.contrib.auth.models import AnonymousUser
1010
from django.core.exceptions import PermissionDenied
1111
from django.test import TestCase
1212
from opaque_keys.edx.locator import CourseLocator
1313

1414
from common.djangoapps.student.auth import add_users, has_studio_read_access, has_studio_write_access, remove_users, user_has_role # lint-amnesty, pylint: disable=line-too-long
1515
from common.djangoapps.student.roles import CourseCreatorRole, CourseInstructorRole, CourseStaffRole
16-
from common.djangoapps.student.tests.factories import AdminFactory
16+
from common.djangoapps.student.tests.factories import AdminFactory, UserFactory
1717

1818

1919
class CreatorGroupTest(TestCase):
@@ -24,8 +24,12 @@ class CreatorGroupTest(TestCase):
2424
def setUp(self):
2525
""" Test case setup """
2626
super().setUp()
27-
self.user = User.objects.create_user('testuser', '[email protected]', 'foo')
28-
self.admin = User.objects.create_user('Mark', '[email protected]', 'foo')
27+
self.user = UserFactory.create(
28+
username='testuser', email='[email protected]', password='foo',
29+
)
30+
self.admin = UserFactory.create(
31+
username='Mark', email='[email protected]', password='foo',
32+
)
2933
self.admin.is_staff = True
3034

3135
def test_creator_group_not_enabled(self):
@@ -51,7 +55,7 @@ def test_creator_group_enabled_nonempty(self):
5155
assert user_has_role(self.user, CourseCreatorRole())
5256

5357
# check that a user who has not been added to the group still returns false
54-
user_not_added = User.objects.create_user('testuser2', '[email protected]', 'foo2')
58+
user_not_added = UserFactory.create(username='testuser2', email='[email protected]', password='foo2')
5559
assert not user_has_role(user_not_added, CourseCreatorRole())
5660

5761
# remove first user from the group and verify that CourseCreatorRole().has_user now returns false
@@ -153,7 +157,7 @@ def setUp(self):
153157
"""
154158
super().setUp()
155159
self.global_admin = AdminFactory()
156-
self.staff = User.objects.create_user('teststaff', '[email protected]', 'foo')
160+
self.staff = UserFactory.create(username='teststaff', email='[email protected]', password='foo')
157161
self.ccx_course_key = CCXLocator.from_string('ccx-v1:edX+DemoX+Demo_Course+ccx@1')
158162
add_users(self.global_admin, CourseStaffRole(self.ccx_course_key), self.staff)
159163

@@ -191,8 +195,12 @@ def setUp(self):
191195
""" Test case setup """
192196
super().setUp()
193197
self.global_admin = AdminFactory()
194-
self.creator = User.objects.create_user('testcreator', '[email protected]', 'foo')
195-
self.staff = User.objects.create_user('teststaff', '[email protected]', 'foo')
198+
self.creator = UserFactory.create(
199+
username='testcreator', email='[email protected]', password='foo',
200+
)
201+
self.staff = UserFactory.create(
202+
username='teststaff', email='[email protected]', password='foo',
203+
)
196204
self.course_key = CourseLocator('mitX', '101', 'test')
197205

198206
def test_add_user_to_course_group(self):
@@ -240,7 +248,9 @@ def test_remove_user_from_course_group_permission_denied(self):
240248
Verifies PermissionDenied if caller of remove_user_from_course_group is not instructor role.
241249
"""
242250
add_users(self.global_admin, CourseInstructorRole(self.course_key), self.creator)
243-
another_staff = User.objects.create_user('another', '[email protected]', 'foo')
251+
another_staff = UserFactory.create(
252+
username='another', email='[email protected]', password='foo',
253+
)
244254
add_users(self.global_admin, CourseStaffRole(self.course_key), self.creator, self.staff, another_staff)
245255
with pytest.raises(PermissionDenied):
246256
remove_users(self.staff, CourseStaffRole(self.course_key), another_staff)

common/djangoapps/student/tests/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ class EnrollInCourseTest(EnrollmentEventTestMixin, CacheIsolationTestCase):
715715

716716
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
717717
def test_enrollment(self):
718-
user = User.objects.create_user("joe", "[email protected]", "password")
718+
user = UserFactory.create(username="joe", email="[email protected]", password="password")
719719
course_id = CourseKey.from_string("edX/Test101/2013")
720720
course_id_partial = CourseKey.from_string("edX/Test101/")
721721
course = CourseOverviewFactory.create(id=course_id)

common/djangoapps/third_party_auth/saml_configuration/tests/test_saml_configuration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"""
44
from django.urls import reverse
55
from django.contrib.sites.models import Site
6-
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
76

87
from rest_framework import status
98
from rest_framework.test import APITestCase
9+
from common.djangoapps.student.tests.factories import UserFactory
1010
from common.djangoapps.third_party_auth.models import SAMLConfiguration
1111
from common.djangoapps.third_party_auth.tests.utils import skip_unless_thirdpartyauth
1212
SAML_CONFIGURATIONS = [
@@ -52,7 +52,7 @@ class SAMLConfigurationTests(APITestCase):
5252
@classmethod
5353
def setUpTestData(cls):
5454
super().setUpTestData()
55-
cls.user = User.objects.create_user(username='testuser', password=TEST_PASSWORD)
55+
cls.user = UserFactory.create(username='testuser', password=TEST_PASSWORD)
5656
cls.site, _ = Site.objects.get_or_create(domain='example.com')
5757
for config in SAML_CONFIGURATIONS:
5858
cls.samlconfiguration = SAMLConfiguration.objects.get_or_create(

0 commit comments

Comments
 (0)