Commit 6490551f by Sanford Student

MA-2103

parent 2b880d2d
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
Course API Course API
""" """
from django.contrib.auth.models import User from django.contrib.auth.models import User, AnonymousUser
from rest_framework.exceptions import PermissionDenied from rest_framework.exceptions import PermissionDenied
from lms.djangoapps.courseware.courses import ( from lms.djangoapps.courseware.courses import (
...@@ -19,6 +19,8 @@ def get_effective_user(requesting_user, target_username): ...@@ -19,6 +19,8 @@ def get_effective_user(requesting_user, target_username):
""" """
if target_username == requesting_user.username: if target_username == requesting_user.username:
return requesting_user return requesting_user
elif target_username == '':
return AnonymousUser()
elif can_view_courses_for_username(requesting_user, target_username): elif can_view_courses_for_username(requesting_user, target_username):
return User.objects.get(username=target_username) return User.objects.get(username=target_username)
else: else:
......
...@@ -22,9 +22,6 @@ class UsernameValidatorMixin(object): ...@@ -22,9 +22,6 @@ class UsernameValidatorMixin(object):
as an anonymous user. as an anonymous user.
""" """
username = self.cleaned_data.get('username') username = self.cleaned_data.get('username')
if not username:
if not self.initial['requesting_user'].is_anonymous():
raise ValidationError("A username is required for non-anonymous access.")
return username or '' return username or ''
......
...@@ -26,8 +26,9 @@ class UsernameTestMixin(object): ...@@ -26,8 +26,9 @@ class UsernameTestMixin(object):
self.assert_valid(self.cleaned_data) self.assert_valid(self.cleaned_data)
def test_no_user_param(self): def test_no_user_param(self):
self.set_up_data(AnonymousUser())
self.form_data.pop('username') self.form_data.pop('username')
self.assert_error('username', "A username is required for non-anonymous access.") self.assert_valid(self.cleaned_data)
@ddt.ddt @ddt.ddt
......
...@@ -86,7 +86,8 @@ class CourseListViewTestCase(CourseApiTestViewMixin, SharedModuleStoreTestCase): ...@@ -86,7 +86,8 @@ class CourseListViewTestCase(CourseApiTestViewMixin, SharedModuleStoreTestCase):
def test_missing_username(self): def test_missing_username(self):
self.setup_user(self.honor_user) self.setup_user(self.honor_user)
self.verify_response(expected_status_code=400) response_to_missing_username = self.verify_response(expected_status_code=200)
self.assertIsNotNone(response_to_missing_username.data) # pylint: disable=no-member
@SharedModuleStoreTestCase.modifies_courseware @SharedModuleStoreTestCase.modifies_courseware
def test_filter_by_org(self): def test_filter_by_org(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment