Skip to content

Commit

Permalink
Make connection_type for list_connections accept enum values (#112)
Browse files Browse the repository at this point in the history
* Coerce `connection_type` to enum

* Add TODO

* Update test

* Emit a deprecation warning
  • Loading branch information
maxdeviant committed Feb 24, 2022
1 parent 603da60 commit 47cd11c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
36 changes: 36 additions & 0 deletions tests/test_sso.py
Expand Up @@ -354,6 +354,42 @@ def test_list_connections(

assert connections_response["data"] == mock_connections["data"]

def test_list_connections_with_connection_type_as_invalid_string(
self, setup_with_client_id, mock_connections, mock_request_method
):
mock_request_method("get", mock_connections, 200)

with pytest.raises(
ValueError, match="'connection_type' must be a member of ConnectionType"
):
self.sso.list_connections(connection_type="UnknownSAML")

def test_list_connections_with_connection_type_as_string(
self, setup_with_client_id, mock_connections, capture_and_mock_request
):
request_args, request_kwargs = capture_and_mock_request(
"get", mock_connections, 200
)

connections_response = self.sso.list_connections(connection_type="GenericSAML")

request_params = request_kwargs["params"]
assert request_params["connection_type"] == "GenericSAML"

def test_list_connections_with_connection_type_as_enum(
self, setup_with_client_id, mock_connections, capture_and_mock_request
):
request_args, request_kwargs = capture_and_mock_request(
"get", mock_connections, 200
)

connections_response = self.sso.list_connections(
connection_type=ConnectionType.OktaSAML
)

request_params = request_kwargs["params"]
assert request_params["connection_type"] == "OktaSAML"

def test_delete_connection(self, setup_with_client_id, mock_raw_request_method):
mock_raw_request_method(
"delete",
Expand Down
21 changes: 20 additions & 1 deletion workos/sso.py
Expand Up @@ -194,8 +194,27 @@ def list_connections(
Returns:
dict: Connections response from WorkOS.
"""

# This method used to accept `connection_type` as a string, so we try
# to convert strings to a `ConnectionType` to support existing callers.
#
# TODO: Remove support for string values of `ConnectionType` in the next
# major version.
if connection_type is not None and isinstance(connection_type, str):
try:
connection_type = ConnectionType[connection_type]

warn(
"Passing a string value as the 'connection_type' parameter for 'list_connections' is deprecated and will be removed in the next major version. Please pass a 'ConnectionType' instead.",
DeprecationWarning,
)
except KeyError:
raise ValueError("'connection_type' must be a member of ConnectionType")

params = {
"connection_type": connection_type,
"connection_type": (
connection_type.value if connection_type is not None else None
),
"domain": domain,
"organization_id": organization_id,
"limit": limit,
Expand Down

0 comments on commit 47cd11c

Please sign in to comment.