|
| 1 | +import pytest |
| 2 | +from pytest_structlog import StructuredLogCapture |
| 3 | +from rest_framework.test import APIClient |
| 4 | + |
| 5 | +from environments.models import Environment |
| 6 | +from organisations.models import Organisation, OrganisationRole |
| 7 | +from users.models import FFAdminUser |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.usefixtures("organisation") |
| 11 | +def test_mcp_usage_logger_middleware__no_mcp_baggage__logs_nothing( |
| 12 | + staff_client: APIClient, |
| 13 | + log: StructuredLogCapture, |
| 14 | +) -> None: |
| 15 | + # Given / When |
| 16 | + response = staff_client.get("/api/v1/projects/") |
| 17 | + |
| 18 | + # Then |
| 19 | + assert response.status_code == 200 |
| 20 | + assert log.events == [] |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.usefixtures("mcp_baggage", "recording_span", "organisation") |
| 24 | +def test_mcp_usage_logger_middleware__organisation_id_span_attribute__logs_span_organisation_id( |
| 25 | + staff_client: APIClient, |
| 26 | + staff_user: FFAdminUser, |
| 27 | + log: StructuredLogCapture, |
| 28 | +) -> None: |
| 29 | + # Given |
| 30 | + other_organisation = Organisation.objects.create(name="Other Org") |
| 31 | + staff_user.add_organisation(other_organisation, role=OrganisationRole.ADMIN) |
| 32 | + |
| 33 | + # When |
| 34 | + response = staff_client.get( |
| 35 | + f"/api/v1/organisations/{other_organisation.pk}/invites/" |
| 36 | + ) |
| 37 | + |
| 38 | + # Then |
| 39 | + assert response.status_code == 200 |
| 40 | + assert log.events == [ |
| 41 | + { |
| 42 | + "level": "info", |
| 43 | + "event": "tool.called", |
| 44 | + "organisation__id": other_organisation.pk, |
| 45 | + "status": "success", |
| 46 | + } |
| 47 | + ] |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.usefixtures("mcp_baggage") |
| 51 | +def test_mcp_usage_logger_middleware__user_with_single_organisation__logs_user_organisation_id( |
| 52 | + staff_client: APIClient, |
| 53 | + organisation: Organisation, |
| 54 | + log: StructuredLogCapture, |
| 55 | +) -> None: |
| 56 | + # Given / When |
| 57 | + response = staff_client.get("/api/v1/projects/") |
| 58 | + |
| 59 | + # Then |
| 60 | + assert response.status_code == 200 |
| 61 | + assert log.events == [ |
| 62 | + { |
| 63 | + "level": "info", |
| 64 | + "event": "tool.called", |
| 65 | + "organisation__id": organisation.pk, |
| 66 | + "status": "success", |
| 67 | + } |
| 68 | + ] |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.usefixtures("mcp_baggage") |
| 72 | +def test_mcp_usage_logger_middleware__user_without_organisations__logs_warning( |
| 73 | + staff_client: APIClient, |
| 74 | + staff_user: FFAdminUser, |
| 75 | + log: StructuredLogCapture, |
| 76 | +) -> None: |
| 77 | + # Given |
| 78 | + assert not staff_user.organisations.exists() |
| 79 | + |
| 80 | + # When |
| 81 | + response = staff_client.get("/api/v1/projects/") |
| 82 | + |
| 83 | + # Then |
| 84 | + assert response.status_code == 200 |
| 85 | + assert log.events == [ |
| 86 | + { |
| 87 | + "level": "warning", |
| 88 | + "event": "tool.called", |
| 89 | + "organisation__id": None, |
| 90 | + "status": "success", |
| 91 | + } |
| 92 | + ] |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.usefixtures("mcp_baggage", "organisation") |
| 96 | +def test_mcp_usage_logger_middleware__user_with_multiple_organisations__logs_warning( |
| 97 | + staff_client: APIClient, |
| 98 | + staff_user: FFAdminUser, |
| 99 | + log: StructuredLogCapture, |
| 100 | +) -> None: |
| 101 | + # Given |
| 102 | + other_organisation = Organisation.objects.create(name="Other Org") |
| 103 | + staff_user.add_organisation(other_organisation, role=OrganisationRole.USER) |
| 104 | + |
| 105 | + # When |
| 106 | + response = staff_client.get("/api/v1/projects/") |
| 107 | + |
| 108 | + # Then |
| 109 | + assert response.status_code == 200 |
| 110 | + assert log.events == [ |
| 111 | + { |
| 112 | + "level": "warning", |
| 113 | + "event": "tool.called", |
| 114 | + "organisation__id": None, |
| 115 | + "status": "success", |
| 116 | + } |
| 117 | + ] |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.usefixtures("mcp_baggage") |
| 121 | +def test_mcp_usage_logger_middleware__unauthenticated_request__logs_nothing( |
| 122 | + api_client: APIClient, |
| 123 | + log: StructuredLogCapture, |
| 124 | +) -> None: |
| 125 | + # Given / When |
| 126 | + response = api_client.get("/api/v1/projects/") |
| 127 | + |
| 128 | + # Then |
| 129 | + assert response.status_code == 401 |
| 130 | + assert log.events == [] |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.usefixtures("mcp_baggage") |
| 134 | +def test_mcp_usage_logger_middleware__error_response__logs_error_status( |
| 135 | + staff_client: APIClient, |
| 136 | + organisation: Organisation, |
| 137 | + log: StructuredLogCapture, |
| 138 | +) -> None: |
| 139 | + # Given / When |
| 140 | + response = staff_client.get(f"/api/v1/organisations/{organisation.pk}/invites/") |
| 141 | + |
| 142 | + # Then |
| 143 | + assert response.status_code == 403 |
| 144 | + assert log.events == [ |
| 145 | + { |
| 146 | + "level": "info", |
| 147 | + "event": "tool.called", |
| 148 | + "organisation__id": organisation.pk, |
| 149 | + "status": "error", |
| 150 | + } |
| 151 | + ] |
| 152 | + |
| 153 | + |
| 154 | +@pytest.mark.usefixtures("mcp_baggage") |
| 155 | +def test_mcp_usage_logger_middleware__sdk_request__logs_nothing( |
| 156 | + api_client: APIClient, |
| 157 | + environment: Environment, |
| 158 | + log: StructuredLogCapture, |
| 159 | +) -> None: |
| 160 | + # Given / When |
| 161 | + response = api_client.get( |
| 162 | + "/api/v1/flags/", |
| 163 | + HTTP_X_ENVIRONMENT_KEY=environment.api_key, |
| 164 | + ) |
| 165 | + |
| 166 | + # Then |
| 167 | + assert response.status_code == 200 |
| 168 | + assert log.events == [] |
0 commit comments