"""Test ows_services tier.""" from unittest.mock import MagicMock from oto import response from feature_fm.api import app from feature_fm.constants.permissions import ADMIN_ROLE_ID from feature_fm.constants.permissions import ADVERTISING_ROLE_ID from feature_fm.constants.permissions import CAMPAIGNS_FEATURE_NAME from feature_fm.constants.permissions import WHITELIST_FEATURE_NAME from feature_fm.constants.permissions import WHITELIST_MONTHLY_BUDGET from feature_fm.logic import ows_services from feature_fm.models import ows_account from feature_fm.models import ows_accounting from feature_fm.models import ows_artist from feature_fm.models import ows_search from feature_fm.models import ows_users def test_get_artists(monkeypatch): """Test get artists.""" account_type = 'vendor' account_id = 1234 artists = { 'items': [ {'artist_id': 1, 'name': 'One', 'genres': []}, {'artist_id': 2, 'name': 'Two', 'genres': []} ], 'pagination': { 'offset': 0, 'total_records': 2, 'limit': 10 } } monkeypatch.setattr( ows_artist, 'get_artists', MagicMock(return_value=response.Response(artists))) result = ows_services.get_artists(account_type, account_id) ows_artist.get_artists.assert_called_once_with( account_type, account_id, None, None, None) assert result.message == { 'items': [ {'artist_id': 1, 'name': 'One', 'genres': []}, {'artist_id': 2, 'name': 'Two', 'genres': []} ], 'pagination': { 'page_limit': 10, 'page_offset': 0, 'total_records': 2 } } def test_get_artists_error(monkeypatch): """Test get artists when the request fails.""" account_type = 'vendor' account_id = 1234 monkeypatch.setattr( ows_artist, 'get_artists', MagicMock(return_value=response.create_fatal_response())) result = ows_services.get_artists(account_type, account_id) assert not result def test_get_artists_bulk(monkeypatch): """Test get artists bulk.""" account_type = 'vendor' account_id = 1234 artist_ids = [1, 2] artists = { 'items': [ {'artist_id': 1, 'name': 'One', 'genres': []}, {'artist_id': 2, 'name': 'Two', 'genres': []} ] } monkeypatch.setattr( ows_artist, 'get_artists_bulk', MagicMock(return_value=response.Response(artists))) result = ows_services.get_artists_bulk( account_type, account_id, artist_ids) ows_artist.get_artists_bulk.assert_called_once_with( account_type, account_id, artist_ids) assert result.message == artists def test_get_artist_releases(monkeypatch): """Test get artist releases.""" account_type = 'vendor' account_id = 1234 artist_id = 1 releases = { 'totalHits': 2, 'pageSize': '20', 'results': { 'rel1': { 'release_id': '1', 'release_name': 'One', 'display_upc': 'UPC11', 'upc': 'UPC1', 'coverart_paths': { 'thumb': 'IMAGE_URL_1' }, 'artist_name': 'Artist One' }, 'rel2': { 'release_id': '2', 'release_name': 'Two', 'upc': 'UPC2', 'display_upc': 'UPC22', 'coverart_paths': { 'thumb': 'IMAGE_URL_2' }, 'artist_name': 'Artist One' } } } monkeypatch.setattr( ows_search, 'get_artist_releases', MagicMock(return_value=response.Response(releases))) result = ows_services.get_artist_releases( account_type, account_id, artist_id) ows_search.get_artist_releases.assert_called_once_with( account_type, account_id, artist_id, None, None) assert result.message['pagination'] == { 'page_limit': 20, 'total_records': 2 } assert { 'release_id': 1, 'release_name': 'One', 'original_release_name': 'One', 'release_image': 'IMAGE_URL_1', 'upc': 'UPC11', 'artist_name': 'Artist One', 'delivered_version': None, 'version': None } in result.message['items'] assert { 'release_id': 2, 'release_name': 'Two', 'original_release_name': 'Two', 'release_image': 'IMAGE_URL_2', 'upc': 'UPC22', 'artist_name': 'Artist One', 'delivered_version': None, 'version': None } in result.message['items'] def test_get_artist_releases_no_results(monkeypatch): """Test get artist releases when no results are returned.""" account_type = 'vendor' account_id = 1234 artist_id = 1 releases = { 'totalHits': 0, 'pageSize': '20', 'results': [] } monkeypatch.setattr( ows_search, 'get_artist_releases', MagicMock(return_value=response.Response(releases))) result = ows_services.get_artist_releases( account_type, account_id, artist_id) assert result.message['items'] == [] assert result.message['pagination'] == { 'page_limit': 20, 'total_records': 0 } def test_get_artist_releases_error(monkeypatch): """Test get artist releases when the request fails.""" account_type = 'vendor' account_id = 1234 artist_id = 1 monkeypatch.setattr( ows_search, 'get_artist_releases', MagicMock(return_value=response.create_fatal_response())) result = ows_services.get_artist_releases( account_type, account_id, artist_id) assert not result def test_get_user_info(monkeypatch): """Test get user info.""" account_type = 'vendor' account_id = 1234 user_id = 'alw:123' correlation_id = 'correlation' user = { 'type': 'alw', 'language': 'en', 'account': { 'subaccount_id': None, 'vendor_id': 1234 }, 'email': 'john.doe@theorchard.com', 'last_name': 'Doe', 'first_name': 'John', 'user_id': 'alw:123', 'company': 'Company', 'role_ids': [1] } artist_response = response.Response({'items': [1, 2, 3, 4]}) features_response = response.Response({'items': []}) monkeypatch.setattr( ows_users, 'get_user_info', MagicMock(return_value=response.Response(user))) monkeypatch.setattr( ows_artist, 'get_all_artist_ids', MagicMock(return_value=artist_response)) monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=features_response)) monkeypatch.setattr( ows_account, 'get_vendor_currency_code', MagicMock(return_value='USD')) with app.test_request_context(): result = ows_services.get_user_info( account_type, account_id, user_id, None, correlation_id) ows_users.get_user_info.assert_called_once_with(user_id, correlation_id) ows_artist.get_all_artist_ids.assert_called_once_with( account_type, account_id) ows_account.get_enabled_features_for_vendor.assert_called_once_with( 1234, correlation_id) assert result.message == { 'user_id': 'alw:123', 'first_name': 'John', 'last_name': 'Doe', 'email': 'john.doe@theorchard.com', 'language': 'en', 'currency': 'USD', 'company': 'Company', 'artist_ids': [1, 2, 3, 4], 'permissions': { 'action_pages': 'W', 'smart_links': 'W', 'campaigns': 'N' }, 'ga_flags': { 'advertising_role': False, 'orchard_advertising': False, 'orchard_advertising_tier_1': False } } def test_get_user_info_no_fetch_artists(monkeypatch): """Test get user info.""" account_type = 'vendor' account_id = 1234 user_id = 'alw:123' correlation_id = 'correlation' user = { 'type': 'alw', 'language': 'en', 'account': { 'subaccount_id': None, 'vendor_id': 1234 }, 'email': 'john.doe@theorchard.com', 'last_name': 'Doe', 'first_name': 'John', 'user_id': 'alw:123', 'company': 'Company', 'role_ids': [1] } features_response = response.Response({'items': []}) monkeypatch.setattr( ows_users, 'get_user_info', MagicMock(return_value=response.Response(user))) monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=features_response)) monkeypatch.setattr( ows_account, 'get_vendor_currency_code', MagicMock(return_value='USD')) with app.test_request_context(): result = ows_services.get_user_info( account_type, account_id, user_id, False, correlation_id) ows_users.get_user_info.assert_called_once_with(user_id, correlation_id) ows_account.get_enabled_features_for_vendor.assert_called_once_with( 1234, correlation_id) assert result.message == { 'user_id': 'alw:123', 'first_name': 'John', 'last_name': 'Doe', 'email': 'john.doe@theorchard.com', 'language': 'en', 'currency': 'USD', 'company': 'Company', 'permissions': { 'action_pages': 'W', 'smart_links': 'W', 'campaigns': 'N' }, 'ga_flags': { 'advertising_role': False, 'orchard_advertising': False, 'orchard_advertising_tier_1': False } } def test_get_user_info_campaign_feature(monkeypatch): """Test get user info with the campaign feature.""" account_type = 'vendor' account_id = 1234 user_id = 'alw:123' correlation_id = 'correlation' user = { 'type': 'alw', 'language': 'en', 'account': { 'subaccount_id': None, 'vendor_id': 1234 }, 'email': 'john.doe@theorchard.com', 'last_name': 'Doe', 'first_name': 'John', 'user_id': 'alw:123', 'company': 'Company', 'role_ids': [1] } artist_response = response.Response({'items': [1, 2, 3, 4]}) features_response = response.Response({'items': [ {'feature_name': CAMPAIGNS_FEATURE_NAME}, {'feature_name': 'Facebook Campaign Boost'} ]}) monkeypatch.setattr( ows_users, 'get_user_info', MagicMock(return_value=response.Response(user))) monkeypatch.setattr( ows_artist, 'get_all_artist_ids', MagicMock(return_value=artist_response)) monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=features_response)) monkeypatch.setattr( ows_account, 'get_vendor_currency_code', MagicMock(return_value='USD')) with app.test_request_context(): result = ows_services.get_user_info( account_type, account_id, user_id, None, correlation_id) ows_users.get_user_info.assert_called_once_with(user_id, correlation_id) ows_artist.get_all_artist_ids.assert_called_once_with( account_type, account_id) ows_account.get_enabled_features_for_vendor.assert_called_once_with( 1234, correlation_id) assert result.message == { 'user_id': 'alw:123', 'first_name': 'John', 'last_name': 'Doe', 'email': 'john.doe@theorchard.com', 'language': 'en', 'currency': 'USD', 'company': 'Company', 'artist_ids': [1, 2, 3, 4], 'permissions': { 'action_pages': 'W', 'smart_links': 'W', 'campaigns': 'N' }, 'ga_flags': { 'advertising_role': False, 'orchard_advertising': True, 'orchard_advertising_tier_1': False } } def test_get_user_info_campaign_feature_advertising_role(monkeypatch): """Test get user info with campaign feature and advertising role.""" account_type = 'vendor' account_id = 1234 user_id = 'alw:123' correlation_id = 'correlation' user = { 'type': 'alw', 'language': 'en', 'account': { 'subaccount_id': None, 'vendor_id': 1234 }, 'email': 'john.doe@theorchard.com', 'last_name': 'Doe', 'first_name': 'John', 'user_id': 'alw:123', 'company': 'Company', 'role_ids': [ADVERTISING_ROLE_ID] } artist_response = response.Response({'items': [1, 2, 3, 4]}) features_response = response.Response({'items': [ {'feature_name': CAMPAIGNS_FEATURE_NAME}, {'feature_name': 'Facebook Campaign Boost'} ]}) monkeypatch.setattr( ows_users, 'get_user_info', MagicMock(return_value=response.Response(user))) monkeypatch.setattr( ows_artist, 'get_all_artist_ids', MagicMock(return_value=artist_response)) monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=features_response)) monkeypatch.setattr( ows_account, 'get_vendor_currency_code', MagicMock(return_value='USD')) with app.test_request_context(): result = ows_services.get_user_info( account_type, account_id, user_id, None, correlation_id) ows_users.get_user_info.assert_called_once_with(user_id, correlation_id) ows_artist.get_all_artist_ids.assert_called_once_with( account_type, account_id) ows_account.get_enabled_features_for_vendor.assert_called_once_with( 1234, correlation_id) assert result.message == { 'user_id': 'alw:123', 'first_name': 'John', 'last_name': 'Doe', 'email': 'john.doe@theorchard.com', 'language': 'en', 'currency': 'USD', 'company': 'Company', 'artist_ids': [1, 2, 3, 4], 'permissions': { 'action_pages': 'W', 'smart_links': 'W', 'campaigns': 'W' }, 'ga_flags': { 'advertising_role': True, 'orchard_advertising': True, 'orchard_advertising_tier_1': False } } def test_get_user_info_campaign_feature_admin_role(monkeypatch): """Test get user info with campaign feature and admin role.""" account_type = 'vendor' account_id = 1234 user_id = 'alw:123' correlation_id = 'correlation' user = { 'type': 'alw', 'language': 'en', 'account': { 'subaccount_id': None, 'vendor_id': 1234 }, 'email': 'john.doe@theorchard.com', 'last_name': 'Doe', 'first_name': 'John', 'user_id': 'alw:123', 'company': 'Company', 'role_ids': [ADMIN_ROLE_ID] } artist_response = response.Response({'items': [1, 2, 3, 4]}) features_response = response.Response({'items': [ {'feature_name': CAMPAIGNS_FEATURE_NAME}, {'feature_name': 'Facebook Campaign Boost'} ]}) monkeypatch.setattr( ows_users, 'get_user_info', MagicMock(return_value=response.Response(user))) monkeypatch.setattr( ows_artist, 'get_all_artist_ids', MagicMock(return_value=artist_response)) monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=features_response)) monkeypatch.setattr( ows_account, 'get_vendor_currency_code', MagicMock(return_value='USD')) with app.test_request_context(): result = ows_services.get_user_info( account_type, account_id, user_id, None, correlation_id) ows_users.get_user_info.assert_called_once_with(user_id, correlation_id) ows_artist.get_all_artist_ids.assert_called_once_with( account_type, account_id) ows_account.get_enabled_features_for_vendor.assert_called_once_with( 1234, correlation_id) assert result.message == { 'user_id': 'alw:123', 'first_name': 'John', 'last_name': 'Doe', 'email': 'john.doe@theorchard.com', 'language': 'en', 'currency': 'USD', 'company': 'Company', 'artist_ids': [1, 2, 3, 4], 'permissions': { 'action_pages': 'W', 'smart_links': 'W', 'campaigns': 'W' }, 'ga_flags': { 'advertising_role': True, 'orchard_advertising': True, 'orchard_advertising_tier_1': False } } def test_get_user_info_error(monkeypatch): """Test get user info when the request to ows-users fails.""" account_type = 'vendor' account_id = 1234 user_id = 'alw:123' correlation_id = 'correlation' features_response = response.Response({'items': [ {'feature_name': CAMPAIGNS_FEATURE_NAME} ]}) features_response = response.Response({'items': [ {'feature_name': CAMPAIGNS_FEATURE_NAME} ]}) monkeypatch.setattr( ows_users, 'get_user_info', MagicMock(return_value=response.create_fatal_response())) monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=features_response)) with app.test_request_context(): result = ows_services.get_user_info( account_type, account_id, user_id, None, correlation_id) assert result.status == 500 def test_get_user_info_artist_error(monkeypatch): """Test get user info when the request to ows-artist fails.""" account_type = 'vendor' account_id = 1234 user_id = 'alw:123' correlation_id = 'correlation' user = { 'type': 'alw', 'language': 'en', 'account': { 'subaccount_id': None, 'vendor_id': 1234 }, 'email': 'john.doe@theorchard.com', 'last_name': 'Doe', 'first_name': 'John', 'user_id': 'alw:123', 'currency': 'USD', 'company': 'Company', 'role_ids': [1] } features_response = response.Response({'items': [ {'feature_name': CAMPAIGNS_FEATURE_NAME} ]}) monkeypatch.setattr( ows_users, 'get_user_info', MagicMock(return_value=response.Response(user))) monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=features_response)) monkeypatch.setattr( ows_account, 'get_vendor_currency_code', MagicMock(return_value='USD')) monkeypatch.setattr( ows_artist, 'get_all_artist_ids', MagicMock(return_value=response.create_fatal_response())) with app.test_request_context(): result = ows_services.get_user_info( account_type, account_id, user_id, None, correlation_id) assert result.status == 500 def test_get_user_info_features_error(monkeypatch): """Test get user info when the request to ows-account fails.""" account_type = 'vendor' account_id = 1234 user_id = 'alw:123' correlation_id = 'correlation' user = { 'type': 'alw', 'language': 'en', 'account': { 'subaccount_id': None, 'vendor_id': 1234 }, 'email': 'john.doe@theorchard.com', 'last_name': 'Doe', 'first_name': 'John', 'user_id': 'alw:123', 'currency': 'USD', 'company': 'Company', 'role_ids': [1] } monkeypatch.setattr( ows_users, 'get_user_info', MagicMock(return_value=response.Response(user))) monkeypatch.setattr( ows_artist, 'get_all_artist_ids', MagicMock(return_value=response.Response())) monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=response.create_fatal_response())) with app.test_request_context(): result = ows_services.get_user_info( account_type, account_id, user_id, None, correlation_id) assert result.status == 500 def test_get_user_info_wrong_account(monkeypatch): """Test get user info when the account is incorrect.""" account_type = 'vendor' account_id = 4567 user_id = 'alw:123' correlation_id = 'correlation' user = { 'type': 'alw', 'language': 'en', 'account': { 'subaccount_id': None, 'vendor_id': 1234 }, 'email': 'john.doe@theorchard.com', 'last_name': 'Doe', 'first_name': 'John', 'user_id': 'alw:123', 'currency': 'USD', 'company': 'Company', 'role_ids': [1] } features_response = response.Response({'items': [ {'feature_name': CAMPAIGNS_FEATURE_NAME} ]}) monkeypatch.setattr( ows_users, 'get_user_info', MagicMock(return_value=response.Response(user))) monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=features_response)) with app.test_request_context(): result = ows_services.get_user_info( account_type, account_id, user_id, None, correlation_id) assert result.status == 404 def test_get_artist_by_id(monkeypatch): """Test get artist by id.""" account_type = 'vendor' account_id = 7123 artist_id = 123 expected_result = { 'name': 'Artist Name', 'genres': ['Latin Music'], 'artist_id': 123} monkeypatch.setattr( ows_artist, 'get_artist_by_id', MagicMock( return_value=response.Response(expected_result))) result = ows_services.get_artist_by_id(account_type, account_id, artist_id) assert result.message == expected_result def test_get_campaign_budget_general(monkeypatch): """Test getting campaign budget for a general label.""" account_type = 'vendor' account_id = 1234 features = { 'items': [ {'feature_id': 1, 'feature_name': CAMPAIGNS_FEATURE_NAME} ] } revenue = {'average_monthly_net_revenue': 1000} monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=response.Response(features))) monkeypatch.setattr( ows_accounting, 'get_average_monthly_net_revenue', MagicMock(return_value=response.Response(revenue))) result = ows_services.get_campaign_budget(account_type, account_id) ows_account.get_enabled_features_for_vendor.assert_called_once_with( account_id) ows_accounting.get_average_monthly_net_revenue.assert_called_once_with( account_type, account_id) assert result.message == {'monthly_total_budget': 1000} def test_get_campaign_budget_general_accounting_error(monkeypatch): """Test getting campaign budget for a general label.""" account_type = 'vendor' account_id = 1234 features = { 'items': [ {'feature_id': 1, 'feature_name': CAMPAIGNS_FEATURE_NAME} ] } monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=response.Response(features))) monkeypatch.setattr( ows_accounting, 'get_average_monthly_net_revenue', MagicMock(return_value=response.create_fatal_response())) result = ows_services.get_campaign_budget(account_type, account_id) ows_account.get_enabled_features_for_vendor.assert_called_once_with( account_id) ows_accounting.get_average_monthly_net_revenue.assert_called_once_with( account_type, account_id) assert result.status == 500 def test_get_campaign_budget_whitelist(monkeypatch): """Test getting campaign budget for a whitelisted label.""" account_type = 'vendor' account_id = 1234 features = { 'items': [ {'feature_id': 1, 'feature_name': CAMPAIGNS_FEATURE_NAME}, {'feature_id': 2, 'feature_name': WHITELIST_FEATURE_NAME} ] } monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=response.Response(features))) result = ows_services.get_campaign_budget(account_type, account_id) ows_account.get_enabled_features_for_vendor.assert_called_once_with( account_id) assert result.message == { 'monthly_total_budget': WHITELIST_MONTHLY_BUDGET } def test_get_campaign_budget_blacklist(monkeypatch): """Test getting campaign budget for a blacklisted label.""" account_type = 'vendor' account_id = 1234 features = { 'items': [] } monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=response.Response(features))) result = ows_services.get_campaign_budget(account_type, account_id) ows_account.get_enabled_features_for_vendor.assert_called_once_with( account_id) assert result.message == { 'monthly_total_budget': 0 } def test_get_campaign_budget_features_error(monkeypatch): """Test getting campaign budget when failing to fetch features.""" account_type = 'vendor' account_id = 1234 monkeypatch.setattr( ows_account, 'get_enabled_features_for_vendor', MagicMock(return_value=response.create_fatal_response())) result = ows_services.get_campaign_budget(account_type, account_id) ows_account.get_enabled_features_for_vendor.assert_called_once_with( account_id) assert result.status == 500 def test_get_budget_caps(monkeypatch): """Test getting budget caps for all the labels.""" payload = { 'items': [{ 'account_type': 'vendor', 'account_id': 123, 'monthly_total_budget': 2500, 'budget_type': 'white_list' }]} monkeypatch.setattr( ows_accounting, 'get_budget_caps', MagicMock(return_value=response.Response(payload)) ) result = ows_services.get_budget_caps() assert result.status == 200 assert result.message == payload def test_label_releases(monkeypatch): """Test get label releases.""" account_type = 'vendor' account_id = 1234 releases = { 'totalHits': 2, 'pageSize': '20', 'results': { 'rel1': { 'release_id': '1', 'release_name': 'One', 'display_upc': 'UPC11', 'upc': 'UPC1', 'coverart_paths': { 'thumb': 'IMAGE_URL_1' }, 'artist_name': 'Artist One' }, 'rel2': { 'release_id': '2', 'release_name': 'Two', 'upc': 'UPC2', 'display_upc': 'UPC22', 'coverart_paths': { 'thumb': 'IMAGE_URL_2' }, 'artist_name': 'Artist One' } } } monkeypatch.setattr( ows_search, 'get_label_releases', MagicMock(return_value=response.Response(releases))) result = ows_services.get_label_releases( account_type, account_id) ows_search.get_label_releases.assert_called_once_with( account_type, account_id, None, None) assert result.message['pagination'] == { 'page_limit': 20, 'total_records': 2 } assert { 'release_id': 1, 'release_name': 'One', 'original_release_name': 'One', 'release_image': 'IMAGE_URL_1', 'upc': 'UPC11', 'artist_name': 'Artist One', 'delivered_version': None, 'version': None } in result.message['items'] assert { 'release_id': 2, 'release_name': 'Two', 'original_release_name': 'Two', 'release_image': 'IMAGE_URL_2', 'upc': 'UPC22', 'artist_name': 'Artist One', 'delivered_version': None, 'version': None } in result.message['items'] def test_get_label_releases_no_results(monkeypatch): """Test get label releases when no results are returned.""" account_type = 'vendor' account_id = 1234 releases = { 'totalHits': 0, 'pageSize': '20', 'results': [] } monkeypatch.setattr( ows_search, 'get_label_releases', MagicMock(return_value=response.Response(releases))) result = ows_services.get_label_releases( account_type, account_id) assert result.message['items'] == [] assert result.message['pagination'] == { 'page_limit': 20, 'total_records': 0 } def test_get_label_releases_error(monkeypatch): """Test get label releases when the request fails.""" account_type = 'vendor' account_id = 1234 monkeypatch.setattr( ows_search, 'get_label_releases', MagicMock(return_value=response.create_fatal_response())) result = ows_services.get_label_releases( account_type, account_id) assert not result