-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from eduNEXT/mjh/update-username-endpoint
feat: Replace Username endpoint.
- Loading branch information
Showing
8 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
""" | ||
Test module for users viewset. | ||
""" | ||
from django.contrib.auth.models import User | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
from mock import patch | ||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
|
||
|
||
class EdxappReplaceUsernameAPITest(TestCase): | ||
"""Test class for update username APIView.""" | ||
|
||
patch_permissions = patch('eox_core.api.support.v1.permissions.EoxCoreSupportAPIPermission.has_permission', return_value=True) | ||
|
||
def setUp(self): | ||
"""Setup method for test class.""" | ||
self.user = User(username="test-username", email="[email protected]", password="testusername") | ||
self.client = APIClient() | ||
self.url = reverse("eox-support-api:eox-support-api:edxapp-replace-username") | ||
self.client.force_authenticate(user=self.user) | ||
|
||
@patch_permissions | ||
@patch('eox_core.api.support.v1.views.replace_username_cs_user') | ||
@patch('eox_core.api.support.v1.serializers.UserSignupSource') | ||
@patch('eox_core.api.support.v1.views.get_edxapp_user') | ||
@patch('eox_core.api.support.v1.views.EdxappUserReadOnlySerializer') | ||
def test_replace_username_success(self, user_serializer, get_edxapp_user, signup_source, replace_username_cs_user, _): | ||
"""Test the replacement of the username of an edxapp user.""" | ||
update_data = { | ||
"username": self.user.username, | ||
"new_username": "replaced-username", | ||
} | ||
|
||
user_serializer.return_value.data = {} | ||
get_edxapp_user.return_value = self.user | ||
signup_source.objects.filter.return_value.count.return_value = 1 | ||
|
||
response = self.client.patch(self.url, data=update_data, format="json") | ||
|
||
self.assertEqual("replaced-username", self.user.username) | ||
self.assertEqual(status.HTTP_200_OK, response.status_code) | ||
|
||
@patch_permissions | ||
@patch('eox_core.api.support.v1.serializers.UserSignupSource') | ||
@patch('eox_core.api.support.v1.views.get_edxapp_user') | ||
def test_replace_username_bad_sign_up_source(self, get_edxapp_user, signup_source, _): | ||
""" | ||
Tests that when a user has more than one signup source then the | ||
username cannot be replaced. | ||
""" | ||
update_data = { | ||
"username": self.user.username, | ||
"new_username": "another-username", | ||
} | ||
|
||
get_edxapp_user.return_value = self.user | ||
signup_source.objects.filter.return_value.count.return_value = 2 | ||
|
||
response = self.client.patch(self.url, data=update_data, format="json") | ||
|
||
self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code) | ||
self.assertEqual(response.content, '{"detail":["You can\'t update users with more than one sign up source."]}' | ||
.encode()) | ||
|
||
@patch_permissions | ||
@patch('eox_core.api.support.v1.serializers.UserSignupSource') | ||
@patch('eox_core.api.support.v1.views.get_edxapp_user') | ||
def test_replace_username_staff_user(self, get_edxapp_user, signup_source, _): | ||
"""Tests that if a user is staff or superuser then the username cannot be replaced.""" | ||
user = User(username="test", email="[email protected]", password="testtest", is_staff=True) | ||
update_data = { | ||
"username": user.username, | ||
"new_username": "new-test-username", | ||
} | ||
|
||
get_edxapp_user.return_value = user | ||
signup_source.objects.filter.return_value.count.return_value = 1 | ||
|
||
response = self.client.patch(self.url, data=update_data, format="json") | ||
|
||
self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code) | ||
self.assertEqual(response.content, '{"detail":["You can\'t update users with roles like staff or superuser."]}' | ||
.encode()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
eox_core/edxapp_wrapper/backends/comments_service_users_j_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" Module for the cs_comments_service User object.""" | ||
from openedx.core.djangoapps.django_comment_common.comment_client.user import User # pylint: disable=import-error | ||
|
||
|
||
def replace_username_cs_user(*args, **kwargs): | ||
""" | ||
Replace user's username in cs_comments_service (forums). | ||
kwargs: | ||
user: edxapp user whose username is being replaced. | ||
new_username: new username. | ||
""" | ||
user = kwargs.get("user") | ||
new_username = kwargs.get("new_username") | ||
|
||
cs_user = User.from_django_user(user) | ||
cs_user.replace_username(new_username) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
""" | ||
User model wrapper for cs_comments_service public definition. | ||
""" | ||
|
||
from importlib import import_module | ||
|
||
from django.conf import settings | ||
|
||
|
||
def replace_username_cs_user(*args, **kwargs): | ||
""" Gets the User model wrapper for comments service""" | ||
|
||
backend_function = settings.EOX_CORE_COMMENTS_SERVICE_USERS_BACKEND | ||
backend = import_module(backend_function) | ||
|
||
return backend.replace_username_cs_user(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters