Merge pull request #17463 from edx/adeel/learner_4165_fix_error_raised_when_anonymous_access

Fixes error caused by invalid Anonymous User passed to a view.
This commit is contained in:
adeel khan
2018-02-14 11:34:13 +01:00
committed by GitHub
2 changed files with 14 additions and 0 deletions

View File

@@ -2499,6 +2499,18 @@ class UpdateEmailOptInTestCase(UserAPITestCase, SharedModuleStoreTestCase):
)
self.assertEquals(preference.value, u"True")
def test_update_email_opt_in_anonymous_user(self):
"""
Test that an anonymous user gets 403 response when
updating email optin preference.
"""
self.client.logout()
response = self.client.post(self.url, {
"course_id": unicode(self.course.id),
"email_opt_in": u"True"
})
self.assertEqual(response.status_code, 403)
def test_update_email_opt_with_invalid_course_key(self):
"""
Test that with invalid key it returns bad request

View File

@@ -14,6 +14,7 @@ from opaque_keys.edx.keys import CourseKey
from rest_framework import authentication, generics, status, viewsets
from rest_framework.exceptions import ParseError
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from six import text_type
import accounts
@@ -256,6 +257,7 @@ class PreferenceUsersListView(generics.ListAPIView):
class UpdateEmailOptInPreference(APIView):
"""View for updating the email opt in preference. """
authentication_classes = (SessionAuthenticationAllowInactiveUser,)
permission_classes = (IsAuthenticated,)
@method_decorator(require_post_params(["course_id", "email_opt_in"]))
@method_decorator(ensure_csrf_cookie)