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
7 changes: 4 additions & 3 deletions cms/djangoapps/cms_user_tasks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

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
from rest_framework.test import APITestCase
from user_tasks.models import UserTaskArtifact, UserTaskStatus
from user_tasks.serializers import ArtifactSerializer, StatusSerializer

from common.djangoapps.student.tests.factories import UserFactory

from .signals import user_task_stopped


Expand Down Expand Up @@ -72,7 +73,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 @@ -145,7 +146,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
5 changes: 2 additions & 3 deletions cms/djangoapps/contentstore/views/tests/test_entrance_exam.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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
Expand Down Expand Up @@ -173,7 +172,7 @@ def test_contentstore_views_entrance_exam_delete(self):
resp = self.client.get(self.exam_url)
self.assertEqual(resp.status_code, 404)

user = User.objects.create(
user = UserFactory.create(
username='test_user',
email='[email protected]',
is_active=True,
Expand Down Expand Up @@ -287,7 +286,7 @@ def test_contentstore_views_entrance_exam_get_invalid_user(self):
"""
Unit Test: test_contentstore_views_entrance_exam_get_invalid_user
"""
user = User.objects.create(
user = UserFactory.create(
username='test_user',
email='[email protected]',
is_active=True,
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from enterprise.models import EnterpriseCustomer, EnterpriseCustomerUser
from pytest import mark

from common.djangoapps.student.tests.factories import UserFactory


@mark.django_db
class ChangeEnterpriseUserUsernameCommandTests(TestCase):
Expand All @@ -25,7 +27,7 @@ def test_user_not_enterprise(self, logger_mock):
"""
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='[email protected]')
user = UserFactory.create(is_active=True, username='old_username', email='[email protected]')
new_username = 'new_username'

post_save_handler = mock.MagicMock()
Expand All @@ -41,7 +43,7 @@ def test_username_updated_successfully(self, logger_mock):
"""
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='[email protected]')
user = UserFactory.create(is_active=True, username='old_username', email='[email protected]')
site, _ = Site.objects.get_or_create(domain='example.com')
enterprise_customer = EnterpriseCustomer.objects.create(
name='Test EnterpriseCustomer',
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)
12 changes: 6 additions & 6 deletions common/djangoapps/student/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,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/")

Expand Down Expand Up @@ -658,7 +658,7 @@ def test_enrollment(self):

def test_enrollment_non_existent_user(self):
# Testing enrollment of newly unsaved user (i.e. no database entry)
user = User(username="rusty", email="[email protected]")
user = UserFactory(username="rusty", email="[email protected]")
course_id = CourseLocator("edX", "Test101", "2013")

assert not CourseEnrollment.is_enrolled(user, course_id)
Expand All @@ -675,7 +675,7 @@ def test_enrollment_non_existent_user(self):

@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="[email protected]")
user = UserFactory.create(username="jack", email="[email protected]")
course_id = CourseLocator("edX", "Test101", "2013")

CourseEnrollment.enroll_by_email("[email protected]", course_id)
Expand Down Expand Up @@ -711,7 +711,7 @@ def test_enrollment_by_email(self):

@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
def test_enrollment_multiple_classes(self):
user = User(username="rusty", email="[email protected]")
user = UserFactory(username="rusty", email="[email protected]")
course_id1 = CourseLocator("edX", "Test101", "2013")
course_id2 = CourseLocator("MITx", "6.003z", "2012")

Expand All @@ -734,7 +734,7 @@ def test_enrollment_multiple_classes(self):

@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
def test_activation(self):
user = User.objects.create(username="jack", email="[email protected]")
user = UserFactory.create(username="jack", email="[email protected]")
course_id = CourseLocator("edX", "Test101", "2013")
assert not CourseEnrollment.is_enrolled(user, course_id)

Expand Down Expand Up @@ -771,7 +771,7 @@ def test_activation(self):
self.assert_enrollment_event_was_emitted(user, course_id)

def test_change_enrollment_modes(self):
user = User.objects.create(username="justin", email="[email protected]")
user = UserFactory.create(username="justin", email="[email protected]")
course_id = CourseLocator("edX", "Test101", "2013")

CourseEnrollment.enroll(user, course_id, "audit")
Expand Down
Loading