|
1 |
| -import pytest |
2 |
| -import requests |
3 |
| - |
4 |
| -import workos |
5 |
| - |
6 |
| - |
7 |
| -class MockResponse(object): |
8 |
| - def __init__(self, response_dict, status_code, headers=None): |
9 |
| - self.response_dict = response_dict |
10 |
| - self.status_code = status_code |
11 |
| - self.headers = {} if headers is None else headers |
12 |
| - |
13 |
| - if "content-type" not in self.headers: |
14 |
| - self.headers["content-type"] = "application/json" |
15 |
| - |
16 |
| - def json(self): |
17 |
| - return self.response_dict |
| 1 | +from typing import Any, Callable, Mapping, Optional |
| 2 | +from unittest.mock import AsyncMock, MagicMock |
18 | 3 |
|
| 4 | +import httpx |
| 5 | +import pytest |
19 | 6 |
|
20 |
| -class MockRawResponse(object): |
21 |
| - def __init__(self, content, status_code, headers=None): |
22 |
| - self.content = content |
23 |
| - self.status_code = status_code |
24 |
| - self.headers = {} if headers is None else headers |
| 7 | +from tests.utils.client_configuration import ClientConfiguration |
| 8 | +from tests.utils.list_resource import list_data_to_dicts, list_response_of |
| 9 | +from workos.types.list_resource import WorkOSListResource |
| 10 | +from workos.utils.http_client import AsyncHTTPClient, HTTPClient, SyncHTTPClient |
25 | 11 |
|
26 | 12 |
|
27 | 13 | @pytest.fixture
|
28 |
| -def set_api_key(monkeypatch): |
29 |
| - monkeypatch.setattr(workos, "api_key", "sk_test") |
| 14 | +def sync_http_client_for_test(): |
| 15 | + return SyncHTTPClient( |
| 16 | + api_key="sk_test", |
| 17 | + base_url="https://api.workos.test/", |
| 18 | + client_id="client_b27needthisforssotemxo", |
| 19 | + version="test", |
| 20 | + ) |
30 | 21 |
|
31 | 22 |
|
32 | 23 | @pytest.fixture
|
33 |
| -def set_client_id(monkeypatch): |
34 |
| - monkeypatch.setattr(workos, "client_id", "client_b27needthisforssotemxo") |
| 24 | +def async_http_client_for_test(): |
| 25 | + return AsyncHTTPClient( |
| 26 | + api_key="sk_test", |
| 27 | + base_url="https://api.workos.test/", |
| 28 | + client_id="client_b27needthisforssotemxo", |
| 29 | + version="test", |
| 30 | + ) |
35 | 31 |
|
36 | 32 |
|
37 | 33 | @pytest.fixture
|
38 |
| -def set_api_key_and_client_id(set_api_key, set_client_id): |
39 |
| - pass |
| 34 | +def mock_http_client_with_response(monkeypatch): |
| 35 | + def inner( |
| 36 | + http_client: HTTPClient, |
| 37 | + response_dict: Optional[dict] = None, |
| 38 | + status_code: int = 200, |
| 39 | + headers: Optional[Mapping[str, str]] = None, |
| 40 | + ): |
| 41 | + mock_class = ( |
| 42 | + AsyncMock if isinstance(http_client, AsyncHTTPClient) else MagicMock |
| 43 | + ) |
| 44 | + mock = mock_class( |
| 45 | + return_value=httpx.Response( |
| 46 | + status_code=status_code, headers=headers, json=response_dict |
| 47 | + ), |
| 48 | + ) |
| 49 | + monkeypatch.setattr(http_client._client, "request", mock) |
| 50 | + |
| 51 | + return inner |
40 | 52 |
|
41 | 53 |
|
42 | 54 | @pytest.fixture
|
43 |
| -def mock_request_method(monkeypatch): |
44 |
| - def inner(method, response_dict, status_code, headers=None): |
45 |
| - def mock(*args, **kwargs): |
46 |
| - return MockResponse(response_dict, status_code, headers=headers) |
| 55 | +def capture_and_mock_http_client_request(monkeypatch): |
| 56 | + def inner( |
| 57 | + http_client: HTTPClient, |
| 58 | + response_dict: Optional[dict] = None, |
| 59 | + status_code: int = 200, |
| 60 | + headers: Optional[Mapping[str, str]] = None, |
| 61 | + ): |
| 62 | + request_kwargs = {} |
47 | 63 |
|
48 |
| - monkeypatch.setattr(requests, method, mock) |
| 64 | + def capture_and_mock(*args, **kwargs): |
| 65 | + request_kwargs.update(kwargs) |
49 | 66 |
|
50 |
| - return inner |
| 67 | + return httpx.Response( |
| 68 | + status_code=status_code, |
| 69 | + headers=headers, |
| 70 | + json=response_dict, |
| 71 | + ) |
51 | 72 |
|
| 73 | + mock_class = ( |
| 74 | + AsyncMock if isinstance(http_client, AsyncHTTPClient) else MagicMock |
| 75 | + ) |
| 76 | + mock = mock_class(side_effect=capture_and_mock) |
52 | 77 |
|
53 |
| -@pytest.fixture |
54 |
| -def mock_raw_request_method(monkeypatch): |
55 |
| - def inner(method, content, status_code, headers=None): |
56 |
| - def mock(*args, **kwargs): |
57 |
| - return MockRawResponse(content, status_code, headers=headers) |
| 78 | + monkeypatch.setattr(http_client._client, "request", mock) |
58 | 79 |
|
59 |
| - monkeypatch.setattr(requests, method, mock) |
| 80 | + return request_kwargs |
60 | 81 |
|
61 | 82 | return inner
|
62 | 83 |
|
63 | 84 |
|
64 | 85 | @pytest.fixture
|
65 |
| -def capture_and_mock_request(monkeypatch): |
66 |
| - def inner(method, response_dict, status_code, headers=None): |
67 |
| - request_args = [] |
68 |
| - request_kwargs = {} |
| 86 | +def mock_pagination_request_for_http_client(monkeypatch): |
| 87 | + # Mocking pagination correctly requires us to index into a list of data |
| 88 | + # and correctly set the before and after metadata in the response. |
| 89 | + def inner( |
| 90 | + http_client: HTTPClient, |
| 91 | + data_list: list, |
| 92 | + status_code: int = 200, |
| 93 | + headers: Optional[Mapping[str, str]] = None, |
| 94 | + ): |
| 95 | + # For convenient index lookup, store the list of object IDs. |
| 96 | + data_ids = list(map(lambda x: x["id"], data_list)) |
| 97 | + |
| 98 | + def mock_function(*args, **kwargs): |
| 99 | + params = kwargs.get("params") or {} |
| 100 | + request_after = params.get("after", None) |
| 101 | + limit = params.get("limit", 10) |
| 102 | + |
| 103 | + if request_after is None: |
| 104 | + # First page |
| 105 | + start = 0 |
| 106 | + else: |
| 107 | + # A subsequent page, return the first item _after_ the index we locate |
| 108 | + start = data_ids.index(request_after) + 1 |
| 109 | + data = data_list[start : start + limit] |
| 110 | + if len(data) < limit or len(data) == 0: |
| 111 | + # No more data, set after to None |
| 112 | + after = None |
| 113 | + else: |
| 114 | + # Set after to the last item in this page of results |
| 115 | + after = data[-1]["id"] |
| 116 | + |
| 117 | + return httpx.Response( |
| 118 | + status_code=status_code, |
| 119 | + headers=headers, |
| 120 | + json=list_response_of(data=data, before=request_after, after=after), |
| 121 | + ) |
| 122 | + |
| 123 | + mock_class = ( |
| 124 | + AsyncMock if isinstance(http_client, AsyncHTTPClient) else MagicMock |
| 125 | + ) |
| 126 | + mock = mock_class(side_effect=mock_function) |
| 127 | + |
| 128 | + monkeypatch.setattr(http_client._client, "request", mock) |
69 | 129 |
|
70 |
| - def capture_and_mock(*args, **kwargs): |
71 |
| - request_args.extend(args) |
72 |
| - request_kwargs.update(kwargs) |
73 |
| - |
74 |
| - return MockResponse(response_dict, status_code, headers=headers) |
| 130 | + return inner |
75 | 131 |
|
76 |
| - monkeypatch.setattr(requests, method, capture_and_mock) |
77 | 132 |
|
78 |
| - return (request_args, request_kwargs) |
| 133 | +@pytest.fixture |
| 134 | +def test_sync_auto_pagination( |
| 135 | + mock_pagination_request_for_http_client, |
| 136 | +): |
| 137 | + def inner( |
| 138 | + http_client: SyncHTTPClient, |
| 139 | + list_function: Callable[[], WorkOSListResource], |
| 140 | + expected_all_page_data: dict, |
| 141 | + list_function_params: Optional[Mapping[str, Any]] = None, |
| 142 | + ): |
| 143 | + mock_pagination_request_for_http_client( |
| 144 | + http_client=http_client, |
| 145 | + data_list=expected_all_page_data, |
| 146 | + status_code=200, |
| 147 | + ) |
| 148 | + |
| 149 | + results = list_function(**list_function_params or {}) |
| 150 | + all_results = [] |
| 151 | + |
| 152 | + for result in results: |
| 153 | + all_results.append(result) |
| 154 | + |
| 155 | + assert len(list(all_results)) == len(expected_all_page_data) |
| 156 | + assert (list_data_to_dicts(all_results)) == expected_all_page_data |
79 | 157 |
|
80 | 158 | return inner
|
0 commit comments