Merge pull request #19502 from edx/jkantor/roles-filter
add an optional parameter roles query to filter by course
This commit is contained in:
@@ -1456,9 +1456,12 @@ class UserRoleTest(ModuleStoreTestCase):
|
||||
'role': role.ROLE,
|
||||
}
|
||||
|
||||
def _assert_roles(self, expected_roles, is_staff):
|
||||
def _assert_roles(self, expected_roles, is_staff, course_id=None):
|
||||
""" Asserts that the api call is successful and returns the expected roles """
|
||||
response = self.client.get(reverse('roles'))
|
||||
if course_id is not None:
|
||||
response = self.client.get(reverse('roles'), {'course_id': course_id})
|
||||
else:
|
||||
response = self.client.get(reverse('roles'))
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
response_data = json.loads(response.content)
|
||||
sort_by_role_id = lambda r: r['course_id']
|
||||
@@ -1497,6 +1500,16 @@ class UserRoleTest(ModuleStoreTestCase):
|
||||
expected_roles.append(expected_role2)
|
||||
self._assert_roles(expected_roles, is_staff)
|
||||
|
||||
def test_roles_filter(self):
|
||||
role1 = CourseStaffRole(self.course1.id)
|
||||
role1.add_users(self.user)
|
||||
expected_role1 = self._create_expected_role_dict(self.course1, role1)
|
||||
role2 = CourseStaffRole(self.course2.id)
|
||||
role2.add_users(self.user)
|
||||
expected_role2 = self._create_expected_role_dict(self.course2, role2)
|
||||
self._assert_roles([expected_role1], False, course_id=text_type(self.course1.id))
|
||||
self._assert_roles([expected_role2], False, course_id=text_type(self.course2.id))
|
||||
|
||||
def test_roles_exception(self):
|
||||
with patch('enrollment.api.get_user_roles') as mock_get_user_roles:
|
||||
mock_get_user_roles.side_effect = Exception()
|
||||
|
||||
@@ -210,17 +210,24 @@ class EnrollmentUserRolesView(APIView):
|
||||
**Use Case**
|
||||
|
||||
Get the roles for the current logged-in user.
|
||||
A field is also included to indicate whether or not the user is a global
|
||||
staff member.
|
||||
If an optional course_id parameter is supplied, the returned roles will be
|
||||
filtered to only include roles for the given course.
|
||||
|
||||
**Example Requests**
|
||||
|
||||
GET /api/enrollment/v1/roles/
|
||||
GET /api/enrollment/v1/roles/?course_id={course_id}
|
||||
|
||||
course_id: (optional) A course id. The returned roles will be filtered to
|
||||
only include roles for the given course.
|
||||
|
||||
**Response Values**
|
||||
|
||||
If the request is successful, an HTTP 200 "OK" response is
|
||||
returned along with a collection of user roles for the
|
||||
logged-in user
|
||||
|
||||
logged-in user, filtered by course_id if given, along with
|
||||
whether or not the user is global staff
|
||||
"""
|
||||
authentication_classes = (JwtAuthentication,
|
||||
OAuth2AuthenticationAllowInactiveUser,
|
||||
@@ -231,10 +238,13 @@ class EnrollmentUserRolesView(APIView):
|
||||
@method_decorator(ensure_csrf_cookie_cross_domain)
|
||||
def get(self, request):
|
||||
"""
|
||||
Gets a list of all roles for the currently logged-in user
|
||||
Gets a list of all roles for the currently logged-in user, filtered by course_id if supplied
|
||||
"""
|
||||
try:
|
||||
course_id = request.GET.get('course_id')
|
||||
roles_data = api.get_user_roles(request.user.username)
|
||||
if course_id:
|
||||
roles_data = [role for role in roles_data if text_type(role.course_id) == course_id]
|
||||
except Exception:
|
||||
return Response(
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
|
||||
Reference in New Issue
Block a user