import base64 import json from unittest.mock import Mock from flexmock import flexmock from googleapiclient.errors import HttpError import pytest from ytownership.connectors.sqs import JSONMessageExt from ytownership.logic.patch_ownership import patch_ownership from ytownership.utils.misc import DotDict @pytest.fixture def patched_update_ownership_response(): return { 'general': [{ 'territories': ['US', 'CA'], 'type': 'include' }] } @pytest.fixture def patched_get_ownership_response(): return { 'general': [{ 'territories': ['RU'], 'type': 'include' }] } @pytest.fixture def patched_asset_search_response(): return { 'items': [{ 'type': 'sound_recording', 'id': '2' }] } @pytest.fixture def patched_ownership_history_response(): return { 'items': [{ 'origination': { 'owner': 'J8vAyKuNSYBIN_9RIdxggQ' } }] } @pytest.fixture def mock_message(): body = { 'isrc': '123', 'territories': ['US', 'CA'] } message_attributes = { 'Correlation-Id': {'StringValue': 123}} message_body = json.dumps(body) message_body = base64.b64encode( message_body.encode('utf-8')).decode('utf-8') return flexmock(body=message_body, message_attributes=message_attributes) @pytest.fixture def mock_youtube_client( patched_asset_search_response, patched_ownership_history_response, patched_get_ownership_response, patched_update_ownership_response): def ownership(): def patch(assetId, onBehalfOfContentOwner, body): def execute(): return patched_update_ownership_response return Mock(execute=execute) def get(assetId, onBehalfOfContentOwner): def execute(): return patched_get_ownership_response return Mock(execute=execute) return Mock(patch=patch, get=get) def assetSearch(): def list( isrcs, onBehalfOfContentOwner, ownershipRestriction): def execute(): return patched_asset_search_response return Mock(execute=execute) return Mock(list=list) def ownershipHistory(): def list( assetId, onBehalfOfContentOwner): def execute(): return patched_ownership_history_response return Mock(execute=execute) return Mock(list=list) return Mock( ownership=ownership, assetSearch=assetSearch, ownershipHistory=ownershipHistory) @pytest.fixture def mock_youtube_client_exception( patched_get_ownership_response, patched_update_ownership_response): def ownership(): def patch(assetId, onBehalfOfContentOwner, body): def execute(): return patched_update_ownership_response return Mock(execute=execute) def get(assetId, onBehalfOfContentOwner): def execute(): return patched_get_ownership_response return Mock(execute=execute) return Mock(patch=patch, get=get) def assetSearch(): def list( isrcs, onBehalfOfContentOwner, ownershipRestriction): def execute(): resp = DotDict() resp.status = 500 raise HttpError(resp=resp, content=b'', uri='http://google.com') return Mock(execute=execute) return Mock(list=list) return Mock(ownership=ownership, assetSearch=assetSearch) @pytest.fixture def mock_youtube_client_404_error( patched_get_ownership_response, patched_update_ownership_response): def ownership(): def patch(assetId, onBehalfOfContentOwner, body): def execute(): return patched_update_ownership_response return Mock(execute=execute) def get(assetId, onBehalfOfContentOwner): def execute(): return patched_get_ownership_response return Mock(execute=execute) return Mock(patch=patch, get=get) def assetSearch(): def list( isrcs, onBehalfOfContentOwner, ownershipRestriction): def execute(): resp = DotDict() resp.status = 404 raise HttpError(resp=resp, content=b'{"reason": "Asset not found"}', uri='') return Mock(execute=execute) return Mock(list=list) return Mock(ownership=ownership, assetSearch=assetSearch) def test_patch_ownership_success(mock_message, mock_youtube_client): """Test for successful result of patching. """ patched_message = JSONMessageExt(mock_message) patched_message.context.youtube_client = mock_youtube_client result = patch_ownership(patched_message) assert result.status == 200 assert result.message == 'Successful patched.' def test_patch_ownership_fail(mock_message, mock_youtube_client_exception): """Test for failed of patch operation. """ mock_msg = flexmock(mock_message) patched_message = JSONMessageExt(mock_msg) patched_message.context.youtube_client = mock_youtube_client_exception result = patch_ownership(patched_message) assert result.status == 500 assert result.message == 'Error received.' def test_patch_ownership_asset_not_found( mock_message, mock_youtube_client_404_error): """Test for error. """ mock_msg = flexmock(mock_message) patched_message = JSONMessageExt(mock_msg) patched_message.context.youtube_client = mock_youtube_client_404_error result = patch_ownership(patched_message) assert result.status == 404