"""Test for salessheets_generation logic module.""" from flexmock import flexmock from oto import response from oto import status import pytest from salessheets.constants import error from salessheets.constants import features as feature_constants from salessheets.constants import ows_services from salessheets.constants import salessheets from salessheets.logic import salessheets_generation from salessheets.models import ows_account from salessheets.models import ows_product from salessheets.models import release from salessheets.models import salessheets_daemon from salessheets.models import vendor_default_template @pytest.mark.parametrize('template_type', [None, 'orchard']) @pytest.mark.parametrize( 'context, context_type, grass_account_type, grass_account_id, ' 'product_ownership_status', [ ([12341234, 1234123478], 'upc', 'vendor', '123', 200), (['012341234', '01234123478'], 'display_upc', 'vendor', '123', 200), ([12341234, 1234123478], 'upc', '', '', None)]) def test_trigger_salessheets_generation_job_by_display_upc_success( context, context_type, grass_account_type, grass_account_id, product_ownership_status, template_type, feature_engine): """Test trigger_salessheets_generation_job for successful result.""" correlation_id = '123' user_id = 'oa:1' release_ids = ('1234512345', '123451234567') localized_template_type_id = None generation_method = salessheets.ALLOWED_GENERATION_METHODS[0] release_data = response.Response(release_ids) job_data = response.Response('1') job_id = job_data.message # If no grass headers were provided validation will not be called. if grass_account_type and grass_account_id: (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( grass_account_type, grass_account_id, str(release_ids[0])) .and_return(response.Response(status=product_ownership_status)) .ordered()) (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( grass_account_type, grass_account_id, str(release_ids[1])) .and_return(response.Response(status=product_ownership_status)) .ordered()) (flexmock(release) .should_receive('get_multiple_release_ids') .with_args(context, context_type) .and_return(release_data) .once()) (flexmock(salessheets_generation) .should_receive('create_job') .with_args( user_id=user_id, context=context, context_type=context_type, output_format=generation_method) .and_return(job_data) .once()) (flexmock(salessheets_daemon) .should_receive('put_job_to_sqs') .with_args( job_id, release_ids, generation_method, correlation_id, user_id, template_type, localized_template_type_id) .and_return(job_data) .once()) result = ( salessheets_generation .trigger_salessheets_generation_job_duplicate_upc( context, context_type, generation_method, user_id, correlation_id, grass_account_type, grass_account_id, template_type)) assert result @pytest.mark.parametrize('localized_template_type_id', [None, 1]) @pytest.mark.parametrize('template_type', [None, 'orchard']) def test_trigger_salessheets_generation_success_for_single_if_duplicate_upc( template_type, localized_template_type_id, feature_engine): """Test trigger_salessheets_generation_job successful for single.""" correlation_id = '123' user_id = 'oa:1' context = [123456789] release_ids = (123456789,) context_type = 'release_id' grass_account_type = 'vendor' grass_account_id = '123' product_ownership_status = 200 generation_method = salessheets.ALLOWED_GENERATION_METHODS[0] job_data = response.Response('1') job_id = job_data.message # If no grass headers were provided validation will not be called. if grass_account_type and grass_account_id: (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( grass_account_type, grass_account_id, str(context[0])) .and_return(response.Response(status=product_ownership_status)) .ordered()) (flexmock(salessheets_generation) .should_receive('create_job') .with_args( user_id=user_id, context=context, context_type=context_type, output_format=generation_method) .and_return(job_data) .once()) (flexmock(salessheets_daemon) .should_receive('put_job_to_sqs') .with_args( job_id, release_ids, generation_method, correlation_id, user_id, template_type, localized_template_type_id) .and_return(job_data) .once()) result = ( salessheets_generation .trigger_salessheets_generation_job_duplicate_upc( context, context_type, generation_method, user_id, correlation_id, grass_account_type, grass_account_id, template_type, localized_template_type_id)) assert result @pytest.mark.parametrize( ('grass_account_type, grass_account_id, first_product_ownership_status,' 'second_product_ownership_status'), [ ('vendor', '12345', 403, 200), ('vendor', '12345', 200, 403)]) def test_trigger_salessheets_generation_fail_ownership_validation_display_upc( grass_account_type, grass_account_id, first_product_ownership_status, second_product_ownership_status): """Test trigger_salessheets_generation_job for ownership validation.""" correlation_id = '123' user_id = 'oa:1' context = ['012341234', '01234123478'] context_type = salessheets.DISPLAY_UPC generation_method = salessheets.ALLOWED_GENERATION_METHODS[0] release_ids = ('1234512345', '123451234567') release_data = response.Response(release_ids) (flexmock(release) .should_receive('get_multiple_release_ids') .with_args(context, context_type) .and_return(release_data) .once()) (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( grass_account_type, grass_account_id, str(release_ids[0])) .and_return(response.Response(status=first_product_ownership_status)) .once()) # If previous validation has failed the next one will not be called. if first_product_ownership_status == 200: (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( grass_account_type, grass_account_id, str(release_ids[1])) .and_return(response.Response(status=second_product_ownership_status)) .once()) result = ( salessheets_generation .trigger_salessheets_generation_job_duplicate_upc( context, context_type, generation_method, user_id, correlation_id, grass_account_type, grass_account_id)) assert not result def test_trigger_salessheets_generation_job_by_display_upc_failed_get_ids(): """Test trigger_salessheets_generation_job by display_upc fail. Test trigger_salessheets_generation_job for expected fail on get_multiple_release_ids by display_upc step. """ correlation_id = '123' user_id = 'oa:1' context = [12341234] context_type = salessheets.DISPLAY_UPC generation_method = salessheets.ALLOWED_GENERATION_METHODS[0] expected_error_message = 'Something went wrong on get ids step' expected_error = response.create_fatal_response(expected_error_message) (flexmock(release) .should_receive('get_multiple_release_ids') .with_args(context, context_type) .and_return(expected_error) .once()) result = ( salessheets_generation .trigger_salessheets_generation_job_duplicate_upc( context, context_type, generation_method, user_id, correlation_id)) assert not result assert result.errors['message'] == expected_error_message def test_trigger_salessheets_generation_by_display_upc_failed_on_create_job( feature_engine): """Test trigger_salessheets_generation_job for fail. Test trigger_salessheets_generation_job for duplicate_upc for expected fail on create_job step. """ correlation_id = '123' user_id = 'oa:1' context = [12341234] context_type = salessheets.DISPLAY_UPC generation_method = salessheets.ALLOWED_GENERATION_METHODS[0] release_data = response.Response(1234512345) expected_error_message = 'Something went wrong on create_job state' job_data = response.create_fatal_response(expected_error_message) (flexmock(release) .should_receive('get_multiple_release_ids') .with_args(context, context_type) .and_return(release_data) .once()) (flexmock(salessheets_generation) .should_receive('create_job') .with_args( user_id=user_id, context=context, context_type=context_type, output_format=generation_method) .and_return(job_data) .once()) result = ( salessheets_generation .trigger_salessheets_generation_job_duplicate_upc( context, context_type, generation_method, user_id, correlation_id)) assert not result assert result.errors['message'] == expected_error_message @pytest.mark.parametrize('template_type', [None, 'orchard']) def test_trigger_salessheets_generation_failed_duplicate_upc_put_job_to_sqs( template_type, feature_engine): """Test trigger_salessheets_generation_job for duplicate_upc for fail. Test trigger_salessheets_generation_job for expected fail on put_job_to_sqs step. """ correlation_id = '123' user_id = 'oa:1' context = [12341234, 919274] context_type = salessheets.DISPLAY_UPC release_ids = ('123416123', '123416123') generation_method = salessheets.ALLOWED_GENERATION_METHODS[0] release_data = response.Response(release_ids) job_data = response.Response('1') job_id = job_data.message expected_error_message = 'Something went wrong on put_job_to_sqs state' expected_error = response.create_fatal_response(expected_error_message) (flexmock(release) .should_receive('get_multiple_release_ids') .with_args(context, context_type) .and_return(release_data) .once()) (flexmock(salessheets_generation) .should_receive('create_job') .with_args( user_id=user_id, context=context, context_type=context_type, output_format=generation_method) .and_return(job_data) .once()) (flexmock(salessheets_daemon) .should_receive('put_job_to_sqs') .with_args( job_id, release_ids, generation_method, correlation_id, user_id, template_type, None) .and_return(expected_error) .once()) result = ( salessheets_generation .trigger_salessheets_generation_job_duplicate_upc( context=context, context_type=context_type, generation_method=generation_method, user_id=user_id, correlation_id=correlation_id, template_type=template_type)) assert not result assert result.errors['message'] == expected_error_message @pytest.mark.parametrize( 'account_type, account_id, get_subaccount_by_id_times_called', [ ('vendor', 12345, 0), ('subaccount', 34567, 1) ] ) def test_get_localized_template_type_id_for_vendor( account_type, account_id, get_subaccount_by_id_times_called): """Expect get_localized_template_type_id_for_vendor success result. Expect that get_localized_template_type_id_for_vendor returns Response containing localized_template_type_id in case of success result. """ test_vendor_id = 12345 subaccount_response = { 'subaccount_id': 34567, 'vendor_id': test_vendor_id, 'subaccount_name': 'test name', 'description': 'test_description', 'country_id': 3 } vendor_default_template_response = { 'vendor_id': test_vendor_id, 'template_id': 2, } (flexmock(ows_account) .should_receive('get_subaccount_by_id') .with_args(account_id) .and_return(response.Response(subaccount_response)) .times(get_subaccount_by_id_times_called)) (flexmock(vendor_default_template) .should_receive('get_vendor_default_template') .with_args(test_vendor_id) .and_return(response.Response(vendor_default_template_response)) .once()) result = salessheets_generation.get_localized_template_type_id_for_vendor( account_type, account_id) assert result assert result.message['localized_template_type_id'] == 2 @pytest.mark.parametrize( ( 'get_subaccount_by_id_resp', 'get_vendor_default_template_times_called', 'get_vendor_default_template_resp', 'expected_error_code'), [ ( response.create_error_response( code=ows_services.CODE_FORBIDDEN_PRODUCT, message='Test error message', status=status.BAD_REQUEST), 0, None, status.BAD_REQUEST ), ( response.Response( { 'subaccount_id': 34567, 'vendor_id': 34567, 'subaccount_name': 'test name', 'description': 'test_description', 'country_id': 3 } ), 1, response.create_not_found_response(error.TEMPLATE_DOES_NOT_EXIST), status.NOT_FOUND ) ] ) def test_get_localized_template_type_id_for_vendor_failure( get_subaccount_by_id_resp, get_vendor_default_template_times_called, get_vendor_default_template_resp, expected_error_code): """Expect get_localized_template_type_id_for_vendor failure. Expect that get_localized_template_type_id_for_vendor returns correct response in case of failure. """ account_type = 'subaccount' account_id = 34567 (flexmock(ows_account) .should_receive('get_subaccount_by_id') .with_args(account_id) .and_return(get_subaccount_by_id_resp) .once()) (flexmock(vendor_default_template) .should_receive('get_vendor_default_template') .with_args(account_id) .and_return(get_vendor_default_template_resp) .times(get_vendor_default_template_times_called)) result = salessheets_generation.get_localized_template_type_id_for_vendor( account_type, account_id) assert not result assert result.status == expected_error_code def test_trigger_salessheets_generation_success_if_localized_enabled( feature_engine): """Test trigger_salessheets_generation_job successful for single. Expect that trigger_salessheets_generation_job returns correct result if localized_sales_sheets feature flag is enabled, the generation is triggered for workstation user and generation method is single. """ correlation_id = '123' user_id = 'alw:198765' context = [123456789] release_ids = (123456789,) context_type = 'release_id' grass_account_type = 'vendor' grass_account_id = '123' product_ownership_status = 200 generation_method = salessheets.ALLOWED_GENERATION_METHODS[0] template_type = 'orchard' test_localized_templ_id = 3 job_data = response.Response('1') job_id = job_data.message feature_engine.force_flag( feature_constants.LOCALIZED_SALES_SHEETS, True) (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( grass_account_type, grass_account_id, str(context[0])) .and_return(response.Response(status=product_ownership_status)) .ordered()) (flexmock(salessheets_generation) .should_receive('get_localized_template_type_id_for_vendor') .with_args(grass_account_type, grass_account_id) .and_return(response.Response( message={'localized_template_type_id': test_localized_templ_id})) .once()) (flexmock(salessheets_generation) .should_receive('create_job') .with_args( user_id=user_id, context=context, context_type=context_type, output_format=generation_method) .and_return(job_data) .once()) (flexmock(salessheets_daemon) .should_receive('put_job_to_sqs') .with_args( job_id, release_ids, generation_method, correlation_id, user_id, template_type, test_localized_templ_id) .and_return(job_data) .once()) result = ( salessheets_generation .trigger_salessheets_generation_job_duplicate_upc( context, context_type, generation_method, user_id, correlation_id, grass_account_type, grass_account_id, template_type)) assert result def test_trigger_salessheets_generation_get_localized_template_fails( feature_engine): """Test trigger_salessheets_generation_job fails. Expect that trigger_salessheets_generation_job fails if get_localized_template_type_id_for_vendor returns error. """ correlation_id = '123' user_id = 'alw:198765' context = [123456789] context_type = 'release_id' grass_account_type = 'vendor' grass_account_id = '123' product_ownership_status = 200 generation_method = salessheets.ALLOWED_GENERATION_METHODS[0] template_type = 'orchard' job_data = response.Response('1') feature_engine.force_flag( feature_constants.LOCALIZED_SALES_SHEETS, True) (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( grass_account_type, grass_account_id, str(context[0])) .and_return(response.Response(status=product_ownership_status)) .ordered()) (flexmock(salessheets_generation) .should_receive('get_localized_template_type_id_for_vendor') .with_args(grass_account_type, grass_account_id) .and_return( response.create_not_found_response(error.TEMPLATE_DOES_NOT_EXIST)) .once()) (flexmock(salessheets_generation) .should_receive('create_job') .with_args( user_id=user_id, context=context, context_type=context_type, output_format=generation_method) .and_return(job_data) .never()) result = ( salessheets_generation .trigger_salessheets_generation_job_duplicate_upc( context, context_type, generation_method, user_id, correlation_id, grass_account_type, grass_account_id, template_type)) assert not result assert result.status == 404