"""Integration API tests for ows-product-artists.""" import json from os import getenv, environ from auth.auth import GrassAuth, UserType import pytest from tests.testutils.api_client.api_client import APIClient from tests.testutils.mysql_client.mysql_client import MySQLClient @pytest.fixture(scope='session') def workstation_user(): """Return data for workstation user.""" return {'user_id': 38271, 'vendor_id': 7123} @pytest.fixture(scope='session') def workstation_japan_user(): """Return data for workstation user in Japan.""" return {'user_id': 5084, 'vendor_id': 4080} @pytest.fixture(scope='session') def workstation_session_token(workstation_user): """Initialize workstation grass auth token at start of test session.""" return GrassAuth(UserType.WORKSTATION, workstation_user['user_id']).grass_token @pytest.fixture def api_client_grass(workstation_session_token): """Initialize artists APIClient through Grass.""" return APIClient(getenv('BASE_QA_GRASS_URL'), workstation_session_token) @pytest.fixture def api_client(): """Initialize artists APIClient.""" return APIClient(getenv('BASE_ARTISTS_URL')) @pytest.fixture def artist_info_id(): """Return an artist info id for testing.""" return 29140 @pytest.fixture def product_id(): """Return a product id for testing.""" return 2364096 @pytest.fixture def track_artist_id(): """Return a track artist id for testing.""" return 83444296 @pytest.fixture def store_id(): """Return a store id for testing.""" return 1 @pytest.fixture def artist_7123(): """Return a 7123 artist.""" return 'aashka' @pytest.fixture def letter_a(): """Return the letter A - useful for generating many search results.""" return 'a' @pytest.fixture def artist_id_7123(): """Return artist of 7123 label.""" return 851266 def data_prep_queries(): """Prepare data in QA for testing.""" mysql_client = MySQLClient() release_artist_query = \ 'UPDATE release_artist SET artist_info_id=%s WHERE release_id=%s' mysql_client.execute_query( release_artist_query, [artist_info_id(), product_id()]) track_artist_query = \ 'UPDATE track_artist SET artist_info_id=%s WHERE id=%s' mysql_client.execute_query( track_artist_query, [artist_info_id(), track_artist_id()]) artist_identifier_select_query = \ 'SELECT * FROM artist_identifier WHERE artist_info_id=%s' artist_identifier_result = mysql_client.execute_query( artist_identifier_select_query, artist_info_id()) if artist_identifier_result == 0: artist_identifier_query = \ 'INSERT INTO artist_identifier (' \ 'artist_info_id, store_id, identifier) VALUES (%s, %s, %s)' mysql_client.execute_query( artist_identifier_query, [artist_info_id(), store_id(), 'apple']) def test_get_artists(product_id, api_client): """Test get artists endpoint.""" get_response = api_client.get_artists(product_id) assert get_response.status_code == 200, \ 'Result of GET was {}, expected 200.'.format(get_response.status_code) def test_search_artist_spotify(letter_a, api_client_grass): """Test that searching the spotify endpoint is successful.""" get_response = api_client_grass.spotify_search(letter_a) assert get_response.status_code == 200, \ 'Result of GET was {}, expected 200.'.format(get_response.status_code) results = get_response.json()['items'] assert len(results) > 1, \ 'Expected results when searching the letter a ' \ 'to be greater than 1, was {}'.format(len(results)) @pytest.mark.skip(reason="issue with the apple credentials") def test_search_artist_apple(letter_a, api_client_grass): """Test that searching the apple endpoint is successful.""" get_response = api_client_grass.apple_search(letter_a) assert get_response.status_code == 200, \ 'Result of GET was {}, expected 200. Full response is {}'.format( get_response.status_code, get_response.content) results = get_response.json()['items'] assert len(results) > 1, \ 'Expected results when searching the letter a ' \ 'to be greater than 1, was {}'.format(len(results)) @pytest.mark.skip(reason="issue with the apple credentials") def test_search_artist_apple_country(letter_a, workstation_japan_user): """Test that searching the apple endpoint is specific to the user's country.""" api_client_grass = APIClient(getenv('BASE_QA_GRASS_URL'), GrassAuth(UserType.WORKSTATION, workstation_japan_user['user_id']).grass_token) get_response = api_client_grass.apple_search(letter_a) assert get_response.status_code == 200, \ 'Result of GET was {}, expected 200.'.format(get_response.status_code) url = get_response.json()['items'][0]['attributes']['url'] url_country = url.split('.com')[1].split('artist')[0] assert 'jp' in url_country, \ 'Expected to find "jp" in country portion of URL, full URL is {}'.format(url) def test_artist_cloudsearch_document(artist_id_7123): """Test get artists cloudsearch document endpoint.""" api_client = APIClient(getenv('BASE_ARTISTS_URL')) get_response = api_client.artist_document(artist_id_7123) assert get_response.status_code == 200, \ 'Result of GET was {}, expected 200.'.format(get_response.status_code) json_get_response = json.loads(get_response.content) assert len(json_get_response) == 4, \ 'Expected 4 elements in response. But found only {}'.format(len(json_get_response)) vendor_id = json_get_response.get('vendor_id') assert vendor_id == 7123, \ 'Artist {} must belong to vendor_id 7123, vendor_id was {}'.\ format(artist_id_7123, vendor_id)