Commit 9472f70d by wajeeha-khalid

MA-1341 User Account: returned updated value from Patch

updated docstring
parent 87e3988c
...@@ -52,7 +52,7 @@ class UserAPITestCase(APITestCase): ...@@ -52,7 +52,7 @@ class UserAPITestCase(APITestCase):
client.login(username=user.username, password=self.test_password) client.login(username=user.username, password=self.test_password)
return client return client
def send_patch(self, client, json_data, content_type="application/merge-patch+json", expected_status=204): def send_patch(self, client, json_data, content_type="application/merge-patch+json", expected_status=200):
""" """
Helper method for sending a patch to the server, defaulting to application/merge-patch+json content_type. Helper method for sending a patch to the server, defaulting to application/merge-patch+json content_type.
Verifies the expected status and returns the response. Verifies the expected status and returns the response.
...@@ -381,10 +381,8 @@ class TestAccountAPI(UserAPITestCase): ...@@ -381,10 +381,8 @@ class TestAccountAPI(UserAPITestCase):
Test the behavior of patch, when using the correct content_type. Test the behavior of patch, when using the correct content_type.
""" """
client = self.login_client("client", "user") client = self.login_client("client", "user")
self.send_patch(client, {field: value}) response = self.send_patch(client, {field: value})
self.assertEqual(value, response.data[field])
get_response = self.send_get(client)
self.assertEqual(value, get_response.data[field])
if fails_validation_value: if fails_validation_value:
error_response = self.send_patch(client, {field: fails_validation_value}, expected_status=400) error_response = self.send_patch(client, {field: fails_validation_value}, expected_status=400)
...@@ -400,19 +398,16 @@ class TestAccountAPI(UserAPITestCase): ...@@ -400,19 +398,16 @@ class TestAccountAPI(UserAPITestCase):
) )
else: else:
# If there are no values that would fail validation, then empty string should be supported. # If there are no values that would fail validation, then empty string should be supported.
self.send_patch(client, {field: ""}) response = self.send_patch(client, {field: ""})
self.assertEqual("", response.data[field])
get_response = self.send_get(client)
self.assertEqual("", get_response.data[field])
def test_patch_inactive_user(self): def test_patch_inactive_user(self):
""" Verify that a user can patch her own account, even if inactive. """ """ Verify that a user can patch her own account, even if inactive. """
self.client.login(username=self.user.username, password=self.test_password) self.client.login(username=self.user.username, password=self.test_password)
self.user.is_active = False self.user.is_active = False
self.user.save() self.user.save()
self.send_patch(self.client, {"goals": "to not activate account"}) response = self.send_patch(self.client, {"goals": "to not activate account"})
get_response = self.send_get(self.client) self.assertEqual("to not activate account", response.data["goals"])
self.assertEqual("to not activate account", get_response.data["goals"])
@ddt.unpack @ddt.unpack
def test_patch_account_noneditable(self): def test_patch_account_noneditable(self):
...@@ -458,15 +453,13 @@ class TestAccountAPI(UserAPITestCase): ...@@ -458,15 +453,13 @@ class TestAccountAPI(UserAPITestCase):
""" """
self.client.login(username=self.user.username, password=self.test_password) self.client.login(username=self.user.username, password=self.test_password)
for field_name in ["gender", "level_of_education", "country"]: for field_name in ["gender", "level_of_education", "country"]:
self.send_patch(self.client, {field_name: ""}) response = self.send_patch(self.client, {field_name: ""})
response = self.send_get(self.client)
# Although throwing a 400 might be reasonable, the default DRF behavior with ModelSerializer # Although throwing a 400 might be reasonable, the default DRF behavior with ModelSerializer
# is to convert to None, which also seems acceptable (and is difficult to override). # is to convert to None, which also seems acceptable (and is difficult to override).
self.assertIsNone(response.data[field_name]) self.assertIsNone(response.data[field_name])
# Verify that the behavior is the same for sending None. # Verify that the behavior is the same for sending None.
self.send_patch(self.client, {field_name: ""}) response = self.send_patch(self.client, {field_name: ""})
response = self.send_get(self.client)
self.assertIsNone(response.data[field_name]) self.assertIsNone(response.data[field_name])
def test_patch_name_metadata(self): def test_patch_name_metadata(self):
...@@ -513,12 +506,11 @@ class TestAccountAPI(UserAPITestCase): ...@@ -513,12 +506,11 @@ class TestAccountAPI(UserAPITestCase):
client = self.login_client("client", "user") client = self.login_client("client", "user")
old_email = self.user.email old_email = self.user.email
new_email = "newemail@example.com" new_email = "newemail@example.com"
self.send_patch(client, {"email": new_email, "goals": "change my email"}) response = self.send_patch(client, {"email": new_email, "goals": "change my email"})
# Since request is multi-step, the email won't change on GET immediately (though goals will update). # Since request is multi-step, the email won't change on GET immediately (though goals will update).
get_response = self.send_get(client) self.assertEqual(old_email, response.data["email"])
self.assertEqual(old_email, get_response.data["email"]) self.assertEqual("change my email", response.data["goals"])
self.assertEqual("change my email", get_response.data["goals"])
# Now call the method that will be invoked with the user clicks the activation key in the received email. # Now call the method that will be invoked with the user clicks the activation key in the received email.
# First we must get the activation key that was sent. # First we must get the activation key that was sent.
...@@ -566,8 +558,7 @@ class TestAccountAPI(UserAPITestCase): ...@@ -566,8 +558,7 @@ class TestAccountAPI(UserAPITestCase):
# identifies language proficiencies based on their language code rather # identifies language proficiencies based on their language code rather
# than django model id. # than django model id.
for proficiencies in ([{"code": "en"}, {"code": "fr"}, {"code": "es"}], [{"code": "fr"}], [{"code": "aa"}], []): for proficiencies in ([{"code": "en"}, {"code": "fr"}, {"code": "es"}], [{"code": "fr"}], [{"code": "aa"}], []):
self.send_patch(client, {"language_proficiencies": proficiencies}) response = self.send_patch(client, {"language_proficiencies": proficiencies})
response = self.send_get(client)
self.assertItemsEqual(response.data["language_proficiencies"], proficiencies) self.assertItemsEqual(response.data["language_proficiencies"], proficiencies)
@ddt.data( @ddt.data(
......
...@@ -134,8 +134,7 @@ class AccountView(APIView): ...@@ -134,8 +134,7 @@ class AccountView(APIView):
"Bad Request" error is returned. The JSON collection contains "Bad Request" error is returned. The JSON collection contains
specific errors. specific errors.
If the update is successful, an HTTP 204 "No Content" response is If the update is successful, updated user account data is returned.
returned with no additional content.
""" """
authentication_classes = (OAuth2AuthenticationAllowInactiveUser, SessionAuthenticationAllowInactiveUser) authentication_classes = (OAuth2AuthenticationAllowInactiveUser, SessionAuthenticationAllowInactiveUser)
permission_classes = (permissions.IsAuthenticated,) permission_classes = (permissions.IsAuthenticated,)
...@@ -163,6 +162,7 @@ class AccountView(APIView): ...@@ -163,6 +162,7 @@ class AccountView(APIView):
try: try:
with transaction.commit_on_success(): with transaction.commit_on_success():
update_account_settings(request.user, request.data, username=username) update_account_settings(request.user, request.data, username=username)
account_settings = get_account_settings(request, username)
except UserNotAuthorized: except UserNotAuthorized:
return Response(status=status.HTTP_403_FORBIDDEN if request.user.is_staff else status.HTTP_404_NOT_FOUND) return Response(status=status.HTTP_403_FORBIDDEN if request.user.is_staff else status.HTTP_404_NOT_FOUND)
except UserNotFound: except UserNotFound:
...@@ -178,4 +178,4 @@ class AccountView(APIView): ...@@ -178,4 +178,4 @@ class AccountView(APIView):
status=status.HTTP_400_BAD_REQUEST status=status.HTTP_400_BAD_REQUEST
) )
return Response(status=status.HTTP_204_NO_CONTENT) return Response(account_settings)
...@@ -137,10 +137,14 @@ class TestPreferencesAPI(UserAPITestCase): ...@@ -137,10 +137,14 @@ class TestPreferencesAPI(UserAPITestCase):
if not is_active: if not is_active:
self.user.is_active = False self.user.is_active = False
self.user.save() self.user.save()
self.send_patch(self.client, { self.send_patch(
"dict_pref": {"int_key": 10}, self.client,
"string_pref": "value", {
}) "dict_pref": {"int_key": 10},
"string_pref": "value",
},
expected_status=204
)
response = self.send_get(self.client) response = self.send_get(self.client)
self.assertEqual({u"dict_pref": u"{u'int_key': 10}", u"string_pref": u"value"}, response.data) self.assertEqual({u"dict_pref": u"{u'int_key': 10}", u"string_pref": u"value"}, response.data)
...@@ -174,11 +178,15 @@ class TestPreferencesAPI(UserAPITestCase): ...@@ -174,11 +178,15 @@ class TestPreferencesAPI(UserAPITestCase):
# Send the patch request # Send the patch request
self.client.login(username=self.user.username, password=self.test_password) self.client.login(username=self.user.username, password=self.test_password)
self.send_patch(self.client, { self.send_patch(
"string_pref": "updated_value", self.client,
"new_pref": "new_value", {
"extra_pref": None, "string_pref": "updated_value",
}) "new_pref": "new_value",
"extra_pref": None,
},
expected_status=204
)
# Verify that GET returns the updated preferences # Verify that GET returns the updated preferences
response = self.send_get(self.client) response = self.send_get(self.client)
......
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