backend.tests.unit.components.camera_verification.error_handling.test_error_service
1import pytest 2from unittest.mock import patch, MagicMock 3 4from backend.components.camera_verification.error_handling import errorService 5from backend.components.camera_verification.error_handling.errorConfig import ErrorResponse 6from backend.components.camera_verification.faceid.faceidService import ( 7 FaceIDError, 8 NoFacesFoundError, 9 MultipleWorkersError, 10 FaceNotMatchingError 11) 12from backend.components.camera_verification.qrcode.qrcodeService import ( 13 QRCodeError, 14 NoCodeFoundError, 15 InvalidCodeError, 16 MultipleCodesError, 17 ExpiredCodeError 18) 19 20 21# ============================================================================ 22# Test verification_response_handler - Success Cases 23# ============================================================================ 24 25def test_verification_response_handler_no_exception(): 26 """Test handler with no exception returns success response.""" 27 response = errorService.verification_response_handler(None) 28 29 assert isinstance(response, ErrorResponse) 30 assert response is not None 31 32 33def test_verification_response_handler_returns_error_response(): 34 """Test that handler always returns ErrorResponse object.""" 35 exception = Exception("Test error") 36 response = errorService.verification_response_handler(exception) 37 38 assert isinstance(response, ErrorResponse) 39 assert hasattr(response, 'code') 40 assert hasattr(response, 'message') 41 assert hasattr(response, 'logged') 42 43 44# ============================================================================ 45# Test verification_response_handler - Generic Exception Cases 46# ============================================================================ 47 48def test_verification_response_handler_unknown_exception(): 49 """Test handler with unknown exception type returns default response.""" 50 exception = ValueError("Unknown error type") 51 response = errorService.verification_response_handler(exception) 52 53 assert isinstance(response, ErrorResponse) 54 assert response.code is not None 55 # Message should include the exception details 56 assert str(exception) in response.message 57 58 59def test_verification_response_handler_runtime_error(): 60 """Test handler with RuntimeError exception.""" 61 exception = RuntimeError("Runtime error occurred") 62 response = errorService.verification_response_handler(exception) 63 64 assert isinstance(response, ErrorResponse) 65 assert "Runtime error occurred" in response.message 66 67 68def test_verification_response_handler_type_error(): 69 """Test handler with TypeError exception.""" 70 exception = TypeError("Type mismatch error") 71 response = errorService.verification_response_handler(exception) 72 73 assert isinstance(response, ErrorResponse) 74 assert "Type mismatch error" in response.message 75 76 77# ============================================================================ 78# Test verification_response_handler - Response Properties 79# ============================================================================ 80 81def test_verification_response_handler_response_has_logged(): 82 """Test that response includes logged property.""" 83 exception = NoFacesFoundError("Test error") 84 response = errorService.verification_response_handler(exception) 85 86 assert hasattr(response, 'logged') 87 assert isinstance(response.logged, bool) 88 89 90def test_verification_response_handler_response_has_code(): 91 """Test that response includes error code.""" 92 exception = MultipleWorkersError("Test error") 93 response = errorService.verification_response_handler(exception) 94 95 assert hasattr(response, 'code') 96 assert response.code is not None 97 98 99def test_verification_response_handler_response_has_message(): 100 """Test that response includes error message.""" 101 exception = FaceNotMatchingError("Face mismatch") 102 response = errorService.verification_response_handler(exception) 103 104 assert hasattr(response, 'message') 105 assert len(response.message) > 0 106 107 108# ============================================================================ 109# Test verification_response_handler - Exception Message Preservation 110# ============================================================================ 111def test_verification_response_handler_appends_to_default_message(): 112 """Test that unknown exception message is appended to default response.""" 113 custom_error = "Detailed error information" 114 exception = IOError(custom_error) 115 response = errorService.verification_response_handler(exception) 116 117 assert custom_error in response.message 118 119 120# ============================================================================ 121# Test verification_response_handler - Edge Cases 122# ============================================================================ 123def test_verification_response_handler_with_empty_exception_message(): 124 """Test handler with exception that has empty message.""" 125 exception = Exception("") 126 response = errorService.verification_response_handler(exception) 127 128 assert isinstance(response, ErrorResponse) 129 assert response.code is not None 130 131 132def test_verification_response_handler_multiple_calls_consistency(): 133 """Test that handler returns consistent responses for same exception type.""" 134 exception1 = NoFacesFoundError("Error 1") 135 exception2 = NoFacesFoundError("Error 2") 136 137 response1 = errorService.verification_response_handler(exception1) 138 response2 = errorService.verification_response_handler(exception2) 139 140 # Both should be valid responses 141 assert isinstance(response1, ErrorResponse) 142 assert isinstance(response2, ErrorResponse) 143 # Codes should match for same exception type 144 assert response1.code == response2.code
def
test_verification_response_handler_no_exception():
26def test_verification_response_handler_no_exception(): 27 """Test handler with no exception returns success response.""" 28 response = errorService.verification_response_handler(None) 29 30 assert isinstance(response, ErrorResponse) 31 assert response is not None
Test handler with no exception returns success response.
def
test_verification_response_handler_returns_error_response():
34def test_verification_response_handler_returns_error_response(): 35 """Test that handler always returns ErrorResponse object.""" 36 exception = Exception("Test error") 37 response = errorService.verification_response_handler(exception) 38 39 assert isinstance(response, ErrorResponse) 40 assert hasattr(response, 'code') 41 assert hasattr(response, 'message') 42 assert hasattr(response, 'logged')
Test that handler always returns ErrorResponse object.
def
test_verification_response_handler_unknown_exception():
49def test_verification_response_handler_unknown_exception(): 50 """Test handler with unknown exception type returns default response.""" 51 exception = ValueError("Unknown error type") 52 response = errorService.verification_response_handler(exception) 53 54 assert isinstance(response, ErrorResponse) 55 assert response.code is not None 56 # Message should include the exception details 57 assert str(exception) in response.message
Test handler with unknown exception type returns default response.
def
test_verification_response_handler_runtime_error():
60def test_verification_response_handler_runtime_error(): 61 """Test handler with RuntimeError exception.""" 62 exception = RuntimeError("Runtime error occurred") 63 response = errorService.verification_response_handler(exception) 64 65 assert isinstance(response, ErrorResponse) 66 assert "Runtime error occurred" in response.message
Test handler with RuntimeError exception.
def
test_verification_response_handler_type_error():
69def test_verification_response_handler_type_error(): 70 """Test handler with TypeError exception.""" 71 exception = TypeError("Type mismatch error") 72 response = errorService.verification_response_handler(exception) 73 74 assert isinstance(response, ErrorResponse) 75 assert "Type mismatch error" in response.message
Test handler with TypeError exception.
def
test_verification_response_handler_response_has_logged():
82def test_verification_response_handler_response_has_logged(): 83 """Test that response includes logged property.""" 84 exception = NoFacesFoundError("Test error") 85 response = errorService.verification_response_handler(exception) 86 87 assert hasattr(response, 'logged') 88 assert isinstance(response.logged, bool)
Test that response includes logged property.
def
test_verification_response_handler_response_has_code():
91def test_verification_response_handler_response_has_code(): 92 """Test that response includes error code.""" 93 exception = MultipleWorkersError("Test error") 94 response = errorService.verification_response_handler(exception) 95 96 assert hasattr(response, 'code') 97 assert response.code is not None
Test that response includes error code.
def
test_verification_response_handler_response_has_message():
100def test_verification_response_handler_response_has_message(): 101 """Test that response includes error message.""" 102 exception = FaceNotMatchingError("Face mismatch") 103 response = errorService.verification_response_handler(exception) 104 105 assert hasattr(response, 'message') 106 assert len(response.message) > 0
Test that response includes error message.
def
test_verification_response_handler_appends_to_default_message():
112def test_verification_response_handler_appends_to_default_message(): 113 """Test that unknown exception message is appended to default response.""" 114 custom_error = "Detailed error information" 115 exception = IOError(custom_error) 116 response = errorService.verification_response_handler(exception) 117 118 assert custom_error in response.message
Test that unknown exception message is appended to default response.
def
test_verification_response_handler_with_empty_exception_message():
124def test_verification_response_handler_with_empty_exception_message(): 125 """Test handler with exception that has empty message.""" 126 exception = Exception("") 127 response = errorService.verification_response_handler(exception) 128 129 assert isinstance(response, ErrorResponse) 130 assert response.code is not None
Test handler with exception that has empty message.
def
test_verification_response_handler_multiple_calls_consistency():
133def test_verification_response_handler_multiple_calls_consistency(): 134 """Test that handler returns consistent responses for same exception type.""" 135 exception1 = NoFacesFoundError("Error 1") 136 exception2 = NoFacesFoundError("Error 2") 137 138 response1 = errorService.verification_response_handler(exception1) 139 response2 = errorService.verification_response_handler(exception2) 140 141 # Both should be valid responses 142 assert isinstance(response1, ErrorResponse) 143 assert isinstance(response2, ErrorResponse) 144 # Codes should match for same exception type 145 assert response1.code == response2.code
Test that handler returns consistent responses for same exception type.