"""Tests for product handler class.""" from copy import deepcopy import json from mock import Mock, patch import pytest from switchboard_consumer.constants.product_metadata_update import ( DUMMY_PRODUCT_DIFFERENT_TARGET ) from switchboard_consumer.constants.system import ORCHARD, SONY from switchboard_consumer.kafka_message import KafkaMessage from switchboard_consumer.logic.product_handler import ProductHandler from tests.integration.test_product_metadata_update import create_message @pytest.fixture() def mock_data(): return { 'labelAccount': { 'companyCode': '12345' }, 'subAccount': None } def test_rejects_different_product_local_ids( mock_logger, swb_client, orchard_client, get_product_by_upc_response): """Test if SWB and Orchard have different localIds message is rejected.""" swb_response = deepcopy(get_product_by_upc_response) swb_response['ids'] = [ { 'system': SONY, 'localId': '1022869', }, { 'system': ORCHARD, 'localId': '99999', } ] # Orchard response with a different localId. orchard_response = deepcopy(get_product_by_upc_response) orchard_response['ids'] = [ { 'system': SONY, 'localId': '1022869', }, { 'system': ORCHARD, 'localId': '000000', } ] swb_client.get_product_by_upc = Mock(return_value=orchard_response) kafka_message = KafkaMessage(json.dumps(create_message)) product_handler = ProductHandler( swb_response, kafka_message, mock_logger, orchard_client, swb_client) result = product_handler.validate_local_ids() assert result is False processing_results = product_handler.processing_results assert len(processing_results) == 1 response_message = processing_results[0]['errors'][0]['message'] assert response_message == 'Product (UPC \'09876543210987\') with ' \ 'localId \'99999\' in SWB already exists in ' \ 'Orchard with a different localId \'000000\'.' def test_rejects_out_of_date_mapping( mock_logger, swb_client, orchard_client, get_product_by_upc_response): """Test if SWB mapping is out of date a message is rejected.""" swb_response = deepcopy(get_product_by_upc_response) swb_response['ids'] = [ { 'system': SONY, 'localId': '1022869', }, { 'system': ORCHARD, 'localId': '99999', } ] # Orchard response with a different localId. orchard_response = deepcopy(get_product_by_upc_response) orchard_response['ids'] = [ { 'system': SONY, 'localId': '1022869', }, ] swb_client.get_product_by_upc = Mock(return_value=orchard_response) kafka_message = KafkaMessage(json.dumps(create_message)) product_handler = ProductHandler( swb_response, kafka_message, mock_logger, orchard_client, swb_client) result = product_handler.validate_local_ids() assert result is False processing_results = product_handler.processing_results assert len(processing_results) == 1 response_message = processing_results[0]['errors'][0]['message'] assert response_message == 'Product (UPC \'09876543210987\') with ' \ 'localId \'99999\' in SWB does not exist in ' \ 'Orchard.' def test_rejects_bad_mapping( mock_logger, swb_client, orchard_client, get_product_by_upc_response): """Test if SWB mapping is out of date a message is rejected.""" swb_response = deepcopy(get_product_by_upc_response) swb_response['ids'] = [ { 'system': SONY, 'localId': '1022869', }, ] # Orchard response with a different localId. orchard_response = deepcopy(get_product_by_upc_response) orchard_response['ids'] = [ { 'system': SONY, 'localId': '1022869', }, { 'system': ORCHARD, 'localId': '99999', } ] swb_client.get_product_by_upc = Mock(return_value=orchard_response) kafka_message = KafkaMessage(json.dumps(create_message)) product_handler = ProductHandler( swb_response, kafka_message, mock_logger, orchard_client, swb_client) result = product_handler.validate_local_ids() assert result is False processing_results = product_handler.processing_results assert len(processing_results) == 1 response_message = processing_results[0]['errors'][0]['message'] assert response_message == 'Product (UPC \'09876543210987\') with no ' \ 'localId in SWB already exists in Orchard.' def test_allow_updates_for_dummy_products_with_no_mapping( mock_logger, swb_client, orchard_client, get_product_by_upc_response): """Test if validate_local_ids accepts Dummy products.""" swb_response = deepcopy(get_product_by_upc_response) swb_response['ids'] = [ { 'system': SONY, 'localId': '1022869', }, ] # Orchard response with a different localId. orchard_response = deepcopy(get_product_by_upc_response) orchard_response['ids'] = [ { 'system': ORCHARD, 'localId': '99999', } ] swb_client.get_product_by_upc = Mock(return_value=orchard_response) orchard_client.get_product_by_id.return_value = { 'notForDistribution': 'SMEAnalyticsDummy', 'vendorId': 1234, 'subaccountId': None } kafka_message = KafkaMessage(json.dumps(create_message)) product_handler = ProductHandler( swb_response, kafka_message, mock_logger, orchard_client, swb_client) result = product_handler.validate_local_ids() assert result def test_disallow_updates_for_dummy_products_with_incorrect_vendor( mock_logger, swb_client, orchard_client, get_product_by_upc_response): """Test if validate_local_ids disallows Products that aren't Dummy.""" swb_response = deepcopy(get_product_by_upc_response) swb_response['ids'] = [ { 'system': SONY, 'localId': '1022869', }, ] # Orchard response with a different localId. orchard_response = deepcopy(get_product_by_upc_response) orchard_response['ids'] = [ { 'system': ORCHARD, 'localId': '99999', } ] swb_client.get_product_by_upc = Mock(return_value=orchard_response) orchard_client.get_product_by_id.return_value = { 'notForDistribution': 'SMEAnalyticsDummy', 'vendorId': 9876, 'subaccountId': None } kafka_message = KafkaMessage(json.dumps(create_message)) product_handler = ProductHandler( swb_response, kafka_message, mock_logger, orchard_client, swb_client) result = product_handler.validate_local_ids() assert not result processing_results = product_handler.processing_results assert len(processing_results) == 1 response_message = processing_results[0]['errors'][0]['message'] assert response_message == DUMMY_PRODUCT_DIFFERENT_TARGET.format( create_message['ids'][0]['businessKey'], orchard_response['ids'][0]['localId'], orchard_client.get_product_by_id().get('vendorId'), orchard_client.get_product_by_id().get('subaccountId') ) def test_orchard_product_not_found( mock_logger, swb_client, orchard_client, get_product_by_upc_response): """Test if processed correctly if no orchard product found.""" swb_response = deepcopy(get_product_by_upc_response) swb_response['ids'] = [ { 'system': SONY, 'localId': '1022869', }, ] swb_client.get_product_by_upc = Mock(return_value=None) kafka_message = KafkaMessage(json.dumps(create_message)) product_handler = ProductHandler( swb_response, kafka_message, mock_logger, orchard_client, swb_client) result = product_handler.validate_local_ids() assert result is True processing_results = product_handler.processing_results assert len(processing_results) == 0 @patch('switchboard_consumer.logic.product_handler.ProductHandler.' 'update_in_progress_product') def test_update_in_progress_product( update_in_progress_product_mock, mock_logger, orchard_client, mock_data): """Test for updating in-progress product.""" mock_message = Mock() mock_message.sending_system_local_id = {} update_in_progress_product_mock.return_value = True orchard_client.get_product_by_id = Mock(return_value={ 'displayStatus': 'in_progress' }) handler = ProductHandler( mock_data, mock_message, mock_logger, orchard_client) product_id = 123 result = handler.update_product(product_id) assert result is True update_in_progress_product_mock.assert_called_once() @patch('switchboard_consumer.logic.product_handler.ProductHandler.' 'update_in_progress_product') @patch('switchboard_consumer.logic.product_handler.ProductHandler.' 'unsubmit_product') @patch('switchboard_consumer.logic.email.ses.send_email') def test_update_submitted_product( mock_send_email, unsubmit_product_mock, update_in_progress_product_mock, mock_logger, orchard_client, mock_data): """Test for updating submitted product.""" mock_message = Mock() mock_message.sending_system_local_id = {} update_in_progress_product_mock.return_value = True unsubmit_product_mock.return_value = True orchard_client.get_product_by_id = Mock(return_value={ 'displayStatus': 'submitted' }) handler = ProductHandler( mock_data, mock_message, mock_logger, orchard_client) product_id = 123 result = handler.update_product(product_id) assert result is True update_in_progress_product_mock.assert_called_once() unsubmit_product_mock.assert_called_once() mock_send_email.assert_called_once() @patch('switchboard_consumer.logic.product_handler.ProductHandler.' 'update_in_progress_product') @patch('switchboard_consumer.logic.product_handler.ProductHandler.' 'unsubmit_product') @patch('switchboard_consumer.logic.email.ses.send_email') def test_update_submitted_product_unsubmit_failed( mock_send_email, unsubmit_product_mock, update_in_progress_product_mock, mock_logger, orchard_client, mock_data): """Test for updating submitted product.""" mock_message = Mock() mock_message.sending_system_local_id = {} unsubmit_product_mock.return_value = False orchard_client.get_product_by_id = Mock(return_value={ 'displayStatus': 'submitted' }) handler = ProductHandler( mock_data, mock_message, mock_logger, orchard_client) product_id = 123 result = handler.update_product(product_id) assert result is False update_in_progress_product_mock.assert_not_called() unsubmit_product_mock.assert_called_once() # We failed to unsubmit so don't email saying we did unsubmit. mock_send_email.assert_not_called() @patch('switchboard_consumer.logic.product_handler.ProductHandler.' 'update_via_release_correction') def test_update_completed_product( update_via_release_correction_mock, mock_logger, orchard_client, mock_data): """Test for updating completed product.""" mock_message = Mock() mock_message.sending_system_local_id = {} update_via_release_correction_mock.return_value = True orchard_client.get_product_by_id = Mock(return_value={ 'displayStatus': 'completed' }) handler = ProductHandler( mock_data, mock_message, mock_logger, orchard_client) product_id = 123 result = handler.update_product(product_id) assert result is True update_via_release_correction_mock.assert_called_once() @patch('switchboard_consumer.logic.product_handler.ProductHandler.' 'update_via_release_correction') def test_update_error_correction_product( update_via_release_correction_mock, mock_logger, orchard_client, mock_data): """Test for updating error correction product.""" mock_message = Mock() mock_message.sending_system_local_id = {} update_via_release_correction_mock.return_value = True orchard_client.get_product_by_id = Mock(return_value={ 'displayStatus': 'error_correction' }) handler = ProductHandler( mock_data, mock_message, mock_logger, orchard_client) product_id = 123 result = handler.update_product(product_id) assert result is True update_via_release_correction_mock.assert_called_once() def test_populate_from_swb_data(): """Test for populate_from_swb_data.""" mock_data = { 'labelAccount': { 'name': 'labelName', 'companyCode': '12345' }, 'subAccount': None, 'displayArtist': { 'name': 'artistName' }, 'formalTitle': { 'titleText': 'title' } } mock_message = Mock() mock_message.sending_system_local_id = { 'businessKey': '1234567890123' } handler = ProductHandler(mock_data, mock_message, None, None) assert handler.upc == '1234567890123' assert handler.product_title == 'title' assert handler.label_name == 'labelName' assert handler.artist_name == 'artistName' assert handler.vendor_id == int(mock_data['labelAccount']['companyCode']) assert not handler.subaccount_id