Repository: auth0/auth0-python
Pull request #190 (2019-04-18 02:19:07)

Add user roles API
### Changes

Adds the roles API. I will add the permissions in another PR.

### Testing
Try it with an existing org with a user

- [x] This change adds unit test coverage



Patch:
diff --git a/auth0/v3/management/users.py b/auth0/v3/management/users.py
index 396d73c..71fb4a9 100644
--- a/auth0/v3/management/users.py
+++ b/auth0/v3/management/users.py
@@ -126,6 +126,45 @@ class Users(object):
         """
         return self.client.patch(self._url(id), data=body)
 
+    def get_roles(self, id):
+        """Get a user's roles.
+
+        Args:
+            id (str): The user's id.
+
+        See https://auth0.com/docs/api/management/v2#!/Users/get_user_roles
+        """
+        url = self._url('{}/roles'.format(id))
+        return self.client.get(url)
+
+    def delete_roles(self, id, roles):
+        """Removes roles from a user.
+
+        Args:
+            id (str): The user's id.
+
+            roles (list of str): A list of roles ids to unassociate from the user.
+
+        See https://auth0.com/docs/api/management/v2#!/Users/delete_user_roles
+        """
+        url = self._url('{}/roles'.format(id))
+        body = {'roles': roles}
+        return self.client.delete(url, data=body)
+
+    def assign_roles(self, id, roles):
+        """Assign roles to a user
+
+        Args:
+            id (str): The user's id.
+
+            roles (list of str): A list of roles ids to associated with the user.
+
+        See https://auth0.com/docs/api/management/v2#!/Users/post_user_roles
+        """
+        url = self._url('{}/roles'.format(id))
+        body = {'roles': roles}
+        return self.client.post(url, data=body)
+
     def delete_multifactor(self, id, provider):
         """Delete a user's multifactor provider.
 


Test patch:
diff --git a/auth0/v3/test/management/test_users.py b/auth0/v3/test/management/test_users.py
index 36a5757..8e33efb 100644
--- a/auth0/v3/test/management/test_users.py
+++ b/auth0/v3/test/management/test_users.py
@@ -113,6 +113,43 @@ class TestUsers(unittest.TestCase):
         self.assertEqual('https://domain/api/v2/users/an-id', args[0])
         self.assertEqual(kwargs['data'], {'a': 'b', 'c': 'd'})
 
+    @mock.patch('auth0.v3.management.users.RestClient')
+    def test_get_roles(self, mock_rc):
+        mock_instance = mock_rc.return_value
+
+        u = Users(domain='domain', token='jwttoken')
+        u.get_roles('an-id')
+
+        mock_instance.get.assert_called_with(
+            'https://domain/api/v2/users/an-id/roles'
+        )
+
+    @mock.patch('auth0.v3.management.users.RestClient')
+    def test_delete_roles(self, mock_rc):
+        mock_instance = mock_rc.return_value
+
+        u = Users(domain='domain', token='jwttoken')
+        u.delete_roles('an-id', ['a', 'b'])
+
+        args, kwargs = mock_instance.delete.call_args
+
+        self.assertEqual('https://domain/api/v2/users/an-id/roles',
+                         args[0])
+        self.assertEqual(kwargs['data'], {'roles': ['a', 'b']})
+
+    @mock.patch('auth0.v3.management.users.RestClient')
+    def test_assign_roles(self, mock_rc):
+        mock_instance = mock_rc.return_value
+
+        u = Users(domain='domain', token='jwttoken')
+        u.assign_roles('an-id', ['a', 'b'])
+
+        args, kwargs = mock_instance.post.call_args
+
+        self.assertEqual('https://domain/api/v2/users/an-id/roles',
+                         args[0])
+        self.assertEqual(kwargs['data'], {'roles': ['a', 'b']})
+
     @mock.patch('auth0.v3.management.users.RestClient')
     def test_delete_multifactor(self, mock_rc):
         mock_instance = mock_rc.return_value