import pytest from django.conf import Settings from django.http import HttpResponse from django.test.client import Client from django.urls import path from pytest_mock import MockerFixture from owslib import context pytestmark = pytest.mark.urls(__name__) urlpatterns = [ path("test", lambda request: HttpResponse("ok")), ] @pytest.fixture(autouse=True) def configure(settings: Settings) -> None: settings.MIDDLEWARE = [ # type: ignore "owslib.ext.django.middleware.context.CorrelationIdMiddleware", "owslib.ext.django.middleware.context.RequestContextMiddleware", "owslib.ext.django.middleware.context.CorrelationIdHeaderMiddleware", ] def test_set_correlation_default(mocker: MockerFixture, client: Client) -> None: correlation_id = "test" correlation_id_mock = mocker.patch( "owslib.ext.starlette.middleware.context.context.correlation_id", ) correlation_id_mock.get.return_value = correlation_id response = client.get("/test") assert response.status_code == 200 assert response.headers["Correlation-Id"] == correlation_id def test_set_correlation_in_header(client: Client) -> None: correlation_id = "test" response = client.get("/test", HTTP_CORRELATION_ID=correlation_id) assert response.status_code == 200 assert response.headers["Correlation-Id"] == correlation_id def test_set_request_context(mocker: MockerFixture, client: Client) -> None: profile_type = "AudienceProfile" profile_id = "1000" request_context_mock = mocker.patch( "owslib.ext.starlette.middleware.context.context.request_context", ) client.get( "/test", HTTP_ORCHARD_PROFILE_TYPE=profile_type, HTTP_ORCHARD_PROFILE_ID=profile_id, ) request_context_mock.set.assert_called_once_with( context.RequestContext( context_type="profile", profile_type=profile_type, profile_id=int(profile_id), ) )