Skip to content

Commit

Permalink
Add typing and async support for SSO.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgd committed Jul 25, 2024
1 parent 1ded444 commit 55dbdaf
Show file tree
Hide file tree
Showing 15 changed files with 659 additions and 730 deletions.
41 changes: 37 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Mapping, Union
from typing import Mapping, Optional, Union
from unittest.mock import AsyncMock, MagicMock

import httpx
Expand Down Expand Up @@ -128,9 +128,9 @@ 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: Mapping[str, str] = None,
headers: Optional[Mapping[str, str]] = None,
):
mock_class = (
AsyncMock if isinstance(http_client, AsyncHTTPClient) else MagicMock
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 All @@ -153,7 +186,7 @@ def inner(
http_client: Union[SyncHTTPClient, AsyncHTTPClient],
data_list: list,
status_code: int = 200,
headers: Mapping[str, str] = None,
headers: Optional[Mapping[str, str]] = None,
):
# For convenient index lookup, store the list of object IDs.
data_ids = list(map(lambda x: x["id"], data_list))
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 @@ -197,7 +197,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 @@ -432,7 +431,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 55dbdaf

Please sign in to comment.