Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3193799

Browse files
committed
add CRUD for organizations and tests
1 parent 110f4ba commit 3193799

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

auth0/v3/management/organizations.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from .rest import RestClient
2+
3+
4+
class Organizations(object):
5+
"""Auth0 organizations endpoints
6+
7+
Args:
8+
domain (str): Your Auth0 domain, e.g: 'username.auth0.com'
9+
10+
token (str): Management API v2 Token
11+
12+
telemetry (bool, optional): Enable or disable Telemetry
13+
(defaults to True)
14+
15+
timeout (float or tuple, optional): Change the requests
16+
connect and read timeout. Pass a tuple to specify
17+
both values separately or a float to set both to it.
18+
(defaults to 5.0 for both)
19+
"""
20+
21+
def __init__(self, domain, token, telemetry=True, timeout=5.0):
22+
self.domain = domain
23+
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout)
24+
25+
def _url(self, id=None):
26+
url = 'https://{}/api/v2/organizations'.format(self.domain)
27+
if id is not None:
28+
return '{}/{}'.format(url, id)
29+
return url
30+
31+
def all_organizations(self, page=None, per_page=None):
32+
"""Retrieves a list of all the organizations.
33+
34+
Args:
35+
page (int): The result's page number (zero based). When not set,
36+
the default value is up to the server.
37+
38+
per_page (int, optional): The amount of entries per page. When not set,
39+
the default value is up to the server.
40+
41+
See: https://auth0.com/docs/api/management/v2#!/Clients/get_clients
42+
"""
43+
params = {}
44+
params['page'] = page
45+
params['per_page'] = per_page
46+
47+
return self.client.get(self._url(), params=params)
48+
49+
def get_organization_by_name(self, name=None):
50+
"""Retrieves an organization given its name.
51+
52+
Args:
53+
name (str): The name of the organization to retrieve.
54+
55+
See: https://auth0.com/docs/api/management/v2#!/Clients/get_clients
56+
"""
57+
params = {}
58+
params['name'] = name
59+
60+
return self.client.get(self._url(), params=params)
61+
62+
def create_organization(self, body):
63+
"""Create a new organization.
64+
65+
Args:
66+
body (dict): Attributes for the new organization.
67+
68+
See: https://auth0.com/docs/api/v2#!/Clients/post_clients
69+
"""
70+
71+
return self.client.post(self._url(), data=body)
72+
73+
def update_organization(self, id, body):
74+
"""Modifies an organization.
75+
76+
Args:
77+
id (str): the ID of the organization.
78+
79+
body (dict): Attributes to modify.
80+
81+
See: https://auth0.com/docs/api/management/v2#!/Clients/patch_clients_by_id
82+
"""
83+
84+
return self.client.patch(self._url(id), data=body)
85+
86+
def delete_organization(self, id):
87+
"""Deletes an organization and all its related assets.
88+
89+
Args:
90+
id (str): Id of organization to delete.
91+
92+
See: https://auth0.com/docs/api/management/v2#!/Clients/delete_clients_by_id
93+
"""
94+
95+
return self.client.delete(self._url(id))
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import unittest
2+
import mock
3+
from ...management.organizations import Organizations
4+
5+
6+
class TestOrganizations(unittest.TestCase):
7+
8+
def test_init_with_optionals(self):
9+
t = Organizations(domain='domain', token='jwttoken', telemetry=False, timeout=(10, 2))
10+
self.assertEqual(t.client.timeout, (10, 2))
11+
telemetry_header = t.client.base_headers.get('Auth0-Client', None)
12+
self.assertEqual(telemetry_header, None)
13+
14+
@mock.patch('auth0.v3.management.organizations.RestClient')
15+
def test_all_organizations(self, mock_rc):
16+
mock_instance = mock_rc.return_value
17+
18+
c = Organizations(domain='domain', token='jwttoken')
19+
20+
# Default parameters are requested
21+
c.all_organizations()
22+
23+
args, kwargs = mock_instance.get.call_args
24+
25+
self.assertEqual('https://domain/api/v2/organizations', args[0])
26+
self.assertEqual(kwargs['params'], {'page': None,
27+
'per_page': None})
28+
29+
# Specific pagination
30+
c.all_organizations(page=7, per_page=25)
31+
32+
args, kwargs = mock_instance.get.call_args
33+
34+
self.assertEqual('https://domain/api/v2/organizations', args[0])
35+
self.assertEqual(kwargs['params'], {'page': 7,
36+
'per_page': 25})
37+
38+
@mock.patch('auth0.v3.management.organizations.RestClient')
39+
def test_get_organization_by_name(self, mock_rc):
40+
mock_instance = mock_rc.return_value
41+
42+
c = Organizations(domain='domain', token='jwttoken')
43+
c.get_organization_by_name('test-org')
44+
45+
args, kwargs = mock_instance.get.call_args
46+
47+
self.assertEqual('https://domain/api/v2/organizations', args[0])
48+
self.assertEqual(kwargs['params'], {'name': 'test-org'})
49+
50+
@mock.patch('auth0.v3.management.organizations.RestClient')
51+
def test_create_organization(self, mock_rc):
52+
mock_instance = mock_rc.return_value
53+
54+
c = Organizations(domain='domain', token='jwttoken')
55+
c.create_organization({'a': 'b', 'c': 'd'})
56+
57+
mock_instance.post.assert_called_with(
58+
'https://domain/api/v2/organizations',
59+
data={'a': 'b', 'c': 'd'}
60+
)
61+
62+
@mock.patch('auth0.v3.management.organizations.RestClient')
63+
def test_update(self, mock_rc):
64+
mock_instance = mock_rc.return_value
65+
66+
c = Organizations(domain='domain', token='jwttoken')
67+
c.update('this-id', {'a': 'b', 'c': 'd'})
68+
69+
args, kwargs = mock_instance.patch.call_args
70+
71+
self.assertEqual('https://domain/api/v2/organizations/this-id', args[0])
72+
self.assertEqual(kwargs['data'], {'a': 'b', 'c': 'd'})
73+
74+
@mock.patch('auth0.v3.management.organizations.RestClient')
75+
def test_delete_organization(self, mock_rc):
76+
mock_instance = mock_rc.return_value
77+
78+
c = Organizations(domain='domain', token='jwttoken')
79+
c.delete_organization('this-id')
80+
81+
mock_instance.delete.assert_called_with(
82+
'https://domain/api/v2/organizations/this-id'
83+
)

0 commit comments

Comments
 (0)