fix python tests

This commit is contained in:
Matt Tuchfarber
2019-03-13 23:04:41 -04:00
parent f224f38a21
commit cda681e2f1
4 changed files with 19 additions and 14 deletions

View File

@@ -1361,6 +1361,7 @@ ADVANCED_PROBLEM_TYPES = [
}
]
USERNAME_REPLACEMENT_WORKER = "REPLACE WITH VALID USERNAME"
# Files and Uploads type filter values

View File

@@ -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)

View File

@@ -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.",

View File

@@ -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):