|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from django.contrib.auth.models import AnonymousUser |
| 5 | +from django.http import JsonResponse, HttpResponse |
| 6 | +from rest_framework.test import APIRequestFactory |
| 7 | + |
| 8 | +from awx.main.middleware import AnonymousAccessRestrictionMiddleware |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def access_restriction_middleware(): |
| 13 | + return AnonymousAccessRestrictionMiddleware(lambda request: HttpResponse()) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def mock_user(is_authenticated): |
| 18 | + return type("User", (), {"is_authenticated": is_authenticated})() |
| 19 | + |
| 20 | + |
| 21 | +class TestAnonymousAccessRestrictionMiddleware: |
| 22 | + @pytest.mark.parametrize( |
| 23 | + "is_authenticated,expected_response", |
| 24 | + [ |
| 25 | + (False, JsonResponse), # Anonymous user, restricted path |
| 26 | + (True, None), # Authenticated user, not restricted |
| 27 | + ], |
| 28 | + ) |
| 29 | + @patch("django.conf.settings.RESTRICT_API_ANONYMOUS_ACCESS", True) |
| 30 | + @patch("django.conf.settings.ANONYMOUS_ACCESS_API_ALLOWED_PATHS", ["/api/public"]) |
| 31 | + def test_restricted_access_to_authenticated_only_path(self, access_restriction_middleware, mock_user, is_authenticated, expected_response): |
| 32 | + request = APIRequestFactory().get("/api/secure-data") |
| 33 | + request.user = mock_user |
| 34 | + response = access_restriction_middleware.process_request(request) |
| 35 | + |
| 36 | + if expected_response: |
| 37 | + assert isinstance(response, expected_response) |
| 38 | + assert response.status_code == 401 |
| 39 | + else: |
| 40 | + assert response is None |
| 41 | + |
| 42 | + @patch("django.conf.settings.RESTRICT_API_ANONYMOUS_ACCESS", True) |
| 43 | + @patch("django.conf.settings.ANONYMOUS_ACCESS_API_ALLOWED_PATHS", ["/api/public"]) |
| 44 | + def test_allowed_path_for_anonymous_user(self, access_restriction_middleware): |
| 45 | + """Test that anonymous users can access paths in the allowed list.""" |
| 46 | + request = APIRequestFactory().get("/api/public") |
| 47 | + request.user = AnonymousUser() |
| 48 | + |
| 49 | + response = access_restriction_middleware.process_request(request) |
| 50 | + assert response is None |
| 51 | + |
| 52 | + @patch("django.conf.settings.RESTRICT_API_ANONYMOUS_ACCESS", False) |
| 53 | + def test_anonymous_access_when_restriction_disabled(self, access_restriction_middleware): |
| 54 | + """Test that anonymous access is allowed when the restriction is disabled.""" |
| 55 | + request = APIRequestFactory().get("/api/secure-data") |
| 56 | + request.user = AnonymousUser() # Anonymous user |
| 57 | + |
| 58 | + response = access_restriction_middleware.process_request(request) |
| 59 | + assert response is None |
| 60 | + |
| 61 | + def test_non_api_path_is_skipped(self, access_restriction_middleware): |
| 62 | + """Test that non-API paths are skipped by the middleware.""" |
| 63 | + request = APIRequestFactory().get("/") |
| 64 | + request.user = AnonymousUser() |
| 65 | + |
| 66 | + response = access_restriction_middleware.process_request(request) |
| 67 | + assert response is None |
0 commit comments