"""Conftest file for unit tests.""" from unittest.mock import patch import pytest from oto import response as oto_response from owsrequest.context import RequestContext from requests.structures import CaseInsensitiveDict from sound_recordings import api from tests.conftest import ( ORCHARD_INSIGHTS_PROFILE_ID, ORCHARD_INSIGHTS_PROFILE_TYPE, ORCHARD_PROFILE_ID, ORCHARD_PROFILE_TYPE, VENDOR_GRASS_ACCOUNT_ID, VENDOR_GRASS_ACCOUNT_TYPE, ) @pytest.fixture def client(): """Return test client.""" APP = api.app yield APP.test_client() @pytest.fixture def request_context(request_headers): """Return a request context.""" request_context = RequestContext(CaseInsensitiveDict(request_headers)) with patch("owsrequest.context.get_request_context_from_headers") as h: h.return_value = request_context yield request_context return request_context @pytest.fixture def request_context_profile(artist_profiles_request_headers): """Return a request context for a profile.""" request_context = RequestContext( CaseInsensitiveDict(artist_profiles_request_headers) ) with patch("owsrequest.context.get_request_context_from_headers") as h: h.return_value = request_context yield request_context return request_context @pytest.fixture def request_context_insights_profile(insights_request_headers): """Return a request context for a profile.""" request_context = RequestContext(CaseInsensitiveDict(insights_request_headers)) with patch("owsrequest.context.get_request_context_from_headers") as h: h.return_value = request_context yield request_context return request_context @pytest.fixture(autouse=True) def mock_has_analytics(): """Mock verify_access_to_analytics.""" with patch("sound_recordings.validation.access.has_analytics") as has_analytics: has_analytics.return_value = True yield has_analytics @pytest.fixture def owsrequest_get_grass_headers(): """Mock owsrequest.flask_request.get_grass_headers.""" with patch("owsrequest.flask_request.get_grass_headers") as get_grass_headers: get_grass_headers.return_value = ( VENDOR_GRASS_ACCOUNT_TYPE, VENDOR_GRASS_ACCOUNT_ID, ) yield get_grass_headers @pytest.fixture def owsrequest_get_empty_grass_headers(): """Mock owsrequest.flask_request.get_grass_headers.""" with patch("owsrequest.flask_request.get_grass_headers") as get_grass_headers: get_grass_headers.return_value = None, None yield get_grass_headers @pytest.fixture def owsrequest_get_profile_headers(): """Mock owsrequest.flask_request.get_profile_headers.""" with patch("owsrequest.flask_request.get_profile_headers") as get_profile_headers: get_profile_headers.return_value = ORCHARD_PROFILE_TYPE, ORCHARD_PROFILE_ID yield get_profile_headers @pytest.fixture def owsrequest_get_insights_headers(): """Mock owsrequest.flask_request.get_profile_headers.""" with patch("owsrequest.flask_request.get_profile_headers") as get_profile_headers: get_profile_headers.return_value = ( ORCHARD_INSIGHTS_PROFILE_TYPE, ORCHARD_INSIGHTS_PROFILE_ID, ) yield get_profile_headers @pytest.fixture def owsrequest_get_empty_profile_headers(): """Mock owsrequest.flask_request.get_profile_headers.""" with patch("owsrequest.flask_request.get_profile_headers") as get_profile_headers: get_profile_headers.return_value = None, None yield get_profile_headers @pytest.fixture def owsrequest_verify_grass_headers(): """Mock owsrequest.flask_request.verify_grass_headers.""" with patch("owsrequest.flask_request.verify_grass_headers") as verify_grass_headers: verify_grass_headers.return_value = oto_response.Response() yield verify_grass_headers @pytest.fixture def owsrequest_verify_profile_headers(): """Mock owsrequest.flask_request.verify_profile_headers.""" with patch( "owsrequest.flask_request.verify_profile_headers" ) as verify_profile_headers: verify_profile_headers.return_value = oto_response.Response() yield verify_profile_headers @pytest.fixture def max_available_date(): """Mock get_streams.""" with patch( "sound_recordings.models.data_availability.get_max_available_date" ) as get_max_available_date: get_max_available_date.return_value = "2019-12-31" yield get_max_available_date