Skip to content

Commit adfcb80

Browse files
cleanup
1 parent c647324 commit adfcb80

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

tests/test_audit_logs.py

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from workos.audit_logs import AuditLogEvent, AuditLogs
66
from workos.exceptions import AuthenticationException, BadRequestException
7+
from workos.types.audit_logs.audit_log_event_response import AuditLogEventResponse
78

89

910
class _TestSetup:
@@ -84,7 +85,9 @@ def test_succeeds(self, capture_and_mock_http_client_request):
8485
"organization_id": organization_id,
8586
"event": event,
8687
}
87-
assert response is None
88+
assert response is not None
89+
assert response.success is True
90+
assert isinstance(response, AuditLogEventResponse)
8891

8992
def test_sends_idempotency_key(
9093
self, mock_audit_log_event, capture_and_mock_http_client_request
@@ -104,7 +107,8 @@ def test_sends_idempotency_key(
104107
)
105108

106109
assert request_kwargs["headers"]["idempotency-key"] == idempotency_key
107-
assert response is None
110+
assert response is not None
111+
assert response.success is True
108112

109113
def test_auto_generates_idempotency_key(
110114
self, mock_audit_log_event, capture_and_mock_http_client_request
@@ -130,7 +134,8 @@ def test_auto_generates_idempotency_key(
130134
assert idempotency_key.startswith("workos-python-")
131135
# Assert the key has the expected UUID format after the prefix
132136
assert len(idempotency_key) > len("workos-python-")
133-
assert response is None
137+
assert response is not None
138+
assert response.success is True
134139

135140
def test_throws_unauthorized_exception(
136141
self, mock_audit_log_event, mock_http_client_with_response
@@ -178,6 +183,74 @@ def test_throws_badrequest_excpetion(
178183
== "Audit Log could not be processed due to missing or incorrect data."
179184
)
180185

186+
def test_handles_missing_success_field(
187+
self, mock_audit_log_event, mock_http_client_with_response
188+
):
189+
"""Test that schema validation fails when response is missing required fields."""
190+
organization_id = "org_123456789"
191+
192+
# Mock response missing the 'success' field
193+
mock_http_client_with_response(
194+
self.http_client,
195+
{}, # Empty response
196+
200,
197+
)
198+
199+
with pytest.raises(Exception) as excinfo: # Pydantic will raise ValidationError
200+
self.audit_logs.create_event(
201+
organization_id=organization_id,
202+
event=mock_audit_log_event,
203+
)
204+
205+
# Assert that validation error occurred
206+
assert "success" in str(excinfo.value).lower() or "validation" in str(
207+
excinfo.value
208+
).lower()
209+
210+
def test_handles_invalid_success_type(
211+
self, mock_audit_log_event, mock_http_client_with_response
212+
):
213+
"""Test that schema validation fails when response has incorrect field types."""
214+
organization_id = "org_123456789"
215+
216+
# Mock response with wrong type for 'success' field (non-coercible value)
217+
mock_http_client_with_response(
218+
self.http_client,
219+
{"success": ["invalid", "list"]}, # List instead of boolean
220+
200,
221+
)
222+
223+
with pytest.raises(Exception) as excinfo: # Pydantic will raise ValidationError
224+
self.audit_logs.create_event(
225+
organization_id=organization_id,
226+
event=mock_audit_log_event,
227+
)
228+
229+
# Assert that validation error occurred
230+
assert excinfo.value is not None
231+
232+
def test_handles_malformed_json_response(
233+
self, mock_audit_log_event, mock_http_client_with_response
234+
):
235+
"""Test that schema validation fails when response is completely malformed."""
236+
organization_id = "org_123456789"
237+
238+
# Mock response with unexpected structure
239+
mock_http_client_with_response(
240+
self.http_client,
241+
{"unexpected": "data", "structure": 123},
242+
200,
243+
)
244+
245+
with pytest.raises(Exception) as excinfo:
246+
self.audit_logs.create_event(
247+
organization_id=organization_id,
248+
event=mock_audit_log_event,
249+
)
250+
251+
# Assert that validation error occurred
252+
assert excinfo.value is not None
253+
181254
class TestCreateExport(_TestSetup):
182255
def test_succeeds(self, mock_http_client_with_response):
183256
organization_id = "org_123456789"

workos/audit_logs.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from workos.types.audit_logs import AuditLogExport
55
from workos.types.audit_logs.audit_log_event import AuditLogEvent
6+
from workos.types.audit_logs.audit_log_event_response import AuditLogEventResponse
67
from workos.utils._base_http_client import RetryConfig
78
from workos.utils.http_client import SyncHTTPClient
89
from workos.utils.request_helper import REQUEST_METHOD_GET, REQUEST_METHOD_POST
@@ -20,15 +21,15 @@ def create_event(
2021
organization_id: str,
2122
event: AuditLogEvent,
2223
idempotency_key: Optional[str] = None,
23-
) -> None:
24+
) -> AuditLogEventResponse:
2425
"""Create an Audit Logs event.
2526
2627
Kwargs:
2728
organization_id (str): Organization's unique identifier.
2829
event (AuditLogEvent): An AuditLogEvent object.
2930
idempotency_key (str): Idempotency key. (Optional)
3031
Returns:
31-
None
32+
AuditLogEventResponse: Response indicating success
3233
"""
3334
...
3435

@@ -80,7 +81,7 @@ def create_event(
8081
organization_id: str,
8182
event: AuditLogEvent,
8283
idempotency_key: Optional[str] = None,
83-
) -> None:
84+
) -> AuditLogEventResponse:
8485
json = {"organization_id": organization_id, "event": event}
8586

8687
headers = {}
@@ -91,14 +92,16 @@ def create_event(
9192
headers["idempotency-key"] = idempotency_key
9293

9394
# Enable retries for audit log event creation with default retryConfig
94-
self._http_client.request(
95+
response = self._http_client.request(
9596
EVENTS_PATH,
9697
method=REQUEST_METHOD_POST,
9798
json=json,
9899
headers=headers,
99100
retry_config=RetryConfig(),
100101
)
101102

103+
return AuditLogEventResponse.model_validate(response)
104+
102105
def create_export(
103106
self,
104107
*,

workos/types/audit_logs/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
from .audit_log_event_context import *
33
from .audit_log_event_target import *
44
from .audit_log_event import *
5+
from .audit_log_event_response import *
56
from .audit_log_export import *
67
from .audit_log_metadata import *
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from workos.types.workos_model import WorkOSModel
2+
3+
4+
class AuditLogEventResponse(WorkOSModel):
5+
"""Response from creating an audit log event."""
6+
7+
success: bool
8+

0 commit comments

Comments
 (0)