"""Unit tests for dms_carveout module.""" from unittest.mock import Mock from unittest.mock import patch import uuid from sqlalchemy import exc from masters_registry import utils from masters_registry.constant import error from masters_registry.constant import field_const from masters_registry.logic import dms_carveout from masters_registry.models import ownership from masters_registry.models import yt_ownership from tests.helpers import patches TUID = 3333 TERRITORIES = ['CA', 'US'] ISRC = 'US4R30920517' @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.ows_carveouts_get_dms_carveout_for_upc_store_carved_out_patch) def test_tuid_check_dms_carveout_store_carveout_exists(): """Assert check_dms_carveout returns empty territory list if store-level carveout exists. """ correlation_id = str(uuid.uuid1()) result = dms_carveout.tuid_check_dms_carveout( TUID, TERRITORIES, correlation_id) assert result assert len(result.message[field_const.ALLOWED_TERRITORIES]) == 0 @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.get_dms_carveout_for_upc_store_substore_carved_out_patch) def test_tuid_check_dms_carveout_store_substore_carveout_exists(): """Assert check_dms_carveout returns empty territory list if store-level carveout exists. """ correlation_id = str(uuid.uuid1()) result = dms_carveout.tuid_check_dms_carveout( TUID, TERRITORIES, correlation_id) assert result assert len(result.message[field_const.ALLOWED_TERRITORIES]) == 0 @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.get_dms_carveout_for_upc_substore_carved_out_patch) def test_tuid_check_dms_carveout_substore_carveout_exists(): """Assert check_dms_carveout returns territories that are not carved-out if a substore-level carveout exists. """ correlation_id = str(uuid.uuid1()) result = dms_carveout.tuid_check_dms_carveout( TUID, TERRITORIES, correlation_id) assert result assert result.message == { field_const.ALLOWED_TERRITORIES: {'US'}, field_const.SUBSTORE_CARVEOUTS: ('453', {'CA'}) } @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.ows_carveouts_get_dms_carveout_for_upc_not_carved_out_patch) def test_tuid_check_dms_carveout_no_carveouts(): """Assert check_dms_carveout returns the original territory list if there is no store or sub-store level carveouts. """ correlation_id = str(uuid.uuid1()) result = dms_carveout.tuid_check_dms_carveout( TUID, TERRITORIES, correlation_id) assert result assert result.message == { field_const.ALLOWED_TERRITORIES: {'CA', 'US'}, field_const.SUBSTORE_CARVEOUTS: ('453', set()) } @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_error_patch) def test_tuid_check_dms_carveout_failed_to_get_upc(): """Assert check_dms_carveout returns error response if it failed to get a upc for provided tuid. """ correlation_id = str(uuid.uuid1()) result = dms_carveout.tuid_check_dms_carveout( TUID, TERRITORIES, correlation_id) assert not result @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.ows_carveouts_get_dms_carveout_failed_patch) def test_tuid_check_dms_carveout_failed_to_get_carveouts(): """Assert check_dms_carveout returns error response if it failed to get dms carveouts from ows-carveouts service. """ correlation_id = str(uuid.uuid1()) result = dms_carveout.tuid_check_dms_carveout( TUID, TERRITORIES, correlation_id) assert not result @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_single_owner_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.ows_carveouts_get_dms_carveout_for_upc_store_carved_out_patch) def test_ocdc_single_owner_store_carveout(): """Assert ownership_check_dms_carveout returns valid response for ownership record with a single rightsholder claim that has a store-level carveout. """ unclaimed_territory = 'US' carveout_territory = 'CA' ownership_info = ownership.get_ownership(ISRC).message correlation_id = str(uuid.uuid1()) result = dms_carveout.ownership_check_dms_carveout( ownership_info, TERRITORIES, correlation_id) assert result assert unclaimed_territory in result.message assert carveout_territory not in result.message @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_single_owner_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.get_dms_carveout_for_upc_substore_carved_out_patch) def test_ocdc_single_owner_substore_carveout(): """Assert ownership_check_dms_carveout returns valid response for ownership record with a single rightsholder claim that has a substore-level carveout """ unclaimed_territory = 'US' carveout_territory = 'CA' ownership_info = ownership.get_ownership(ISRC).message correlation_id = str(uuid.uuid1()) result = dms_carveout.ownership_check_dms_carveout( ownership_info, TERRITORIES, correlation_id) assert result assert unclaimed_territory in result.message assert carveout_territory not in result.message @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_single_owner_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.get_dms_carveout_for_upc_store_substore_carved_out_patch) def test_ocdc_single_owner_store_substore_carveout(): """Assert ownership_check_dms_carveout returns valid response for ownership record with a single rightsholder claim that has both store and substore-level carveouts. """ unclaimed_territory = 'US' carveout_territory = 'CA' ownership_info = ownership.get_ownership(ISRC).message correlation_id = str(uuid.uuid1()) result = dms_carveout.ownership_check_dms_carveout( ownership_info, TERRITORIES, correlation_id) assert result assert unclaimed_territory in result.message assert carveout_territory not in result.message @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_single_owner_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.ows_carveouts_get_dms_carveout_for_upc_not_carved_out_patch) def test_ocdc_single_owner_no_carveouts(): """Assert ownership_check_dms_carveout returns valid response for ownership record that has no carve-outs. """ ownership_info = ownership.get_ownership(ISRC).message correlation_id = str(uuid.uuid1()) result = dms_carveout.ownership_check_dms_carveout( ownership_info, TERRITORIES, correlation_id) assert result assert sorted(TERRITORIES) == sorted(result.message) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_multi_owner_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.ows_carveouts_get_dms_carveout_for_upc_store_carved_out_patch) def test_ocdc_multi_owner_store_carveout(): """Assert ownership_check_dms_carveout returns valid response for ownership record with multiple rightsholder claims that has store-level carveouts. """ ownership_info = ownership.get_ownership(ISRC).message correlation_id = str(uuid.uuid1()) result = dms_carveout.ownership_check_dms_carveout( ownership_info, TERRITORIES, correlation_id) assert result assert len(result.message) == 0 @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_multi_owner_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.get_dms_carveout_for_upc_substore_carved_out_patch) def test_ocdc_multi_owner_substore_carveout(): """Assert ownership_check_dms_carveout returns valid response for ownership record with multiple rightsholder claims that has substore-level carveouts """ ownership_info = ownership.get_ownership(ISRC).message correlation_id = str(uuid.uuid1()) result = dms_carveout.ownership_check_dms_carveout( ownership_info, TERRITORIES, correlation_id) assert result assert len(result.message) == 0 @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_multi_owner_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.get_dms_carveout_for_upc_store_substore_carved_out_patch) def test_ocdc_multi_owner_store_substore_carveout(): """Assert ownership_check_dms_carveout returns valid response for ownership record with multiple rightsholder claims that has both store and substore-level carveouts. """ ownership_info = ownership.get_ownership(ISRC).message correlation_id = str(uuid.uuid1()) result = dms_carveout.ownership_check_dms_carveout( ownership_info, TERRITORIES, correlation_id) assert result assert len(result.message) == 0 @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_multi_owner_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.ows_carveouts_get_dms_carveout_for_upc_not_carved_out_patch) def test_ocdc_multi_owner_no_carveouts(): """Assert ownership_check_dms_carveout returns valid response for ownership record with multiple rightsholder claims that has no carveouts. """ ownership_info = ownership.get_ownership(ISRC).message correlation_id = str(uuid.uuid1()) result = dms_carveout.ownership_check_dms_carveout( ownership_info, TERRITORIES, correlation_id) assert result assert sorted(TERRITORIES) == sorted(result.message) @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_multi_owner_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.ows_carveouts_get_carveout_without_full_track_distro) def test_tuid_check_dms_carveout_without_full_track_distro(): """Assert ownership_check_dms_carveout returns valid response for ownership record with carveout that has a store level carveout but not for full track. """ ownership_info = ownership.get_ownership(ISRC).message correlation_id = str(uuid.uuid1()) result = dms_carveout.ownership_check_dms_carveout( ownership_info, TERRITORIES, correlation_id) assert result assert sorted(TERRITORIES) == sorted(result.message) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_multi_owner_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_error_patch) def test_ocdc_failed_to_get_upc(): """Assert ownership_check_dms_carveout returns valid response for ownership record with multiple rightsholder claims when it failed to get UPCs for TUIDs in the onwnership record. """ ownership_info = ownership.get_ownership(ISRC).message correlation_id = str(uuid.uuid1()) result = dms_carveout.ownership_check_dms_carveout( ownership_info, TERRITORIES, correlation_id) assert not result @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_multi_owner_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_error_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.ows_carveouts_get_dms_carveout_failed_patch) def test_ocdc_failed_to_get_carveouts(): """Assert ownership_check_dms_carveout returns valid response for ownership record with multiple rightsholder claims when it dailed to get information about carveouts from ows-carveouts service. """ ownership_info = ownership.get_ownership(ISRC).message correlation_id = str(uuid.uuid1()) result = dms_carveout.ownership_check_dms_carveout( ownership_info, TERRITORIES, correlation_id) assert not result @patch( 'masters_registry.logic.dms_carveout.tuid_check_dms_carveout', new=Mock()) @patch('masters_registry.models.yt_ownership.send_message', new=Mock()) def test_process_dms_carveout_no_territories(): """Expect to send message to Youtube without checking DMS carveout.""" processing_response = dms_carveout.send_message_to_yt_ownership( ISRC, TUID, [], 'test_correlation_id') assert processing_response.status == 200 yt_ownership.send_message.assert_called_with( ISRC, [], 'test_correlation_id') assert not dms_carveout.tuid_check_dms_carveout.called @patch( 'masters_registry.models.ownership.mysql.mr_session_scope', new=Mock(side_effect=exc.SQLAlchemyError)) @patch('masters_registry.models.yt_ownership.send_message', new=Mock()) def test_process_dms_carveout_mysql_error(): """Expect to get error response because of mysql error.""" processing_response = dms_carveout.send_message_to_yt_ownership( ISRC, TUID, TERRITORIES, 'test_correlation_id') assert processing_response.status == 500 assert not yt_ownership.send_message.called @patch( 'masters_registry.models.ows_carveouts._get_from_ows_carveouts_service', new=Mock(side_effect=utils.OwsCarveoutError(status_code=500, message=''))) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch('masters_registry.models.yt_ownership.send_message', new=Mock()) def test_process_dms_carveout_ows_carveouts_error(): """Expect error from ows-carveouts.""" processing_response = dms_carveout.send_message_to_yt_ownership( ISRC, TUID, TERRITORIES, 'test_correlation_id') assert processing_response.status == 500 assert processing_response.errors['code'] == error.OWS_CARVEOUTS_ERROR assert not yt_ownership.send_message.called @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.ows_carveouts_get_dms_carveout_for_upc_store_carved_out_patch) @patch('masters_registry.models.yt_ownership.send_message', new=Mock()) def test_process_dms_carveout_no_allowed_territories(): """Expect no message to be sent when there are no allowed territories.""" processing_response = dms_carveout.send_message_to_yt_ownership( ISRC, TUID, TERRITORIES, 'test_correlation_id') assert processing_response.status == 200 assert not yt_ownership.send_message.called @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.models.ows_carveouts.get_dms_carveout_for_upc', new=patches.ows_carveouts_get_dms_carveout_for_upc_store_carved_out_patch) @patch('masters_registry.models.yt_ownership.send_message', new=Mock()) def test_enforce_update_when_no_territories(): """Expect message to be sent when no allowed territories and flag.""" processing_response = dms_carveout.send_message_to_yt_ownership( ISRC, TUID, TERRITORIES, 'test_correlation_id', True) assert processing_response.status == 200 yt_ownership.send_message.assert_called_with( ISRC, [], 'test_correlation_id')