From cda681e2f1d383ee38767fc45197ca47b9207d88 Mon Sep 17 00:00:00 2001 From: Matt Tuchfarber Date: Wed, 13 Mar 2019 23:04:41 -0400 Subject: [PATCH] fix python tests --- cms/envs/common.py | 1 + .../discussion_api/tests/test_views.py | 8 +++---- lms/djangoapps/discussion_api/views.py | 22 +++++++++++-------- .../user_api/accounts/tests/test_views.py | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/cms/envs/common.py b/cms/envs/common.py index 2463a9e8a8..d2390996cb 100644 --- a/cms/envs/common.py +++ b/cms/envs/common.py @@ -1361,6 +1361,7 @@ ADVANCED_PROBLEM_TYPES = [ } ] +USERNAME_REPLACEMENT_WORKER = "REPLACE WITH VALID USERNAME" # Files and Uploads type filter values diff --git a/lms/djangoapps/discussion_api/tests/test_views.py b/lms/djangoapps/discussion_api/tests/test_views.py index 1d78136ada..6c0335c2b9 100644 --- a/lms/djangoapps/discussion_api/tests/test_views.py +++ b/lms/djangoapps/discussion_api/tests/test_views.py @@ -282,7 +282,7 @@ class ReplaceUsernamesViewTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): data = { "username_mappings": mapping_data } - response = self.call_api(self.url, self.client_user, data) + response = self.call_api(self.client_user, data) self.assertEqual(response.status_code, 400) def test_auth(self): @@ -300,11 +300,11 @@ class ReplaceUsernamesViewTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): # Test non-service worker random_user = UserFactory() - response = self.call_api(self.url, random_user, data) + response = self.call_api(random_user, data) self.assertEqual(response.status_code, 403) # Test service worker - response = self.call_api(self.url, self.client_user, data) + response = self.call_api(self.client_user, data) self.assertEqual(response.status_code, 200) def test_basic(self): @@ -319,7 +319,7 @@ class ReplaceUsernamesViewTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): 'successful_replacements': data["username_mappings"] } self.register_get_username_replacement_response(self.user) - response = self.call_api(self.url, self.client_user, data) + response = self.call_api(self.client_user, data) self.assertEqual(response.status_code, 200) self.assertEqual(response.data, expected_response) diff --git a/lms/djangoapps/discussion_api/views.py b/lms/djangoapps/discussion_api/views.py index 8af1995270..53d9f99948 100644 --- a/lms/djangoapps/discussion_api/views.py +++ b/lms/djangoapps/discussion_api/views.py @@ -10,7 +10,7 @@ from edx_rest_framework_extensions.auth.session.authentication import SessionAut from opaque_keys.edx.keys import CourseKey from rest_framework import permissions from rest_framework import status -from rest_framework.exceptions import UnsupportedMediaType +from rest_framework.exceptions import ParseError, UnsupportedMediaType from rest_framework.parsers import JSONParser from rest_framework.response import Response from rest_framework.views import APIView @@ -621,7 +621,7 @@ class ReplaceUsernamesView(APIView): username_mappings = request.data.get("username_mappings") if not self._has_valid_schema(username_mappings): - raise ValidationError("Request data does not match schema") + raise ParseError("Request data does not match schema") successful_replacements, failed_replacements = [], [] @@ -630,13 +630,17 @@ class ReplaceUsernamesView(APIView): new_username = list(username_pair.values())[0] successfully_replaced = self._replace_username(current_username, new_username) if successfully_replaced: - successful_replacements += {current_username: new_username} + successful_replacements.append({current_username: new_username}) else: - failed_replacements += {current_username: new_username} - return JsonResponse({ - "successful_replacements": successful_replacements, - "failed_replacements": failed_replacements, - }) + failed_replacements.append({current_username: new_username}) + + return Response( + status=status.HTTP_200_OK, + data={ + "successful_replacements": successful_replacements, + "failed_replacements": failed_replacements + } + ) def _replace_username(self, current_username, new_username): try: @@ -652,7 +656,7 @@ class ReplaceUsernamesView(APIView): new_username, new_username, ) - return False + return True except comment_client.CommentClientRequestError as exc: log.exception( u"Unable to change username from %s to %s in forums because forums API call failed with: %s.", diff --git a/openedx/core/djangoapps/user_api/accounts/tests/test_views.py b/openedx/core/djangoapps/user_api/accounts/tests/test_views.py index c4b4c0a305..1f664bd456 100644 --- a/openedx/core/djangoapps/user_api/accounts/tests/test_views.py +++ b/openedx/core/djangoapps/user_api/accounts/tests/test_views.py @@ -951,7 +951,7 @@ class UsernameReplacementViewTests(APITestCase): Helper function for creating headers for the JWT authentication. """ token = create_jwt_for_user(user) - headers = {'HTTP_AUTHORIZATION': token} + headers = {'HTTP_AUTHORIZATION': 'JWT {}'.format(token)} return headers def call_api(self, user, data):