diff --git a/cms/djangoapps/contentstore/tests/test_core_caching.py b/cms/djangoapps/contentstore/tests/test_core_caching.py index 6c606d596c..2421b8e2a0 100644 --- a/cms/djangoapps/contentstore/tests/test_core_caching.py +++ b/cms/djangoapps/contentstore/tests/test_core_caching.py @@ -2,6 +2,8 @@ Tests core caching facilities. """ +from __future__ import absolute_import + from django.test import TestCase from opaque_keys.edx.locator import AssetLocator, CourseLocator diff --git a/cms/djangoapps/contentstore/tests/test_course_settings.py b/cms/djangoapps/contentstore/tests/test_course_settings.py index 3a1372fd33..74f6b0fbfc 100644 --- a/cms/djangoapps/contentstore/tests/test_course_settings.py +++ b/cms/djangoapps/contentstore/tests/test_course_settings.py @@ -1,26 +1,28 @@ """ Tests for Studio Course Settings. """ +from __future__ import absolute_import + import copy import datetime import json import unittest import ddt +import mock +import six +from crum import set_current_request from django.conf import settings from django.test import RequestFactory from django.test.utils import override_settings -from pytz import UTC -import mock -from mock import Mock, patch -from crum import set_current_request -from milestones.tests.utils import MilestonesTestCaseMixin - - -from contentstore.utils import reverse_course_url, reverse_usage_url -from contentstore.config.waffle import ENABLE_PROCTORING_PROVIDER_OVERRIDES from milestones.models import MilestoneRelationshipType -from models.settings.course_grading import CourseGradingModel, GRADING_POLICY_CHANGED_EVENT_TYPE, hash_grading_policy +from milestones.tests.utils import MilestonesTestCaseMixin +from mock import Mock, patch +from pytz import UTC + +from contentstore.config.waffle import ENABLE_PROCTORING_PROVIDER_OVERRIDES +from contentstore.utils import reverse_course_url, reverse_usage_url +from models.settings.course_grading import GRADING_POLICY_CHANGED_EVENT_TYPE, CourseGradingModel, hash_grading_policy from models.settings.course_metadata import CourseMetadata from models.settings.encoder import CourseSettingsEncoder from openedx.core.djangoapps.models.course_details import CourseDetails @@ -197,7 +199,7 @@ class CourseDetailsViewTest(CourseTestCase, MilestonesTestCaseMixin): # update pre requisite courses with a new course keys pre_requisite_course = CourseFactory.create(org='edX', course='900', run='test_run') pre_requisite_course2 = CourseFactory.create(org='edX', course='902', run='test_run') - pre_requisite_course_keys = [unicode(pre_requisite_course.id), unicode(pre_requisite_course2.id)] + pre_requisite_course_keys = [six.text_type(pre_requisite_course.id), six.text_type(pre_requisite_course2.id)] course_detail_json['pre_requisite_courses'] = pre_requisite_course_keys self.client.ajax_post(url, course_detail_json) @@ -227,7 +229,7 @@ class CourseDetailsViewTest(CourseTestCase, MilestonesTestCaseMixin): # update pre requisite courses one valid and one invalid key pre_requisite_course = CourseFactory.create(org='edX', course='900', run='test_run') - pre_requisite_course_keys = [unicode(pre_requisite_course.id), 'invalid_key'] + pre_requisite_course_keys = [six.text_type(pre_requisite_course.id), 'invalid_key'] course_detail_json['pre_requisite_courses'] = pre_requisite_course_keys response = self.client.ajax_post(url, course_detail_json) self.assertEqual(400, response.status_code) @@ -513,10 +515,10 @@ class CourseGradingTest(CourseTestCase): mock.call( GRADING_POLICY_CHANGED_EVENT_TYPE, { - 'course_id': unicode(self.course.id), + 'course_id': six.text_type(self.course.id), 'event_transaction_type': 'edx.grades.grading_policy_changed', 'grading_policy_hash': policy_hash, - 'user_id': unicode(self.user.id), + 'user_id': six.text_type(self.user.id), 'event_transaction_id': 'mockUUID', } ) for policy_hash in ( @@ -562,10 +564,10 @@ class CourseGradingTest(CourseTestCase): mock.call( GRADING_POLICY_CHANGED_EVENT_TYPE, { - 'course_id': unicode(self.course.id), + 'course_id': six.text_type(self.course.id), 'event_transaction_type': 'edx.grades.grading_policy_changed', 'grading_policy_hash': policy_hash, - 'user_id': unicode(self.user.id), + 'user_id': six.text_type(self.user.id), 'event_transaction_id': 'mockUUID', } ) for policy_hash in {grading_policy_1, grading_policy_2, grading_policy_3} @@ -600,10 +602,10 @@ class CourseGradingTest(CourseTestCase): mock.call( GRADING_POLICY_CHANGED_EVENT_TYPE, { - 'course_id': unicode(self.course.id), + 'course_id': six.text_type(self.course.id), 'event_transaction_type': 'edx.grades.grading_policy_changed', 'grading_policy_hash': policy_hash, - 'user_id': unicode(self.user.id), + 'user_id': six.text_type(self.user.id), 'event_transaction_id': 'mockUUID', } ) for policy_hash in (grading_policy_1, grading_policy_2, grading_policy_3) @@ -677,10 +679,10 @@ class CourseGradingTest(CourseTestCase): mock.call( GRADING_POLICY_CHANGED_EVENT_TYPE, { - 'course_id': unicode(self.course.id), + 'course_id': six.text_type(self.course.id), 'event_transaction_type': 'edx.grades.grading_policy_changed', 'grading_policy_hash': policy_hash, - 'user_id': unicode(self.user.id), + 'user_id': six.text_type(self.user.id), 'event_transaction_id': 'mockUUID', } ) for policy_hash in (grading_policy_1, grading_policy_2) @@ -1491,7 +1493,7 @@ id=\"course-enrollment-end-time\" value=\"\" placeholder=\"HH:MM\" autocomplete= """ super(CourseEnrollmentEndFieldTest, self).setUp() self.course = CourseFactory.create(org='edX', number='dummy', display_name='Marketing Site Course') - self.course_details_url = reverse_course_url('settings_handler', unicode(self.course.id)) + self.course_details_url = reverse_course_url('settings_handler', six.text_type(self.course.id)) def _get_course_details_response(self, global_staff): """ diff --git a/cms/djangoapps/contentstore/tests/test_crud.py b/cms/djangoapps/contentstore/tests/test_crud.py index 700c3a6c4c..5cbce66b6a 100644 --- a/cms/djangoapps/contentstore/tests/test_crud.py +++ b/cms/djangoapps/contentstore/tests/test_crud.py @@ -1,3 +1,7 @@ +"""Tests for CRUD Operations""" + +from __future__ import absolute_import + from xmodule import templates from xmodule.capa_module import ProblemBlock from xmodule.course_module import CourseDescriptor diff --git a/cms/djangoapps/contentstore/tests/test_import_draft_order.py b/cms/djangoapps/contentstore/tests/test_import_draft_order.py index 68d6488cf9..c81016898f 100644 --- a/cms/djangoapps/contentstore/tests/test_import_draft_order.py +++ b/cms/djangoapps/contentstore/tests/test_import_draft_order.py @@ -1,6 +1,8 @@ """ Tests Draft import order. """ +from __future__ import absolute_import + from django.conf import settings from xmodule.modulestore.django import modulestore diff --git a/cms/djangoapps/contentstore/tests/test_import_pure_xblock.py b/cms/djangoapps/contentstore/tests/test_import_pure_xblock.py index 05d2079fd3..72a1ba661c 100644 --- a/cms/djangoapps/contentstore/tests/test_import_pure_xblock.py +++ b/cms/djangoapps/contentstore/tests/test_import_pure_xblock.py @@ -2,6 +2,8 @@ Integration tests for importing courses containing pure XBlocks. """ +from __future__ import absolute_import + from django.conf import settings from xblock.core import XBlock from xblock.fields import String @@ -31,6 +33,7 @@ class StubXBlock(XBlock): class XBlockImportTest(ModuleStoreTestCase): + """Test class to verify xblock import operations""" @XBlock.register_temp_plugin(StubXBlock) def test_import_public(self): diff --git a/cms/djangoapps/contentstore/tests/test_libraries.py b/cms/djangoapps/contentstore/tests/test_libraries.py index 17ce84d88a..5030deccbf 100644 --- a/cms/djangoapps/contentstore/tests/test_libraries.py +++ b/cms/djangoapps/contentstore/tests/test_libraries.py @@ -1,10 +1,14 @@ """ Content library unit tests that require the CMS runtime. """ +from __future__ import absolute_import + import ddt +import six from django.test.utils import override_settings from mock import Mock, patch from opaque_keys.edx.locator import CourseKey, LibraryLocator +from six.moves import range from contentstore.tests.utils import AjaxEnabledTestClient, parse_json from contentstore.utils import reverse_library_url, reverse_url, reverse_usage_url @@ -81,7 +85,7 @@ class LibraryTestCase(ModuleStoreTestCase): parent_location=course.location, user_id=self.user.id, publish_item=publish_item, - source_library_id=unicode(library_key), + source_library_id=six.text_type(library_key), **(other_settings or {}) ) @@ -511,11 +515,11 @@ class TestLibraryAccess(LibraryTestCase): `library` can be a LibraryLocator or the library's root XBlock """ - if isinstance(library, (basestring, LibraryLocator)): + if isinstance(library, (six.string_types, LibraryLocator)): lib_key = library else: lib_key = library.location.library_key - response = self.client.get(reverse_library_url('library_handler', unicode(lib_key))) + response = self.client.get(reverse_library_url('library_handler', six.text_type(lib_key))) self.assertIn(response.status_code, (200, 302, 403)) return response.status_code == 200 @@ -579,7 +583,7 @@ class TestLibraryAccess(LibraryTestCase): # Now non_staff_user should be able to access library2_key only: lib_list = self._list_libraries() self.assertEqual(len(lib_list), 1) - self.assertEqual(lib_list[0]["library_key"], unicode(library2_key)) + self.assertEqual(lib_list[0]["library_key"], six.text_type(library2_key)) self.assertTrue(self._can_access_library(library2_key)) self.assertFalse(self._can_access_library(self.library)) @@ -606,7 +610,7 @@ class TestLibraryAccess(LibraryTestCase): # Now non_staff_user should be able to access lib_key_pacific only: lib_list = self._list_libraries() self.assertEqual(len(lib_list), 1) - self.assertEqual(lib_list[0]["library_key"], unicode(lib_key_pacific)) + self.assertEqual(lib_list[0]["library_key"], six.text_type(lib_key_pacific)) self.assertTrue(self._can_access_library(lib_key_pacific)) self.assertFalse(self._can_access_library(lib_key_atlantic)) self.assertFalse(self._can_access_library(self.lib_key)) @@ -646,8 +650,8 @@ class TestLibraryAccess(LibraryTestCase): def can_copy_block(): """ Check if studio lets us duplicate the XBlock in the library """ response = self.client.ajax_post(reverse_url('xblock_handler'), { - 'parent_locator': unicode(self.library.location), - 'duplicate_source_locator': unicode(block.location), + 'parent_locator': six.text_type(self.library.location), + 'duplicate_source_locator': six.text_type(block.location), }) self.assertIn(response.status_code, (200, 403)) # 400 would be ambiguous return response.status_code == 200 @@ -655,7 +659,7 @@ class TestLibraryAccess(LibraryTestCase): def can_create_block(): """ Check if studio lets us make a new XBlock in the library """ response = self.client.ajax_post(reverse_url('xblock_handler'), { - 'parent_locator': unicode(self.library.location), 'category': 'html', + 'parent_locator': six.text_type(self.library.location), 'category': 'html', }) self.assertIn(response.status_code, (200, 403)) # 400 would be ambiguous return response.status_code == 200 @@ -708,8 +712,8 @@ class TestLibraryAccess(LibraryTestCase): # Copy block to the course: response = self.client.ajax_post(reverse_url('xblock_handler'), { - 'parent_locator': unicode(course.location), - 'duplicate_source_locator': unicode(block.location), + 'parent_locator': six.text_type(course.location), + 'duplicate_source_locator': six.text_type(block.location), }) self.assertIn(response.status_code, (200, 403)) # 400 would be ambiguous duplicate_action_allowed = (response.status_code == 200) diff --git a/cms/djangoapps/contentstore/tests/test_signals.py b/cms/djangoapps/contentstore/tests/test_signals.py index 7126fb9ea6..c9c1f724a1 100644 --- a/cms/djangoapps/contentstore/tests/test_signals.py +++ b/cms/djangoapps/contentstore/tests/test_signals.py @@ -1,10 +1,12 @@ -import ddt -from mock import patch, Mock +"""Tests for verifying availability of resources for locking""" -from cms.djangoapps.contentstore.signals.handlers import ( - GRADING_POLICY_COUNTDOWN_SECONDS, - handle_grading_policy_changed -) +from __future__ import absolute_import + +import ddt +import six +from mock import Mock, patch + +from cms.djangoapps.contentstore.signals.handlers import GRADING_POLICY_COUNTDOWN_SECONDS, handle_grading_policy_changed from student.models import CourseEnrollment from student.tests.factories import UserFactory from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase @@ -13,6 +15,8 @@ from xmodule.modulestore.tests.factories import CourseFactory @ddt.ddt class LockedTest(ModuleStoreTestCase): + """Test class to verify locking of mocked resources""" + def setUp(self): super(LockedTest, self).setUp() self.course = CourseFactory.create( @@ -24,16 +28,15 @@ class LockedTest(ModuleStoreTestCase): CourseEnrollment.enroll(self.user, self.course.id) @patch('cms.djangoapps.contentstore.signals.handlers.cache.add') - @patch('cms.djangoapps.contentstore.signals.handlers.cache.delete') @patch('cms.djangoapps.contentstore.signals.handlers.task_compute_all_grades_for_course.apply_async') @ddt.data(True, False) - def test_locked(self, lock_available, compute_grades_async_mock, delete_mock, add_mock): + def test_locked(self, lock_available, compute_grades_async_mock, add_mock): add_mock.return_value = lock_available sender = Mock() - handle_grading_policy_changed(sender, course_key=unicode(self.course.id)) + handle_grading_policy_changed(sender, course_key=six.text_type(self.course.id)) - cache_key = 'handle_grading_policy_changed-{}'.format(unicode(self.course.id)) + cache_key = 'handle_grading_policy_changed-{}'.format(six.text_type(self.course.id)) self.assertEqual(lock_available, compute_grades_async_mock.called) if lock_available: add_mock.assert_called_once_with(cache_key, "true", GRADING_POLICY_COUNTDOWN_SECONDS) diff --git a/cms/djangoapps/contentstore/tests/test_utils.py b/cms/djangoapps/contentstore/tests/test_utils.py index ef769339ad..487ab0afec 100644 --- a/cms/djangoapps/contentstore/tests/test_utils.py +++ b/cms/djangoapps/contentstore/tests/test_utils.py @@ -1,7 +1,10 @@ """ Tests for utils. """ +from __future__ import absolute_import + import collections from datetime import datetime, timedelta +import six from django.test import TestCase from opaque_keys.edx.locator import CourseLocator from pytz import UTC @@ -24,19 +27,22 @@ class LMSLinksTestCase(TestCase): course_key = CourseLocator('mitX', '101', 'test') location = course_key.make_usage_key('vertical', 'contacting_us') link = utils.get_lms_link_for_item(location, False) - self.assertEquals(link, "//localhost:8000/courses/course-v1:mitX+101+test/jump_to/block-v1:mitX+101+test+type@vertical+block@contacting_us") + self.assertEquals(link, "//localhost:8000/courses/course-v1:mitX+101+test/jump_to/block-v1:mitX+101+test+type" + "@vertical+block@contacting_us") # test preview link = utils.get_lms_link_for_item(location, True) self.assertEquals( link, - "//preview.localhost/courses/course-v1:mitX+101+test/jump_to/block-v1:mitX+101+test+type@vertical+block@contacting_us" + "//preview.localhost/courses/course-v1:mitX+101+test/jump_to/block-v1:mitX+101+test+type@vertical+block" + "@contacting_us " ) # now test with the course' location location = course_key.make_usage_key('course', 'test') link = utils.get_lms_link_for_item(location) - self.assertEquals(link, "//localhost:8000/courses/course-v1:mitX+101+test/jump_to/block-v1:mitX+101+test+type@course+block@test") + self.assertEquals(link, "//localhost:8000/courses/course-v1:mitX+101+test/jump_to/block-v1:mitX+101+test+type" + "@course+block@test") def lms_link_for_certificate_web_view_test(self): """ Tests get_lms_link_for_certificate_web_view. """ @@ -79,7 +85,7 @@ class ExtraPanelTabTestCase(TestCase): if tabs is None: tabs = [] course = collections.namedtuple('MockCourse', ['tabs']) - if isinstance(tabs, basestring): + if isinstance(tabs, six.string_types): course.tabs = self.get_tab_type_dicts(tabs) else: course.tabs = tabs diff --git a/cms/djangoapps/contentstore/tests/tests.py b/cms/djangoapps/contentstore/tests/tests.py index c33a60ff38..0b12e2451e 100644 --- a/cms/djangoapps/contentstore/tests/tests.py +++ b/cms/djangoapps/contentstore/tests/tests.py @@ -1,31 +1,33 @@ """ This test file will test registration, login, activation, and session activity timeouts """ -from __future__ import print_function +from __future__ import absolute_import, print_function + import datetime import time import mock import pytest +from contentstore.models import PushNotificationConfig +from contentstore.tests.test_course_settings import CourseTestCase +from contentstore.tests.utils import AjaxEnabledTestClient, parse_json, registration, user from ddt import data, ddt, unpack from django.conf import settings from django.contrib.auth.models import User from django.core.cache import cache -from django.urls import reverse from django.test import TestCase from django.test.utils import override_settings +from django.urls import reverse from freezegun import freeze_time from pytz import UTC -from six.moves import xrange - -from contentstore.models import PushNotificationConfig -from contentstore.tests.test_course_settings import CourseTestCase -from contentstore.tests.utils import AjaxEnabledTestClient, parse_json, registration, user +from six.moves import range from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory class ContentStoreTestCase(ModuleStoreTestCase): + """Test class to verify user account operations""" + def _login(self, email, password): """ Login. View should always return 200. The success/fail is in the @@ -62,8 +64,8 @@ class ContentStoreTestCase(ModuleStoreTestCase): """Create the account and check that it worked""" resp = self._create_account(username, email, password) self.assertEqual(resp.status_code, 200) - data = parse_json(resp) - self.assertEqual(data['success'], True) + json_data = parse_json(resp) + self.assertEqual(json_data['success'], True) # Check both that the user is created, and inactive self.assertFalse(user(email).is_active) @@ -173,7 +175,7 @@ class AuthTestCase(ContentStoreTestCase): self.create_account(self.username, self.email, self.pw) # Not activated yet. Login should fail. - resp = self._login(self.email, self.pw) + self._login(self.email, self.pw) self.activate_user(self.email) @@ -183,7 +185,7 @@ class AuthTestCase(ContentStoreTestCase): def test_login_ratelimited(self): # try logging in 30 times, the default limit in the number of failed # login attempts in one 5 minute period before the rate gets limited - for i in xrange(30): + for i in range(30): resp = self._login(self.email, 'wrong_password{0}'.format(i)) self.assertEqual(resp.status_code, 403) resp = self._login(self.email, 'wrong_password') @@ -200,7 +202,7 @@ class AuthTestCase(ContentStoreTestCase): self.create_account(self.username, self.email, self.pw) self.activate_user(self.email) - for i in xrange(3): + for i in range(3): resp = self._login(self.email, 'wrong_password{0}'.format(i)) self.assertEqual(resp.status_code, 403) self.assertIn( @@ -341,6 +343,8 @@ class AuthTestCase(ContentStoreTestCase): class ForumTestCase(CourseTestCase): + """Tests class to verify course to forum operations""" + def setUp(self): """ Creates the test course. """ super(ForumTestCase, self).setUp() @@ -388,6 +392,8 @@ class ForumTestCase(CourseTestCase): @ddt class CourseKeyVerificationTestCase(CourseTestCase): + """Test class to verify course decorator operations""" + def setUp(self): """ Create test course. @@ -417,6 +423,7 @@ class PushNotificationConfigTestCase(TestCase): """ Tests PushNotificationConfig. """ + def test_notifications_defaults(self): self.assertFalse(PushNotificationConfig.is_enabled())