"""Test permissions logic layer.""" import flexmock from oto import response as oto_response from owsrequest.constants.headers import PROFILE_TYPE_INSIGHTS import pytest from charts.logic import permissions from charts.services import ows_permissions from charts.constants.service import ARTIST_INFO_RESOURCE_TYPE, VENDOR_RESOURCE_TYPE class TestGetPermissionForInsightsProfile: """Test call to get_all_permissions_for_profile.""" profile_id = '1234' profile_type = 'InsightsProfile' response = { 'items': [] } vendor_item = { 'type': VENDOR_RESOURCE_TYPE, 'name': 'Frenchkiss', 'id': 6971, 'roles': ['analytics'] } artist_items = [ { 'type': ARTIST_INFO_RESOURCE_TYPE, 'name': 'BTS in Bighit Entertainment', 'id': 917636, 'roles': ['analytics'] }, { 'type': ARTIST_INFO_RESOURCE_TYPE, 'name': 'BTS in Bighit Soundtrack', 'id': 1552280, 'roles': ['analytics'] }, ] employee_item = { 'type': VENDOR_RESOURCE_TYPE, 'name': 'All Orchard Labels', 'id': '*', 'roles': ['analytics'] } @pytest.fixture def mock_ows_permissions_employee_response(self): """Return mock response from ows_permissions.""" (flexmock(ows_permissions) .should_receive('get_all_resources') .with_args(self.profile_type, self.profile_id) .and_return(oto_response.Response({ 'items': [self.employee_item] }))) @pytest.fixture def mock_ows_permissions_vendor_response(self): """Return mock response from ows_permissions.""" (flexmock(ows_permissions) .should_receive('get_all_resources') .with_args(self.profile_type, self.profile_id) .and_return(oto_response.Response({ 'items': [self.vendor_item] }))) @pytest.fixture def mock_ows_permissions_artist_response(self): """Return mock response from ows_permissions.""" (flexmock(ows_permissions) .should_receive('get_all_resources') .with_args(self.profile_type, self.profile_id) .and_return(oto_response.Response({ 'items': self.artist_items }))) @pytest.fixture def mock_ows_permissions_failure_response(self): """Return mock failure response from ows_permissions.""" (flexmock(ows_permissions) .should_receive('get_all_resources') .with_args(self.profile_type, self.profile_id) .and_return(oto_response.Response(status=500))) def test_employee_success(self, mock_ows_permissions_employee_response): """Test get_all_permissions_for_profile returns permissions.""" result = permissions.get_all_permissions_for_profile( PROFILE_TYPE_INSIGHTS, self.profile_id) assert result == { 'artist_ids': ['*'], 'vendor_ids': ['*'], 'subaccount_ids': ['*'], 'label_participant_ids': ['*'] } def test_artist_success(self, mock_ows_permissions_artist_response): """Test get_all_permissions_for_profile returns permissions.""" result = permissions.get_all_permissions_for_profile( PROFILE_TYPE_INSIGHTS, self.profile_id) assert result == { 'artist_ids': [917636, 1552280], 'vendor_ids': [], 'subaccount_ids': [], 'label_participant_ids': [] } def test_vendor_success(self, mock_ows_permissions_vendor_response): """Test get_all_permissions_for_profile returns permissions.""" result = permissions.get_all_permissions_for_profile( PROFILE_TYPE_INSIGHTS, self.profile_id) assert result == { 'artist_ids': [], 'vendor_ids': [6971], 'subaccount_ids': [], 'label_participant_ids': [] } def test_failure(self, mock_ows_permissions_failure_response): """Test get_artist_info_ids returns [] from ows_permissions failure.""" result = permissions.get_all_permissions_for_profile( PROFILE_TYPE_INSIGHTS, self.profile_id) assert result == { 'artist_ids': None, 'vendor_ids': None, 'subaccount_ids': None, 'label_participant_ids': None } def test_called_with_empty_params(self): """Test get_artist_info_ids for empty parameters.""" result = permissions.get_all_permissions_for_profile(None, None) assert result == { 'artist_ids': None, 'vendor_ids': None, 'subaccount_ids': None, 'label_participant_ids': None }