Repository: auth0/auth0-python
Pull request #108 (2018-06-15 16:08:28)

Add Email-Templates Management API endpoints


Patch:
diff --git a/.codecov b/.codecov.yml
similarity index 100%
rename from .codecov
rename to .codecov.yml
diff --git a/README.rst b/README.rst
index 87fed5a..01b93e8 100644
--- a/README.rst
+++ b/README.rst
@@ -151,20 +151,22 @@ For example:
 Available Management Endpoints
 ==============================
 
+    - Blacklists() ( ``Auth0().blacklists`` )
     - Clients() ( ``Auth0().clients`` )
+    - ClientGrants() ( ``Auth0().client_grants`` )
     - Connections() ( ``Auth0().connections`` )
     - DeviceCredentials() ( ``Auth0().device_credentials`` )
-    - Rules() ( ``Auth0().rules`` )
-    - Users() ( ``Auth0().users`` )
-    - Blacklists() ( ``Auth0().blacklists`` )
     - Emails() ( ``Auth0().emails`` )
-    - Jobs() ( ``Auth0().jobs`` )
-    - Stats() ( ``Auth0().stats`` )
-    - Tenants() ( ``Auth0().tenants`` )
-    - ClientGrants() ( ``Auth0().client_grants`` )
+    - EmailTemplates() ( ``Auth0().email_templates`` )
     - Guardian() ( ``Auth0().guardian`` )
+    - Jobs() ( ``Auth0().jobs`` )
     - Logs() ( ``Auth0().logs`` )
     - ResourceServers() (``Auth0().resource_servers`` )
+    - Rules() ( ``Auth0().rules`` )
+    - Stats() ( ``Auth0().stats`` )
+    - Tenants() ( ``Auth0().tenants`` )
+    - Tickets() ( ``Auth0().tickets`` )
+    - Users() ( ``Auth0().users`` )
     - UserBlocks() (``Auth0().user_blocks`` )
 
 Available Authentication Endpoints
diff --git a/auth0/v3/authentication/get_token.py b/auth0/v3/authentication/get_token.py
index 5693a29..b163d58 100644
--- a/auth0/v3/authentication/get_token.py
+++ b/auth0/v3/authentication/get_token.py
@@ -30,7 +30,7 @@ class GetToken(AuthenticationBase):
 
             code (str): The Authorization Code received from the /authorize Calls
 
-            redirect_uri (srt, optional): This is required only if it was set at
+            redirect_uri (str, optional): This is required only if it was set at
             the GET /authorize endpoint. The values must match
 
         Returns:
@@ -67,7 +67,7 @@ class GetToken(AuthenticationBase):
 
             code (str): The Authorization Code received from the /authorize Calls
 
-            redirect_uri (srt, optional): This is required only if it was set at
+            redirect_uri (str, optional): This is required only if it was set at
             the GET /authorize endpoint. The values must match
 
         Returns:
@@ -146,7 +146,7 @@ class GetToken(AuthenticationBase):
 
             password (str): resource owner's Secret
 
-            scope(srt): String value of the different scopes the client is asking for.
+            scope(str): String value of the different scopes the client is asking for.
             Multiple scopes are separated with whitespace.
 
             realm (str): String value of the realm the user belongs.
diff --git a/auth0/v3/management/__init__.py b/auth0/v3/management/__init__.py
index e5854e3..7b9eba2 100644
--- a/auth0/v3/management/__init__.py
+++ b/auth0/v3/management/__init__.py
@@ -1,10 +1,11 @@
 from .auth0 import Auth0
 from .blacklists import Blacklists
-from .client_grants import ClientGrants
 from .clients import Clients
+from .client_grants import ClientGrants
 from .connections import Connections
 from .device_credentials import DeviceCredentials
 from .emails import Emails
+from .email_templates import EmailTemplates
 from .guardian import Guardian
 from .jobs import Jobs
 from .logs import Logs
@@ -13,6 +14,5 @@ from .rules import Rules
 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
diff --git a/auth0/v3/management/auth0.py b/auth0/v3/management/auth0.py
index cc94a89..1583f66 100644
--- a/auth0/v3/management/auth0.py
+++ b/auth0/v3/management/auth0.py
@@ -1,18 +1,19 @@
-from .connections import Connections
+from .blacklists import Blacklists
 from .clients import Clients
+from .client_grants import ClientGrants
+from .connections import Connections
 from .device_credentials import DeviceCredentials
-from .blacklists import Blacklists
 from .emails import Emails
+from .email_templates import EmailTemplates
+from .guardian import Guardian
 from .jobs import Jobs
+from .logs import Logs
+from .resource_servers import ResourceServers
 from .rules import Rules
 from .stats import Stats
+from .tenants import Tenants
 from .tickets import Tickets
 from .users import Users
-from .tenants import Tenants
-from .client_grants import ClientGrants
-from .guardian import Guardian
-from .logs import Logs
-from .resource_servers import ResourceServers
 from .user_blocks import UserBlocks
 
 class Auth0(object):
@@ -25,19 +26,20 @@ class Auth0(object):
     """
 
     def __init__(self, domain, token):
+        self.blacklists = Blacklists(domain, token)
         self.clients = Clients(domain, token)
+        self.client_grants = ClientGrants(domain, token)
         self.connections = Connections(domain, token)
         self.device_credentials = DeviceCredentials(domain, token)
-        self.blacklists = Blacklists(domain, token)
         self.emails = Emails(domain, token)
+        self.email_templates = EmailTemplates(domain, token)
+        self.guardian = Guardian(domain, token)
         self.jobs = Jobs(domain, token)
+        self.logs = Logs(domain, token)
+        self.resource_servers = ResourceServers(domain, token)
         self.rules = Rules(domain, token)
         self.stats = Stats(domain, token)
+        self.tenants = Tenants(domain, token)
         self.tickets = Tickets(domain, token)
         self.users = Users(domain, token)
-        self.tenants = Tenants(domain, token)
-        self.client_grants = ClientGrants(domain, token)
-        self.guardian = Guardian(domain, token)
-        self.logs = Logs(domain, token)
-        self.resource_servers = ResourceServers(domain, token)
         self.user_blocks = UserBlocks(domain, token)
diff --git a/auth0/v3/management/email_templates.py b/auth0/v3/management/email_templates.py
new file mode 100644
index 0000000..22b8b76
--- /dev/null
+++ b/auth0/v3/management/email_templates.py
@@ -0,0 +1,65 @@
+from .rest import RestClient
+
+
+class EmailTemplates(object):
+
+    """Auth0 email templates 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, id=None):
+        url = 'https://%s/api/v2/email-templates' % self.domain
+        if id is not None:
+            return url + '/' + id
+        return url
+
+    def create(self, body):
+        """Create a new email template.
+
+        Args: 
+           body (dict): Attributes for the new email template.
+              See: https://auth0.com/docs/api/management/v2#!/Email_Templates/post_email_templates
+        """
+
+        return self.client.post(self._url(), data=body)
+
+
+    def get(self, template_name):
+        """Retrieves an email template by its name.
+
+        Args:
+           template_name (str): Name of the email template to get. 
+              Must be one of: 'verify_email', 'reset_email', 'welcome_email', 
+              'blocked_account', 'stolen_credentials', 'enrollment_email', 
+              'change_password', 'password_reset', 'mfa_oob_code'.
+              See: https://auth0.com/docs/api/management/v2#!/Email_Templates/get_email_templates_by_templateName
+        """
+
+        return self.client.get(self._url(template_name))
+
+
+    def update(self, template_name, body):
+        """Update an existing email template.
+
+        Args:
+           template_name (str): Name of the email template to update. 
+              Must be one of: 'verify_email', 'reset_email', 'welcome_email', 
+              'blocked_account', 'stolen_credentials', 'enrollment_email', 
+              'change_password', 'password_reset', 'mfa_oob_code'. 
+
+           body (dict): Attributes to update on the email template.
+              See: https://auth0.com/docs/api/management/v2#!/Email_Templates/patch_email_templates_by_templateName
+        """
+
+        return self.client.patch(self._url(template_name), data=body)


Test patch:
diff --git a/auth0/v3/test/management/test_email_endpoints.py b/auth0/v3/test/management/test_email_endpoints.py
new file mode 100644
index 0000000..d494c37
--- /dev/null
+++ b/auth0/v3/test/management/test_email_endpoints.py
@@ -0,0 +1,41 @@
+import unittest
+import mock
+from ...management.email_templates import EmailTemplates
+
+
+class TestClients(unittest.TestCase):
+
+    @mock.patch('auth0.v3.management.email_templates.RestClient')
+    def test_create(self, mock_rc):
+        mock_instance = mock_rc.return_value
+
+        c = EmailTemplates(domain='domain', token='jwttoken')
+        c.create({'a': 'b', 'c': 'd'})
+
+        mock_instance.post.assert_called_with(
+            'https://domain/api/v2/email-templates',
+            data={'a': 'b', 'c': 'd'}
+        )
+
+    @mock.patch('auth0.v3.management.email_templates.RestClient')
+    def test_get(self, mock_rc):
+        mock_instance = mock_rc.return_value
+
+        c = EmailTemplates(domain='domain', token='jwttoken')
+        c.get('this-template-name')
+
+        mock_instance.get.assert_called_with(
+            'https://domain/api/v2/email-templates/this-template-name'
+        )
+
+    @mock.patch('auth0.v3.management.email_templates.RestClient')
+    def test_update(self, mock_rc):
+        mock_instance = mock_rc.return_value
+
+        c = EmailTemplates(domain='domain', token='jwttoken')
+        c.update('this-template-name', {'a': 'b', 'c': 'd'})
+
+        mock_instance.patch.assert_called_with(
+            'https://domain/api/v2/email-templates/this-template-name',
+            data={'a': 'b', 'c': 'd'}
+        )