"""Unit tests for context.py.""" from unittest.mock import MagicMock from owsresponse import response import pytest from owsrequest import error_response from features import context from features.constants import context as context_constants from features.constants import error from features.models import ows_users @pytest.mark.parametrize( ( 'user_id', 'vendor_id', 'subaccount_id', 'expected_response', 'mock_ows_users_response', 'context_to_dict_response', ), [ # no context (None, None, None, True, False, {}), # vendor_id, no user id ( None, 888, None, response.create_error_response( code=error.ERROR_CODE_ACCOUNT_WITHOUT_USER, message=error.ERROR_MESSAGE_ACCOUNT_WITHOUT_USER, status=400, ), None, False, ), # subaccount_id, no user id ( None, None, 999, response.create_error_response( code=error.ERROR_CODE_ACCOUNT_WITHOUT_USER, message=error.ERROR_MESSAGE_ACCOUNT_WITHOUT_USER, status=400, ), None, False, ), # ows-users 500 ( 'alw:444', None, 999, True, MagicMock( return_value=response.create_error_response( status=500, code='pizza', message='party' ) ), { context_constants.USER_ID: 'alw:444', context_constants.PROFILE_TYPE_AND_ID: 'LabelProfile:444', context_constants.SUBACCOUNT_ID: '999', }, ), # ows-users response not ok ( 'alw:444', None, 999, response.create_error_response(status=400, code='pizza', message='party'), MagicMock( return_value=response.create_error_response( status=400, code='pizza', message='party' ) ), False, ), # subaccount_id, ows-users response ok, user subaccount mismatch ( 'alw:444', None, 55555555555, response.create_error_response(status=400, code='pizza', message='party'), MagicMock( return_value=response.Response( status=200, message={ 'type': 'alw', 'account': {'vendor_id': 888, 'subaccount_id': 999}, }, ) ), False, ), # user_id, vendor_id - do not call ows-users ( 'alw:444', 888, None, True, False, { context_constants.USER_ID: 'alw:444', context_constants.VENDOR_ID: '888', context_constants.PROFILE_TYPE_AND_ID: 'LabelProfile:444', }, ), # user_id, subaccount_id, users reponse ok - add vendor_id ( 'alw:444', None, 999, True, MagicMock( return_value=response.Response( status=200, message={ 'type': 'alw', 'account': {'vendor_id': 888, 'subaccount_id': 999}, }, ) ), { context_constants.USER_ID: 'alw:444', context_constants.VENDOR_ID: '888', context_constants.SUBACCOUNT_ID: '999', context_constants.PROFILE_TYPE_AND_ID: 'LabelProfile:444', }, ), # oa user ( 'oa:123', None, 999, True, MagicMock( return_value=response.Response(status=200, message={'type': 'oa'}) ), { context_constants.USER_ID: 'oa:123', context_constants.PROFILE_TYPE_AND_ID: 'OrchAdminProfile:123', }, ), ], ) def test_validate( monkeypatch, user_id, vendor_id, subaccount_id, expected_response, mock_ows_users_response, context_to_dict_response, ): """Test Context.validate().""" instance = context.Context( orchard_user_id=user_id, vendor_id=vendor_id, subaccount_id=subaccount_id ) monkeypatch.setattr( ows_users, 'get_user_details_by_user_id', mock_ows_users_response or MagicMock() ) validation_response = instance.validate() if expected_response: assert validation_response == expected_response else: assert validation_response.status == expected_response.status if mock_ows_users_response: if mock_ows_users_response is False: assert ows_users.get_user_details_by_user_id.called is False else: assert ows_users.get_user_details_by_user_id.called if context_to_dict_response: assert instance.to_dict() == context_to_dict_response @pytest.mark.parametrize( ( 'profile_type', 'profile_id', 'identity_id', 'mock_ows_users_response', 'expected_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(), {}, ), # LabelProfile, verify alw: added ( 'LabelProfile', 111, 5555, MagicMock( return_value=response.Response( status=200, message={ 'type': 'alw', 'account': {'vendor_id': 888, 'subaccount_id': 999}, }, ) ), True, { context_constants.PROFILE_TYPE_AND_ID: 'LabelProfile:111', context_constants.IDENTITY_ID: '5555', context_constants.USER_ID: 'alw:111', context_constants.VENDOR_ID: '888', context_constants.SUBACCOUNT_ID: '999', }, ), # OrchAdminProfile, verify alw: added ( 'OrchAdminProfile', 789, 5555, MagicMock( return_value=response.Response(status=200, message={'type': 'oa'}) ), True, { context_constants.PROFILE_TYPE_AND_ID: 'OrchAdminProfile:789', context_constants.IDENTITY_ID: '5555', context_constants.USER_ID: 'oa:789', }, ), ], ) def test_validate_profile( monkeypatch, profile_type, profile_id, identity_id, mock_ows_users_response, expected_response, context_to_dict_response, ): """Test ProfileContext.validate().""" instance = context.ProfileContext( profile_type=profile_type, profile_id=profile_id, identity_id=identity_id ) monkeypatch.setattr( ows_users, 'get_user_details_by_user_id', mock_ows_users_response or MagicMock() ) validation_response = instance.validate() if hasattr(expected_response, 'status'): assert validation_response.status == expected_response.status assert validation_response.errors == expected_response.errors else: assert validation_response == expected_response assert instance.to_dict() == context_to_dict_response