"""Tests for user context model.""" from owsrequest import error_response from owsrequest.constants import errors from owsrequest.constants import headers from owsresponse import response import pytest from pythonfeatures.constants import context as context_constants from pythonfeatures.models import context @pytest.mark.parametrize(( 'orchard_user_id', 'vendor_id', 'subaccount_id', 'orchard_identity_uuid', 'orchard_identity_id', 'expected_validation_response', 'context_to_dict_response'), [ # no context ( None, None, None, None, None, True, {} ), # vendor_id, no user id ( None, 888, None, None, None, response.create_error_response( code=errors.ERROR_CODE_ACCOUNT_WITHOUT_USER, message=errors.ERROR_MESSAGE_ACCOUNT_WITHOUT_USER, status=400), {} ), # subaccount_id, no user id ( None, None, 999, None, None, response.create_error_response( code=errors.ERROR_CODE_ACCOUNT_WITHOUT_USER, message=errors.ERROR_MESSAGE_ACCOUNT_WITHOUT_USER, status=400), {} ), # just orchard uuid ( None, None, None, '77f607ed-c303-4eb7-9c05-f8b19eb94757', None, True, { context_constants.ORCHARD_IDENTITY_UUID: '77f607ed-c303-4eb7-9c05-f8b19eb94757' # noqa } ), # orchard uuid, and ALW orchard user id ( 'alw:444', None, None, '77f607ed-c303-4eb7-9c05-f8b19eb94757', None, True, { context_constants.PROFILE_TYPE_AND_ID: 'LabelProfile:444', context_constants.ORCHARD_USER_ID: 'alw:444', context_constants.ORCHARD_IDENTITY_UUID: '77f607ed-c303-4eb7-9c05-f8b19eb94757' # noqa } ), # orchard uuid, ALW orchard user id, vendor id, # and orchard identity id (auth0 id) ( 'alw:444', None, None, '77f607ed-c303-4eb7-9c05-f8b19eb94757', '5cd2dd3390c6cb0f6927b8c8', True, { context_constants.PROFILE_TYPE_AND_ID: 'LabelProfile:444', context_constants.ORCHARD_USER_ID: 'alw:444', context_constants.ORCHARD_IDENTITY_UUID: '77f607ed-c303-4eb7-9c05-f8b19eb94757', # noqa context_constants.IDENTITY_ID: '5cd2dd3390c6cb0f6927b8c8' } ) ]) def test_account_context( monkeypatch, orchard_user_id, vendor_id, subaccount_id, orchard_identity_uuid, orchard_identity_id, expected_validation_response, context_to_dict_response): """Tests for legacy AccountContext user context.""" instance = context.AccountContext( orchard_user_id=orchard_user_id, vendor_id=vendor_id, subaccount_id=subaccount_id, orchard_identity_uuid=orchard_identity_uuid, identity_id=orchard_identity_id ) validation_response = instance.validate() if expected_validation_response: assert validation_response else: assert validation_response.status == \ expected_validation_response.status assert instance.to_dict() == context_to_dict_response @pytest.mark.parametrize(( 'profile_type', 'profile_id', 'identity_id', 'orchard_identity_uuid', 'expected_validation_response', 'context_to_dict_response'), [ # None ( None, None, None, None, True, {} ), # all 3 ( 'ArtistProfile', 111, 5555, None, True, { context_constants.PROFILE_TYPE_AND_ID: 'ArtistProfile:111', context_constants.IDENTITY_ID: '5555' } ), # profile type and profile id, no identity id ( 'ArtistProfile', 111, None, None, True, { context_constants.PROFILE_TYPE_AND_ID: 'ArtistProfile:111' } ), # just identity id ( None, None, 5555, None, True, { context_constants.IDENTITY_ID: '5555' } ), # profile_type, no profile_id ( 'ArtistProfile', None, None, None, error_response.create_error_incomplete_profile_headers(), {} ), # profile_type, no profile_id, idenity_id ( 'ArtistProfile', None, 5555, None, error_response.create_error_incomplete_profile_headers(), {} ), # profile_type orchadmin ( headers.PROFILE_TYPE_ORCH_ADMIN, 888, None, None, True, { context_constants.PROFILE_TYPE_AND_ID: 'OrchAdminProfile:888', context_constants.ORCHARD_USER_ID: 'oa:888'} ), # just orchard uuid ( None, None, None, '77f607ed-c303-4eb7-9c05-f8b19eb94757', True, { context_constants.ORCHARD_IDENTITY_UUID: '77f607ed-c303-4eb7-9c05-f8b19eb94757' # noqa } ), # orchard uuid, profile_type labelprofile ( headers.PROFILE_TYPE_LABEL, 888, None, '77f607ed-c303-4eb7-9c05-f8b19eb94757', True, { context_constants.PROFILE_TYPE_AND_ID: 'LabelProfile:888', context_constants.ORCHARD_IDENTITY_UUID: '77f607ed-c303-4eb7-9c05-f8b19eb94757' # noqa } ) ]) def test_profile_context( monkeypatch, profile_type, profile_id, identity_id, orchard_identity_uuid, expected_validation_response, context_to_dict_response): """Tests for ProfileContext user context.""" instance = context.ProfileContext( profile_type=profile_type, profile_id=profile_id, identity_id=identity_id, orchard_identity_uuid=orchard_identity_uuid) validation_response = instance.validate() if expected_validation_response: assert validation_response else: assert validation_response.status == \ expected_validation_response.status assert instance.to_dict() == context_to_dict_response