Skip to content

Commit

Permalink
Add typing and async support for SSO (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgd authored Jul 25, 2024
1 parent f2ecb38 commit 2894241
Show file tree
Hide file tree
Showing 19 changed files with 677 additions and 772 deletions.
35 changes: 34 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def mock(*args, **kwargs):
def mock_http_client_with_response(monkeypatch):
def inner(
http_client: Union[SyncHTTPClient, AsyncHTTPClient],
response_dict: dict,
response_dict: Optional[dict] = None,
status_code: int = 200,
headers: Optional[Mapping[str, str]] = None,
):
Expand All @@ -145,6 +145,39 @@ def inner(
return inner


@pytest.fixture
def capture_and_mock_http_client_request(monkeypatch):
def inner(
http_client: Union[SyncHTTPClient, AsyncHTTPClient],
response_dict: dict,
status_code: int = 200,
headers: Optional[Mapping[str, str]] = None,
):
request_args = []
request_kwargs = {}

def capture_and_mock(*args, **kwargs):
request_args.extend(args)
request_kwargs.update(kwargs)

return httpx.Response(
status_code=status_code,
headers=headers,
json=response_dict,
)

mock_class = (
AsyncMock if isinstance(http_client, AsyncHTTPClient) else MagicMock
)
mock = mock_class(side_effect=capture_and_mock)

monkeypatch.setattr(http_client._client, "request", mock)

return (request_args, request_kwargs)

return inner


@pytest.fixture
def mock_pagination_request_for_http_client(monkeypatch):
# Mocking pagination correctly requires us to index into a list of data
Expand Down
35 changes: 35 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,38 @@ def test_initialize_events_missing_api_key(self):
message = str(ex)

assert "api_key" in message

def test_initialize_sso(self, set_api_key_and_client_id):
assert bool(async_client.sso)

def test_initialize_sso_missing_api_key(self, set_client_id):
with pytest.raises(ConfigurationException) as ex:
async_client.sso

message = str(ex)

assert "api_key" in message
assert "client_id" not in message

def test_initialize_sso_missing_client_id(self, set_api_key):
with pytest.raises(ConfigurationException) as ex:
async_client.sso

message = str(ex)

assert "client_id" in message
assert "api_key" not in message

def test_initialize_sso_missing_api_key_and_client_id(self):
with pytest.raises(ConfigurationException) as ex:
async_client.sso

message = str(ex)

assert all(
setting in message
for setting in (
"api_key",
"client_id",
)
)
2 changes: 0 additions & 2 deletions tests/test_directory_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ def test_delete_directory(self, mock_http_client_with_response):
mock_http_client_with_response(
http_client=self.http_client,
status_code=202,
response_dict=None,
headers={"content-type": "text/plain; charset=utf-8"},
)

Expand Down Expand Up @@ -450,7 +449,6 @@ async def test_delete_directory(self, mock_http_client_with_response):
mock_http_client_with_response(
http_client=self.http_client,
status_code=202,
response_dict=None,
headers={"content-type": "text/plain; charset=utf-8"},
)

Expand Down
5 changes: 1 addition & 4 deletions tests/test_organizations.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import datetime
from typing import Dict, List, Union, cast

import pytest
import requests

from tests.conftest import MockResponse
from tests.utils.list_resource import list_data_to_dicts, list_response_of
from tests.utils.list_resource import list_data_to_dicts
from workos.organizations import Organizations
from tests.utils.fixtures.mock_organization import MockOrganization

Expand Down
Loading

0 comments on commit 2894241

Please sign in to comment.