Skip to content

Commit a82dd6a

Browse files
authored
Fix bug for create_user and create_organization_membership and update all tests (#346)
1 parent f1dbfca commit a82dd6a

14 files changed

+839
-221
lines changed

tests/conftest.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from workos.types.list_resource import WorkOSListResource
1010
from workos.utils._base_http_client import DEFAULT_REQUEST_TIMEOUT
1111
from workos.utils.http_client import AsyncHTTPClient, HTTPClient, SyncHTTPClient
12+
from workos.utils.request_helper import DEFAULT_LIST_RESPONSE_LIMIT
1213

1314

1415
@pytest.fixture
@@ -122,7 +123,7 @@ def capture_and_mock(*args, **kwargs):
122123

123124

124125
@pytest.fixture
125-
def mock_pagination_request_for_http_client(monkeypatch):
126+
def capture_and_mock_pagination_request_for_http_client(monkeypatch):
126127
# Mocking pagination correctly requires us to index into a list of data
127128
# and correctly set the before and after metadata in the response.
128129
def inner(
@@ -131,10 +132,14 @@ def inner(
131132
status_code: int = 200,
132133
headers: Optional[Mapping[str, str]] = None,
133134
):
135+
request_kwargs = {}
136+
134137
# For convenient index lookup, store the list of object IDs.
135138
data_ids = list(map(lambda x: x["id"], data_list))
136139

137140
def mock_function(*args, **kwargs):
141+
request_kwargs.update(kwargs)
142+
138143
params = kwargs.get("params") or {}
139144
request_after = params.get("after", None)
140145
limit = params.get("limit", 10)
@@ -166,20 +171,20 @@ def mock_function(*args, **kwargs):
166171

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

174+
return request_kwargs
175+
169176
return inner
170177

171178

172179
@pytest.fixture
173-
def test_sync_auto_pagination(
174-
mock_pagination_request_for_http_client,
175-
):
180+
def test_sync_auto_pagination(capture_and_mock_pagination_request_for_http_client):
176181
def inner(
177182
http_client: SyncHTTPClient,
178183
list_function: Callable[[], WorkOSListResource],
179184
expected_all_page_data: dict,
180185
list_function_params: Optional[Mapping[str, Any]] = None,
181186
):
182-
mock_pagination_request_for_http_client(
187+
request_kwargs = capture_and_mock_pagination_request_for_http_client(
183188
http_client=http_client,
184189
data_list=expected_all_page_data,
185190
status_code=200,
@@ -193,5 +198,15 @@ def inner(
193198

194199
assert len(list(all_results)) == len(expected_all_page_data)
195200
assert (list_data_to_dicts(all_results)) == expected_all_page_data
201+
assert request_kwargs["method"] == "get"
202+
203+
# Validate parameters
204+
assert "after" in request_kwargs["params"]
205+
assert request_kwargs["params"]["limit"] == DEFAULT_LIST_RESPONSE_LIMIT
206+
assert request_kwargs["params"]["order"] == "desc"
207+
208+
params = list_function_params or {}
209+
for param in params:
210+
assert request_kwargs["params"][param] == params[param]
196211

197212
return inner

tests/test_async_http_client.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,27 @@ async def test_request_parses_json_when_encoding_in_content_type(self):
287287
)
288288

289289
assert await self.http_client.request("ok_place") == {"foo": "bar"}
290+
291+
async def test_request_removes_none_parameter_values(
292+
self, capture_and_mock_http_client_request
293+
):
294+
request_kwargs = capture_and_mock_http_client_request(self.http_client, {}, 200)
295+
296+
await self.http_client.request(
297+
path="/test",
298+
method="get",
299+
params={"organization_id": None, "test": "value"},
300+
)
301+
assert request_kwargs["params"] == {"test": "value"}
302+
303+
async def test_request_removes_none_json_values(
304+
self, capture_and_mock_http_client_request
305+
):
306+
request_kwargs = capture_and_mock_http_client_request(self.http_client, {}, 200)
307+
308+
await self.http_client.request(
309+
path="/test",
310+
method="post",
311+
json={"organization_id": None, "test": "value"},
312+
)
313+
assert request_kwargs["json"] == {"test": "value"}

tests/test_audit_logs.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def test_succeeds(self, capture_and_mock_http_client_request):
7878
idempotency_key="test_123456",
7979
)
8080

81+
assert request_kwargs["url"].endswith("/audit_logs/events")
82+
assert request_kwargs["method"] == "post"
8183
assert request_kwargs["json"] == {
8284
"organization_id": organization_id,
8385
"event": event,
@@ -177,7 +179,9 @@ def test_succeeds(self, mock_http_client_with_response):
177179

178180
assert response.dict() == expected_payload
179181

180-
def test_succeeds_with_additional_filters(self, mock_http_client_with_response):
182+
def test_succeeds_with_additional_filters(
183+
self, capture_and_mock_http_client_request
184+
):
181185
now = datetime.now().isoformat()
182186
organization_id = "org_123456789"
183187
range_start = now
@@ -196,7 +200,9 @@ def test_succeeds_with_additional_filters(self, mock_http_client_with_response):
196200
"updated_at": now,
197201
}
198202

199-
mock_http_client_with_response(self.http_client, expected_payload, 201)
203+
request_kwargs = capture_and_mock_http_client_request(
204+
self.http_client, expected_payload, 201
205+
)
200206

201207
response = self.audit_logs.create_export(
202208
actions=actions,
@@ -208,6 +214,17 @@ def test_succeeds_with_additional_filters(self, mock_http_client_with_response):
208214
actor_ids=actor_ids,
209215
)
210216

217+
assert request_kwargs["url"].endswith("/audit_logs/exports")
218+
assert request_kwargs["method"] == "post"
219+
assert request_kwargs["json"] == {
220+
"actions": actions,
221+
"organization_id": organization_id,
222+
"range_end": range_end,
223+
"range_start": range_start,
224+
"targets": targets,
225+
"actor_names": actor_names,
226+
"actor_ids": actor_ids,
227+
}
211228
assert response.dict() == expected_payload
212229

213230
def test_throws_unauthorized_excpetion(self, mock_http_client_with_response):
@@ -233,7 +250,7 @@ def test_throws_unauthorized_excpetion(self, mock_http_client_with_response):
233250
)
234251

235252
class TestGetExport(_TestSetup):
236-
def test_succeeds(self, mock_http_client_with_response):
253+
def test_succeeds(self, capture_and_mock_http_client_request):
237254
now = datetime.now().isoformat()
238255
expected_payload = {
239256
"object": "audit_log_export",
@@ -244,12 +261,18 @@ def test_succeeds(self, mock_http_client_with_response):
244261
"updated_at": now,
245262
}
246263

247-
mock_http_client_with_response(self.http_client, expected_payload, 200)
264+
request_kwargs = capture_and_mock_http_client_request(
265+
self.http_client, expected_payload, 200
266+
)
248267

249268
response = self.audit_logs.get_export(
250269
expected_payload["id"],
251270
)
252271

272+
assert request_kwargs["url"].endswith(
273+
"/audit_logs/exports/audit_log_export_1234"
274+
)
275+
assert request_kwargs["method"] == "get"
253276
assert response.dict() == expected_payload
254277

255278
def test_throws_unauthorized_excpetion(self, mock_http_client_with_response):

0 commit comments

Comments
 (0)