"""Integration tests for API endpoints.""" import base64 import json import pytest import requests from socials import config SOCIAL_ACCOUNT_URL = 'https://open.spotify.com/artist/0WJc0VDtzsLIk33XRB20Dy' ARTIST_ID = 38702 class TestHello(object): """Test hello endpoint.""" @pytest.fixture def response(self): """Return response.""" return requests.get(config.HEALTH_CHECK_URL) def test_succeeds(self, response): """Test successful response.""" assert response.status_code == 200, 'Reason: {}, URL: {}'.format( response.reason, response.url ) def test_returns_payload(self, response): """Test returns correct payload.""" assert response.json() == {'status': 'ok'} class TestSocials: """Test charts related endpoints.""" def test_get_socials_timeseries(self): """Test '/socials/timeseries' endpoint """ params = { 'account_url': base64.b64encode(SOCIAL_ACCOUNT_URL.encode()).decode(), 'timestamp_gte': '2021-01-01', } url = '/socials/timeseries' response = requests.get(config.BASE_URL + url, params=params) assert response.status_code == 200 response_payload = response.json() assert isinstance(response_payload, list) assert len(response_payload) > 0 entry = response_payload[0] assert entry['followers'] def test_get_socials_stats(self): """Test '/socials/stats' endpoint """ params = { 'account_url': base64.b64encode(SOCIAL_ACCOUNT_URL.encode()).decode(), } url = '/socials/stats' response = requests.get(config.BASE_URL + url, params=params) assert response.status_code == 200 response_payload = response.json() assert isinstance(response_payload, dict) assert len(response_payload) > 0 assert response_payload['followers'] assert response_payload['platform'] == 'spotify' def test_get_socials_timeseries_by_account_id(self): """Test '/socials/artists//timeseries' endpoint """ params = { 'timestamp_gte': '2021-01-01', } url = f'/socials/artists/{ARTIST_ID}/timeseries' response = requests.get(config.BASE_URL + url, params=params) assert response.status_code == 200 response_payload = response.json() assert isinstance(response_payload, list) assert len(response_payload) > 0 entry = response_payload[0] assert 'timeseriesV2' in entry def test_get_socials_stats_by_account_id(self): """Test '/socials/artists//stats' endpoint """ params = {} url = f'/socials/artists/{ARTIST_ID}/stats' response = requests.get(config.BASE_URL + url, params=params) assert response.status_code == 200 response_payload = response.json() assert isinstance(response_payload, dict) assert 'followers' in response_payload assert 'popularity' in response_payload assert 'monthlyListeners' in response_payload def test_get_social_account_stats_by_account_id(self): """Test '/socials/artists//stats-by-account' endpoint """ params = {} url = f'/socials/artists/{ARTIST_ID}/stats-by-account' response = requests.get(config.BASE_URL + url, params=params) assert response.status_code == 200 response_payload = response.json() print(response_payload) assert isinstance(response_payload, list) assert len(response_payload) > 0 entry = response_payload[0] assert 'platform' in entry assert 'url' in entry assert 'followers' in entry assert 'popularity' in entry assert 'monthlyListeners' in entry assert 'fanConversionPercentage' in entry def test_get_social_account_stats_by_account_id_fitered_by_platform(self): """Test '/socials/artists//stats-by-account' endpoint """ params = {'platform': 'SPOTIFY'} url = f'/socials/artists/{ARTIST_ID}/stats-by-account' response = requests.get(config.BASE_URL + url, params=params) assert response.status_code == 200 response_payload = response.json() print(response_payload) assert isinstance(response_payload, list) assert len(response_payload) == 1 entry = response_payload[0] assert entry.get('platform') == 'spotify' assert 'platform' in entry assert 'url' in entry assert 'followers' in entry assert 'popularity' in entry assert 'monthlyListeners' in entry assert 'fanConversionPercentage' in entry def test_get_social_accounts_stats_by_artists_ids(self): """Test '/socials/stats-by-accounts' endpoint """ data = {'artists_ids': [ARTIST_ID]} url = '/socials/artists/stats-by-artists-ids' response = requests.post( config.BASE_URL + url, data=json.dumps(data), headers={'Content-Type': 'application/json'} ) assert response.status_code == 200 response_payload = response.json() print(response_payload) assert isinstance(response_payload, list) assert len(response_payload) == 1 entry = response_payload[0] assert entry.get('chartmetricArtistId') == ARTIST_ID assert 'spotifyMonthlyListeners' in entry assert 'instagramFollowers' in entry assert 'tiktokFollowers' in entry assert 'spotifyFollowers' in entry assert 'facebookFollowers' in entry assert 'youtubeFollowers' in entry assert 'twitterFollowers' in entry assert 'soundcloudFollowers' in entry assert 'deezerFollowers' in entry