"""Tests for Spotify collector.""" from unittest.mock import MagicMock from unittest.mock import Mock from unittest.mock import patch from social_analytics.logic import spotify_collector @patch('social_analytics.logic.spotify_collector.requests') @patch('social_analytics.logic.spotify_collector.construct_spotify_url') def test_collect_spotify_metrics(construct_spotify_url_mock, requests_mock): """Test collect spotify metrics.""" url_mock = Mock() construct_spotify_url_mock.return_value = url_mock get_mock = Mock() requests_mock.get.return_value = get_mock profile_id = '06HL4z' spotify_auth_token = 'aaasdfsfHHHHH' expected_result = Mock() get_mock.json.return_value = expected_result result = spotify_collector.collect_spotify_metrics( profile_id, spotify_auth_token) construct_spotify_url_mock.assert_called_once_with(profile_id, '/artists') assert result == expected_result def test_construct_spotify_url(): """"Test construct spotify url.""" profile_id = '123' query_params = '/artists' url = spotify_collector.construct_spotify_url(profile_id, query_params) assert url assert url == 'https://api.spotify.com/v1/artists/123' @patch('social_analytics.logic.spotify_collector.requests') def test_refresh_token(requests_mock): """Test spotify refresh token.""" post_mock = Mock() requests_mock.post.return_value = post_mock expected_value = { 'access_token': 'ABVVVVV', 'scope': 'user-library-read user-read-email user-read-private', 'token_type': 'Bearer', 'expires_in': 3600} post_mock.json.return_value = expected_value result = spotify_collector.refresh_token() assert isinstance(result, dict) assert result == expected_value @patch('social_analytics.logic.spotify_collector.requests') def test_recommend_spotify_profile_most_popular_profile_with_most_likes( requests_mock, monkeypatch): """Test recommend_spotify_profile with verified profile and most likes.""" token = 'IamAnAccessToken' spotify_token = { 'access_token': token } monkeypatch.setattr(spotify_collector, 'refresh_token', MagicMock( return_value=spotify_token)) get_mock = Mock() requests_mock.get.return_value = get_mock json_result = { 'artists': {'items': [ { 'id': '19614945368', 'followers': { 'total': 126025 }, 'name': 'Mastodon', 'popularity': 99 }, { 'id': '19614945368', 'followers': { 'total': 400 }, 'name': 'Mastodon111', 'popularity': 14}]}} get_mock.json.return_value = json_result expected_result = { 'platform': 'spotify', 'platform_id': '19614945368', 'platform_name': 'Mastodon', 'metric_value': 126025} expected_called_url = ( 'https://api.spotify.com/v1/search?query=Mastodon&type=artist') expected_called_headers = {'Authorization': 'Bearer {0}'.format( spotify_token['access_token'])} profile_name = 'Mastodon' result = spotify_collector.recommend_spotify_profile(profile_name, None) requests_mock.get.assert_called_once_with( expected_called_url, headers=expected_called_headers) assert result == expected_result @patch('social_analytics.logic.spotify_collector.requests') def test_verify_spotify_profile(requests_mock, monkeypatch): """Test verify spotify profile.""" spotify_token = { 'access_token': 'IamAnAccessToken' } monkeypatch.setattr(spotify_collector, 'refresh_token', MagicMock( return_value=spotify_token)) get_mock = Mock() requests_mock.get.return_value = get_mock json_result = { 'external_urls': { 'spotify': 'https://open.spotify.com/artist/06HL4z0CvFAxyc27GXpf02' }, 'followers': { 'href': None, 'total': 7048899 }, 'genres': [ 'dance pop', 'pop', 'pop christmas', 'post-teen pop' ], 'href': 'https://api.spotify.com/v1/artists/06HL4z0CvFAxyc27GXpf02', 'id': '06HL4z0CvFAxyc27GXpf02', 'name': 'Taylor Swift', 'popularity': 96, 'type': 'artist', 'uri': 'spotify:artist:06HL4z0CvFAxyc27GXpf02' } get_mock.json.return_value = json_result expected_result = { 'platform': 'spotify', 'platform_id': '06HL4z0CvFAxyc27GXpf02', 'platform_name': 'Taylor Swift', 'metric_value': 7048899} expected_called_headers = {'Authorization': 'Bearer {0}'.format( spotify_token['access_token'])} profile_id = '06HL4z0CvFAxyc27GXpf02' expected_called_url = ( 'https://api.spotify.com/v1/artists/{profile_id}'.format( profile_id=profile_id)) result = spotify_collector.verify_spotify_profile(profile_id) requests_mock.get.assert_called_once_with( expected_called_url, headers=expected_called_headers) assert result == expected_result @patch('social_analytics.logic.spotify_collector.requests') def test_verify_spotify_profile_error(requests_mock, monkeypatch): """Test verify_spotify_profile not found.""" spotify_token = { 'access_token': 'IamAnAccessToken' } monkeypatch.setattr(spotify_collector, 'refresh_token', MagicMock( return_value=spotify_token)) get_mock = Mock() requests_mock.get.return_value = get_mock json_result = { 'error': 'I am an error' } get_mock.json.return_value = json_result expected_called_headers = {'Authorization': 'Bearer {0}'.format( spotify_token['access_token'])} profile_id = '06HL4z0CvFAxyc27GXpf02' expected_called_url = ( 'https://api.spotify.com/v1/artists/{profile_id}'.format( profile_id=profile_id)) result = spotify_collector.verify_spotify_profile(profile_id) requests_mock.get.assert_called_once_with( expected_called_url, headers=expected_called_headers) assert not result