From 4b16ce69d558d0979172aaf1c822cf3ae73c9823 Mon Sep 17 00:00:00 2001 From: Taylor Hodge Date: Tue, 26 Jan 2021 15:31:06 -0500 Subject: [PATCH 1/4] add get connections --- tests/test_sso.py | 28 ++++++++++++++++++++++++++++ workos/sso.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/tests/test_sso.py b/tests/test_sso.py index 47537739..b1791fc3 100644 --- a/tests/test_sso.py +++ b/tests/test_sso.py @@ -1,4 +1,5 @@ import json +from requests import Response from six.moves.urllib.parse import parse_qsl, urlparse import pytest @@ -60,6 +61,24 @@ def mock_connection(self): ], } + @pytest.fixture + def mock_connections(self): + return { + "data": [ + { + "id": "connection_id", + "external_key": "key", + "state": "linked", + "type": "gsuite directory", + "name": "Foo Corp", + "bearer_token": None, + "project_id": "project_id", + "domain": "example.com", + } + ], + "listMetadata": {"before": None, "after": None}, + } + def test_authorization_url_throws_value_error_with_missing_domain_and_provider( self, ): @@ -177,3 +196,12 @@ def test_create_connection(self, mock_request_method, mock_connection): connection = self.sso.create_connection("draft_conn_id") assert connection == response_dict + + def test_list_connections(self, mock_connections, mock_request_method): + mock_response = Response() + mock_response.status_code = 200 + mock_response.response_dict = mock_connections + mock_request_method("get", mock_response, 200) + response = self.sso.list_connections() + assert response.status_code == 200 + assert response.response_dict == mock_connections diff --git a/workos/sso.py b/workos/sso.py index a6cca1e4..6e23f83a 100644 --- a/workos/sso.py +++ b/workos/sso.py @@ -7,7 +7,7 @@ from workos.exceptions import ConfigurationException from workos.resources.sso import WorkOSProfile from workos.utils.connection_types import ConnectionType -from workos.utils.request import RequestHelper, RESPONSE_TYPE_CODE, REQUEST_METHOD_POST +from workos.utils.request import RequestHelper, RESPONSE_TYPE_CODE, REQUEST_METHOD_GET, REQUEST_METHOD_POST from workos.utils.validation import SSO_MODULE, validate_settings AUTHORIZATION_PATH = "sso/authorize" @@ -17,6 +17,8 @@ OAUTH_GRANT_TYPE = "authorization_code" +RESPONSE_LIMIT = 10 + class SSO(object): """Offers methods to assist in authenticating through the WorkOS SSO service.""" @@ -157,3 +159,32 @@ def create_connection(self, source): params=params, token=workos.api_key, ) + + def list_connections( + self, connection_type=None, domain=None, limit=RESPONSE_LIMIT, before=None, after=None + ): + """Gets details for existing Connections. + + Args: + connection_type (ConnectionType): Authentication service provider descriptor. (Optional) + domain (str): Domain of a Directory. (Optional) + limit (int): Maximum number of records to return. (Optional) + before (str): Pagination cursor to receive records before a provided Directory ID. (Optional) + after (str): Pagination cursor to receive records after a provided Directory ID. (Optional) + + Returns: + dict: Connections response from WorkOS. + """ + params = { + "connetion_type": connection_type, + "domain": domain, + "limit": limit, + "before": before, + "after": after, + } + return self.request_helper.request( + "connections", + method=REQUEST_METHOD_GET, + params=params, + token=workos.api_key, + ) From 34112ce91497bd75126faf7156aa9403d1f6bfe5 Mon Sep 17 00:00:00 2001 From: Taylor Hodge Date: Tue, 26 Jan 2021 15:38:03 -0500 Subject: [PATCH 2/4] format --- workos/sso.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/workos/sso.py b/workos/sso.py index 6e23f83a..9770ef65 100644 --- a/workos/sso.py +++ b/workos/sso.py @@ -7,7 +7,12 @@ from workos.exceptions import ConfigurationException from workos.resources.sso import WorkOSProfile from workos.utils.connection_types import ConnectionType -from workos.utils.request import RequestHelper, RESPONSE_TYPE_CODE, REQUEST_METHOD_GET, REQUEST_METHOD_POST +from workos.utils.request import ( + RequestHelper, + RESPONSE_TYPE_CODE, + REQUEST_METHOD_GET, + REQUEST_METHOD_POST, +) from workos.utils.validation import SSO_MODULE, validate_settings AUTHORIZATION_PATH = "sso/authorize" @@ -161,7 +166,12 @@ def create_connection(self, source): ) def list_connections( - self, connection_type=None, domain=None, limit=RESPONSE_LIMIT, before=None, after=None + self, + connection_type=None, + domain=None, + limit=RESPONSE_LIMIT, + before=None, + after=None, ): """Gets details for existing Connections. From 92c850362208624fd013911e0213a7a557ee846c Mon Sep 17 00:00:00 2001 From: Taylor Hodge Date: Tue, 26 Jan 2021 15:39:31 -0500 Subject: [PATCH 3/4] format 2: the formattening --- tests/test_sso.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sso.py b/tests/test_sso.py index b1791fc3..d5904864 100644 --- a/tests/test_sso.py +++ b/tests/test_sso.py @@ -196,7 +196,7 @@ def test_create_connection(self, mock_request_method, mock_connection): connection = self.sso.create_connection("draft_conn_id") assert connection == response_dict - + def test_list_connections(self, mock_connections, mock_request_method): mock_response = Response() mock_response.status_code = 200 From a1744e9701a31872b233fe33441bbc5364b86156 Mon Sep 17 00:00:00 2001 From: Taylor Hodge Date: Tue, 26 Jan 2021 17:33:57 -0500 Subject: [PATCH 4/4] fix --- tests/test_sso.py | 27 +++++++++++++++++++-------- workos/sso.py | 6 +++--- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/tests/test_sso.py b/tests/test_sso.py index d5904864..7fd62921 100644 --- a/tests/test_sso.py +++ b/tests/test_sso.py @@ -66,14 +66,25 @@ def mock_connections(self): return { "data": [ { - "id": "connection_id", - "external_key": "key", - "state": "linked", - "type": "gsuite directory", - "name": "Foo Corp", - "bearer_token": None, - "project_id": "project_id", - "domain": "example.com", + "object": "connection", + "id": "conn_id", + "status": "linked", + "name": "Google OAuth 2.0", + "connection_type": "GoogleOAuth", + "oauth_uid": "oauth-uid.apps.googleusercontent.com", + "oauth_secret": "oauth-secret", + "oauth_redirect_uri": "https://auth.workos.com/sso/oauth/google/chicken/callback", + "saml_entity_id": None, + "saml_idp_url": None, + "saml_relying_party_trust_cert": None, + "saml_x509_certs": None, + "domains": [ + { + "object": "connection_domain", + "id": "domain_id", + "domain": "terrace-house.com", + }, + ], } ], "listMetadata": {"before": None, "after": None}, diff --git a/workos/sso.py b/workos/sso.py index 9770ef65..b97dd2cc 100644 --- a/workos/sso.py +++ b/workos/sso.py @@ -177,10 +177,10 @@ def list_connections( Args: connection_type (ConnectionType): Authentication service provider descriptor. (Optional) - domain (str): Domain of a Directory. (Optional) + domain (str): Domain of a Connection. (Optional) limit (int): Maximum number of records to return. (Optional) - before (str): Pagination cursor to receive records before a provided Directory ID. (Optional) - after (str): Pagination cursor to receive records after a provided Directory ID. (Optional) + before (str): Pagination cursor to receive records before a provided Connection ID. (Optional) + after (str): Pagination cursor to receive records after a provided Connection ID. (Optional) Returns: dict: Connections response from WorkOS.