refactor: replace some create_user with UserFactory to avoid non-existent profile errors

This commit is contained in:
Maria Grimaldi
2021-08-23 17:55:03 -04:00
parent b7dfaa9b17
commit 2ee52ea96c
24 changed files with 142 additions and 86 deletions

View File

@@ -13,7 +13,6 @@ import pytest
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
@@ -760,7 +759,9 @@ class CcxDetailTest(CcxRestApiTest):
Check authorization for staff users logged in without oauth
"""
# create a staff user
staff_user = User.objects.create_user('test_staff_user', 'test_staff_user@openedx.org', 'test')
staff_user = UserFactory.create(
username='test_staff_user', email='test_staff_user@openedx.org', password='test',
)
# add staff role to the staff user
CourseStaffRole(self.master_course_key).add_users(staff_user)
@@ -777,7 +778,9 @@ class CcxDetailTest(CcxRestApiTest):
Check authorization for users logged in without oauth
"""
# create an instructor user
instructor_user = User.objects.create_user('test_instructor_user', 'test_instructor_user@openedx.org', 'test')
instructor_user = UserFactory.create(
username='test_instructor_user', email='test_instructor_user@openedx.org', password='test',
)
# add instructor role to the instructor user
CourseInstructorRole(self.master_course_key).add_users(instructor_user)
@@ -794,7 +797,9 @@ class CcxDetailTest(CcxRestApiTest):
Check authorization for other coach users logged in without oauth
"""
# create an coach user
coach_user = User.objects.create_user('test_coach_user', 'test_coach_user@openedx.org', 'test')
coach_user = UserFactory.create(
username='test_coach_user', email='test_coach_user@openedx.org', password='test',
)
# add coach role to the coach user
CourseCcxCoachRole(self.master_course_key).add_users(coach_user)

View File

@@ -11,6 +11,7 @@ from django.urls import reverse
from rest_framework.test import APIClient
from common.djangoapps.student.models import CourseEnrollment
from common.djangoapps.student.tests.factories import UserFactory
from lms.djangoapps.course_goals.models import CourseGoal
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory
@@ -31,7 +32,7 @@ class TestCourseGoalsAPI(SharedModuleStoreTestCase):
super().setUp()
self.course = CourseFactory.create(emit_signals=True)
self.user = User.objects.create_user('john', 'lennon@thebeatles.com', 'password')
self.user = UserFactory.create(username='john', email='lennon@thebeatles.com', password='password')
CourseEnrollment.enroll(self.user, self.course.id)
self.client = APIClient(enforce_csrf_checks=True)

View File

@@ -39,7 +39,9 @@ class TestCourseGoalsAPI(SharedModuleStoreTestCase):
super().setUp()
self.course = CourseFactory.create(emit_signals=True)
self.user = User.objects.create_user('john', 'lennon@thebeatles.com', 'password')
self.user = UserFactory.create(
username='john', email='lennon@thebeatles.com', password='password',
)
CourseEnrollment.enroll(self.user, self.course.id)
self.client = APIClient(enforce_csrf_checks=True)

View File

@@ -241,7 +241,7 @@ class ViewsTestCaseMixin:
self.password = 'test'
# Create the user and make them active so we can log them in.
self.student = User.objects.create_user(uname, email, self.password)
self.student = UserFactory.create(username=uname, email=email, password=self.password)
self.student.is_active = True
self.student.save()
@@ -464,7 +464,7 @@ class ViewsTestCase(
self.password = 'test'
# Create the user and make them active so we can log them in.
self.student = User.objects.create_user(uname, email, self.password)
self.student = UserFactory.create(username=uname, email=email, password=self.password)
self.student.is_active = True
self.student.save()

View File

@@ -10,7 +10,6 @@ from uuid import UUID, uuid4
import ddt
from django.conf import settings
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.core.cache import cache
from django.test import override_settings
from django.urls import reverse
@@ -499,7 +498,7 @@ class ProgramEnrollmentsPostTests(ProgramEnrollmentsWriteMixin, APITestCase):
'curriculum_uuid': str(self.curriculum_uuid)
}
]
user = User.objects.create_user('test_user', 'test@example.com', 'password')
user = UserFactory.create(username='test_user', email='test@example.com', password='password')
url = self.get_url()
with mock.patch(
_get_users_patch_path,

View File

@@ -7,11 +7,11 @@ from collections import OrderedDict
import ddt
import pytest
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.test.client import Client
from common.djangoapps.student.tests.factories import UserFactory
from lms.djangoapps.survey.exceptions import SurveyFormNameAlreadyExists, SurveyFormNotFound
from lms.djangoapps.survey.models import SurveyAnswer, SurveyForm
@@ -31,8 +31,12 @@ class SurveyModelsTests(TestCase):
# Create two accounts
self.password = 'abc'
self.student = User.objects.create_user('student', 'student@test.com', self.password)
self.student2 = User.objects.create_user('student2', 'student2@test.com', self.password)
self.student = UserFactory.create(
username='student', email='student@test.com', password=self.password,
)
self.student2 = UserFactory.create(
username='student2', email='student2@test.com', password=self.password,
)
self.test_survey_name = 'TestForm'
self.test_form = '<li><input name="field1" /></li><li><input name="field2" /></li><li><select name="ddl"><option>1</option></select></li>' # lint-amnesty, pylint: disable=line-too-long

View File

@@ -5,9 +5,9 @@ Python tests for the Survey models
from collections import OrderedDict
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.test.client import Client
from common.djangoapps.student.tests.factories import UserFactory
from lms.djangoapps.survey.models import SurveyForm
from lms.djangoapps.survey.utils import check_survey_required_and_unanswered, is_survey_required_for_course
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
@@ -29,10 +29,16 @@ class SurveyModelsTests(ModuleStoreTestCase):
# Create two accounts
self.password = 'abc'
self.student = User.objects.create_user('student', 'student@test.com', self.password)
self.student2 = User.objects.create_user('student2', 'student2@test.com', self.password)
self.student = UserFactory.create(
username='student', email='student@test.com', password=self.password,
)
self.student2 = UserFactory.create(
username='student2', email='student2@test.com', password=self.password,
)
self.staff = User.objects.create_user('staff', 'staff@test.com', self.password)
self.staff = UserFactory.create(
username='staff', email='staff@test.com', password=self.password,
)
self.staff.is_staff = True
self.staff.save()