Skip to content

Commit dd935ec

Browse files
committed
add CRUD for organization member roles
1 parent 8b29825 commit dd935ec

2 files changed

Lines changed: 108 additions & 9 deletions

File tree

auth0/v3/management/organizations.py

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ def __init__(self, domain, token, telemetry=True, timeout=5.0):
2222
self.domain = domain
2323
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)
2424

25-
def _url(self, id=None, path=None, secondary_id=None):
25+
def _url(self, *args):
2626
url = 'https://{}/api/v2/organizations'.format(self.domain)
27-
if id is not None:
28-
url = '{}/{}'.format(url, id)
29-
if path is not None:
30-
url = '{}/{}'.format(url, path)
31-
if secondary_id is not None:
32-
url = '{}/{}'.format(url, secondary_id)
27+
for p in args:
28+
if p is not None:
29+
url = '{}/{}'.format(url, p)
3330
return url
3431

3532
# Organizations
@@ -221,4 +218,57 @@ def delete_organization_members(self, id, body):
221218
See: https://auth0.com/docs/api/management/v2#!/Clients/delete_clients_by_id
222219
"""
223220

224-
return self.client.delete(self._url(id, 'members'), data=body)
221+
return self.client.delete(self._url(id, 'members'), data=body)
222+
223+
# Organization Member Roles
224+
def all_organization_member_roles(self, id, user_id, page=None, per_page=None):
225+
"""Retrieves a list of all the roles from the given organization member.
226+
227+
Args:
228+
id (str): the ID of the organization.
229+
230+
user_id (str): the ID of the user member of the organization.
231+
232+
page (int): The result's page number (zero based). When not set,
233+
the default value is up to the server.
234+
235+
per_page (int, optional): The amount of entries per page. When not set,
236+
the default value is up to the server.
237+
238+
See: https://auth0.com/docs/api/management/v2#!/Clients/get_clients
239+
"""
240+
params = {}
241+
params['page'] = page
242+
params['per_page'] = per_page
243+
244+
return self.client.get(self._url(id, 'members', user_id, 'roles'), params=params)
245+
246+
def create_organization_member_roles(self, id, user_id, body):
247+
"""Adds roles to a member of an organization.
248+
249+
Args:
250+
id (str): the ID of the organization.
251+
252+
user_id (str): the ID of the user member of the organization.
253+
254+
body (dict): Attributes from the members to add.
255+
256+
See: https://auth0.com/docs/api/v2#!/Clients/post_clients
257+
"""
258+
259+
return self.client.post(self._url(id, 'members', user_id, 'roles'), data=body)
260+
261+
def delete_organization_member_roles(self, id, user_id, body):
262+
"""Deletes roles from a member of an organization.
263+
264+
Args:
265+
id (str): Id of organization.
266+
267+
user_id (str): the ID of the user member of the organization.
268+
269+
body (dict): Attributes from the members to delete
270+
271+
See: https://auth0.com/docs/api/management/v2#!/Clients/delete_clients_by_id
272+
"""
273+
274+
return self.client.delete(self._url(id, 'members', user_id, 'roles'), data=body)

auth0/v3/test/management/test_organizations.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_delete_organization_connection(self, mock_rc):
155155
'https://domain/api/v2/organizations/test-org/enabled_connections/test-con'
156156
)
157157

158-
# Organization Connections
158+
# Organization Members
159159
@mock.patch('auth0.v3.management.organizations.RestClient')
160160
def test_all_organization_members(self, mock_rc):
161161
mock_instance = mock_rc.return_value
@@ -202,4 +202,53 @@ def test_delete_organization_members(self, mock_rc):
202202
mock_instance.delete.assert_called_with(
203203
'https://domain/api/v2/organizations/test-org/members',
204204
data={'a': 'b', 'c': 'd'}
205+
)
206+
207+
# Organization Member Roles
208+
@mock.patch('auth0.v3.management.organizations.RestClient')
209+
def test_all_organization_member_roles(self, mock_rc):
210+
mock_instance = mock_rc.return_value
211+
212+
c = Organizations(domain='domain', token='jwttoken')
213+
214+
# Default parameters are requested
215+
c.all_organization_member_roles('test-org', 'test-user')
216+
217+
args, kwargs = mock_instance.get.call_args
218+
219+
self.assertEqual('https://domain/api/v2/organizations/test-org/members/test-user/roles', args[0])
220+
self.assertEqual(kwargs['params'], {'page': None,
221+
'per_page': None})
222+
223+
# Specific pagination
224+
c.all_organization_member_roles('test-org', 'test-user', page=7, per_page=25)
225+
226+
args, kwargs = mock_instance.get.call_args
227+
228+
self.assertEqual('https://domain/api/v2/organizations/test-org/members/test-user/roles', args[0])
229+
self.assertEqual(kwargs['params'], {'page': 7,
230+
'per_page': 25})
231+
232+
@mock.patch('auth0.v3.management.organizations.RestClient')
233+
def test_create_organization_member_roles(self, mock_rc):
234+
mock_instance = mock_rc.return_value
235+
236+
c = Organizations(domain='domain', token='jwttoken')
237+
c.create_organization_member_roles('test-org', 'test-user', {'a': 'b', 'c': 'd'})
238+
239+
mock_instance.post.assert_called_with(
240+
'https://domain/api/v2/organizations/test-org/members/test-user/roles',
241+
data={'a': 'b', 'c': 'd'}
242+
)
243+
244+
@mock.patch('auth0.v3.management.organizations.RestClient')
245+
def test_delete_organization_member_roles(self, mock_rc):
246+
mock_instance = mock_rc.return_value
247+
248+
c = Organizations(domain='domain', token='jwttoken')
249+
c.delete_organization_member_roles('test-org', 'test-user', {'a': 'b', 'c': 'd'})
250+
251+
mock_instance.delete.assert_called_with(
252+
'https://domain/api/v2/organizations/test-org/members/test-user/roles',
253+
data={'a': 'b', 'c': 'd'}
205254
)

0 commit comments

Comments
 (0)