Added an email search parameter to accounts

This commit is contained in:
Albert (AJ) St. Aubin
2020-03-26 09:28:04 -04:00
parent 42ccc5564c
commit 9fb65532d6
2 changed files with 35 additions and 4 deletions

View File

@@ -328,6 +328,22 @@ class TestAccountsAPI(CacheIsolationTestCase, UserAPITestCase):
response = client.get(reverse("accounts_api", kwargs={'username': "does_not_exist"}))
self.assertEqual(404, response.status_code)
@ddt.data(
("client", "user"),
("staff_client", "staff_user"),
)
@ddt.unpack
def test_get_account_by_email(self, api_client, user):
"""
Test that requesting a user email search works.
"""
client = self.login_client(api_client, user)
self.create_mock_profile(self.user)
set_user_preference(self.user, ACCOUNT_VISIBILITY_PREF_KEY, PRIVATE_VISIBILITY)
response = self.send_get(client, query_parameters='email={}'.format(self.user.email))
self._verify_full_account_response(response)
# Note: using getattr so that the patching works even if there is no configuration.
# This is needed when testing CMS as the patching is still executed even though the
# suite is skipped.
@@ -397,6 +413,9 @@ class TestAccountsAPI(CacheIsolationTestCase, UserAPITestCase):
response = self.send_get(client, query_parameters='view=shared')
verify_fields_visible_to_all_users(response)
response = self.send_get(client, query_parameters='view=shared&email={}'.format(self.user.email))
verify_fields_visible_to_all_users(response)
@ddt.data(
("client", "user"),
("staff_client", "staff_user"),

View File

@@ -125,6 +125,7 @@ class AccountViewSet(ViewSet):
GET /api/user/v1/me[?view=shared]
GET /api/user/v1/accounts?usernames={username1,username2}[?view=shared]
GET /api/user/v1/accounts?email={user_email}
GET /api/user/v1/accounts/{username}/[?view=shared]
PATCH /api/user/v1/accounts/{username}/{"key":"value"} "application/merge-patch+json"
@@ -148,7 +149,7 @@ class AccountViewSet(ViewSet):
**Response Values for GET requests to /accounts endpoints**
If no user exists with the specified username, an HTTP 404 "Not
If no user exists with the specified username, or email, an HTTP 404 "Not
Found" response is returned.
If the user makes the request for her own account, or makes a
@@ -284,13 +285,24 @@ class AccountViewSet(ViewSet):
def list(self, request):
"""
GET /api/user/v1/accounts?username={username1,username2}
GET /api/user/v1/accounts?email={user_email}
"""
usernames = request.GET.get('username')
user_email = request.GET.get('email')
search_usernames = []
if usernames:
search_usernames = usernames.strip(',').split(',')
elif user_email:
user_email = user_email.strip('')
try:
user = User.objects.get(email=user_email)
except (UserNotFound, User.DoesNotExist):
return Response(status=status.HTTP_404_NOT_FOUND)
search_usernames = [user.username]
try:
if usernames:
usernames = usernames.strip(',').split(',')
account_settings = get_account_settings(
request, usernames, view=request.query_params.get('view'))
request, search_usernames, view=request.query_params.get('view'))
except UserNotFound:
return Response(status=status.HTTP_404_NOT_FOUND)