Repository: auth0/auth0-python
Pull request #291 (2021-09-24 10:10:27)

Added support for prompts API
### Changes

Added support for prompts API:
- https://auth0.com/docs/api/management/v2/#!/Prompts/get_prompts
- https://auth0.com/docs/api/management/v2/#!/Prompts/patch_prompts
- https://auth0.com/docs/api/management/v2/#!/Prompts/get_custom_text_by_language
- https://auth0.com/docs/api/management/v2/#!/Prompts/put_custom_text_by_language

Classes added:
- `auth0.v3.management.prompts.Prompts`

Classes modified:
- `auth0.v3.management.auth0.Auth0`

### References

No references

### Testing

Please describe how this can be tested by reviewers. Be specific about anything not tested and reasons why. If this library has unit and/or integration testing, tests should be added for new functionality and existing tests should complete without errors. 

- [x] This change adds unit test coverage
- [x] This change adds integration test coverage
- [x] This change has been tested on the latest version of the platform/language or why not

### Checklist

- [x] I have read the [Auth0 general contribution guidelines](https://github.com/auth0/open-source-template/blob/master/GENERAL-CONTRIBUTING.md)
- [x] I have read the [Auth0 Code of Conduct](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md)
- [x] All existing and new tests complete without errors



Patch:
diff --git a/README.rst b/README.rst
index db6c18c..4c8fb8e 100644
--- a/README.rst
+++ b/README.rst
@@ -356,6 +356,7 @@ Management Endpoints
 - LogStreams() ( ``Auth0().log_streams`` )
 - Logs() ( ``Auth0().logs`` )
 - Organizations() ( ``Auth0().organizations`` )
+- Prompts() ( ``Auth0().prompts`` )
 - ResourceServers() (``Auth0().resource_servers`` )
 - Roles() ( ``Auth0().roles`` )
 - RulesConfigs() ( ``Auth0().rules_configs`` )
diff --git a/auth0/v3/management/auth0.py b/auth0/v3/management/auth0.py
index 9ac0be9..819e392 100644
--- a/auth0/v3/management/auth0.py
+++ b/auth0/v3/management/auth0.py
@@ -13,16 +13,17 @@ from .jobs import Jobs
 from .log_streams import LogStreams
 from .logs import Logs
 from .organizations import Organizations
+from .prompts import Prompts
 from .resource_servers import ResourceServers
 from .roles import Roles
-from .rules_configs import RulesConfigs
 from .rules import Rules
+from .rules_configs import RulesConfigs
 from .stats import Stats
 from .tenants import Tenants
 from .tickets import Tickets
 from .user_blocks import UserBlocks
-from .users_by_email import UsersByEmail
 from .users import Users
+from .users_by_email import UsersByEmail
 
 
 class Auth0(object):
@@ -55,6 +56,7 @@ class Auth0(object):
         self.log_streams = LogStreams(domain=domain, token=token, rest_options=rest_options)
         self.logs = Logs(domain=domain, token=token, rest_options=rest_options)
         self.organizations = Organizations(domain=domain, token=token, rest_options=rest_options)
+        self.prompts = Prompts(domain=domain, token=token, rest_options=rest_options)
         self.resource_servers = ResourceServers(domain=domain, token=token, rest_options=rest_options)
         self.roles = Roles(domain=domain, token=token, rest_options=rest_options)
         self.rules_configs = RulesConfigs(domain=domain, token=token, rest_options=rest_options)
diff --git a/auth0/v3/management/prompts.py b/auth0/v3/management/prompts.py
new file mode 100644
index 0000000..91108ab
--- /dev/null
+++ b/auth0/v3/management/prompts.py
@@ -0,0 +1,67 @@
+from .rest import RestClient
+
+
+class Prompts(object):
+    """Auth0 prompts 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)
+
+        timeout (float or tuple, optional): Change the requests
+            connect and read timeout. Pass a tuple to specify
+            both values separately or a float to set both to it.
+            (defaults to 5.0 for both)
+
+        rest_options (RestClientOptions): Pass an instance of
+            RestClientOptions to configure additional RestClient
+            options, such as rate-limit retries.
+            (defaults to None)
+    """
+
+    def __init__(self, domain, token, telemetry=True, timeout=5.0, protocol="https", rest_options=None):
+        self.domain = domain
+        self.protocol = protocol
+        self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout, options=rest_options)
+
+    def _url(self, prompt=None, language=None):
+        url = "{}://{}/api/v2/prompts".format(self.protocol, self.domain)
+        if prompt is not None and language is not None:
+            return "{}/{}/custom-text/{}".format(url, prompt, language)
+        return url
+
+    def get(self):
+        """Retrieves prompts settings.
+
+        See: https://auth0.com/docs/api/management/v2#!/Prompts/get_prompts
+        """
+
+        return self.client.get(self._url())
+
+    def update(self, body):
+        """Updates prompts settings.
+
+        See: https://auth0.com/docs/api/management/v2#!/Prompts/patch_prompts
+        """
+
+        return self.client.patch(self._url(), data=body)
+
+    def get_custom_text(self, prompt, language):
+        """Retrieves custom text for a prompt in a specific language.
+
+        See: https://auth0.com/docs/api/management/v2#!/Prompts/get_custom_text_by_language
+        """
+
+        return self.client.get(self._url(prompt, language))
+
+    def update_custom_text(self, prompt, language, body):
+        """Updates custom text for a prompt in a specific language.
+
+        See: https://auth0.com/docs/api/management/v2#!/Prompts/put_custom_text_by_language
+        """
+
+        return self.client.put(self._url(prompt, language), data=body)
diff --git a/docs/source/v3.management.rst b/docs/source/v3.management.rst
index 4491ae0..8bc17b2 100644
--- a/docs/source/v3.management.rst
+++ b/docs/source/v3.management.rst
@@ -129,6 +129,14 @@ management.organizations module
    :undoc-members:
    :show-inheritance:
 
+management.prompts module
+----------------------------------
+
+.. automodule:: auth0.v3.management.prompts
+   :members:
+   :undoc-members:
+   :show-inheritance:
+
 management.resource\_servers module
 --------------------------------------
 


Test patch:
diff --git a/auth0/v3/test/management/test_auth0.py b/auth0/v3/test/management/test_auth0.py
index 679b65b..d11529a 100644
--- a/auth0/v3/test/management/test_auth0.py
+++ b/auth0/v3/test/management/test_auth0.py
@@ -16,23 +16,23 @@ from ...management.jobs import Jobs
 from ...management.log_streams import LogStreams
 from ...management.logs import Logs
 from ...management.organizations import Organizations
+from ...management.prompts import Prompts
 from ...management.resource_servers import ResourceServers
 from ...management.roles import Roles
-from ...management.rules_configs import RulesConfigs
 from ...management.rules import Rules
+from ...management.rules_configs import RulesConfigs
 from ...management.stats import Stats
 from ...management.tenants import Tenants
 from ...management.tickets import Tickets
 from ...management.user_blocks import UserBlocks
-from ...management.users_by_email import UsersByEmail
 from ...management.users import Users
+from ...management.users_by_email import UsersByEmail
 
 
 class TestAuth0(unittest.TestCase):
-
     def setUp(self):
-        self.domain = 'user.some.domain'
-        self.token = 'a-token'
+        self.domain = "user.some.domain"
+        self.token = "a-token"
         self.a0 = Auth0(self.domain, self.token)
 
     def test_blacklists(self):
@@ -73,13 +73,16 @@ class TestAuth0(unittest.TestCase):
 
     def test_log_streams(self):
         self.assertIsInstance(self.a0.log_streams, LogStreams)
-    
+
     def test_logs(self):
         self.assertIsInstance(self.a0.logs, Logs)
 
     def test_organizations(self):
         self.assertIsInstance(self.a0.organizations, Organizations)
 
+    def test_prompts(self):
+        self.assertIsInstance(self.a0.prompts, Prompts)
+
     def test_resource_servers(self):
         self.assertIsInstance(self.a0.resource_servers, ResourceServers)
 
diff --git a/auth0/v3/test/management/test_prompts.py b/auth0/v3/test/management/test_prompts.py
new file mode 100644
index 0000000..06423eb
--- /dev/null
+++ b/auth0/v3/test/management/test_prompts.py
@@ -0,0 +1,59 @@
+import unittest
+
+import mock
+
+from ...management.prompts import Prompts
+
+
+class TestPrompts(unittest.TestCase):
+    def test_init_with_optionals(self):
+        t = Prompts(domain="domain", token="jwttoken", telemetry=False, timeout=(10, 2))
+        self.assertEqual(t.client.options.timeout, (10, 2))
+        telemetry_header = t.client.base_headers.get("Auth0-Client", None)
+        self.assertEqual(telemetry_header, None)
+
+    @mock.patch("auth0.v3.management.prompts.RestClient")
+    def test_get(self, mock_rc):
+        mock_instance = mock_rc.return_value
+
+        p = Prompts(domain="domain", token="jwttoken")
+        p.get()
+
+        args, _ = mock_instance.get.call_args
+
+        self.assertEqual("https://domain/api/v2/prompts", args[0])
+
+    @mock.patch("auth0.v3.management.prompts.RestClient")
+    def test_update(self, mock_rc):
+        mock_instance = mock_rc.return_value
+
+        p = Prompts(domain="domain", token="jwttoken")
+        p.update({"a": "b", "c": "d"})
+
+        args, kwargs = mock_instance.patch.call_args
+
+        self.assertEqual("https://domain/api/v2/prompts", args[0])
+        self.assertEqual(kwargs["data"], {"a": "b", "c": "d"})
+
+    @mock.patch("auth0.v3.management.prompts.RestClient")
+    def test_get_custom_text(self, mock_rc):
+        mock_instance = mock_rc.return_value
+
+        p = Prompts(domain="domain", token="jwttoken")
+        p.get_custom_text("some-prompt", "some-language")
+
+        args, _ = mock_instance.get.call_args
+
+        self.assertEqual("https://domain/api/v2/prompts/some-prompt/custom-text/some-language", args[0])
+
+    @mock.patch("auth0.v3.management.prompts.RestClient")
+    def test_update_custom_text(self, mock_rc):
+        mock_instance = mock_rc.return_value
+
+        p = Prompts(domain="domain", token="jwttoken")
+        p.update_custom_text("some-prompt", "some-language", {"a": "b", "c": "d"})
+
+        args, kwargs = mock_instance.put.call_args
+
+        self.assertEqual("https://domain/api/v2/prompts/some-prompt/custom-text/some-language", args[0])
+        self.assertEqual(kwargs["data"], {"a": "b", "c": "d"})