"""Test handlers for Participant related endpoints.""" import json from unittest.mock import patch import pytest from oto import response as oto_response from analytics import config class TestParticipantMetrics: """Test /participant-metrics endpoint.""" mock_permissions = { "permission_label_ids": [36135], "permission_artist_ids": None, "permission_subaccount_ids": None, "permission_label_participant_ids": None, "permission_feed_ids": [1, 2, 38], } @pytest.fixture def participant_metrics_mock(self): """Mock get_participant_metrics.""" with patch( "analytics.logic.participant_metrics.get_participant_metrics", ) as get_participant_metrics_mock: get_participant_metrics_mock.return_value = oto_response.Response( [{"metrics": "mock_payload"}] ) yield get_participant_metrics_mock @pytest.fixture def mock_get_permission_values(self): """Mock get_permission_values.""" with patch( "analytics.participant_handlers.get_permission_values", return_value=self.mock_permissions, ): yield @pytest.fixture def response( self, client, participant_metrics_mock, mock_get_permission_values, insights_request_headers, ): """Return response from requesting endpoint.""" return client.get( config.PARTICIPANT_METRICS_PATH, headers=insights_request_headers ) @pytest.fixture def payload(self, response): """Return payload.""" return json.loads(response.data.decode("utf-8")) def test_succeeds(self, response, payload): """Test successful response.""" assert response.status_code == 200 assert payload == [{"metrics": "mock_payload"}] def test_calls_logic_layer(self, response, participant_metrics_mock): """Test get_participant_metrics call.""" participant_metrics_mock.assert_called_once_with( { "distributors": ["theorchard", "sme", "awal"], "country_ids": [], "store_ids": [], "label_ids": [], "subaccount_ids": [], "fin_label_ids": [], "upper_profit_center_ids": [], "global_participant_ids": [], "parent_companies": [], "company_brands": [], "service_tier": None, "order_by": "streams_7_days", "order_dir": "DESC", "limit": 25, "offset": 0, "transfer_product_ownership_enabled": False, }, self.mock_permissions, ) class TestParticipantTrackStreamsAll: """Test /participant//track-streams-all endpoint.""" global_participant_id = "f67b0892-f0bc-4575-b23e-59566ccb44bc" mock_permissions = { "permission_label_ids": [36135], "permission_artist_ids": None, "permission_subaccount_ids": None, "permission_label_participant_ids": None, "permission_feed_ids": [1, 2, 38], } @pytest.fixture def participant_track_streams_all_mock(self): """Mock get_participant_track_streams_all.""" with patch( "analytics.logic.participant_track_streams.get_track_streams_all", ) as get_track_streams_all_mock: get_track_streams_all_mock.return_value = oto_response.Response( [{"mock": "payload"}] ) yield get_track_streams_all_mock @pytest.fixture def mock_get_permission_values(self): """Mock get_permission_values.""" with patch( "analytics.participant_handlers.get_permission_values", return_value=self.mock_permissions, ): yield @pytest.fixture def response( self, client, participant_track_streams_all_mock, mock_get_permission_values, insights_request_headers, ): """Return response from requesting endpoint.""" return client.get( config.PARTICIPANT_TRACK_STREAMS_ALL_PATH.replace( "", self.global_participant_id ), headers=insights_request_headers, ) @pytest.fixture def payload(self, response): """Return payload.""" return json.loads(response.data.decode("utf-8")) def test_succeeds(self, response, payload): """Test successful response.""" assert response.status_code == 200 assert payload == [{"mock": "payload"}] def test_calls_logic_layer( self, response, participant_track_streams_all_mock, ): """Test get_participant_track_streams_all call.""" participant_track_streams_all_mock.assert_called_once_with( { "global_participant_id": self.global_participant_id, "country_ids": [], "store_ids": [], "start_date": None, "end_date": None, "distributors": ["theorchard", "sme", "awal"], "transfer_product_ownership_enabled": False, }, self.mock_permissions, ) class TestParticipantTrackStreamsStore: """Test /participant//track-streams-by-store endpoint.""" global_participant_id = "f67b0892-f0bc-4575-b23e-59566ccb44bc" mock_permissions = { "permission_label_ids": [36135], "permission_artist_ids": None, "permission_subaccount_ids": None, "permission_label_participant_ids": None, "permission_feed_ids": [1, 2, 38], } @pytest.fixture def participant_track_streams_store_mock(self): """Mock get_participant_track_streams_store.""" with patch( "analytics.logic.participant_track_streams.get_track_streams_store", ) as get_track_streams_store_mock: get_track_streams_store_mock.return_value = oto_response.Response( [{"mock": "payload"}] ) yield get_track_streams_store_mock @pytest.fixture def mock_get_permission_values(self): """Mock get_permission_values.""" with patch( "analytics.participant_handlers.get_permission_values", return_value=self.mock_permissions, ): yield @pytest.fixture def response( self, client, participant_track_streams_store_mock, mock_get_permission_values, insights_request_headers, ): """Return response from requesting endpoint.""" return client.get( config.PARTICIPANT_TRACK_STREAMS_STORE_PATH.replace( "", self.global_participant_id ), headers=insights_request_headers, ) @pytest.fixture def payload(self, response): """Return payload.""" return json.loads(response.data.decode("utf-8")) def test_succeeds(self, response, payload): """Test successful response.""" assert response.status_code == 200 assert payload == [{"mock": "payload"}] def test_calls_logic_layer( self, response, participant_track_streams_store_mock, ): """Test get_participant_track_streams_store call.""" participant_track_streams_store_mock.assert_called_once_with( { "global_participant_id": self.global_participant_id, "country_ids": [], "store_ids": [], "start_date": None, "end_date": None, "distributors": ["theorchard", "sme", "awal"], "transfer_product_ownership_enabled": False, }, self.mock_permissions, ) class TestParticipantSummary: """Test /participant//summary.""" global_participant_id = "f67b0892-f0bc-4575-b23e-59566ccb44bc" mock_permissions = { "permission_label_ids": [36135], "permission_artist_ids": None, "permission_subaccount_ids": None, "permission_label_participant_ids": None, "permission_feed_ids": [1, 2, 38], } @pytest.fixture def participant_summary_mock(self): """Mock get_participant_summary.""" with patch("analytics.logic.participant.get_summary") as summary_mock: summary_mock.return_value = oto_response.Response([{"mock": "payload"}]) yield summary_mock @pytest.fixture def mock_get_permission_values(self): """Mock get_permission_values.""" with patch( "analytics.participant_handlers.get_permission_values", return_value=self.mock_permissions, ): yield @pytest.fixture def response( self, client, participant_summary_mock, mock_get_permission_values, insights_request_headers, ): """Return response from requesting endpoint.""" return client.get( config.PARTICIPANT_SUMMARY_PATH.replace( "", self.global_participant_id ), headers=insights_request_headers, ) @pytest.fixture def payload(self, response): """Return payload.""" return json.loads(response.data.decode("utf-8")) def test_succeeds(self, response, payload): """Test successful response.""" assert response.status_code == 200 assert payload == [{"mock": "payload"}] def test_calls_logic_layer(self, response, participant_summary_mock): """Test get_participant_summary call.""" participant_summary_mock.assert_called_once_with( { "global_participant_id": self.global_participant_id, "query_type": "total", "start_date": None, "end_date": None, "distributors": ["theorchard", "sme", "awal"], "country_ids": [], "store_ids": [], "stream_sources": [], "order_by": "streams", "order_dir": "desc", "limit": 100, "offset": 0, "transfer_product_ownership_enabled": False, "line_soundcloud_collection_as_active_enabled": False, }, self.mock_permissions, ) class TestParticipantTimeSeries: """Test /participant//timeseries.""" global_participant_id = "f67b0892-f0bc-4575-b23e-59566ccb44bc" mock_permissions = { "permission_label_ids": [36135], "permission_artist_ids": None, "permission_subaccount_ids": None, "permission_label_participant_ids": None, "permission_feed_ids": [1, 2, 38], } @pytest.fixture def participant_timeseries_mock(self): """Mock get_participant_timeseries.""" with patch("analytics.logic.participant.get_timeseries") as timeseries_mock: timeseries_mock.return_value = oto_response.Response([{"mock": "payload"}]) yield timeseries_mock @pytest.fixture def mock_get_permission_values(self): """Mock get_permission_values.""" with patch( "analytics.participant_handlers.get_permission_values", return_value=self.mock_permissions, ): yield @pytest.fixture def response( self, client, participant_timeseries_mock, mock_get_permission_values, insights_request_headers, ): """Return response from requesting endpoint.""" return client.get( config.PARTICIPANT_TIMESERIES_PATH.replace( "", self.global_participant_id ), headers=insights_request_headers, ) @pytest.fixture def payload(self, response): """Return payload.""" return json.loads(response.data.decode("utf-8")) def test_succeeds(self, response, payload): """Test successful response.""" assert response.status_code == 200 assert payload == [{"mock": "payload"}] def test_calls_logic_layer(self, response, participant_timeseries_mock): """Test get_participant_timeseries call.""" participant_timeseries_mock.assert_called_once_with( { "global_participant_id": self.global_participant_id, "query_type": "total", "ids": [], "start_date": None, "end_date": None, "days_back": None, "distributors": ["theorchard", "sme", "awal"], "country_ids": [], "store_ids": [], "stream_sources": [], "resolution": None, "transfer_product_ownership_enabled": False, "line_soundcloud_collection_as_active_enabled": False, }, self.mock_permissions, ) def test_participant_aggregated_streams_handler( client, insights_request_headers, request_context ): with patch( "analytics.logic.streams.get_aggregated_streams" ) as get_aggregated_streams: get_aggregated_streams.return_value = [] url = ( "/participant/f67b0892-f0bc-4575-b23e-59566ccb44bc/" "aggregated-streams?dimension=COUNTRY" ) response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert response_payload == {}