Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cms/djangoapps/cms_user_tasks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import ddt
from boto.exception import NoAuthHandlerFound
from django.conf import settings
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.core import mail
from django.test import override_settings
from django.urls import reverse
Expand All @@ -20,6 +19,7 @@
from user_tasks.serializers import ArtifactSerializer, StatusSerializer

from cms.djangoapps.contentstore.toggles import BYPASS_OLX_FAILURE
from common.djangoapps.student.tests.factories import UserFactory

from .signals import user_task_stopped

Expand Down Expand Up @@ -78,7 +78,7 @@ class TestUserTasks(APITestCase):

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

@classmethod
def setUpTestData(cls): # lint-amnesty, pylint: disable=super-method-not-called
cls.user = User.objects.create_user('test_user', '[email protected]', 'password')
cls.user = UserFactory.create(username='test_user', email='[email protected]', password='password')
cls.status = UserTaskStatus.objects.create(
user=cls.user, task_id=str(uuid4()), task_class='test_rest_api.sample_task', name='SampleTask 2',
total_steps=5)
Expand Down
4 changes: 2 additions & 2 deletions cms/djangoapps/contentstore/tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import gettext
from unittest import mock, skip

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

from cms.djangoapps.contentstore.tests.utils import AjaxEnabledTestClient
from cms.djangoapps.contentstore.views.preview import _preview_module_system
from common.djangoapps.student.tests.factories import UserFactory
from openedx.core.lib.edx_six import get_gettext
from xmodule.modulestore.django import ModuleI18nService
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
Expand Down Expand Up @@ -192,7 +192,7 @@ def setUp(self):
self.password = 'foo'

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

# Note that we do not actually need to do anything
# for registration if we directly mark them active.
Expand Down
5 changes: 2 additions & 3 deletions cms/djangoapps/contentstore/tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

import copy

from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user

from cms.djangoapps.contentstore.tests.utils import AjaxEnabledTestClient
from cms.djangoapps.contentstore.utils import reverse_course_url, reverse_url
from common.djangoapps.student import auth
from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole, OrgInstructorRole, OrgStaffRole
from common.djangoapps.student.tests.factories import UserFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase


Expand Down Expand Up @@ -52,7 +51,7 @@ def _create_users(self):
for i in range(8):
username = f"user{i}"
email = f"test+user{i}@edx.org"
user = User.objects.create_user(username, email, 'foo')
user = UserFactory.create(username=username, email=email, password='foo')
user.is_active = True
user.save()
users.append(user)
Expand Down
15 changes: 11 additions & 4 deletions cms/djangoapps/contentstore/views/tests/test_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
"""


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

from common.djangoapps.student.auth import add_users
from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole
from common.djangoapps.student.tests.factories import AdminFactory
from common.djangoapps.student.tests.factories import AdminFactory, UserFactory

from ..access import get_user_role

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

self.global_admin = AdminFactory()
self.instructor = User.objects.create_user('testinstructor', '[email protected]', 'foo')
self.staff = User.objects.create_user('teststaff', '[email protected]', 'foo')
self.instructor = UserFactory.create(
username='testinstructor',
email='[email protected]',
password='foo',
)
self.staff = UserFactory.create(
username='teststaff',
email='[email protected]',
password='foo',
)
self.course_key = CourseLocator('mitX', '101', 'test')

def test_get_user_role_instructor(self):
Expand Down
11 changes: 7 additions & 4 deletions cms/djangoapps/contentstore/views/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@
from common.djangoapps.student import auth
from common.djangoapps.student.models import CourseEnrollment
from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole
from common.djangoapps.student.tests.factories import UserFactory


class UsersTestCase(CourseTestCase): # lint-amnesty, pylint: disable=missing-class-docstring
def setUp(self):
super().setUp()
self.ext_user = User.objects.create_user(
"joe", "[email protected]", "haha")
self.ext_user = UserFactory.create(
username="joe", email="[email protected]", password="haha",
)
self.ext_user.is_active = True
self.ext_user.is_staff = False
self.ext_user.save()
self.inactive_user = User.objects.create_user(
"carl", "[email protected]", "haha")
self.inactive_user = UserFactory.create(
username="carl", email="[email protected]", password="haha",
)
self.inactive_user.is_active = False
self.inactive_user.is_staff = False
self.inactive_user.save()
Expand Down
14 changes: 11 additions & 3 deletions cms/djangoapps/course_creators/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from unittest import mock

from django.contrib.admin.sites import AdminSite
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.core import mail
from django.http import HttpRequest
from django.test import TestCase
Expand All @@ -15,6 +14,7 @@
from cms.djangoapps.course_creators.models import CourseCreator
from common.djangoapps.student import auth
from common.djangoapps.student.roles import CourseCreatorRole
from common.djangoapps.student.tests.factories import UserFactory


def mock_render_to_string(template_name, context):
Expand All @@ -30,11 +30,19 @@ class CourseCreatorAdminTest(TestCase):
def setUp(self):
""" Test case setup """
super().setUp()
self.user = User.objects.create_user('test_user', '[email protected]', 'foo')
self.user = UserFactory.create(
username='test_user',
email='[email protected]',
password='foo',
)
self.table_entry = CourseCreator(user=self.user)
self.table_entry.save()

self.admin = User.objects.create_user('Mark', '[email protected]', 'foo')
self.admin = UserFactory.create(
username='Mark',
email='[email protected]',
password='foo',
)
self.admin.is_staff = True

self.request = HttpRequest()
Expand Down
14 changes: 11 additions & 3 deletions cms/djangoapps/course_creators/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from unittest import mock

from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.core.exceptions import PermissionDenied
from django.test import TestCase
from django.urls import reverse
Expand All @@ -19,6 +18,7 @@
)
from common.djangoapps.student import auth
from common.djangoapps.student.roles import CourseCreatorRole
from common.djangoapps.student.tests.factories import UserFactory


class CourseCreatorView(TestCase):
Expand All @@ -29,8 +29,16 @@ class CourseCreatorView(TestCase):
def setUp(self):
""" Test case setup """
super().setUp()
self.user = User.objects.create_user('test_user', '[email protected]', 'foo')
self.admin = User.objects.create_user('Mark', '[email protected]', 'foo')
self.user = UserFactory.create(
username='test_user',
email='[email protected]',
password='foo',
)
self.admin = UserFactory.create(
username='Mark',
email='[email protected]',
password='foo',
)
self.admin.is_staff = True

def test_staff_permission_required(self):
Expand Down
28 changes: 19 additions & 9 deletions common/djangoapps/student/tests/test_authz.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import pytest

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

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
from common.djangoapps.student.roles import CourseCreatorRole, CourseInstructorRole, CourseStaffRole
from common.djangoapps.student.tests.factories import AdminFactory
from common.djangoapps.student.tests.factories import AdminFactory, UserFactory


class CreatorGroupTest(TestCase):
Expand All @@ -24,8 +24,12 @@ class CreatorGroupTest(TestCase):
def setUp(self):
""" Test case setup """
super().setUp()
self.user = User.objects.create_user('testuser', '[email protected]', 'foo')
self.admin = User.objects.create_user('Mark', '[email protected]', 'foo')
self.user = UserFactory.create(
username='testuser', email='[email protected]', password='foo',
)
self.admin = UserFactory.create(
username='Mark', email='[email protected]', password='foo',
)
self.admin.is_staff = True

def test_creator_group_not_enabled(self):
Expand All @@ -51,7 +55,7 @@ def test_creator_group_enabled_nonempty(self):
assert user_has_role(self.user, CourseCreatorRole())

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

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

Expand Down Expand Up @@ -191,8 +195,12 @@ def setUp(self):
""" Test case setup """
super().setUp()
self.global_admin = AdminFactory()
self.creator = User.objects.create_user('testcreator', '[email protected]', 'foo')
self.staff = User.objects.create_user('teststaff', '[email protected]', 'foo')
self.creator = UserFactory.create(
username='testcreator', email='[email protected]', password='foo',
)
self.staff = UserFactory.create(
username='teststaff', email='[email protected]', password='foo',
)
self.course_key = CourseLocator('mitX', '101', 'test')

def test_add_user_to_course_group(self):
Expand Down Expand Up @@ -240,7 +248,9 @@ def test_remove_user_from_course_group_permission_denied(self):
Verifies PermissionDenied if caller of remove_user_from_course_group is not instructor role.
"""
add_users(self.global_admin, CourseInstructorRole(self.course_key), self.creator)
another_staff = User.objects.create_user('another', '[email protected]', 'foo')
another_staff = UserFactory.create(
username='another', email='[email protected]', password='foo',
)
add_users(self.global_admin, CourseStaffRole(self.course_key), self.creator, self.staff, another_staff)
with pytest.raises(PermissionDenied):
remove_users(self.staff, CourseStaffRole(self.course_key), another_staff)
2 changes: 1 addition & 1 deletion common/djangoapps/student/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ class EnrollInCourseTest(EnrollmentEventTestMixin, CacheIsolationTestCase):

@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
def test_enrollment(self):
user = User.objects.create_user("joe", "[email protected]", "password")
user = UserFactory.create(username="joe", email="[email protected]", password="password")
course_id = CourseKey.from_string("edX/Test101/2013")
course_id_partial = CourseKey.from_string("edX/Test101/")
course = CourseOverviewFactory.create(id=course_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"""
from django.urls import reverse
from django.contrib.sites.models import Site
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user

from rest_framework import status
from rest_framework.test import APITestCase
from common.djangoapps.student.tests.factories import UserFactory
from common.djangoapps.third_party_auth.models import SAMLConfiguration
from common.djangoapps.third_party_auth.tests.utils import skip_unless_thirdpartyauth
SAML_CONFIGURATIONS = [
Expand Down Expand Up @@ -52,7 +52,7 @@ class SAMLConfigurationTests(APITestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()
cls.user = User.objects.create_user(username='testuser', password=TEST_PASSWORD)
cls.user = UserFactory.create(username='testuser', password=TEST_PASSWORD)
cls.site, _ = Site.objects.get_or_create(domain='example.com')
for config in SAML_CONFIGURATIONS:
cls.samlconfiguration = SAMLConfiguration.objects.get_or_create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from enterprise.models import EnterpriseCustomerIdentityProvider, EnterpriseCustomer
from enterprise.constants import ENTERPRISE_ADMIN_ROLE, ENTERPRISE_LEARNER_ROLE
from common.djangoapps.student.tests.factories import UserFactory
from common.djangoapps.third_party_auth.tests.samlutils import set_jwt_cookie
from common.djangoapps.third_party_auth.models import SAMLProviderConfig, SAMLConfiguration
from common.djangoapps.third_party_auth.tests.utils import skip_unless_thirdpartyauth
Expand Down Expand Up @@ -49,7 +50,7 @@ class SAMLProviderConfigTests(APITestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()
cls.user = User.objects.create_user(username='testuser', password='testpwd')
cls.user = UserFactory.create(username='testuser', password='testpwd')
cls.site, _ = Site.objects.get_or_create(domain='example.com')
cls.enterprise_customer = EnterpriseCustomer.objects.create(
uuid=ENTERPRISE_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from enterprise.models import EnterpriseCustomer, EnterpriseCustomerIdentityProvider
from enterprise.constants import ENTERPRISE_ADMIN_ROLE, ENTERPRISE_LEARNER_ROLE
from common.djangoapps.student.tests.factories import UserFactory
from common.djangoapps.third_party_auth.models import SAMLProviderData, SAMLProviderConfig
from common.djangoapps.third_party_auth.tests.samlutils import set_jwt_cookie
from common.djangoapps.third_party_auth.tests.utils import skip_unless_thirdpartyauth
Expand Down Expand Up @@ -49,7 +50,7 @@ class SAMLProviderDataTests(APITestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()
cls.user = User.objects.create_user(username='testuser', password='testpwd')
cls.user = UserFactory.create(username='testuser', password='testpwd')
cls.site, _ = Site.objects.get_or_create(domain='example.com')
cls.enterprise_customer = EnterpriseCustomer.objects.create(
uuid=ENTERPRISE_ID,
Expand Down
13 changes: 9 additions & 4 deletions lms/djangoapps/ccx/api/v0/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import ddt
from ccx_keys.locator import CCXLocator
from django.conf import settings
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.urls import Resolver404, resolve, reverse
from django.utils.timezone import now
from oauth2_provider import models as dot_models
Expand Down Expand Up @@ -760,7 +759,9 @@ def test_authorization_no_oauth_staff(self):
Check authorization for staff users logged in without oauth
"""
# create a staff user
staff_user = User.objects.create_user('test_staff_user', '[email protected]', 'test')
staff_user = UserFactory.create(
username='test_staff_user', email='[email protected]', password='test',
)
# add staff role to the staff user
CourseStaffRole(self.master_course_key).add_users(staff_user)

Expand All @@ -777,7 +778,9 @@ def test_authorization_no_oauth_instructor(self):
Check authorization for users logged in without oauth
"""
# create an instructor user
instructor_user = User.objects.create_user('test_instructor_user', '[email protected]', 'test')
instructor_user = UserFactory.create(
username='test_instructor_user', email='[email protected]', password='test',
)
# add instructor role to the instructor user
CourseInstructorRole(self.master_course_key).add_users(instructor_user)

Expand All @@ -794,7 +797,9 @@ def test_authorization_no_oauth_other_coach(self):
Check authorization for other coach users logged in without oauth
"""
# create an coach user
coach_user = User.objects.create_user('test_coach_user', '[email protected]', 'test')
coach_user = UserFactory.create(
username='test_coach_user', email='[email protected]', password='test',
)
# add coach role to the coach user
CourseCcxCoachRole(self.master_course_key).add_users(coach_user)

Expand Down
Loading