@@ -183,6 +183,74 @@ def test_throws_badrequest_excpetion(
183183 == "Audit Log could not be processed due to missing or incorrect data."
184184 )
185185
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+
186254 class TestCreateExport (_TestSetup ):
187255 def test_succeeds (self , mock_http_client_with_response ):
188256 organization_id = "org_123456789"
0 commit comments