import json from unittest.mock import Mock from unittest.mock import patch from oto.response import Response from masters_registry.constant import error from masters_registry.constant import field_const from masters_registry.logic import conflicts from masters_registry.models import ownership from tests.helpers import patches def test_get_ownership_no_isrc(feature_engine, client, valid_headers): """Test passing in no isrc """ res = client.get( '/ownership', headers=valid_headers) assert json.loads(res.data.decode()) == {} assert 200 == res.status_code def test_get_ownership_blank_isrc(feature_engine, client, valid_headers): """Test passing in blank isrc """ res = client.get( '/ownership?isrc=', headers=valid_headers) assert json.loads(res.data.decode()) == {} assert 200 == res.status_code @patch( 'masters_registry.models.ownership.get_tracks', new=patches.ownership_get_tracks_patch) @patch( 'masters_registry.models.ownership.get_ownership', new=patches.ownership_get_ownership_patch) @patch( 'masters_registry.models.ows_territories.get_complement_territories', new=patches.ownership_get_complement_patch) def test_get_ownership_valid_isrc( feature_engine, client, valid_headers, monkeypatch): """Test passing in a valid isrc that exists in the system """ get_lock_history = Mock(return_value=True) monkeypatch.setattr( ownership, 'get_lock_history_for_ownership', get_lock_history) get_conflicts_history = Mock(return_value=True) monkeypatch.setattr( conflicts, 'get_conflicts_history_for_ownership', get_conflicts_history) test_isrc = 'US4R30920517' res = client.get( '/ownership?isrc={}'.format(test_isrc), headers=valid_headers) assert 200 == res.status_code json_response = json.loads(res.data.decode()) assert test_isrc == json_response[field_const.ISRC] assert field_const.LOCKED_TERRITORIES in json_response assert field_const.TRACKS in json_response assert field_const.UNCLAIMED_TERRITORIES in json_response def test_get_ownership_missing_correlation_id(client): """Test that missing Correlation-Id in request header returns validation err """ res = client.get('/ownership?isrc=abc123') assert 400 == res.status_code json_response = json.loads(res.data.decode()) assert error.HEADER_VALIDATION_ERROR == json_response[field_const.CODE] assert "'{}' is a required property".format( field_const.CORRELATION_ID) in json_response[field_const.MESSAGE] def test_get_masterrights_no_isrcs(client, valid_headers): """Test 404 response """ res = client.get( '/mastersrights', headers=valid_headers) assert 404 == res.status_code @patch( 'masters_registry.logic.ownership.bulk_get_ownership', return_value=Response('ok')) def test_get_masterrights(mock_bulk_get_rights, client, valid_headers): """Test 200 response """ res = client.get( '/masterrights?isrcs=QA123,QA1234', headers=valid_headers) assert 200 == res.status_code mock_bulk_get_rights.assert_called_with(isrcs=['QA123', 'QA1234'], ignore_missing=False) @patch( 'masters_registry.logic.ownership.bulk_get_ownership', return_value=Response('ok')) def test_get_masterrights_with_post_method(mock_bulk_get_rights, client, valid_headers): """Test 200 response """ data = { field_const.ISRCS: ['QA123', 'QA1234'] } valid_headers['Content-Type'] = 'application/json' res = client.post( '/masterrights', headers=valid_headers, data=json.dumps(data)) assert 200 == res.status_code mock_bulk_get_rights.assert_called_with(isrcs=['QA123', 'QA1234'], ignore_missing=False) @patch( 'masters_registry.logic.ownership.bulk_get_ownership', return_value=Response('ok')) def test_get_masterrights_not_ignore_missing(mock_bulk_get_rights, client, valid_headers): """Test 200 response """ res = client.get( '/masterrights?isrcs=QA123,QA1234&ignore_missing=false', headers=valid_headers) assert 200 == res.status_code mock_bulk_get_rights.assert_called_with(isrcs=['QA123', 'QA1234'], ignore_missing=False) @patch( 'masters_registry.logic.ownership.bulk_get_ownership', return_value=Response('ok')) def test_get_masterrights_ignore_missing(mock_bulk_get_rights, client, valid_headers): """Test 200 response """ res = client.get( '/masterrights?isrcs=QA123,QA1234&ignore_missing=true', headers=valid_headers) assert 200 == res.status_code mock_bulk_get_rights.assert_called_with(isrcs=['QA123', 'QA1234'], ignore_missing=True)