import uuid from typing import Mapping import pytest from audience_common import constants from audience_common.context import RequestContext, request_context_from_headers @pytest.mark.parametrize( "headers, label_profile, expected", [ pytest.param( {}, False, RequestContext(context_type=constants.CONTEXT_TYPE_NONE), id="empty", ), pytest.param( {"Orchard-Roles": "audience, label"}, False, RequestContext( context_type=constants.CONTEXT_TYPE_NONE, roles=["audience", " label"], ), id="roles", ), pytest.param( { "Orchard-Profile-Type": "AudienceProfile", "Orchard-Profile-Id": "1000", "Orchard-Profile-UUID": "d130f3ef-ae51-4d1d-8117-5485d81681c1", }, False, RequestContext( context_type=constants.CONTEXT_TYPE_PROFILE, profile_type="AudienceProfile", profile_id=1000, profile_uuid=uuid.UUID("d130f3ef-ae51-4d1d-8117-5485d81681c1"), ), id="profile-context", ), pytest.param( { "Orchard-User-Id": "1000", "Grass-Account-Type": "Grass", "Grass-Account-Id": "999", }, False, RequestContext( context_type=constants.CONTEXT_TYPE_ACCOUNT, orchard_user_id="1000", profile_type="Grass", profile_id=999, ), id="grass-account", ), pytest.param( { "Orchard-User-Id": "alw:1000", }, True, RequestContext( context_type=constants.CONTEXT_TYPE_PROFILE, orchard_user_id="alw:1000", profile_type=constants.PROFILE_TYPE_LABEL, profile_id=1000, ), id="label-profile", ), pytest.param( { "Orchard-User-Id": "oa:1000", }, True, RequestContext( context_type=constants.CONTEXT_TYPE_PROFILE, orchard_user_id="oa:1000", profile_type=constants.PROFILE_TYPE_ORCH_ADMIN, profile_id=1000, ), id="label-profile", ), pytest.param( { "Orchard-Identity-Id": "999", }, True, RequestContext( context_type=constants.CONTEXT_TYPE_PROFILE, identity_id="999", ), id="context-profile", ), ], ) def test_request_context_from_headers( headers: Mapping[str, str], label_profile: bool, expected: RequestContext ) -> None: request_context = request_context_from_headers(headers, label_profile=label_profile) assert request_context == expected def test_request_context_dict() -> None: context_type = constants.CONTEXT_TYPE_PROFILE profile_type = "AudienceProfile" profile_id = 100 request_context = RequestContext( context_type=context_type, profile_type=profile_type, profile_id=profile_id, ) assert request_context.dict() == { "brand": "orchard", "context_type": context_type, "identity_id": None, "identity_uuid": None, "orchard_user_id": None, "profile_type": profile_type, "profile_id": profile_id, "profile_uuid": None, "requestor_service_name": None, "roles": [], "authorization": None, } def test_request_context_dict_exclude_none() -> None: context_type = constants.CONTEXT_TYPE_PROFILE profile_type = "AudienceProfile" profile_id = 100 request_context = RequestContext( context_type=context_type, profile_type=profile_type, profile_id=profile_id, ) assert request_context.dict(exclude_empty=True) == { "brand": "orchard", "context_type": context_type, "profile_type": profile_type, "profile_id": profile_id, } def test_request_context_dict_exclude_none_and_authorization() -> None: context_type = constants.CONTEXT_TYPE_PROFILE profile_type = "AudienceProfile" profile_id = 100 request_context = RequestContext( context_type=context_type, profile_type=profile_type, profile_id=profile_id, authorization="Bearer: token", ) actual = request_context.dict(exclude_empty=True, exclude_keys=["authorization"]) assert actual == { "brand": "orchard", "context_type": context_type, "profile_type": profile_type, "profile_id": profile_id, }