"""Tests for instagram collector.""" from unittest.mock import call from unittest.mock import Mock from unittest.mock import patch from social_analytics.logic import instagram_collector @patch('social_analytics.logic.instagram_collector.requests') @patch('social_analytics.logic.instagram_collector.construct_instagram_url') def test_collect_instagram_metrics( construct_instagram_url_mock, requests_mock): """Test collect instagram metrics.""" # mocking url_mock = Mock() construct_instagram_url_mock.return_value = url_mock get_mock = Mock() requests_mock.get.return_value = get_mock expected_result = Mock() get_mock.json.return_value = expected_result profile_id = '11830955' instagram_auth_token = '123' # tested function call result = instagram_collector.collect_instagram_metrics( profile_id, instagram_auth_token) # checks construct_instagram_url_mock.assert_called_once_with( profile_id, instagram_auth_token) requests_mock.get_assert_called_once_with(profile_id) assert result == expected_result def test_collect_instagram_metrics_no_id(): """Test collect instagram metrics error without a given id.""" profile_id = '' instagram_auth_token = '' result = instagram_collector.collect_instagram_metrics( profile_id, instagram_auth_token) assert result is None @patch('social_analytics.logic.instagram_collector.requests') @patch('social_analytics.logic.instagram_collector.collect_instagram_metrics') def test_recommend_instagram_profile( collect_instagram_metrics_mock, requests_mock): """Test recommend instagram profile.""" # mocking expected_get_result = { 'data': [ { 'id': '2', 'username': 'profile1', 'full_name': 'Profile 1', 'profile_picture': 'https://content/pic1.jpg', }, { 'id': '5', 'username': 'profile2', 'full_name': 'Profile 2', 'profile_picture': 'https://content/pic2.jpg', } ], 'meta': { 'code': 200 } } expected_profile_results = [ { 'data': { 'id': '2', 'username': 'profile1', 'profile_picture': 'https://content/pic1.jpg', 'full_name': 'Profile 1', 'bio': 'The Official Profile 1 Instagram Account.', 'website': 'htts://profile1.com', 'is_business': False, 'counts': { 'media': 268, 'follows': 18, 'followed_by': 119373 } }, 'meta': { 'code': 200 } }, { 'data': { 'id': '5', 'username': 'profile2', 'profile_picture': 'https://content/pic2.jpg', 'full_name': 'Profile 2', 'bio': 'The Official Profile 2 Instagram Account.', 'website': 'htts://profile2.com', 'is_business': True, 'counts': { 'media': 26, 'follows': 8, 'followed_by': 131 } }, 'meta': { 'code': 200 } } ] get_mock = Mock() get_mock.json.return_value = expected_get_result requests_mock.get.return_value = get_mock collect_instagram_metrics_mock.side_effect = expected_profile_results result = instagram_collector.recommend_instagram_profile( 'profile', 'token') requests_mock.get.assert_called_once() collect_instagram_metrics_mock.assert_has_calls([ call('2', 'token'), call('5', 'token'), ]) assert result assert result['platform_id'] == '2' @patch('social_analytics.logic.instagram_collector.requests') def test_recommend_instagram_profile_no_profiles(requests_mock): """Test recommend instagram profile no profiles.""" get_mock = Mock() get_mock.json.return_value = { 'data': [], 'meta': { 'code': 200 } } requests_mock.get.return_value = get_mock result = instagram_collector.recommend_instagram_profile( 'profile', 'token') assert not result @patch('social_analytics.logic.instagram_collector.requests') @patch('social_analytics.logic.instagram_collector.collect_instagram_metrics') def test_recommend_instagram_profile_no_public_profiles( collect_instagram_metrics_mock, requests_mock): """Test recommend instagram profile.""" # mocking expected_get_result = { 'data': [ { 'id': '2', 'username': 'profile1', 'full_name': 'Profile 1', 'profile_picture': 'https://content/pic1.jpg', }, { 'id': '5', 'username': 'profile2', 'full_name': 'Profile 2', 'profile_picture': 'https://content/pic2.jpg', } ], 'meta': { 'code': 200 } } expected_profile_results = [ { 'meta': { 'code': 400, 'error_type': 'APINotAllowedError', 'error_message': 'you cannot view this resource' } }, { 'meta': { 'code': 400, 'error_type': 'APINotAllowedError', 'error_message': 'you cannot view this resource' } } ] get_mock = Mock() get_mock.json.return_value = expected_get_result requests_mock.get.return_value = get_mock collect_instagram_metrics_mock.side_effect = expected_profile_results result = instagram_collector.recommend_instagram_profile( 'profile', 'token') requests_mock.get.assert_called_once() collect_instagram_metrics_mock.assert_has_calls([ call('2', 'token'), call('5', 'token'), ]) assert not result @patch('social_analytics.logic.instagram_collector.requests') def test_verify_instagram_profile(requests_mock): """Test verify instagram profile.""" response = { 'data': { 'id': '235519220', 'username': 'joansebastian', 'full_name': 'Joan Sebastian', 'website': 'http://smarturl.it/joanpuebloit', 'is_business': True, 'counts': { 'media': 23, 'follows': 7, 'followed_by': 246503 } }, 'meta': { 'code': 200 } } get_mock = Mock() get_mock.json.return_value = response requests_mock.get.return_value = get_mock expected_result = { 'platform': 'instagram', 'platform_id': '235519220', 'platform_name': 'joansebastian', 'metric_value': 246503} result = instagram_collector.verify_instagram_profile('profile', 'token') requests_mock.get.assert_called_once() assert result == expected_result @patch('social_analytics.logic.instagram_collector.requests') def test_verify_instagram_profile_not_found(requests_mock): """Test verify instagram profile not found.""" response = { 'meta': { 'code': 400, 'error_type': 'APINotFoundError', 'error_message': 'this user does not exist'}} get_mock = Mock() get_mock.json.return_value = response requests_mock.get.return_value = get_mock result = instagram_collector.verify_instagram_profile('profile', 'token') requests_mock.get.assert_called_once() assert not result