diff --git a/common/djangoapps/student/tests/test_authz.py b/common/djangoapps/student/tests/test_authz.py index 2395d58e07..048ec34c5f 100644 --- a/common/djangoapps/student/tests/test_authz.py +++ b/common/djangoapps/student/tests/test_authz.py @@ -113,8 +113,9 @@ class CreatorGroupTest(TestCase): def test_add_user_to_group_requires_authenticated(self): with self.assertRaises(PermissionDenied): - self.admin.is_authenticated = mock.Mock(return_value=False) - add_users(self.admin, CourseCreatorRole(), self.user) + with mock.patch('django.contrib.auth.models.User.is_authenticated') as mock_is_auth: + mock_is_auth.return_value = False + add_users(self.admin, CourseCreatorRole(), self.user) def test_remove_user_from_group_requires_staff_access(self): with self.assertRaises(PermissionDenied): @@ -128,8 +129,9 @@ class CreatorGroupTest(TestCase): def test_remove_user_from_group_requires_authenticated(self): with self.assertRaises(PermissionDenied): - self.admin.is_authenticated = mock.Mock(return_value=False) - remove_users(self.admin, CourseCreatorRole(), self.user) + with mock.patch('django.contrib.auth.models.User.is_authenticated') as mock_is_auth: + mock_is_auth.return_value = False + remove_users(self.admin, CourseCreatorRole(), self.user) class CCXCourseGroupTest(TestCase): diff --git a/lms/djangoapps/courseware/tests/test_tabs.py b/lms/djangoapps/courseware/tests/test_tabs.py index 298c0ace49..00825797b1 100644 --- a/lms/djangoapps/courseware/tests/test_tabs.py +++ b/lms/djangoapps/courseware/tests/test_tabs.py @@ -3,6 +3,7 @@ Test cases for tabs. """ import pytest +from django.contrib.auth.models import AnonymousUser from django.core.urlresolvers import reverse from django.http import Http404 from milestones.tests.utils import MilestonesTestCaseMixin @@ -159,8 +160,9 @@ class TabTestCase(SharedModuleStoreTestCase): if for_authenticated_users_only: user = self.create_mock_user(is_staff=False, is_enrolled=False) self.assertEquals(expected_value, self.is_tab_enabled(tab, self.course, user)) + assert False if not for_staff_only and not for_authenticated_users_only and not for_enrolled_users_only: - user = self.create_mock_user(is_staff=False, is_enrolled=False) + user = AnonymousUser() self.assertEquals(expected_value, self.is_tab_enabled(tab, self.course, user)) if for_enrolled_users_only: user = self.create_mock_user(is_staff=False, is_enrolled=True) diff --git a/lms/djangoapps/lti_provider/tests/test_users.py b/lms/djangoapps/lti_provider/tests/test_users.py index ca8f5364b4..0865efa99e 100644 --- a/lms/djangoapps/lti_provider/tests/test_users.py +++ b/lms/djangoapps/lti_provider/tests/test_users.py @@ -116,7 +116,7 @@ class AuthenticateLtiUserTest(TestCase): def test_authentication_with_authenticated_user(self, create_user, switch_user): lti_user = self.create_lti_user_model() self.request.user = lti_user.edx_user - self.request.user.is_authenticated = MagicMock(return_value=True) + assert self.request.user.is_authenticated() users.authenticate_lti_user(self.request, self.lti_user_id, self.lti_consumer) self.assertFalse(create_user.called) self.assertFalse(switch_user.called) @@ -124,15 +124,16 @@ class AuthenticateLtiUserTest(TestCase): def test_authentication_with_unauthenticated_user(self, create_user, switch_user): lti_user = self.create_lti_user_model() self.request.user = lti_user.edx_user - self.request.user.is_authenticated = MagicMock(return_value=False) - users.authenticate_lti_user(self.request, self.lti_user_id, self.lti_consumer) - self.assertFalse(create_user.called) - switch_user.assert_called_with(self.request, lti_user, self.lti_consumer) + with patch('django.contrib.auth.models.User.is_authenticated') as mock_is_auth: + mock_is_auth.return_value = False + users.authenticate_lti_user(self.request, self.lti_user_id, self.lti_consumer) + self.assertFalse(create_user.called) + switch_user.assert_called_with(self.request, lti_user, self.lti_consumer) def test_authentication_with_wrong_user(self, create_user, switch_user): lti_user = self.create_lti_user_model() self.request.user = self.old_user - self.request.user.is_authenticated = MagicMock(return_value=True) + assert self.request.user.is_authenticated() users.authenticate_lti_user(self.request, self.lti_user_id, self.lti_consumer) self.assertFalse(create_user.called) switch_user.assert_called_with(self.request, lti_user, self.lti_consumer) diff --git a/lms/djangoapps/lti_provider/tests/test_views.py b/lms/djangoapps/lti_provider/tests/test_views.py index a8a1ee7902..482dc866f6 100644 --- a/lms/djangoapps/lti_provider/tests/test_views.py +++ b/lms/djangoapps/lti_provider/tests/test_views.py @@ -45,13 +45,12 @@ COURSE_PARAMS = { ALL_PARAMS = dict(LTI_DEFAULT_PARAMS.items() + COURSE_PARAMS.items()) -def build_launch_request(authenticated=True): +def build_launch_request(): """ Helper method to create a new request object for the LTI launch. """ request = RequestFactory().post('/') request.user = UserFactory.create() - request.user.is_authenticated = MagicMock(return_value=authenticated) request.session = {} request.POST.update(LTI_DEFAULT_PARAMS) return request