Skip to content

Commit 4abfc60

Browse files
refactor: replace some create_user with UserFactory to avoid non-existent profile errors
fix failing tests due to current date fix tests
1 parent 9503f74 commit 4abfc60

File tree

30 files changed

+154
-96
lines changed

30 files changed

+154
-96
lines changed

cms/djangoapps/cms_user_tasks/tests.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88

99
from boto.exception import NoAuthHandlerFound
1010
from django.conf import settings
11-
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
1211
from django.core import mail
1312
from django.test import override_settings
1413
from django.urls import reverse
1514
from rest_framework.test import APITestCase
1615
from user_tasks.models import UserTaskArtifact, UserTaskStatus
1716
from user_tasks.serializers import ArtifactSerializer, StatusSerializer
1817

18+
from common.djangoapps.student.tests.factories import UserFactory
19+
1920
from .signals import user_task_stopped
2021

2122

@@ -72,7 +73,7 @@ class TestUserTasks(APITestCase):
7273

7374
@classmethod
7475
def setUpTestData(cls): # lint-amnesty, pylint: disable=super-method-not-called
75-
cls.user = User.objects.create_user('test_user', '[email protected]', 'password')
76+
cls.user = UserFactory.create(username='test_user', email='[email protected]', password='password')
7677
cls.status = UserTaskStatus.objects.create(
7778
user=cls.user, task_id=str(uuid4()), task_class='test_rest_api.sample_task', name='SampleTask 2',
7879
total_steps=5)
@@ -145,7 +146,7 @@ class TestUserTaskStopped(APITestCase):
145146

146147
@classmethod
147148
def setUpTestData(cls): # lint-amnesty, pylint: disable=super-method-not-called
148-
cls.user = User.objects.create_user('test_user', '[email protected]', 'password')
149+
cls.user = UserFactory.create(username='test_user', email='[email protected]', password='password')
149150
cls.status = UserTaskStatus.objects.create(
150151
user=cls.user, task_id=str(uuid4()), task_class='test_rest_api.sample_task', name='SampleTask 2',
151152
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_entrance_exam.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from unittest.mock import patch
88

99
from django.conf import settings
10-
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
1110
from django.test.client import RequestFactory
1211
from milestones.tests.utils import MilestonesTestCaseMixin
1312
from opaque_keys.edx.keys import UsageKey
@@ -173,7 +172,7 @@ def test_contentstore_views_entrance_exam_delete(self):
173172
resp = self.client.get(self.exam_url)
174173
self.assertEqual(resp.status_code, 404)
175174

176-
user = User.objects.create(
175+
user = UserFactory.create(
177176
username='test_user',
178177
179178
is_active=True,
@@ -287,7 +286,7 @@ def test_contentstore_views_entrance_exam_get_invalid_user(self):
287286
"""
288287
Unit Test: test_contentstore_views_entrance_exam_get_invalid_user
289288
"""
290-
user = User.objects.create(
289+
user = UserFactory.create(
291290
username='test_user',
292291
293292
is_active=True,

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/management/tests/test_change_enterprise_user_username.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from enterprise.models import EnterpriseCustomer, EnterpriseCustomerUser
1313
from pytest import mark
1414

15+
from common.djangoapps.student.tests.factories import UserFactory
16+
1517

1618
@mark.django_db
1719
class ChangeEnterpriseUserUsernameCommandTests(TestCase):
@@ -25,7 +27,7 @@ def test_user_not_enterprise(self, logger_mock):
2527
"""
2628
Test that the command does not update a user's username if it is not linked to an Enterprise.
2729
"""
28-
user = User.objects.create(is_active=True, username='old_username', email='[email protected]')
30+
user = UserFactory.create(is_active=True, username='old_username', email='[email protected]')
2931
new_username = 'new_username'
3032

3133
post_save_handler = mock.MagicMock()
@@ -41,7 +43,7 @@ def test_username_updated_successfully(self, logger_mock):
4143
"""
4244
Test that the command updates the user's username when the user is linked to an Enterprise.
4345
"""
44-
user = User.objects.create(is_active=True, username='old_username', email='[email protected]')
46+
user = UserFactory.create(is_active=True, username='old_username', email='[email protected]')
4547
site, _ = Site.objects.get_or_create(domain='example.com')
4648
enterprise_customer = EnterpriseCustomer.objects.create(
4749
name='Test EnterpriseCustomer',

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)

0 commit comments

Comments
 (0)