"""Tests for update ownership logic layer.""" from unittest.mock import Mock from unittest.mock import patch from oto import response from masters_registry.constant import error from masters_registry.constant import field_const from masters_registry.logic import dms_carveout from masters_registry.logic import masters_registry from masters_registry.models import yt_ownership from tests.helpers import patches @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.logic.ownership.update_ownership', new=patches.ownership_update_ownership_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_patch) @patch( 'masters_registry.models.ownership.get_tracks', new=patches.ownership_get_tracks_patch) @patch( 'masters_registry.models.ows_territories.get_territories', new=patches.ows_territories_get_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_error_patch) @patch('masters_registry.models.yt_ownership.send_message', new=Mock()) def test_update_ownership_dms_carveout_error(feature_engine): """Expect to get error response from ows-carveouts.""" test_isrc = 'ZW1010600008' test_tuid = 12345 test_correlation_id = '54321' test_territories = ['DZ', 'AS'] test_user = '1281' ownership_response = masters_registry.update_ownership( test_isrc, test_tuid, test_territories, test_correlation_id, test_user) assert not yt_ownership.send_message.called assert ownership_response.status == 400 assert ownership_response.errors['code'] == 'ows_carveouts_error' @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.logic.ownership.update_ownership', new=patches.ownership_update_ownership_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_single_owner_patch) @patch( 'masters_registry.models.ownership.get_tracks', new=patches.ownership_get_tracks_patch) @patch( 'masters_registry.models.ows_territories.get_territories', new=patches.ows_territories_get_territories_patch) @patch( 'masters_registry.models.ows_territories.get_complement_territories', new=patches.ows_territories_get_error_response) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.logic.dms_carveout.tuid_check_dms_carveout', new=Mock()) @patch('masters_registry.models.yt_ownership.send_message', new=Mock()) def test_update_ownership_get_unclaimed_territories_error_ww_flag_on( feature_engine): """Expect to get error response from ows-territories. List of given territories contains only `worldwide` flag. """ test_isrc = 'ZW1010600008' test_tuid = 12345 test_correlation_id = '54321' test_territories = ['WW'] test_user = '1281' ownership_response = masters_registry.update_ownership( test_isrc, test_tuid, test_territories, test_correlation_id, test_user) assert not yt_ownership.send_message.called assert not dms_carveout.tuid_check_dms_carveout.called assert ownership_response.status == 404 assert ownership_response.errors['code'] == 'ows_territories_error' @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.logic.ownership.update_ownership', new=patches.ownership_update_ownership_nothing_updated_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_no_territories_patch) @patch( 'masters_registry.models.ownership.get_tracks', new=patches.ownership_get_tracks_patch) @patch( 'masters_registry.models.ows_territories.get_territories', new=patches.ows_territories_get_territories_patch) @patch( 'masters_registry.models.ows_territories.get_complement_territories', new=patches.ownership_get_complement_no_territories_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) @patch( 'masters_registry.logic.dms_carveout.tuid_check_dms_carveout', new=Mock()) @patch('masters_registry.models.yt_ownership.send_message', new=Mock()) def test_update_ownership_no_updated_territories(feature_engine): """Expect message to Youtube to be send without calling dms_carveout. This test is for case when user has all territories locked and tries to claim territories passing 'WW' flag. """ test_isrc = 'ZW1010600008' test_tuid = 12345 test_correlation_id = '54321' test_territories = ['WW'] test_user = '1281' ownership_response = masters_registry.update_ownership( test_isrc, test_tuid, test_territories, test_correlation_id, test_user) assert not dms_carveout.tuid_check_dms_carveout.called yt_ownership.send_message.assert_called_with('ZW1010600008', [], '54321') assert ownership_response.status == 200 assert ownership_response.message == {'territories': [], 'tuid': test_tuid} def test_put_ownership_missing_arguments(): test_isrc = '' test_tuid = 4323433 test_correlation_id = '54321' test_territories = ['US', 'MX'] test_user = '1281' ownership_response = masters_registry.update_ownership( test_isrc, test_tuid, test_territories, test_correlation_id, test_user) errors = { 'code': error.MISSING_ARGUMENTS, 'message': 'Update Operation Failed: ISRC, TUID, or territory list ' 'is empty.'} assert 400 == ownership_response.status assert errors == ownership_response.errors @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.logic.ownership.update_ownership', new=patches.ownership_update_ownership_success_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_patch) @patch( 'masters_registry.models.ownership.get_tracks', new=patches.ownership_get_tracks_patch) @patch( 'masters_registry.models.ows_territories.get_territories', new=patches.ows_territories_get_territories_patch) @patch( 'masters_registry.models.ows_territories.get_complement_territories', new=patches.ownership_get_complement_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) @patch('masters_registry.models.yt_ownership.send_message', new=Mock()) def test_put_ownership_worldwide_success(feature_engine): test_isrc = 'ZW1010600008' test_tuid = 12345 test_correlation_id = '54321' test_territories = ['WW'] test_user = '1281' expected_territories = patches.ownership_get_complement_patch('') expected_territories = sorted([ item['territory_code_a2'] for item in expected_territories.message['items']]) ownership_response = masters_registry.update_ownership( test_isrc, test_tuid, test_territories, test_correlation_id, test_user) assert 200 == ownership_response.status message = ownership_response.message assert expected_territories == sorted(message.get(field_const.TERRITORIES)) @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.logic.ownership.update_ownership', new=patches.ownership_update_ownership_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_patch) @patch( 'masters_registry.models.ownership.get_tracks', new=patches.ownership_get_tracks_patch) @patch( 'masters_registry.models.ows_territories.get_territories', new=patches.ows_territories_get_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) @patch('masters_registry.models.yt_ownership.send_message', new=Mock()) def test_put_ownership_success(feature_engine): test_isrc = 'ZW1010600008' test_tuid = 12345 test_correlation_id = '54321' test_territories = ['DZ', 'AS'] test_user = '1281' ownership_response = masters_registry.update_ownership( test_isrc, test_tuid, test_territories, test_correlation_id, test_user) assert yt_ownership.send_message.call_count == 1 assert 200 == ownership_response.status message = ownership_response.message assert len(test_territories) == len(message.get(field_const.TERRITORIES)) @patch( 'masters_registry.logic.ownership.update_ownership', new=patches.ownership_update_ownership_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_patch) @patch( 'masters_registry.models.ownership.get_tracks', new=patches.ownership_get_tracks_patch) @patch( 'masters_registry.models.ows_territories.get_territories', new=patches.ows_territories_get_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_store_carved_out_patch) @patch('masters_registry.models.yt_ownership.send_message', new=Mock()) def test_put_ownership_dms_carveout_success(feature_engine): test_isrc = 'ZW1010600008' test_tuid = 12345 test_correlation_id = '54321' test_territories = ['DZ', 'AS'] test_user = '1281' ownership_response = masters_registry.update_ownership( test_isrc, test_tuid, test_territories, test_correlation_id, test_user) yt_ownership.send_message.assert_not_called() assert 200 == ownership_response.status message = ownership_response.message assert len(test_territories) == len(message.get(field_const.TERRITORIES)) @patch( 'masters_registry.logic.ownership.update_ownership', new=patches.ownership_update_ownership_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=Mock(return_value=response.Response(message={}))) def test_put_ownership_isrc_not_found(): test_isrc = 'ZW1010600008' test_tuid = 12345 test_correlation_id = '54321' test_territories = ['DZ', 'AS'] test_user = '1281' ownership_response = masters_registry.update_ownership( test_isrc, test_tuid, test_territories, test_correlation_id, test_user) errors = { 'code': error.ISRC_NOT_FOUND, 'message': 'ISRC not found in registry'} assert 400 == ownership_response.status assert errors == ownership_response.errors @patch( 'masters_registry.logic.ownership.update_ownership', new=patches.ownership_update_ownership_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_patch) @patch( 'masters_registry.models.ownership.get_tracks', new=patches.ownership_get_tracks_patch) @patch( 'masters_registry.models.ows_territories.get_territories', new=patches.ows_territories_get_territories_patch) def test_put_ownership_invalid_territories(feature_engine): test_isrc = 'ZW1010600008' test_tuid = 12345 test_correlation_id = '54321' test_territories = ['DZ', 'QQ'] test_user = '1281' ownership_response = masters_registry.update_ownership( test_isrc, test_tuid, test_territories, test_correlation_id, test_user) errors = { 'code': error.INVALID_TERRITORIES, 'message': 'Invalid territories'} assert 400 == ownership_response.status assert errors == ownership_response.errors @patch( 'masters_registry.logic.ownership.update_ownership', new=patches.ownership_update_ownership_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_patch) @patch( 'masters_registry.models.ownership.get_tracks', new=Mock(return_value=response.Response(message={}))) def test_put_ownership_invalid_isrc_tuid(): test_isrc = 'ZW1010600008' test_tuid = 4323433 test_correlation_id = '54321' test_territories = ['US', 'MX'] test_user = '1281' ownership_response = masters_registry.update_ownership( test_isrc, test_tuid, test_territories, test_correlation_id, test_user) errors = { 'code': error.INVALID_TUID_ISRC, 'message': 'TUID and ISRC do not match'} assert 400 == ownership_response.status assert errors == ownership_response.errors @patch( 'masters_registry.models.ows_territories.convert_territories', new=patches.ows_territories_convert_territories_patch) @patch( 'masters_registry.logic.ownership.update_ownership', new=patches.ownership_update_ownership_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_patch) @patch( 'masters_registry.models.ownership.get_tracks', new=patches.ownership_get_tracks_patch) @patch( 'masters_registry.models.ows_territories.get_territories', new=patches.ows_territories_get_territories_patch) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_patch) def test_update_ownership_claimed_by_the_same_label(): """Test update_ownership function for the case: some of territories are claimed by the same label """ test_isrc = 'ZW1010600008' test_tuid = 11 test_correlation_id = '54321' test_territories = ['AF', 'CA', 'DZ'] test_user = '1281' ownership_response = masters_registry.update_ownership( test_isrc, test_tuid, test_territories, test_correlation_id, test_user) assert 400 == ownership_response.status errors = ownership_response.errors assert error.TERRITORIES_CLAIMED_BY_THE_SAME_LABEL == errors['code'] message = errors['message'] assert set(message['territories']) == set(['AF', 'CA'])