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

Skip to content

Commit 36d0b37

Browse files
committed
add CRUD for organization connections
1 parent aa563d8 commit 36d0b37

File tree

2 files changed

+157
-2
lines changed

2 files changed

+157
-2
lines changed

auth0/v3/management/organizations.py

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ 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):
25+
def _url(self, id=None, path=None, secondary_id=None):
2626
url = 'https://{}/api/v2/organizations'.format(self.domain)
2727
if id is not None:
28-
return '{}/{}'.format(url, id)
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)
2933
return url
3034

35+
# Organizations
3136
def all_organizations(self, page=None, per_page=None):
3237
"""Retrieves a list of all the organizations.
3338
@@ -93,3 +98,80 @@ def delete_organization(self, id):
9398
"""
9499

95100
return self.client.delete(self._url(id))
101+
102+
103+
# Organization Connections
104+
def all_organization_connections(self, id, page=None, per_page=None):
105+
"""Retrieves a list of all the organization connections.
106+
107+
Args:
108+
id (str): the ID of the organization.
109+
110+
page (int): The result's page number (zero based). When not set,
111+
the default value is up to the server.
112+
113+
per_page (int, optional): The amount of entries per page. When not set,
114+
the default value is up to the server.
115+
116+
See: https://auth0.com/docs/api/management/v2#!/Clients/get_clients
117+
"""
118+
params = {}
119+
params['page'] = page
120+
params['per_page'] = per_page
121+
122+
return self.client.get(self._url(id, 'enabled_connections'), params=params)
123+
124+
def get_organization_connection(self, id, connection_id):
125+
"""Retrieves an organization connection by its ID.
126+
127+
Args:
128+
id (str): the ID of the organization.
129+
130+
connection_id (str): the ID of the connection.
131+
132+
See: https://auth0.com/docs/api/management/v2#!/Clients/get_clients
133+
"""
134+
params = {}
135+
136+
return self.client.get(self._url(id, 'enabled_connections', connection_id), params=params)
137+
138+
def create_organization_connection(self, id, body):
139+
"""Adds a connection to an organization.
140+
141+
Args:
142+
id (str): the ID of the organization.
143+
144+
body (dict): Attributes for the connection to add.
145+
146+
See: https://auth0.com/docs/api/v2#!/Clients/post_clients
147+
"""
148+
149+
return self.client.post(self._url(id, 'enabled_connections'), data=body)
150+
151+
def update_organization_connection(self, id, connection_id, body):
152+
"""Modifies an organization.
153+
154+
Args:
155+
id (str): the ID of the organization.
156+
157+
connection_id (str): the ID of the connection to update.
158+
159+
body (dict): Attributes to modify.
160+
161+
See: https://auth0.com/docs/api/management/v2#!/Clients/patch_clients_by_id
162+
"""
163+
164+
return self.client.patch(self._url(id, 'enabled_connections', connection_id), data=body)
165+
166+
def delete_organization_connection(self, id, connection_id):
167+
"""Deletes a connection from the given organization.
168+
169+
Args:
170+
id (str): Id of organization.
171+
172+
connection_id (str): the ID of the connection to delete.
173+
174+
See: https://auth0.com/docs/api/management/v2#!/Clients/delete_clients_by_id
175+
"""
176+
177+
return self.client.delete(self._url(id, 'enabled_connections', connection_id))

auth0/v3/test/management/test_organizations.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def test_init_with_optionals(self):
1111
telemetry_header = t.client.base_headers.get('Auth0-Client', None)
1212
self.assertEqual(telemetry_header, None)
1313

14+
# Organizations
1415
@mock.patch('auth0.v3.management.organizations.RestClient')
1516
def test_all_organizations(self, mock_rc):
1617
mock_instance = mock_rc.return_value
@@ -80,4 +81,76 @@ def test_delete_organization(self, mock_rc):
8081

8182
mock_instance.delete.assert_called_with(
8283
'https://domain/api/v2/organizations/this-id'
84+
)
85+
86+
# Organization Connections
87+
@mock.patch('auth0.v3.management.organizations.RestClient')
88+
def test_all_organization_connections(self, mock_rc):
89+
mock_instance = mock_rc.return_value
90+
91+
c = Organizations(domain='domain', token='jwttoken')
92+
93+
# Default parameters are requested
94+
c.all_organization_connections('test-org')
95+
96+
args, kwargs = mock_instance.get.call_args
97+
98+
self.assertEqual('https://domain/api/v2/organizations/test-org/enabled_connections', args[0])
99+
self.assertEqual(kwargs['params'], {'page': None,
100+
'per_page': None})
101+
102+
# Specific pagination
103+
c.all_organization_connections('test-org', page=7, per_page=25)
104+
105+
args, kwargs = mock_instance.get.call_args
106+
107+
self.assertEqual('https://domain/api/v2/organizations/test-org/enabled_connections', args[0])
108+
self.assertEqual(kwargs['params'], {'page': 7,
109+
'per_page': 25})
110+
111+
@mock.patch('auth0.v3.management.organizations.RestClient')
112+
def test_get_organization_connection(self, mock_rc):
113+
mock_instance = mock_rc.return_value
114+
115+
c = Organizations(domain='domain', token='jwttoken')
116+
c.get_organization_connection('test-org', 'test-con')
117+
118+
args, kwargs = mock_instance.get.call_args
119+
120+
self.assertEqual('https://domain/api/v2/organizations/test-org/enabled_connections/test-con', args[0])
121+
self.assertEqual(kwargs['params'], {})
122+
123+
@mock.patch('auth0.v3.management.organizations.RestClient')
124+
def test_create_organization_connection(self, mock_rc):
125+
mock_instance = mock_rc.return_value
126+
127+
c = Organizations(domain='domain', token='jwttoken')
128+
c.create_organization_connection('test-org', {'a': 'b', 'c': 'd'})
129+
130+
mock_instance.post.assert_called_with(
131+
'https://domain/api/v2/organizations/test-org/enabled_connections',
132+
data={'a': 'b', 'c': 'd'}
133+
)
134+
135+
@mock.patch('auth0.v3.management.organizations.RestClient')
136+
def test_update_organization_connection(self, mock_rc):
137+
mock_instance = mock_rc.return_value
138+
139+
c = Organizations(domain='domain', token='jwttoken')
140+
c.update_organization_connection('test-org', 'test-con', {'a': 'b', 'c': 'd'})
141+
142+
args, kwargs = mock_instance.patch.call_args
143+
144+
self.assertEqual('https://domain/api/v2/organizations/test-org/enabled_connections/test-con', args[0])
145+
self.assertEqual(kwargs['data'], {'a': 'b', 'c': 'd'})
146+
147+
@mock.patch('auth0.v3.management.organizations.RestClient')
148+
def test_delete_organization_connection(self, mock_rc):
149+
mock_instance = mock_rc.return_value
150+
151+
c = Organizations(domain='domain', token='jwttoken')
152+
c.delete_organization_connection('test-org', 'test-con')
153+
154+
mock_instance.delete.assert_called_with(
155+
'https://domain/api/v2/organizations/test-org/enabled_connections/test-con'
83156
)

0 commit comments

Comments
 (0)