11import pytest
22
3- from app .errors .mapping import ErrorSpec , should_log , spec_for
3+ from app .errors .mapping import DEFAULT_SPEC , EXCEPTION_MAP , ErrorSpec , should_log , spec_for
44from app .models .auth .data import AuthorizationScope
55from app .services .exceptions import (
66 ConflictError ,
1717)
1818
1919
20- def _spec (fhir_code : str , http_status : int , * , log : bool = False ) -> ErrorSpec :
21- return ErrorSpec (severity = "error" , fhir_code = fhir_code , http_status = http_status , log = log )
20+ def _spec (fhir_code : str , http_status : int , * , log : bool = False , invalid_input : bool = False ) -> ErrorSpec :
21+ return ErrorSpec (
22+ severity = "error" , fhir_code = fhir_code , http_status = http_status , log = log , invalid_input = invalid_input
23+ )
2224
2325
2426def _scope_error () -> UnauthorizedScopeError :
@@ -33,10 +35,14 @@ def _scope_error() -> UnauthorizedScopeError:
3335 pytest .param (ForbiddenError (), _spec ("forbidden" , 403 ), id = "forbidden" ),
3436 pytest .param (InvalidKeyInfoError (), _spec ("transient" , 503 ), id = "invalid-key-info" ),
3537 pytest .param (InvalidHeaderPropertyError ("aud" , "x" ), _spec ("invalid" , 401 ), id = "invalid-header-property" ),
36- pytest .param (PseudonymError (), _spec ("invalid" , 422 ), id = "pseudonym" ),
37- pytest .param (ValueError ("boom" ), _spec ("invalid" , 400 , log = True ), id = "value-error" ),
38+ pytest .param (PseudonymError (), _spec ("invalid" , 422 , invalid_input = True ), id = "pseudonym" ),
39+ pytest .param (ValueError ("boom" ), _spec ("invalid" , 400 , invalid_input = True ), id = "value-error" ),
3840 pytest .param (PseudonymExchangeError ("rejected" ), _spec ("invalid" , 400 , log = True ), id = "pseudonym-exchange" ),
39- pytest .param (InvalidModelError ("bad" ), _spec ("structure" , 400 ), id = "invalid-model-outranks-value-error" ),
41+ pytest .param (
42+ InvalidModelError ("bad" ),
43+ _spec ("structure" , 400 , invalid_input = True ),
44+ id = "invalid-model-outranks-value-error" ,
45+ ),
4046 pytest .param (_scope_error (), _spec ("security" , 403 ), id = "unauthorized-scope-via-mro" ),
4147 pytest .param (UnauthorizedManagingRequestError (), _spec ("security" , 403 ), id = "unauthorized-managing-via-mro" ),
4248 pytest .param (UnauthorizedUraError (), _spec ("security" , 403 ), id = "unauthorized-ura-via-mro" ),
@@ -51,7 +57,7 @@ def test_spec_for_resolves_the_expected_spec(exc: Exception, expected: ErrorSpec
5157 ("exc" , "expected" ),
5258 [
5359 pytest .param (PseudonymError (), False , id = "pseudonym" ),
54- pytest .param (ValueError ("boom" ), True , id = "value-error" ),
60+ pytest .param (ValueError ("boom" ), False , id = "value-error" ),
5561 pytest .param (PseudonymExchangeError ("rejected" ), True , id = "pseudonym-exchange" ),
5662 pytest .param (RuntimeError ("unexpected" ), True , id = "unmapped" ),
5763 pytest .param (NotFoundError (), False , id = "not-found" ),
@@ -61,3 +67,8 @@ def test_spec_for_resolves_the_expected_spec(exc: Exception, expected: ErrorSpec
6167)
6268def test_should_log (exc : Exception , expected : bool ) -> None :
6369 assert should_log (exc ) is expected
70+
71+
72+ def test_no_spec_is_both_a_failure_and_invalid_input () -> None :
73+ for spec in [* EXCEPTION_MAP .values (), DEFAULT_SPEC ]:
74+ assert not (spec .log and spec .invalid_input )
0 commit comments