From 43ac38aa874841f75b969733d6bfa68e14cd490b Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 29 Sep 2015 07:05:51 -0400 Subject: [PATCH] Add autospec to all mocks --- .../contentstore/tests/test_libraries.py | 2 +- .../track/views/tests/test_views.py | 2 +- .../xmodule/modulestore/tests/django_utils.py | 2 +- .../xmodule/tests/test_library_content.py | 4 +- .../bulk_email/tests/test_course_optout.py | 2 +- lms/djangoapps/bulk_email/tests/test_email.py | 4 +- .../bulk_email/tests/test_err_handling.py | 2 +- .../bulk_email/tests/test_models.py | 2 +- lms/djangoapps/bulk_email/tests/test_tasks.py | 2 +- .../courseware/tests/test_module_render.py | 6 +-- .../django_comment_client/base/tests.py | 38 +++++++------- .../django_comment_client/forum/tests.py | 44 ++++++++-------- lms/djangoapps/edxnotes/tests.py | 50 +++++++++---------- lms/djangoapps/instructor/tests/test_api.py | 6 +-- .../instructor_task/tests/test_api.py | 2 +- .../lti_provider/tests/test_users.py | 4 +- .../verify_student/tests/test_views.py | 8 ++- .../course_groups/tests/test_cohorts.py | 2 +- 18 files changed, 93 insertions(+), 89 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/test_libraries.py b/cms/djangoapps/contentstore/tests/test_libraries.py index 969aec1323..377180ed37 100644 --- a/cms/djangoapps/contentstore/tests/test_libraries.py +++ b/cms/djangoapps/contentstore/tests/test_libraries.py @@ -387,7 +387,7 @@ class TestLibraries(LibraryTestCase): html_block = modulestore().get_item(lc_block.children[0]) self.assertEqual(html_block.data, data2) - @patch("xmodule.library_tools.SearchEngine.get_search_engine", Mock(return_value=None)) + @patch("xmodule.library_tools.SearchEngine.get_search_engine", Mock(return_value=None, autospec=True)) def test_refreshes_children_if_capa_type_change(self): """ Tests that children are automatically refreshed if capa type field changes """ name1, name2 = "Option Problem", "Multiple Choice Problem" diff --git a/common/djangoapps/track/views/tests/test_views.py b/common/djangoapps/track/views/tests/test_views.py index 7e23136b3b..f92ce0b03a 100644 --- a/common/djangoapps/track/views/tests/test_views.py +++ b/common/djangoapps/track/views/tests/test_views.py @@ -19,7 +19,7 @@ class TestTrackViews(EventTrackingTestCase): self.request_factory = RequestFactory() - patcher = patch('track.views.tracker') + patcher = patch('track.views.tracker', autospec=True) self.mock_tracker = patcher.start() self.addCleanup(patcher.stop) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py index c0feac00fa..075081c917 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py @@ -158,7 +158,7 @@ def xml_store_config(data_dir, source_dirs=None): return store -@patch('xmodule.modulestore.django.create_modulestore_instance') +@patch('xmodule.modulestore.django.create_modulestore_instance', autospec=True) def drop_mongo_collections(mock_create): """ If using a Mongo-backed modulestore & contentstore, drop the collections. diff --git a/common/lib/xmodule/xmodule/tests/test_library_content.py b/common/lib/xmodule/xmodule/tests/test_library_content.py index ec7c8230ad..ab967761e3 100644 --- a/common/lib/xmodule/xmodule/tests/test_library_content.py +++ b/common/lib/xmodule/xmodule/tests/test_library_content.py @@ -242,7 +242,7 @@ class LibraryContentModuleTestMixin(object): self.assertNotIn(LibraryContentDescriptor.display_name, non_editable_metadata_fields) -@patch('xmodule.library_tools.SearchEngine.get_search_engine', Mock(return_value=None)) +@patch('xmodule.library_tools.SearchEngine.get_search_engine', Mock(return_value=None, autospec=True)) class TestLibraryContentModuleNoSearchIndex(LibraryContentModuleTestMixin, LibraryContentTest): """ Tests for library container when no search index is available. @@ -254,7 +254,7 @@ class TestLibraryContentModuleNoSearchIndex(LibraryContentModuleTestMixin, Libra search_index_mock = Mock(spec=SearchEngine) # pylint: disable=invalid-name -@patch('xmodule.library_tools.SearchEngine.get_search_engine', Mock(return_value=search_index_mock)) +@patch('xmodule.library_tools.SearchEngine.get_search_engine', Mock(return_value=search_index_mock, autospec=True)) class TestLibraryContentModuleWithSearchIndex(LibraryContentModuleTestMixin, LibraryContentTest): """ Tests for library container with mocked search engine response. diff --git a/lms/djangoapps/bulk_email/tests/test_course_optout.py b/lms/djangoapps/bulk_email/tests/test_course_optout.py index e72edd222d..68567e3b0e 100644 --- a/lms/djangoapps/bulk_email/tests/test_course_optout.py +++ b/lms/djangoapps/bulk_email/tests/test_course_optout.py @@ -18,7 +18,7 @@ from xmodule.modulestore.tests.factories import CourseFactory @attr('shard_1') -@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message')) +@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True)) class TestOptoutCourseEmails(ModuleStoreTestCase): """ Test that optouts are referenced in sending course email. diff --git a/lms/djangoapps/bulk_email/tests/test_email.py b/lms/djangoapps/bulk_email/tests/test_email.py index e5f0acc01b..fe95b073b3 100644 --- a/lms/djangoapps/bulk_email/tests/test_email.py +++ b/lms/djangoapps/bulk_email/tests/test_email.py @@ -88,7 +88,7 @@ class EmailSendFromDashboardTestCase(ModuleStoreTestCase): @attr('shard_1') @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False}) -@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message')) +@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True)) class TestEmailSendFromDashboardMockedHtmlToText(EmailSendFromDashboardTestCase): """ Tests email sending with mocked html_to_text. @@ -108,7 +108,7 @@ class TestEmailSendFromDashboardMockedHtmlToText(EmailSendFromDashboardTestCase) # We should get back a HttpResponseForbidden (status code 403) self.assertContains(response, "Email is not enabled for this course.", status_code=403) - @patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message')) + @patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True)) def test_send_to_self(self): """ Make sure email send to myself goes to myself. diff --git a/lms/djangoapps/bulk_email/tests/test_err_handling.py b/lms/djangoapps/bulk_email/tests/test_err_handling.py index b2615199fd..04547d7e9c 100644 --- a/lms/djangoapps/bulk_email/tests/test_err_handling.py +++ b/lms/djangoapps/bulk_email/tests/test_err_handling.py @@ -37,7 +37,7 @@ class EmailTestException(Exception): @attr('shard_1') -@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message')) +@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True)) @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False}) class TestEmailErrors(ModuleStoreTestCase): """ diff --git a/lms/djangoapps/bulk_email/tests/test_models.py b/lms/djangoapps/bulk_email/tests/test_models.py index 29433c9877..ef274558e2 100644 --- a/lms/djangoapps/bulk_email/tests/test_models.py +++ b/lms/djangoapps/bulk_email/tests/test_models.py @@ -15,7 +15,7 @@ from opaque_keys.edx.locations import SlashSeparatedCourseKey @attr('shard_1') -@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message')) +@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True)) class CourseEmailTest(TestCase): """Test the CourseEmail model.""" diff --git a/lms/djangoapps/bulk_email/tests/test_tasks.py b/lms/djangoapps/bulk_email/tests/test_tasks.py index ca0ed34372..583f649eea 100644 --- a/lms/djangoapps/bulk_email/tests/test_tasks.py +++ b/lms/djangoapps/bulk_email/tests/test_tasks.py @@ -76,7 +76,7 @@ def my_update_subtask_status(entry_id, current_task_id, new_subtask_status): @attr('shard_1') -@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message')) +@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True)) class TestBulkEmailInstructorTask(InstructorTaskCourseTestCase): """Tests instructor task that send bulk email.""" diff --git a/lms/djangoapps/courseware/tests/test_module_render.py b/lms/djangoapps/courseware/tests/test_module_render.py index e5f7fa8ce7..04693b1171 100644 --- a/lms/djangoapps/courseware/tests/test_module_render.py +++ b/lms/djangoapps/courseware/tests/test_module_render.py @@ -1351,7 +1351,7 @@ class XmlViewInStudioTest(ViewInStudioTest): @attr('shard_1') @patch.dict('django.conf.settings.FEATURES', {'DISPLAY_DEBUG_INFO_TO_STAFF': True, 'DISPLAY_HISTOGRAMS_TO_STAFF': True}) -@patch('courseware.module_render.has_access', Mock(return_value=True)) +@patch('courseware.module_render.has_access', Mock(return_value=True, autospec=True)) class TestStaffDebugInfo(ModuleStoreTestCase): """Tests to verify that Staff Debug Info panel and histograms are displayed to staff.""" @@ -1483,7 +1483,7 @@ class TestAnonymousStudentId(ModuleStoreTestCase, LoginEnrollmentTestCase): self.course_key = ToyCourseFactory.create().id self.course = modulestore().get_course(self.course_key) - @patch('courseware.module_render.has_access', Mock(return_value=True)) + @patch('courseware.module_render.has_access', Mock(return_value=True, autospec=True)) def _get_anonymous_id(self, course_id, xblock_class): location = course_id.make_usage_key('dummy_category', 'dummy_name') descriptor = Mock( @@ -1550,7 +1550,7 @@ class TestAnonymousStudentId(ModuleStoreTestCase, LoginEnrollmentTestCase): @attr('shard_1') -@patch('track.views.tracker') +@patch('track.views.tracker', autospec=True) class TestModuleTrackingContext(ModuleStoreTestCase): """ Ensure correct tracking information is included in events emitted during XBlock callback handling. diff --git a/lms/djangoapps/django_comment_client/base/tests.py b/lms/djangoapps/django_comment_client/base/tests.py index 776bf7d493..464750dda4 100644 --- a/lms/djangoapps/django_comment_client/base/tests.py +++ b/lms/djangoapps/django_comment_client/base/tests.py @@ -48,7 +48,7 @@ class MockRequestSetupMixin(object): mock_request.return_value = self._create_response_mock(data) -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) class CreateThreadGroupIdTestCase( MockRequestSetupMixin, CohortedTestCase, @@ -83,7 +83,7 @@ class CreateThreadGroupIdTestCase( self._assert_json_response_contains_group_info(response) -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) @disable_signal(views, 'thread_edited') @disable_signal(views, 'thread_voted') @disable_signal(views, 'thread_deleted') @@ -348,7 +348,7 @@ class ViewsTestCaseMixin(object): @ddt.ddt -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) @disable_signal(views, 'thread_created') @disable_signal(views, 'thread_edited') class ViewsQueryCountTestCase(UrlResetMixin, ModuleStoreTestCase, MockRequestSetupMixin, ViewsTestCaseMixin): @@ -401,7 +401,7 @@ class ViewsQueryCountTestCase(UrlResetMixin, ModuleStoreTestCase, MockRequestSet @ddt.ddt -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) class ViewsTestCase( UrlResetMixin, ModuleStoreTestCase, @@ -984,7 +984,7 @@ class ViewsTestCase( self.assertEqual(response.status_code, 200) -@patch("lms.lib.comment_client.utils.requests.request") +@patch("lms.lib.comment_client.utils.requests.request", autospec=True) @disable_signal(views, 'comment_endorsed') class ViewPermissionsTestCase(UrlResetMixin, ModuleStoreTestCase, MockRequestSetupMixin): @patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True}) @@ -1088,7 +1088,7 @@ class CreateThreadUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin, MockReq self.student = UserFactory.create() CourseEnrollmentFactory(user=self.student, course_id=self.course.id) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def _test_unicode_data(self, text, mock_request,): """ Test to make sure unicode data in a thread doesn't break it. @@ -1118,7 +1118,7 @@ class UpdateThreadUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin, MockReq CourseEnrollmentFactory(user=self.student, course_id=self.course.id) @patch('django_comment_client.utils.get_discussion_categories_ids', return_value=["test_commentable"]) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def _test_unicode_data(self, text, mock_request, mock_get_discussion_id_map): self._set_mock_request_data(mock_request, { "user_id": str(self.student.id), @@ -1147,7 +1147,7 @@ class CreateCommentUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin, MockRe self.student = UserFactory.create() CourseEnrollmentFactory(user=self.student, course_id=self.course.id) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def _test_unicode_data(self, text, mock_request): commentable_id = "non_team_dummy_id" self._set_mock_request_data(mock_request, { @@ -1182,7 +1182,7 @@ class UpdateCommentUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin, MockRe self.student = UserFactory.create() CourseEnrollmentFactory(user=self.student, course_id=self.course.id) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def _test_unicode_data(self, text, mock_request): self._set_mock_request_data(mock_request, { "user_id": str(self.student.id), @@ -1211,7 +1211,7 @@ class CreateSubCommentUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin, Moc self.student = UserFactory.create() CourseEnrollmentFactory(user=self.student, course_id=self.course.id) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def _test_unicode_data(self, text, mock_request): """ Create a comment with unicode in it. @@ -1239,7 +1239,7 @@ class CreateSubCommentUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin, Moc @ddt.ddt -@patch("lms.lib.comment_client.utils.requests.request") +@patch("lms.lib.comment_client.utils.requests.request", autospec=True) @disable_signal(views, 'thread_voted') @disable_signal(views, 'thread_edited') @disable_signal(views, 'comment_created') @@ -1515,7 +1515,7 @@ class ForumEventTestCase(ModuleStoreTestCase, MockRequestSetupMixin): CourseAccessRoleFactory(course_id=self.course.id, user=self.student, role='Wizard') @patch('eventtracking.tracker.emit') - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def test_thread_event(self, __, mock_emit): request = RequestFactory().post( "dummy_url", { @@ -1544,7 +1544,7 @@ class ForumEventTestCase(ModuleStoreTestCase, MockRequestSetupMixin): self.assertEquals(event['anonymous_to_peers'], False) @patch('eventtracking.tracker.emit') - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def test_response_event(self, mock_request, mock_emit): """ Check to make sure an event is fired when a user responds to a thread. @@ -1570,7 +1570,7 @@ class ForumEventTestCase(ModuleStoreTestCase, MockRequestSetupMixin): self.assertEqual(event['options']['followed'], True) @patch('eventtracking.tracker.emit') - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def test_comment_event(self, mock_request, mock_emit): """ Ensure an event is fired when someone comments on a response. @@ -1597,7 +1597,7 @@ class ForumEventTestCase(ModuleStoreTestCase, MockRequestSetupMixin): self.assertEqual(event['options']['followed'], False) @patch('eventtracking.tracker.emit') - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) @ddt.data(( 'create_thread', 'edx.forum.thread.created', { @@ -1649,7 +1649,7 @@ class ForumEventTestCase(ModuleStoreTestCase, MockRequestSetupMixin): ) @ddt.unpack @patch('eventtracking.tracker.emit') - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def test_thread_voted_event(self, view_name, obj_id_name, obj_type, mock_request, mock_emit): undo = view_name.startswith('undo') @@ -1704,7 +1704,7 @@ class UsersEndpointTestCase(ModuleStoreTestCase, MockRequestSetupMixin): request.view_name = "users" return views.users(request, course_id=course_id.to_deprecated_string()) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def test_finds_exact_match(self, mock_request): self.set_post_counts(mock_request) response = self.make_request(username="other") @@ -1714,7 +1714,7 @@ class UsersEndpointTestCase(ModuleStoreTestCase, MockRequestSetupMixin): [{"id": self.other_user.id, "username": self.other_user.username}] ) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def test_finds_no_match(self, mock_request): self.set_post_counts(mock_request) response = self.make_request(username="othor") @@ -1751,7 +1751,7 @@ class UsersEndpointTestCase(ModuleStoreTestCase, MockRequestSetupMixin): self.assertIn("errors", content) self.assertNotIn("users", content) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def test_requires_matched_user_has_forum_content(self, mock_request): self.set_post_counts(mock_request, 0, 0) response = self.make_request(username="other") diff --git a/lms/djangoapps/django_comment_client/forum/tests.py b/lms/djangoapps/django_comment_client/forum/tests.py index e1c0804e46..a052fed399 100644 --- a/lms/djangoapps/django_comment_client/forum/tests.py +++ b/lms/djangoapps/django_comment_client/forum/tests.py @@ -216,7 +216,7 @@ class PartialDictMatcher(object): ]) -@patch('requests.request') +@patch('requests.request', autospec=True) class SingleThreadTestCase(ModuleStoreTestCase): def setUp(self): super(SingleThreadTestCase, self).setUp(create_user=False) @@ -327,7 +327,7 @@ class SingleThreadTestCase(ModuleStoreTestCase): @ddt.ddt -@patch('requests.request') +@patch('requests.request', autospec=True) class SingleThreadQueryCountTestCase(ModuleStoreTestCase): """ Ensures the number of modulestore queries and number of sql queries are @@ -394,7 +394,7 @@ class SingleThreadQueryCountTestCase(ModuleStoreTestCase): call_single_thread() -@patch('requests.request') +@patch('requests.request', autospec=True) class SingleCohortedThreadTestCase(CohortedTestCase): def _create_mock_cohorted_thread(self, mock_request): self.mock_text = "dummy content" @@ -453,7 +453,7 @@ class SingleCohortedThreadTestCase(CohortedTestCase): self.assertRegexpMatches(html, r'"group_name": "student_cohort"') -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) class SingleThreadAccessTestCase(CohortedTestCase): def call_view(self, mock_request, commentable_id, user, group_id, thread_group_id=None, pass_group_id=True): thread_id = "test_thread_id" @@ -540,7 +540,7 @@ class SingleThreadAccessTestCase(CohortedTestCase): self.assertEqual(resp.status_code, 200) -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) class SingleThreadGroupIdTestCase(CohortedTestCase, CohortedTopicGroupIdTestMixin): cs_endpoint = "/threads" @@ -592,7 +592,7 @@ class SingleThreadGroupIdTestCase(CohortedTestCase, CohortedTopicGroupIdTestMixi ) -@patch('requests.request') +@patch('requests.request', autospec=True) class SingleThreadContentGroupTestCase(ContentGroupTestCase): def assert_can_access(self, user, discussion_id, thread_id, should_have_access): """ @@ -704,7 +704,7 @@ class SingleThreadContentGroupTestCase(ContentGroupTestCase): self.assert_can_access(self.beta_user, self.alpha_module.discussion_id, thread_id, True) -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) class InlineDiscussionContextTestCase(ModuleStoreTestCase): def setUp(self): super(InlineDiscussionContextTestCase, self).setUp() @@ -740,7 +740,7 @@ class InlineDiscussionContextTestCase(ModuleStoreTestCase): self.assertEqual(json_response['discussion_data'][0]['context'], ThreadContext.STANDALONE) -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) class InlineDiscussionGroupIdTestCase( CohortedTestCase, CohortedTopicGroupIdTestMixin, @@ -791,7 +791,7 @@ class InlineDiscussionGroupIdTestCase( ) -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) class ForumFormDiscussionGroupIdTestCase(CohortedTestCase, CohortedTopicGroupIdTestMixin): cs_endpoint = "/threads" @@ -841,7 +841,7 @@ class ForumFormDiscussionGroupIdTestCase(CohortedTestCase, CohortedTopicGroupIdT ) -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) class UserProfileDiscussionGroupIdTestCase(CohortedTestCase, CohortedTopicGroupIdTestMixin): cs_endpoint = "/active_threads" @@ -1007,7 +1007,7 @@ class UserProfileDiscussionGroupIdTestCase(CohortedTestCase, CohortedTopicGroupI verify_group_id_not_present(profiled_user=self.moderator, pass_group_id=False) -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) class FollowedThreadsDiscussionGroupIdTestCase(CohortedTestCase, CohortedTopicGroupIdTestMixin): cs_endpoint = "/subscribed_threads" @@ -1044,7 +1044,7 @@ class FollowedThreadsDiscussionGroupIdTestCase(CohortedTestCase, CohortedTopicGr ) -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) class InlineDiscussionTestCase(ModuleStoreTestCase): def setUp(self): super(InlineDiscussionTestCase, self).setUp() @@ -1102,7 +1102,7 @@ class InlineDiscussionTestCase(ModuleStoreTestCase): self.verify_response(response) -@patch('requests.request') +@patch('requests.request', autospec=True) class UserProfileTestCase(ModuleStoreTestCase): TEST_THREAD_TEXT = 'userprofile-test-text' @@ -1219,7 +1219,7 @@ class UserProfileTestCase(ModuleStoreTestCase): self.assertEqual(response.status_code, 405) -@patch('requests.request') +@patch('requests.request', autospec=True) class CommentsServiceRequestHeadersTestCase(UrlResetMixin, ModuleStoreTestCase): @patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True}) def setUp(self): @@ -1289,7 +1289,7 @@ class InlineDiscussionUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin): self.student = UserFactory.create() CourseEnrollmentFactory(user=self.student, course_id=self.course.id) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def _test_unicode_data(self, text, mock_request): mock_request.side_effect = make_mock_request_impl(course=self.course, text=text) request = RequestFactory().get("dummy_url") @@ -1312,7 +1312,7 @@ class ForumFormDiscussionUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin): self.student = UserFactory.create() CourseEnrollmentFactory(user=self.student, course_id=self.course.id) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def _test_unicode_data(self, text, mock_request): mock_request.side_effect = make_mock_request_impl(course=self.course, text=text) request = RequestFactory().get("dummy_url") @@ -1327,7 +1327,7 @@ class ForumFormDiscussionUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin): @ddt.ddt -@patch('lms.lib.comment_client.utils.requests.request') +@patch('lms.lib.comment_client.utils.requests.request', autospec=True) class ForumDiscussionXSSTestCase(UrlResetMixin, ModuleStoreTestCase): @patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True}) def setUp(self): @@ -1384,7 +1384,7 @@ class ForumDiscussionSearchUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin self.student = UserFactory.create() CourseEnrollmentFactory(user=self.student, course_id=self.course.id) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def _test_unicode_data(self, text, mock_request): mock_request.side_effect = make_mock_request_impl(course=self.course, text=text) data = { @@ -1410,7 +1410,7 @@ class SingleThreadUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin): self.student = UserFactory.create() CourseEnrollmentFactory(user=self.student, course_id=self.course.id) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def _test_unicode_data(self, text, mock_request): thread_id = "test_thread_id" mock_request.side_effect = make_mock_request_impl(course=self.course, text=text, thread_id=thread_id) @@ -1433,7 +1433,7 @@ class UserProfileUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin): self.student = UserFactory.create() CourseEnrollmentFactory(user=self.student, course_id=self.course.id) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def _test_unicode_data(self, text, mock_request): mock_request.side_effect = make_mock_request_impl(course=self.course, text=text) request = RequestFactory().get("dummy_url") @@ -1455,7 +1455,7 @@ class FollowedThreadsUnicodeTestCase(ModuleStoreTestCase, UnicodeTestMixin): self.student = UserFactory.create() CourseEnrollmentFactory(user=self.student, course_id=self.course.id) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def _test_unicode_data(self, text, mock_request): mock_request.side_effect = make_mock_request_impl(course=self.course, text=text) request = RequestFactory().get("dummy_url") @@ -1482,7 +1482,7 @@ class EnrollmentTestCase(ModuleStoreTestCase): self.student = UserFactory.create() @patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True}) - @patch('lms.lib.comment_client.utils.requests.request') + @patch('lms.lib.comment_client.utils.requests.request', autospec=True) def test_unenrolled(self, mock_request): mock_request.side_effect = make_mock_request_impl(course=self.course, text='dummy') request = RequestFactory().get('dummy_url') diff --git a/lms/djangoapps/edxnotes/tests.py b/lms/djangoapps/edxnotes/tests.py index 9916cff37f..07e905d252 100644 --- a/lms/djangoapps/edxnotes/tests.py +++ b/lms/djangoapps/edxnotes/tests.py @@ -79,10 +79,10 @@ class EdxNotesDecoratorTest(ModuleStoreTestCase): self.problem = TestProblem(self.course) @patch.dict("django.conf.settings.FEATURES", {'ENABLE_EDXNOTES': True}) - @patch("edxnotes.decorators.get_public_endpoint") - @patch("edxnotes.decorators.get_token_url") - @patch("edxnotes.decorators.get_edxnotes_id_token") - @patch("edxnotes.decorators.generate_uid") + @patch("edxnotes.decorators.get_public_endpoint", autospec=True) + @patch("edxnotes.decorators.get_token_url", autospec=True) + @patch("edxnotes.decorators.get_edxnotes_id_token", autospec=True) + @patch("edxnotes.decorators.generate_uid", autospec=True) def test_edxnotes_enabled(self, mock_generate_uid, mock_get_id_token, mock_get_token_url, mock_get_endpoint): """ Tests if get_html is wrapped when feature flag is on and edxnotes are @@ -275,7 +275,7 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): with patch_edxnotes_api_settings(None): self.assertRaises(ImproperlyConfigured, get_endpoint_function) - @patch("edxnotes.helpers.requests.get") + @patch("edxnotes.helpers.requests.get", autospec=True) def test_get_notes_correct_data(self, mock_get): """ Tests the result if correct data is received. @@ -347,7 +347,7 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): json.loads(helpers.get_notes(self.user, self.course)) ) - @patch("edxnotes.helpers.requests.get") + @patch("edxnotes.helpers.requests.get", autospec=True) def test_get_notes_json_error(self, mock_get): """ Tests the result if incorrect json is received. @@ -355,7 +355,7 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): mock_get.return_value.content = "Error" self.assertIsNone(helpers.get_notes(self.user, self.course)) - @patch("edxnotes.helpers.requests.get") + @patch("edxnotes.helpers.requests.get", autospec=True) def test_get_notes_empty_collection(self, mock_get): """ Tests the result if an empty collection is received. @@ -363,7 +363,7 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): mock_get.return_value.content = json.dumps([]) self.assertIsNone(helpers.get_notes(self.user, self.course)) - @patch("edxnotes.helpers.requests.get") + @patch("edxnotes.helpers.requests.get", autospec=True) def test_search_correct_data(self, mock_get): """ Tests the result if correct data is received. @@ -443,7 +443,7 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): json.loads(helpers.search(self.user, self.course, "test")) ) - @patch("edxnotes.helpers.requests.get") + @patch("edxnotes.helpers.requests.get", autospec=True) def test_search_json_error(self, mock_get): """ Tests the result if incorrect json is received. @@ -451,7 +451,7 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): mock_get.return_value.content = "Error" self.assertRaises(EdxNotesParseError, helpers.search, self.user, self.course, "test") - @patch("edxnotes.helpers.requests.get") + @patch("edxnotes.helpers.requests.get", autospec=True) def test_search_wrong_data_format(self, mock_get): """ Tests the result if incorrect data structure is received. @@ -459,7 +459,7 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): mock_get.return_value.content = json.dumps({"1": 2}) self.assertRaises(EdxNotesParseError, helpers.search, self.user, self.course, "test") - @patch("edxnotes.helpers.requests.get") + @patch("edxnotes.helpers.requests.get", autospec=True) def test_search_empty_collection(self, mock_get): """ Tests no results. @@ -604,8 +604,8 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): helpers.preprocess_collection(self.user, self.course, initial_collection) ) - @patch("edxnotes.helpers.has_access") - @patch("edxnotes.helpers.modulestore") + @patch("edxnotes.helpers.has_access", autospec=True) + @patch("edxnotes.helpers.modulestore", autospec=True) def test_preprocess_collection_no_unit(self, mock_modulestore, mock_has_access): """ Tests the result if the unit does not exist. @@ -690,9 +690,9 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): @override_settings(EDXNOTES_PUBLIC_API="http://example.com") @override_settings(EDXNOTES_INTERNAL_API="http://example.com") - @patch("edxnotes.helpers.anonymous_id_for_user") - @patch("edxnotes.helpers.get_edxnotes_id_token") - @patch("edxnotes.helpers.requests.get") + @patch("edxnotes.helpers.anonymous_id_for_user", autospec=True) + @patch("edxnotes.helpers.get_edxnotes_id_token", autospec=True) + @patch("edxnotes.helpers.requests.get", autospec=True) def test_send_request_with_query_string(self, mock_get, mock_get_id_token, mock_anonymous_id_for_user): """ Tests that requests are send with correct information. @@ -719,9 +719,9 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): @override_settings(EDXNOTES_PUBLIC_API="http://example.com") @override_settings(EDXNOTES_INTERNAL_API="http://example.com") - @patch("edxnotes.helpers.anonymous_id_for_user") - @patch("edxnotes.helpers.get_edxnotes_id_token") - @patch("edxnotes.helpers.requests.get") + @patch("edxnotes.helpers.anonymous_id_for_user", autospec=True) + @patch("edxnotes.helpers.get_edxnotes_id_token", autospec=True) + @patch("edxnotes.helpers.requests.get", autospec=True) def test_send_request_without_query_string(self, mock_get, mock_get_id_token, mock_anonymous_id_for_user): """ Tests that requests are send with correct information. @@ -876,7 +876,7 @@ class EdxNotesViewsTest(ModuleStoreTestCase): self.assertEqual(response.status_code, 404) @patch.dict("django.conf.settings.FEATURES", {"ENABLE_EDXNOTES": True}) - @patch("edxnotes.views.get_notes") + @patch("edxnotes.views.get_notes", autospec=True) def test_edxnotes_view_404_service_unavailable(self, mock_get_notes): """ Tests that 404 status code is received if EdxNotes service is unavailable. @@ -887,7 +887,7 @@ class EdxNotesViewsTest(ModuleStoreTestCase): self.assertEqual(response.status_code, 404) @patch.dict("django.conf.settings.FEATURES", {"ENABLE_EDXNOTES": True}) - @patch("edxnotes.views.search") + @patch("edxnotes.views.search", autospec=True) def test_search_notes_successfully_respond(self, mock_search): """ Tests that `search_notes` successfully respond if EdxNotes feature is enabled. @@ -905,7 +905,7 @@ class EdxNotesViewsTest(ModuleStoreTestCase): self.assertEqual(response.status_code, 200) @patch.dict("django.conf.settings.FEATURES", {"ENABLE_EDXNOTES": False}) - @patch("edxnotes.views.search") + @patch("edxnotes.views.search", autospec=True) def test_search_notes_is_disabled(self, mock_search): """ Tests that 404 status code is received if EdxNotes feature is disabled. @@ -918,7 +918,7 @@ class EdxNotesViewsTest(ModuleStoreTestCase): self.assertEqual(response.status_code, 404) @patch.dict("django.conf.settings.FEATURES", {"ENABLE_EDXNOTES": True}) - @patch("edxnotes.views.search") + @patch("edxnotes.views.search", autospec=True) def test_search_404_service_unavailable(self, mock_search): """ Tests that 404 status code is received if EdxNotes service is unavailable. @@ -930,7 +930,7 @@ class EdxNotesViewsTest(ModuleStoreTestCase): self.assertIn("error", response.content) @patch.dict("django.conf.settings.FEATURES", {"ENABLE_EDXNOTES": True}) - @patch("edxnotes.views.search") + @patch("edxnotes.views.search", autospec=True) def test_search_notes_without_required_parameters(self, mock_search): """ Tests that 400 status code is received if the required parameters were not sent. @@ -944,7 +944,7 @@ class EdxNotesViewsTest(ModuleStoreTestCase): self.assertEqual(response.status_code, 400) @patch.dict("django.conf.settings.FEATURES", {"ENABLE_EDXNOTES": True}) - @patch("edxnotes.views.search") + @patch("edxnotes.views.search", autospec=True) def test_search_notes_exception(self, mock_search): """ Tests that 500 status code is received if invalid data was received from diff --git a/lms/djangoapps/instructor/tests/test_api.py b/lms/djangoapps/instructor/tests/test_api.py index ef4bfa9d7c..016af27a45 100644 --- a/lms/djangoapps/instructor/tests/test_api.py +++ b/lms/djangoapps/instructor/tests/test_api.py @@ -192,7 +192,7 @@ class TestCommonExceptions400(TestCase): @attr('shard_1') -@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message')) +@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True)) @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False}) class TestInstructorAPIDenyLevels(SharedModuleStoreTestCase, LoginEnrollmentTestCase): """ @@ -3234,7 +3234,7 @@ class TestEntranceExamInstructorAPIRegradeTask(SharedModuleStoreTestCase, LoginE @attr('shard_1') -@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message')) +@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True)) @patch.dict(settings.FEATURES, {'ENABLE_INSTRUCTOR_EMAIL': True, 'REQUIRE_COURSE_EMAIL_AUTH': False}) class TestInstructorSendEmail(SharedModuleStoreTestCase, LoginEnrollmentTestCase): """ @@ -3484,7 +3484,7 @@ class TestInstructorAPITaskLists(SharedModuleStoreTestCase, LoginEnrollmentTestC @attr('shard_1') -@patch.object(instructor_task.api, 'get_instructor_task_history') +@patch.object(instructor_task.api, 'get_instructor_task_history', autospec=True) class TestInstructorEmailContentList(SharedModuleStoreTestCase, LoginEnrollmentTestCase): """ Test the instructor email content history endpoint. diff --git a/lms/djangoapps/instructor_task/tests/test_api.py b/lms/djangoapps/instructor_task/tests/test_api.py index 9621c3db81..6dbcb71abf 100644 --- a/lms/djangoapps/instructor_task/tests/test_api.py +++ b/lms/djangoapps/instructor_task/tests/test_api.py @@ -167,7 +167,7 @@ class InstructorTaskModuleSubmitTest(InstructorTaskModuleTestCase): self._test_submit_task(submit_delete_problem_state_for_all_students) -@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message')) +@patch('bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True)) class InstructorTaskCourseSubmitTest(TestReportMixin, InstructorTaskCourseTestCase): """Tests API methods that involve the submission of course-based background tasks.""" diff --git a/lms/djangoapps/lti_provider/tests/test_users.py b/lms/djangoapps/lti_provider/tests/test_users.py index 90025e3f98..03b9f21ea7 100644 --- a/lms/djangoapps/lti_provider/tests/test_users.py +++ b/lms/djangoapps/lti_provider/tests/test_users.py @@ -70,8 +70,8 @@ class UserManagementHelperTest(TestCase): ) -@patch('lti_provider.users.switch_user') -@patch('lti_provider.users.create_lti_user') +@patch('lti_provider.users.switch_user', autospec=True) +@patch('lti_provider.users.create_lti_user', autospec=True) class AuthenticateLtiUserTest(TestCase): """ Tests for the authenticate_lti_user function in users.py diff --git a/lms/djangoapps/verify_student/tests/test_views.py b/lms/djangoapps/verify_student/tests/test_views.py index ad000078de..df0eb464d7 100644 --- a/lms/djangoapps/verify_student/tests/test_views.py +++ b/lms/djangoapps/verify_student/tests/test_views.py @@ -1156,7 +1156,7 @@ class CheckoutTestMixin(object): self.assertEqual(data, {'foo': 'bar'}) -@patch('lms.djangoapps.verify_student.views.checkout_with_shoppingcart', return_value=TEST_PAYMENT_DATA) +@patch('lms.djangoapps.verify_student.views.checkout_with_shoppingcart', return_value=TEST_PAYMENT_DATA, autospec=True) class TestCreateOrderShoppingCart(CheckoutTestMixin, ModuleStoreTestCase): """ Test view behavior when the shoppingcart is used. """ @@ -1170,7 +1170,11 @@ class TestCreateOrderShoppingCart(CheckoutTestMixin, ModuleStoreTestCase): @override_settings(ECOMMERCE_API_URL=TEST_API_URL, ECOMMERCE_API_SIGNING_KEY=TEST_API_SIGNING_KEY) -@patch('lms.djangoapps.verify_student.views.checkout_with_ecommerce_service', return_value=TEST_PAYMENT_DATA) +@patch( + 'lms.djangoapps.verify_student.views.checkout_with_ecommerce_service', + return_value=TEST_PAYMENT_DATA, + autospec=True, +) class TestCreateOrderEcommerceService(CheckoutTestMixin, ModuleStoreTestCase): """ Test view behavior when the ecommerce service is used. """ diff --git a/openedx/core/djangoapps/course_groups/tests/test_cohorts.py b/openedx/core/djangoapps/course_groups/tests/test_cohorts.py index 3771563702..1782a9c62f 100644 --- a/openedx/core/djangoapps/course_groups/tests/test_cohorts.py +++ b/openedx/core/djangoapps/course_groups/tests/test_cohorts.py @@ -25,7 +25,7 @@ from ..tests.helpers import ( ) -@patch("openedx.core.djangoapps.course_groups.cohorts.tracker") +@patch("openedx.core.djangoapps.course_groups.cohorts.tracker", autospec=True) class TestCohortSignals(TestCase): """ Test cases to validate event emissions for various cohort-related workflows