From 9fb65532d66816ea3ee8cfa8b09f218671d3fb81 Mon Sep 17 00:00:00 2001 From: "Albert (AJ) St. Aubin" Date: Thu, 26 Mar 2020 09:28:04 -0400 Subject: [PATCH] Added an email search parameter to accounts --- .../user_api/accounts/tests/test_views.py | 19 ++++++++++++++++++ .../djangoapps/user_api/accounts/views.py | 20 +++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) 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 d67ffcc693..f6eaa96f13 100644 --- a/openedx/core/djangoapps/user_api/accounts/tests/test_views.py +++ b/openedx/core/djangoapps/user_api/accounts/tests/test_views.py @@ -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"), diff --git a/openedx/core/djangoapps/user_api/accounts/views.py b/openedx/core/djangoapps/user_api/accounts/views.py index c9ce1f3492..99d06d0dcc 100644 --- a/openedx/core/djangoapps/user_api/accounts/views.py +++ b/openedx/core/djangoapps/user_api/accounts/views.py @@ -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)