"""Unit tests for analytics handlers.""" import json from unittest.mock import MagicMock from oto import response as oto_response from owsrequest import constants as context from analytics.logic import aggregate_stats from tests.unit.fixtures import fixture_api def test_fetch_all_analytics(monkeypatch, headers): """Test getting all analytic metrics on /analytics/all endpoint.""" mock = MagicMock( return_value=oto_response.Response(message=dict(), status=200)) monkeypatch.setattr(aggregate_stats, 'fetch_transactions', mock) url = '/all/?from_date=2014-10-01&to_date=2014-10-30' response = fixture_api.CLIENT.get(url, headers=headers) data = json.loads(response.data.decode('utf8')) assert mock.called assert isinstance(data.get('stats'), dict) assert isinstance(data.get('stats').get('results'), dict) assert response.status_code == 200 format_response = mock.call_args[1].get('format') expected_format = aggregate_stats.format_response_for_analytics_frontend assert format_response == expected_format # Check the user passed to the call is the user that we need. assert mock.call_args[0][1] == int(headers.get( context.GRASS_ACCOUNT_ID)) def test_fetch_all_analytics_format_mobile(monkeypatch, headers): """Test getting all analytic metrics on /analytics/all endpoint.""" mock = MagicMock( return_value=oto_response.Response(message=dict(), status=200)) monkeypatch.setattr(aggregate_stats, 'fetch_transactions', mock) url = '/all/?from_date=2014-10-01&to_date=2014-10-30&format=mobile' fixture_api.CLIENT.get(url, headers=headers) assert mock.call_args[1].get('format') == aggregate_stats.format_response def test_fetch_all_analytics_with_missing_headers(monkeypatch): """Test getting all analytic metrics on /analytics/all endpoint.""" mock = MagicMock(return_value=oto_response.Response( message=dict(), status=200)) monkeypatch.setattr(aggregate_stats, 'fetch_transactions', mock) url = '/all/?from_date=2014-10-01&to_date=2014-10-30' response = fixture_api.CLIENT.get(url) assert not mock.called assert response.status_code == 403 def test_fetch_all_analytics_with_missing_dates(monkeypatch, headers): """Test getting all analytic metrics on /analytics/all endpoint.""" mock = MagicMock(return_value=oto_response.Response( message=dict(), status=200)) monkeypatch.setattr(aggregate_stats, 'fetch_transactions', mock) url = '/all/?from_date=2014-10-01&' response = fixture_api.CLIENT.get(url, headers=headers) assert not mock.called assert response.status_code == 400 def test_get_call_no_transaction_params(monkeypatch, headers): """Test / endpoint with no get param transaction types.""" mock = MagicMock(return_value=oto_response.Response( message=dict(), status=200)) monkeypatch.setattr(aggregate_stats, 'fetch_transactions', mock) url = '/?from_date=2014-10-01&to_date=2014-10-30' response = fixture_api.CLIENT.get(url, headers=headers) data = json.loads(response.data.decode('utf8')) assert mock.called assert isinstance(data.get('stats'), dict) assert isinstance(data.get('stats').get('results'), dict) assert response.status_code == 200 format_response = mock.call_args[1].get('format') expected_format = aggregate_stats.format_response_for_analytics_frontend assert format_response == expected_format # Check the user passed to the call is the user that we need. assert mock.call_args[0][1] == int(headers.get( context.GRASS_ACCOUNT_ID)) def test_get_call_no_transaction_params_format_mobile(monkeypatch, headers): """Test getting all analytic metrics on /analytics/all endpoint.""" mock = MagicMock( return_value=oto_response.Response(message=dict(), status=200)) monkeypatch.setattr(aggregate_stats, 'fetch_transactions', mock) url = '/?from_date=2014-10-01&to_date=2014-10-30&format=mobile' fixture_api.CLIENT.get(url, headers=headers) assert mock.call_args[1].get('format') == aggregate_stats.format_response def test_get_call_with_transaction_params(monkeypatch, headers): """Test / endpoint with valid string of transaction_types.""" mock = MagicMock(return_value=oto_response.Response( message=dict(), status=200)) monkeypatch.setattr(aggregate_stats, 'fetch_transactions', mock) url = ('/?from_date=2014-10-01&to_date=2014-10-30&' 'transaction_types={transaction_types}').format( transaction_types='S,SR,VB,VR') response = fixture_api.CLIENT.get(url, headers=headers) data = json.loads(response.data.decode('utf8')) assert mock.called assert isinstance(data.get('stats'), dict) assert isinstance(data.get('stats').get('results'), dict) assert response.status_code == 200 # Check the user passed to the call is the user that we need. assert mock.call_args[0][1] == int(headers.get( context.GRASS_ACCOUNT_ID)) def test_get_call_with_invalid_transaction_params(monkeypatch, headers): """Test / endpoint with invalid string of transaction_types. In this case, we expect all transactions to be returned and a status 200. """ mock = MagicMock(return_value=oto_response.Response( message=dict(), status=200)) monkeypatch.setattr(aggregate_stats, 'fetch_transactions', mock) url = ('/?from_date=2014-10-01&' 'to_date=2014-10-30&transaction_types={transaction_types}').format( transaction_types='S90283,SR239,VBgwpo,VR23or') response = fixture_api.CLIENT.get(url, headers=headers) data = json.loads(response.data.decode('utf8')) assert mock.called assert isinstance(data.get('stats'), dict) assert isinstance(data.get('stats').get('results'), dict) assert response.status_code == 200 # Check the user passed to the call is the user that we need. assert mock.call_args[0][1] == int(headers.get( context.GRASS_ACCOUNT_ID)) def test_fetch_type_analytics(monkeypatch, headers): """Test getting all analytic metrics on a specific entity.""" mock = MagicMock( return_value=oto_response.Response(message=dict(), status=200)) monkeypatch.setattr(aggregate_stats, 'fetch_transactions', mock) url = '/album/39873/?from_date=2014-10-01&to_date=2014-10-30&format=mobile' fixture_api.CLIENT.get(url, headers=headers) account_field = 'label_id' if headers.get(context.GRASS_ACCOUNT_TYPE) == 'subaccount': account_field = 'subaccount_id' owner = (account_field, int(headers.get(context.GRASS_ACCOUNT_ID))) assert mock.call_args[1].get('format').keywords.get('owner') == owner