Merge pull request #17130 from edx/jeskew/fix_lms_shard_4_tests_django_111

LMS shard 4 tests Django 1.11
This commit is contained in:
Troy Sankey
2018-01-29 14:14:22 -05:00
committed by GitHub
31 changed files with 242 additions and 192 deletions

View File

@@ -1,6 +1,7 @@
from django import forms
from django.conf import settings
from django.contrib import admin
from django.http.request import QueryDict
from django.utils.translation import ugettext_lazy as _
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
@@ -18,6 +19,7 @@ from course_modes.models import CourseMode, CourseModeExpirationConfig
# but the test suite for Studio will fail because
# the verification deadline table won't exist.
from lms.djangoapps.verify_student import models as verification_models
from openedx.core.lib.courses import clean_course_id
from util.date_utils import get_time_display
from xmodule.modulestore.django import modulestore
@@ -51,10 +53,23 @@ class CourseModeForm(forms.ModelForm):
)
def __init__(self, *args, **kwargs):
# If args is a QueryDict, then the ModelForm addition request came in as a POST with a course ID string.
# Change the course ID string to a CourseLocator object by copying the QueryDict to make it mutable.
if len(args) > 0 and 'course' in args[0] and isinstance(args[0], QueryDict):
args_copy = args[0].copy()
args_copy['course'] = CourseKey.from_string(args_copy['course'])
args = [args_copy]
super(CourseModeForm, self).__init__(*args, **kwargs)
if self.data.get('course'):
self.data['course'] = CourseKey.from_string(self.data['course'])
try:
if self.data.get('course'):
self.data['course'] = CourseKey.from_string(self.data['course'])
except AttributeError:
# Change the course ID string to a CourseLocator.
# On a POST request, self.data is a QueryDict and is immutable - so this code will fail.
# However, the args copy above before the super() call handles this case.
pass
default_tz = timezone(settings.TIME_ZONE)
@@ -78,16 +93,10 @@ class CourseModeForm(forms.ModelForm):
)
def clean_course_id(self):
course_id = self.cleaned_data['course']
try:
course_key = CourseKey.from_string(course_id)
except InvalidKeyError:
raise forms.ValidationError("Cannot make a valid CourseKey from id {}!".format(course_id))
if not modulestore().has_course(course_key):
raise forms.ValidationError("Cannot find course with id {} in the modulestore".format(course_id))
return course_key
"""
Validate the course id
"""
return clean_course_id(self)
def clean__expiration_datetime(self):
"""

View File

@@ -202,6 +202,9 @@ class CourseMode(models.Model):
# Ensure currency is always lowercase.
self.clean() # ensure object-level validation is performed before we save.
self.currency = self.currency.lower()
if self.id is None:
# If this model has no primary key at save time, it needs to be force-inserted.
force_insert = True
super(CourseMode, self).save(force_insert, force_update, using)
@property

View File

@@ -9,6 +9,7 @@ from django.utils.translation import ugettext_lazy as _
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from openedx.core.lib.courses import clean_course_id
from student.models import (
CourseAccessRole,
CourseEnrollment,
@@ -41,23 +42,10 @@ class CourseAccessRoleForm(forms.ModelForm):
def clean_course_id(self):
"""
Checking course-id format and course exists in module store.
This field can be null.
Validate the course id
"""
if self.cleaned_data['course_id']:
course_id = self.cleaned_data['course_id']
try:
course_key = CourseKey.from_string(course_id)
except InvalidKeyError:
raise forms.ValidationError(u"Invalid CourseID. Please check the format and re-try.")
if not modulestore().has_course(course_key):
raise forms.ValidationError(u"Cannot find course with id {} in the modulestore".format(course_id))
return course_key
return None
return clean_course_id(self)
def clean_org(self):
"""If org and course-id exists then Check organization name

View File

@@ -124,7 +124,7 @@ class AdminCourseRolesPageTest(SharedModuleStoreTestCase):
response = self.client.post(reverse('admin:student_courseaccessrole_add'), data=data)
self.assertContains(
response,
'Cannot find course with id {} in the modulestore'.format(
'Course not found. Entered course id was: "{}".'.format(
course
)
)

View File

@@ -16,6 +16,7 @@ from mock import Mock, patch
from edxmako.shortcuts import render_to_string
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
from openedx.core.djangoapps.theming.tests.test_util import with_comprehensive_theme
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, CacheIsolationMixin
from student.models import PendingEmailChange, Registration, UserProfile
from student.tests.factories import PendingEmailChangeFactory, RegistrationFactory, UserFactory
from student.views import (
@@ -84,7 +85,7 @@ class EmailTestMixin(object):
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
class ActivationEmailTests(TestCase):
class ActivationEmailTests(CacheIsolationTestCase):
"""
Test sending of the activation email.
"""
@@ -164,7 +165,7 @@ class ActivationEmailTests(TestCase):
@patch('student.views.login.render_to_string', Mock(side_effect=mock_render_to_string, autospec=True))
@patch('django.contrib.auth.models.User.email_user')
class ReactivationEmailTests(EmailTestMixin, TestCase):
class ReactivationEmailTests(EmailTestMixin, CacheIsolationTestCase):
"""
Test sending a reactivation email to a user
"""
@@ -241,7 +242,7 @@ class ReactivationEmailTests(EmailTestMixin, TestCase):
self.assertTrue(response_data['success'])
class EmailChangeRequestTests(EventTestMixin, TestCase):
class EmailChangeRequestTests(EventTestMixin, CacheIsolationTestCase):
"""
Test changing a user's email address
"""
@@ -365,12 +366,14 @@ class EmailChangeRequestTests(EventTestMixin, TestCase):
@patch('django.contrib.auth.models.User.email_user')
@patch('student.views.management.render_to_response', Mock(side_effect=mock_render_to_response, autospec=True))
@patch('student.views.management.render_to_string', Mock(side_effect=mock_render_to_string, autospec=True))
class EmailChangeConfirmationTests(EmailTestMixin, TransactionTestCase):
class EmailChangeConfirmationTests(EmailTestMixin, CacheIsolationMixin, TransactionTestCase):
"""
Test that confirmation of email change requests function even in the face of exceptions thrown while sending email
"""
def setUp(self):
super(EmailChangeConfirmationTests, self).setUp()
self.clear_caches()
self.addCleanup(self.clear_caches)
self.user = UserFactory.create()
self.profile = UserProfile.objects.get(user=self.user)
self.req_factory = RequestFactory()
@@ -380,6 +383,16 @@ class EmailChangeConfirmationTests(EmailTestMixin, TransactionTestCase):
self.pending_change_request = PendingEmailChangeFactory.create(user=self.user)
self.key = self.pending_change_request.activation_key
@classmethod
def setUpClass(cls):
super(EmailChangeConfirmationTests, cls).setUpClass()
cls.start_cache_isolation()
@classmethod
def tearDownClass(cls):
cls.end_cache_isolation()
super(EmailChangeConfirmationTests, cls).tearDownClass()
def assertRolledBack(self):
"""
Assert that no changes to user, profile, or pending email have been made to the db