44
55from workos .audit_logs import AuditLogEvent , AuditLogs
66from workos .exceptions import AuthenticationException , BadRequestException
7+ from workos .types .audit_logs .audit_log_event_response import AuditLogEventResponse
78
89
910class _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"
0 commit comments