"""Tests for analytics.services.request.""" from unittest.mock import MagicMock, patch import pytest from sound_recordings.config import TOP_SOUND_RECORDINGS_URL from sound_recordings.constants import service as service_constants from sound_recordings.services import request class TestGet(object): """Tests for get.""" @pytest.fixture def args(self): """Mock args.""" return ["service", "resource"] @pytest.fixture def kwargs(self): """Mock kwargs.""" return {"these": "are", "my": "kwargs"} @pytest.fixture def mock_log_response_if_error(self): """Mock out the log_response_if_error function.""" with patch( "sound_recordings.services.request.log_response_if_error" ) as log_response_if_error: yield log_response_if_error @pytest.fixture def response(self): """Mock response.""" return MagicMock(status_code=200) @pytest.fixture def mock_get(self, response): """Mock owsrequest.get and return mock response.""" with patch("owsrequest.request.get") as get: get.return_value = response yield get def test_calls_request_get_with_args_and_kwargs(self, args, kwargs, mock_get): """Test that owsrequest.get is being called with args and kwargs.""" request.get(*args, **kwargs) mock_get.assert_called_once_with(*args, **kwargs) def test_calls_log_response_if_error_with_args_and_response( self, args, kwargs, mock_get, mock_log_response_if_error, response ): """Test that log_response_if_error is called with args and response.""" request.get(*args, **kwargs) mock_log_response_if_error.assert_called_once_with(*args, response, True) def test_does_not_call_log_response_if_error( self, args, kwargs, mock_get, mock_log_response_if_error, response ): """Test that log_response_if_error is called with args and response. Also does not report to sentry. """ request.get(*args, **kwargs, report_failures_to_sentry=False) mock_log_response_if_error.assert_called_once_with(*args, response, False) def test_returns_response_from_request_get(self, args, kwargs, mock_get, response): """Test response from get is equal to owsrequest.get response.""" return_value = request.get(*args, **kwargs) assert return_value == response class TestLogResponseIfError(object): """Tests for log_response_if_error.""" @pytest.fixture def service(self): """Mock service.""" return service_constants.OWS_USERS @pytest.fixture def resource(self): """Mock resource.""" return TOP_SOUND_RECORDINGS_URL @pytest.fixture def reason(self): """Mock reason for request failure.""" return "The server is on fire!" @pytest.fixture def headers(self): """Mock headers.""" return {"correlation-id": "pizza-party"} @pytest.fixture def failure_response(self, headers, reason): """Mock failure response.""" return MagicMock(status_code=500, headers=headers, reason=reason) @pytest.fixture def success_response(self): """Mock success response.""" return MagicMock(status_code=200) @pytest.fixture def mock_send_response_to_sentry(self): """Mock send_response_to_sentry function.""" with patch( "sound_recordings.connectors.sentry." "send_response_to_sentry" ) as send_response_to_sentry: yield send_response_to_sentry def test_successful_response_does_not_call_sentry( self, mock_send_response_to_sentry, service, resource, success_response ): """Test that successful response does not try to call sentry.""" request.log_response_if_error(service, resource, success_response) mock_send_response_to_sentry.assert_not_called() def test_successful_response_calls_sentry_with_params( self, mock_send_response_to_sentry, service, resource, failure_response ): """Test that failed response calls sentry with expected params.""" request.log_response_if_error(service, resource, failure_response) mock_send_response_to_sentry.assert_called_once_with( failure_response, request.SENTRY_MESSAGE_FORMAT.format(service=service, resource=resource), extra={ "service": service, "resource": resource, "reason": failure_response.reason, "correlation_id": failure_response.headers.get("correlation-id", None), }, )