From c07d34d9f556e3b87ce767f3b90274c8afb2e3af Mon Sep 17 00:00:00 2001 From: Saltuk Alakus Date: Thu, 12 Apr 2018 02:23:51 +0300 Subject: [PATCH 1/4] implementation of get user by email management endpoint --- auth0/v3/management/users_by_email.py | 44 +++++++++++++++++++ .../v3/test/management/test_users_by_email.py | 37 ++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 auth0/v3/management/users_by_email.py create mode 100644 auth0/v3/test/management/test_users_by_email.py diff --git a/auth0/v3/management/users_by_email.py b/auth0/v3/management/users_by_email.py new file mode 100644 index 00000000..fa2407ad --- /dev/null +++ b/auth0/v3/management/users_by_email.py @@ -0,0 +1,44 @@ +from .rest import RestClient + + +class UsersByEmail(object): + + """Auth0 users by email endpoints + + Args: + domain (str): Your Auth0 domain, e.g: 'username.auth0.com' + + token (str): Management API v2 Token + + telemetry (bool, optional): Enable or disable Telemetry + (defaults to True) + """ + + def __init__(self, domain, token, telemetry=True): + self.domain = domain + self.client = RestClient(jwt=token, telemetry=telemetry) + + def _url(self): + url = 'https://%s/api/v2/users-by-email' % self.domain + return url + + def search_users_by_email(self, email, fields=[], include_fields=True): + """List or search users. + + Args: + + email: Email to search + + fields (list of str, optional): A list of fields to include or + exclude from the result (depending on include_fields). Empty to + retrieve all fields. + + include_fields (bool, optional): True if the fields specified are + to be include in the result, False otherwise. + """ + params = { + 'email': email, + 'fields': ','.join(fields) or None, + 'include_fields': str(include_fields).lower() + } + return self.client.get(self._url(), params=params) diff --git a/auth0/v3/test/management/test_users_by_email.py b/auth0/v3/test/management/test_users_by_email.py new file mode 100644 index 00000000..51a09db8 --- /dev/null +++ b/auth0/v3/test/management/test_users_by_email.py @@ -0,0 +1,37 @@ +import unittest +import mock +from ...management.users_by_email import UsersByEmail + + +class TestUsersByEmail(unittest.TestCase): + + @mock.patch('auth0.v3.management.users_by_email.RestClient') + def test_search_users_by_email(self, mock_rc): + mock_instance = mock_rc.return_value + + u = UsersByEmail(domain='domain', token='jwttoken') + u.search_users_by_email('a@b.com') + + args, kwargs = mock_instance.get.call_args + + self.assertEqual('https://domain/api/v2/users-by-email', args[0]) + self.assertEqual(kwargs['params'], { + 'email': 'a@b.com', + 'fields': None, + 'include_fields': 'true' + }) + + u.search_users_by_email(email='a@b.com', + fields=['a', 'b'], + include_fields=False) + + args, kwargs = mock_instance.get.call_args + + self.assertEqual('https://domain/api/v2/users-by-email', args[0]) + self.assertEqual(kwargs['params'], { + 'email': 'a@b.com', + 'fields': 'a,b', + 'include_fields': 'false' + }) + + \ No newline at end of file From 58c016385610e4eb8c71e82788d287752b3cd6b0 Mon Sep 17 00:00:00 2001 From: Saltuk Alakus Date: Thu, 12 Apr 2018 02:42:18 +0300 Subject: [PATCH 2/4] ensure lower case email is sent to management api --- auth0/v3/management/users_by_email.py | 2 +- auth0/v3/test/management/test_users_by_email.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/auth0/v3/management/users_by_email.py b/auth0/v3/management/users_by_email.py index fa2407ad..7050dfb9 100644 --- a/auth0/v3/management/users_by_email.py +++ b/auth0/v3/management/users_by_email.py @@ -37,7 +37,7 @@ def search_users_by_email(self, email, fields=[], include_fields=True): to be include in the result, False otherwise. """ params = { - 'email': email, + 'email': email.lower(), 'fields': ','.join(fields) or None, 'include_fields': str(include_fields).lower() } diff --git a/auth0/v3/test/management/test_users_by_email.py b/auth0/v3/test/management/test_users_by_email.py index 51a09db8..59a2665f 100644 --- a/auth0/v3/test/management/test_users_by_email.py +++ b/auth0/v3/test/management/test_users_by_email.py @@ -10,7 +10,7 @@ def test_search_users_by_email(self, mock_rc): mock_instance = mock_rc.return_value u = UsersByEmail(domain='domain', token='jwttoken') - u.search_users_by_email('a@b.com') + u.search_users_by_email('A@B.com') args, kwargs = mock_instance.get.call_args From 5343fd83412ecff603b7af31ecb7bd27b64d9514 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Tue, 26 Jun 2018 18:45:22 -0300 Subject: [PATCH 3/4] change type to immutable argument --- auth0/v3/management/users_by_email.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/auth0/v3/management/users_by_email.py b/auth0/v3/management/users_by_email.py index 7050dfb9..ac64b7b0 100644 --- a/auth0/v3/management/users_by_email.py +++ b/auth0/v3/management/users_by_email.py @@ -22,7 +22,7 @@ def _url(self): url = 'https://%s/api/v2/users-by-email' % self.domain return url - def search_users_by_email(self, email, fields=[], include_fields=True): + def search_users_by_email(self, email, fields=None, include_fields=True): """List or search users. Args: @@ -38,7 +38,7 @@ def search_users_by_email(self, email, fields=[], include_fields=True): """ params = { 'email': email.lower(), - 'fields': ','.join(fields) or None, + 'fields': fields and ','.join(fields) or None, 'include_fields': str(include_fields).lower() } return self.client.get(self._url(), params=params) From 3b0737b5afc78f83b25d77bc8e929c364819a32c Mon Sep 17 00:00:00 2001 From: Saltuk Alakus Date: Tue, 3 Jul 2018 21:52:47 +0300 Subject: [PATCH 4/4] expose the new api --- README.rst | 1 + auth0/v3/management/__init__.py | 1 + auth0/v3/management/auth0.py | 2 ++ 3 files changed, 4 insertions(+) diff --git a/README.rst b/README.rst index 87fed5ac..f32589a9 100644 --- a/README.rst +++ b/README.rst @@ -156,6 +156,7 @@ Available Management Endpoints - DeviceCredentials() ( ``Auth0().device_credentials`` ) - Rules() ( ``Auth0().rules`` ) - Users() ( ``Auth0().users`` ) + - UsersByEmail() ( ``Auth0().users_by_email`` ) - Blacklists() ( ``Auth0().blacklists`` ) - Emails() ( ``Auth0().emails`` ) - Jobs() ( ``Auth0().jobs`` ) diff --git a/auth0/v3/management/__init__.py b/auth0/v3/management/__init__.py index e5854e3f..2743692b 100644 --- a/auth0/v3/management/__init__.py +++ b/auth0/v3/management/__init__.py @@ -15,4 +15,5 @@ from .tickets import Tickets from .user_blocks import UserBlocks from .users import Users +from .users_by_email import UsersByEmail diff --git a/auth0/v3/management/auth0.py b/auth0/v3/management/auth0.py index cc94a897..de59f3a9 100644 --- a/auth0/v3/management/auth0.py +++ b/auth0/v3/management/auth0.py @@ -8,6 +8,7 @@ from .stats import Stats from .tickets import Tickets from .users import Users +from .users_by_email import UsersByEmail from .tenants import Tenants from .client_grants import ClientGrants from .guardian import Guardian @@ -35,6 +36,7 @@ def __init__(self, domain, token): self.stats = Stats(domain, token) self.tickets = Tickets(domain, token) self.users = Users(domain, token) + self.users_by_email = UsersByEmail(domain, token) self.tenants = Tenants(domain, token) self.client_grants = ClientGrants(domain, token) self.guardian = Guardian(domain, token)