# tests/system/test_error_handling.py import pytest @pytest.mark.system class TestErrorHandling: def test_invalid_json(self, client, auth_headers): """Test handling of invalid JSON""" response = client.post( "/api/v1/product", headers=auth_headers, content="invalid json" ) assert response.status_code == 422 # Validation error def test_missing_required_fields(self, client, auth_headers): """Test handling of missing required fields""" response = client.post( "/api/v1/product", headers=auth_headers, json={"title": "Test"} ) # Missing product_id assert response.status_code == 422 def test_invalid_authentication(self, client): """Test handling of invalid authentication""" response = client.get( "/api/v1/product", headers={"Authorization": "Bearer invalid_token"} ) assert response.status_code == 401 # Token is not valid def test_nonexistent_resource(self, client, auth_headers): """Test handling of nonexistent resource access""" response = client.get("/api/v1/product/NONEXISTENT", headers=auth_headers) assert response.status_code == 404 response = client.get("/api/v1/shop/NONEXISTENT", headers=auth_headers) assert response.status_code == 404 def test_duplicate_resource_creation(self, client, auth_headers, test_product): """Test handling of duplicate resource creation""" product_data = { "product_id": test_product.product_id, # Duplicate ID "title": "Another Product", } response = client.post( "/api/v1/product", headers=auth_headers, json=product_data ) assert response.status_code == 400 def test_server_error_handling(self, client, auth_headers): """Test handling of server errors""" # This would test 500 errors if you have endpoints that can trigger them # For now, test that the error handling middleware works response = client.get("/api/v1/nonexistent-endpoint", headers=auth_headers) assert response.status_code == 404 def test_rate_limiting_behavior(self, client, auth_headers): """Test rate limiting behavior if implemented""" # Make multiple rapid requests to test rate limiting responses = [] for i in range(10): response = client.get("/api/v1/product", headers=auth_headers) responses.append(response) # All should succeed unless rate limiting is very aggressive # Adjust based on your rate limiting configuration success_count = sum(1 for r in responses if r.status_code == 200) assert success_count >= 5 # At least half should succeed def test_malformed_requests(self, client, auth_headers): """Test handling of various malformed requests""" # Test extremely long URLs long_search = "x" * 10000 response = client.get(f"/api/v1/product?search={long_search}", headers=auth_headers) # Should handle gracefully, either 200 with no results or 422 for too long assert response.status_code in [200, 422] # Test special characters in parameters special_chars = "!@#$%^&*(){}[]|\\:;\"'<>,.?/~`" response = client.get(f"/api/v1/product?search={special_chars}", headers=auth_headers) # Should handle gracefully assert response.status_code in [200, 422] def test_database_error_recovery(self, client, auth_headers): """Test application behavior during database issues""" # This is more complex to test - you'd need to simulate DB issues # For now, just test that basic operations work response = client.get("/api/v1/product", headers=auth_headers) assert response.status_code == 200 def test_content_type_errors(self, client, auth_headers): """Test handling of incorrect content types""" # Send XML to JSON endpoint response = client.post( "/api/v1/product", headers={**auth_headers, "Content-Type": "application/xml"}, content="not json" ) assert response.status_code in [400, 422, 415] # Bad request or unsupported media type def test_large_payload_handling(self, client, auth_headers): """Test handling of unusually large payloads""" # Create a very large product description large_data = { "product_id": "LARGE_TEST", "title": "Large Test Product", "description": "x" * 50000 # Very long description } response = client.post("/api/v1/product", headers=auth_headers, json=large_data) # Should either accept it or reject with 422 (too large) assert response.status_code in [200, 201, 422, 413]