diff --git a/README.rst b/README.rst index 01b93e81..3cc54775 100644 --- a/README.rst +++ b/README.rst @@ -166,8 +166,9 @@ Available Management Endpoints - Stats() ( ``Auth0().stats`` ) - Tenants() ( ``Auth0().tenants`` ) - Tickets() ( ``Auth0().tickets`` ) - - Users() ( ``Auth0().users`` ) - UserBlocks() (``Auth0().user_blocks`` ) + - Users() ( ``Auth0().users`` ) + - UsersByEmail() ( ``Auth0().users_by_email`` ) Available Authentication Endpoints ================================== diff --git a/auth0/v3/management/__init__.py b/auth0/v3/management/__init__.py index 7b9eba27..3bad10ac 100644 --- a/auth0/v3/management/__init__.py +++ b/auth0/v3/management/__init__.py @@ -14,5 +14,6 @@ from .stats import Stats from .tenants import Tenants from .tickets import Tickets +from .user_blocks import UserBlocks from .users import Users -from .user_blocks import UserBlocks \ No newline at end of file +from .users_by_email import UsersByEmail \ No newline at end of file diff --git a/auth0/v3/management/auth0.py b/auth0/v3/management/auth0.py index 1583f661..b741e1a9 100644 --- a/auth0/v3/management/auth0.py +++ b/auth0/v3/management/auth0.py @@ -13,8 +13,9 @@ from .stats import Stats from .tenants import Tenants from .tickets import Tickets -from .users import Users from .user_blocks import UserBlocks +from .users import Users +from .users_by_email import UsersByEmail class Auth0(object): """Provides easy access to all endpoint classes @@ -41,5 +42,6 @@ def __init__(self, domain, token): self.stats = Stats(domain, token) self.tenants = Tenants(domain, token) self.tickets = Tickets(domain, token) - self.users = Users(domain, token) self.user_blocks = UserBlocks(domain, token) + self.users = Users(domain, token) + self.users_by_email = UsersByEmail(domain, token) \ No newline at end of file diff --git a/auth0/v3/management/users_by_email.py b/auth0/v3/management/users_by_email.py new file mode 100644 index 00000000..ac64b7b0 --- /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=None, 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.lower(), + 'fields': fields and ','.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..59a2665f --- /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