"""Tests for refresh_ownership endpoint.""" from decimal import Decimal import json from unittest.mock import Mock from unittest.mock import patch from moto import mock_aws import boto3 import pytest from masters_registry.models import ownership from masters_registry.models import yt_ownership from tests import test_utils from tests.helpers import patches from masters_registry.connectors.dynamodb import client as outside_dynamodb_client dynamodb_resource = boto3.resource("dynamodb") def dynamodb_get_item_response(): """Response containing item from DynamoDB for given ISRC.""" item = { 'Item': { 'territories': { 'US': [{'tuid': Decimal('12345678')}] }, 'locked_territories': {}, 'isrc': 'QA123', 'updated_timestamp': Decimal('1514457624.334598'), 'timestamp': Decimal('1489014691557.2031') }, 'ResponseMetadata': {} } return item @pytest.fixture def dynamodb_get_empty_response(): """Response containing empty response from DynamoDB for given ISRC.""" item = { 'Item': {}, 'ResponseMetadata': {} } return item @pytest.fixture def test_headers(): """Headers required to test this endpoint.""" headers = { 'Correlation-Id': 'test', 'Content-Type': 'application/json' } return headers @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.logic.masters_registry._is_valid_isrc_tuid', new=lambda *args: True) @patch( 'masters_registry.models.yt_ownership.send_message', new=Mock()) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_functional_tests_patch) @mock_aws def test_refresh_ownership( client, test_headers, feature_engine, test_isrc, setup_masters_active_table, seed_masters_active_table): """Expect ownership to remain the same and message to be sent to queue.""" from moto.core import patch_client, patch_resource patch_client(outside_dynamodb_client) patch_resource(dynamodb_resource) setup_masters_active_table(dynamodb_resource.meta.client) seed_masters_active_table( dynamodb_resource.meta.client, item=dynamodb_get_item_response()['Item']) ignore_fields = ['timestamp', 'updated_timestamp'] data = json.dumps({ 'tuid': 12345678 }) refresh_url = '/ownership/isrc/{}/refresh'.format(test_isrc) client.put(refresh_url, headers=test_headers, data=data) get_response = ownership.get_ownership(test_isrc) expected_saved_item = { 'isrc': test_isrc, 'locked_territories': {}, 'territories': { 'US': [ {'tuid': Decimal('12345678')}], }} assert test_utils.equal_dicts( get_response.message, expected_saved_item, ignore_fields) yt_ownership.send_message.assert_called_with( test_isrc, {'US'}, 'test') @patch( 'masters_registry.models.ownership.get_ownership', new=patches.db_empty_response ) @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.logic.masters_registry._is_valid_isrc_tuid', new=lambda *args: True) @patch( 'masters_registry.models.yt_ownership.send_message', new=Mock()) @patch( 'masters_registry.models.ownership.get_upc_by_tuid', new=patches.ownership_get_upc_by_tuid_functional_tests_patch) @mock_aws def test_refresh_ownership_empty( client, test_headers, feature_engine, test_isrc): """Expect ownership to remain empty and message to be sent to queue.""" data = json.dumps({ 'tuid': 12345678 }) refresh_url = '/ownership/isrc/{}/refresh'.format(test_isrc) client.put(refresh_url, headers=test_headers, data=data) yt_ownership.send_message.assert_called_with( test_isrc, {}, 'test')