From 6490551f1b9c859ee9e98ab5e6534a9e7262b011 Mon Sep 17 00:00:00 2001 From: Sanford Student Date: Fri, 4 Mar 2016 11:43:37 -0500 Subject: [PATCH] MA-2103 --- lms/djangoapps/course_api/api.py | 4 +++- lms/djangoapps/course_api/forms.py | 3 --- lms/djangoapps/course_api/tests/test_forms.py | 3 ++- lms/djangoapps/course_api/tests/test_views.py | 3 ++- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lms/djangoapps/course_api/api.py b/lms/djangoapps/course_api/api.py index cfe0207a36..87c8e44ebe 100644 --- a/lms/djangoapps/course_api/api.py +++ b/lms/djangoapps/course_api/api.py @@ -2,7 +2,7 @@ Course API """ -from django.contrib.auth.models import User +from django.contrib.auth.models import User, AnonymousUser from rest_framework.exceptions import PermissionDenied from lms.djangoapps.courseware.courses import ( @@ -19,6 +19,8 @@ def get_effective_user(requesting_user, target_username): """ if target_username == requesting_user.username: return requesting_user + elif target_username == '': + return AnonymousUser() elif can_view_courses_for_username(requesting_user, target_username): return User.objects.get(username=target_username) else: diff --git a/lms/djangoapps/course_api/forms.py b/lms/djangoapps/course_api/forms.py index aca01f7c06..1767cc7422 100644 --- a/lms/djangoapps/course_api/forms.py +++ b/lms/djangoapps/course_api/forms.py @@ -22,9 +22,6 @@ class UsernameValidatorMixin(object): as an anonymous user. """ 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 '' diff --git a/lms/djangoapps/course_api/tests/test_forms.py b/lms/djangoapps/course_api/tests/test_forms.py index 2d3aa6fe32..5ea6d2343a 100644 --- a/lms/djangoapps/course_api/tests/test_forms.py +++ b/lms/djangoapps/course_api/tests/test_forms.py @@ -26,8 +26,9 @@ class UsernameTestMixin(object): self.assert_valid(self.cleaned_data) def test_no_user_param(self): + self.set_up_data(AnonymousUser()) 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 diff --git a/lms/djangoapps/course_api/tests/test_views.py b/lms/djangoapps/course_api/tests/test_views.py index 166ece1d25..641081b913 100644 --- a/lms/djangoapps/course_api/tests/test_views.py +++ b/lms/djangoapps/course_api/tests/test_views.py @@ -86,7 +86,8 @@ class CourseListViewTestCase(CourseApiTestViewMixin, SharedModuleStoreTestCase): def test_missing_username(self): 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 def test_filter_by_org(self):