import uuid from collections.abc import Mapping import pytest from fansifter_common.context import RequestContext, request_context_from_headers @pytest.mark.parametrize( "headers, expected", [ pytest.param( { "Orchard-Profile-Type": "AudienceProfile", "Orchard-Profile-Id": "1000", "Orchard-Profile-UUID": "d130f3ef-ae51-4d1d-8117-5485d81681c1", }, RequestContext( profile_type="AudienceProfile", profile_id=1000, profile_uuid=uuid.UUID("d130f3ef-ae51-4d1d-8117-5485d81681c1"), ), id="profile-context", ), pytest.param( { "Orchard-Identity-Id": "999", }, RequestContext( identity_id="999", ), id="context-profile", ), ], ) def test_request_context_from_headers( headers: Mapping[str, str], expected: RequestContext ) -> None: request_context = request_context_from_headers(headers) assert request_context == expected def test_request_context_dict() -> None: profile_type = "AudienceProfile" profile_id = 100 request_context = RequestContext( profile_type=profile_type, profile_id=profile_id, ) assert request_context.dict() == { "identity_id": None, "identity_uuid": None, "profile_type": profile_type, "profile_id": profile_id, "profile_uuid": None, "requestor_service_name": None, "authorization": None, } def test_request_context_dict_exclude_none() -> None: profile_type = "AudienceProfile" profile_id = 100 request_context = RequestContext( profile_type=profile_type, profile_id=profile_id, ) assert request_context.dict(exclude_empty=True) == { "profile_type": profile_type, "profile_id": profile_id, } def test_request_context_dict_exclude_none_and_authorization() -> None: profile_type = "AudienceProfile" profile_id = 100 request_context = RequestContext( profile_type=profile_type, profile_id=profile_id, authorization="Bearer: token", ) actual = request_context.dict(exclude_empty=True, exclude_keys=["authorization"]) assert actual == { "profile_type": profile_type, "profile_id": profile_id, }