From 411f1f796463e610f7903568413017e11d766930 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 25 May 2016 11:35:34 -0400 Subject: [PATCH] Use the Django TestClient for forum unit tests, so that middleware is cleaned up properly --- .../django_comment_client/forum/tests.py | 96 ++++++++----------- .../django_comment_client/tests/utils.py | 3 +- lms/djangoapps/notification_prefs/tests.py | 23 ++--- 3 files changed, 50 insertions(+), 72 deletions(-) diff --git a/lms/djangoapps/django_comment_client/forum/tests.py b/lms/djangoapps/django_comment_client/forum/tests.py index 13d9c4fd79..dfeb5d9e4c 100644 --- a/lms/djangoapps/django_comment_client/forum/tests.py +++ b/lms/djangoapps/django_comment_client/forum/tests.py @@ -8,7 +8,6 @@ from django.test.client import Client, RequestFactory from django.test.utils import override_settings from django.utils import translation from lms.lib.comment_client.utils import CommentClientPaginatedResult -from edxmako.tests import mako_middleware_process_request from django_comment_common.utils import ThreadContext from django_comment_client.forum import views @@ -441,14 +440,13 @@ class SingleCohortedThreadTestCase(CohortedTestCase): def test_html(self, mock_request): self._create_mock_cohorted_thread(mock_request) - request = RequestFactory().get("dummy_url") - request.user = self.student - mako_middleware_process_request(request) - response = views.single_thread( - request, - self.course.id.to_deprecated_string(), - "cohorted_topic", - self.mock_thread_id + self.client.login(username=self.student.username, password='test') + response = self.client.get( + reverse('single_thread', kwargs={ + 'course_id': unicode(self.course.id), + 'discussion_id': "cohorted_topic", + 'thread_id': self.mock_thread_id, + }) ) self.assertEquals(response.status_code, 200) @@ -561,19 +559,14 @@ class SingleThreadGroupIdTestCase(CohortedTestCase, CohortedTopicGroupIdTestMixi headers = {} if is_ajax: headers['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest" - request = RequestFactory().get( - "dummy_url", + + self.client.login(username=user.username, password='test') + + return self.client.get( + reverse('single_thread', args=[unicode(self.course.id), commentable_id, "dummy_thread_id"]), data=request_data, **headers ) - request.user = user - mako_middleware_process_request(request) - return views.single_thread( - request, - self.course.id.to_deprecated_string(), - commentable_id, - "dummy_thread_id" - ) def test_group_info_in_html_response(self, mock_request): response = self.call_view( @@ -599,30 +592,28 @@ class SingleThreadGroupIdTestCase(CohortedTestCase, CohortedTopicGroupIdTestMixi @patch('requests.request', autospec=True) -class SingleThreadContentGroupTestCase(ContentGroupTestCase): +class SingleThreadContentGroupTestCase(UrlResetMixin, ContentGroupTestCase): + + @patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True}) + def setUp(self): + super(SingleThreadContentGroupTestCase, self).setUp() + def assert_can_access(self, user, discussion_id, thread_id, should_have_access): """ Verify that a user has access to a thread within a given discussion_id when should_have_access is True, otherwise verify that the user does not have access to that thread. """ - request = RequestFactory().get("dummy_url") - request.user = user - mako_middleware_process_request(request) - def call_single_thread(): - return views.single_thread( - request, - unicode(self.course.id), - discussion_id, - thread_id + self.client.login(username=user.username, password='test') + return self.client.get( + reverse('single_thread', args=[unicode(self.course.id), discussion_id, thread_id]) ) if should_have_access: self.assertEqual(call_single_thread().status_code, 200) else: - with self.assertRaises(Http404): - call_single_thread() + self.assertEqual(call_single_thread().status_code, 404) def test_staff_user(self, mock_request): """ @@ -813,17 +804,13 @@ class ForumFormDiscussionGroupIdTestCase(CohortedTestCase, CohortedTopicGroupIdT headers = {} if is_ajax: headers['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest" - request = RequestFactory().get( - "dummy_url", + + self.client.login(username=user.username, password='test') + return self.client.get( + reverse(views.forum_form_discussion, args=[unicode(self.course.id)]), data=request_data, **headers ) - request.user = user - mako_middleware_process_request(request) - return views.forum_form_discussion( - request, - self.course.id.to_deprecated_string() - ) def test_group_info_in_html_response(self, mock_request): response = self.call_view( @@ -869,18 +856,13 @@ class UserProfileDiscussionGroupIdTestCase(CohortedTestCase, CohortedTopicGroupI headers = {} if is_ajax: headers['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest" - request = RequestFactory().get( - "dummy_url", + + self.client.login(username=requesting_user.username, password='test') + return self.client.get( + reverse('user_profile', args=[unicode(self.course.id), profiled_user.id]), data=request_data, **headers ) - request.user = requesting_user - mako_middleware_process_request(request) - return views.user_profile( - request, - self.course.id.to_deprecated_string(), - profiled_user.id - ) def call_view(self, mock_request, _commentable_id, user, group_id, pass_group_id=True, is_ajax=False): return self.call_view_for_profiled_user( @@ -1109,11 +1091,12 @@ class InlineDiscussionTestCase(ModuleStoreTestCase): @patch('requests.request', autospec=True) -class UserProfileTestCase(ModuleStoreTestCase): +class UserProfileTestCase(UrlResetMixin, ModuleStoreTestCase): TEST_THREAD_TEXT = 'userprofile-test-text' TEST_THREAD_ID = 'userprofile-test-thread-id' + @patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True}) def setUp(self): super(UserProfileTestCase, self).setUp() @@ -1126,14 +1109,15 @@ class UserProfileTestCase(ModuleStoreTestCase): mock_request.side_effect = make_mock_request_impl( course=self.course, text=self.TEST_THREAD_TEXT, thread_id=self.TEST_THREAD_ID ) - request = RequestFactory().get("dummy_url", data=params, **headers) - request.user = self.student + self.client.login(username=self.student.username, password='test') - mako_middleware_process_request(request) - response = views.user_profile( - request, - self.course.id.to_deprecated_string(), - self.profiled_user.id + response = self.client.get( + reverse('user_profile', kwargs={ + 'course_id': unicode(self.course.id), + 'user_id': self.profiled_user.id, + }), + data=params, + **headers ) mock_request.assert_any_call( "get", diff --git a/lms/djangoapps/django_comment_client/tests/utils.py b/lms/djangoapps/django_comment_client/tests/utils.py index e9c0ca966b..eda735dd14 100644 --- a/lms/djangoapps/django_comment_client/tests/utils.py +++ b/lms/djangoapps/django_comment_client/tests/utils.py @@ -7,11 +7,12 @@ from openedx.core.djangoapps.course_groups.tests.helpers import CohortFactory from django_comment_common.models import Role from django_comment_common.utils import seed_permissions_roles from student.tests.factories import CourseEnrollmentFactory, UserFactory +from util.testing import UrlResetMixin from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase -class CohortedTestCase(SharedModuleStoreTestCase): +class CohortedTestCase(UrlResetMixin, SharedModuleStoreTestCase): """ Sets up a course with a student, a moderator and their cohorts. """ diff --git a/lms/djangoapps/notification_prefs/tests.py b/lms/djangoapps/notification_prefs/tests.py index 19e556a660..e67d4e91ab 100644 --- a/lms/djangoapps/notification_prefs/tests.py +++ b/lms/djangoapps/notification_prefs/tests.py @@ -2,6 +2,7 @@ import json from django.contrib.auth.models import AnonymousUser from django.core.exceptions import PermissionDenied +from django.core.urlresolvers import reverse from django.http import Http404 from django.test import TestCase from django.test.client import RequestFactory @@ -11,7 +12,6 @@ from mock import Mock, patch from notification_prefs import NOTIFICATION_PREF_KEY from notification_prefs.views import ajax_enable, ajax_disable, ajax_status, set_subscription, UsernameCipher from student.tests.factories import UserFactory -from edxmako.tests import mako_middleware_process_request from openedx.core.djangoapps.user_api.models import UserPreference from util.testing import UrlResetMixin @@ -214,11 +214,9 @@ class NotificationPrefViewTest(UrlResetMixin, TestCase): self.create_prefs() def test_user(user): - request = self.request_factory.get("dummy") - request.user = AnonymousUser() + url = reverse('unsubscribe_forum_update', args=[self.tokens[user]]) - mako_middleware_process_request(request) - response = set_subscription(request, self.tokens[user], subscribe=False) + response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertNotPrefExists(user) @@ -227,12 +225,10 @@ class NotificationPrefViewTest(UrlResetMixin, TestCase): def test_unsubscribe_twice(self): self.create_prefs() - request = self.request_factory.get("dummy") - request.user = AnonymousUser() - mako_middleware_process_request(request) - set_subscription(request, self.tokens[self.user], False) - response = set_subscription(request, self.tokens[self.user], subscribe=False) + url = reverse('unsubscribe_forum_update', args=[self.tokens[self.user]]) + self.client.get(url) + response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertNotPrefExists(self.user) @@ -240,11 +236,8 @@ class NotificationPrefViewTest(UrlResetMixin, TestCase): def test_user(user): # start without a pref key self.assertFalse(UserPreference.objects.filter(user=user, key=NOTIFICATION_PREF_KEY)) - request = self.request_factory.get("dummy") - request.user = AnonymousUser() - - mako_middleware_process_request(request) - response = set_subscription(request, self.tokens[user], subscribe=True) + url = reverse('resubscribe_forum_update', args=[self.tokens[user]]) + response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertPrefValid(user)