|
| 1 | +"""Tests for the middleware module, including CORS configuration.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import pytest |
| 5 | +from fastapi import FastAPI |
| 6 | +from fastapi.testclient import TestClient |
| 7 | + |
| 8 | +from cloud_pipelines_backend import middleware |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def app(): |
| 13 | + """Create a minimal FastAPI app for testing.""" |
| 14 | + app = FastAPI() |
| 15 | + |
| 16 | + @app.get("/test") |
| 17 | + def test_endpoint(): |
| 18 | + return {"message": "success"} |
| 19 | + |
| 20 | + return app |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def clean_env(monkeypatch): |
| 25 | + """Ensure clean environment for each test.""" |
| 26 | + monkeypatch.delenv("TANGLE_CORS_ALLOWED_ORIGINS", raising=False) |
| 27 | + |
| 28 | + |
| 29 | +def test_cors_middleware_with_default_origins(app, clean_env, monkeypatch): |
| 30 | + """Test that CORS middleware uses default origins when env var is not set.""" |
| 31 | + # Don't set TANGLE_CORS_ALLOWED_ORIGINS - should use defaults |
| 32 | + middleware.setup_cors_middleware(app) |
| 33 | + client = TestClient(app) |
| 34 | + |
| 35 | + # Test request from default origin (localhost:3000) |
| 36 | + response = client.get("/test", headers={"Origin": "http://localhost:3000"}) |
| 37 | + assert response.status_code == 200 |
| 38 | + assert response.headers.get("access-control-allow-origin") == "http://localhost:3000" |
| 39 | + |
| 40 | + # Test request from default origin (127.0.0.1:3000) |
| 41 | + response = client.get("/test", headers={"Origin": "http://127.0.0.1:3000"}) |
| 42 | + assert response.status_code == 200 |
| 43 | + assert response.headers.get("access-control-allow-origin") == "http://127.0.0.1:3000" |
| 44 | + |
| 45 | + |
| 46 | +def test_cors_middleware_with_custom_single_origin(app, clean_env, monkeypatch): |
| 47 | + """Test CORS middleware with a single custom origin.""" |
| 48 | + monkeypatch.setenv("TANGLE_CORS_ALLOWED_ORIGINS", "https://app.example.com") |
| 49 | + middleware.setup_cors_middleware(app) |
| 50 | + client = TestClient(app) |
| 51 | + |
| 52 | + # Test request from allowed origin |
| 53 | + response = client.get("/test", headers={"Origin": "https://app.example.com"}) |
| 54 | + assert response.status_code == 200 |
| 55 | + assert response.headers.get("access-control-allow-origin") == "https://app.example.com" |
| 56 | + |
| 57 | + # Test request from disallowed origin |
| 58 | + response = client.get("/test", headers={"Origin": "http://localhost:3000"}) |
| 59 | + assert response.status_code == 200 |
| 60 | + # Should not match the disallowed origin |
| 61 | + assert response.headers.get("access-control-allow-origin") != "http://localhost:3000" |
| 62 | + |
| 63 | + |
| 64 | +def test_cors_middleware_with_multiple_origins(app, clean_env, monkeypatch): |
| 65 | + """Test CORS middleware with multiple comma-separated origins.""" |
| 66 | + origins = "http://localhost:3000,https://staging.example.com,https://app.example.com" |
| 67 | + monkeypatch.setenv("TANGLE_CORS_ALLOWED_ORIGINS", origins) |
| 68 | + middleware.setup_cors_middleware(app) |
| 69 | + client = TestClient(app) |
| 70 | + |
| 71 | + # Test each allowed origin |
| 72 | + for origin in ["http://localhost:3000", "https://staging.example.com", "https://app.example.com"]: |
| 73 | + response = client.get("/test", headers={"Origin": origin}) |
| 74 | + assert response.status_code == 200 |
| 75 | + assert response.headers.get("access-control-allow-origin") == origin |
| 76 | + |
| 77 | + # Test disallowed origin |
| 78 | + response = client.get("/test", headers={"Origin": "http://evil.com"}) |
| 79 | + assert response.status_code == 200 |
| 80 | + assert response.headers.get("access-control-allow-origin") != "http://evil.com" |
| 81 | + |
| 82 | + |
| 83 | +def test_cors_middleware_with_whitespace_in_origins(app, clean_env, monkeypatch): |
| 84 | + """Test that whitespace around origins is properly handled.""" |
| 85 | + origins = " http://localhost:3000 , https://app.example.com , http://127.0.0.1:3000 " |
| 86 | + monkeypatch.setenv("TANGLE_CORS_ALLOWED_ORIGINS", origins) |
| 87 | + middleware.setup_cors_middleware(app) |
| 88 | + client = TestClient(app) |
| 89 | + |
| 90 | + # Test that whitespace is properly stripped |
| 91 | + response = client.get("/test", headers={"Origin": "http://localhost:3000"}) |
| 92 | + assert response.status_code == 200 |
| 93 | + assert response.headers.get("access-control-allow-origin") == "http://localhost:3000" |
| 94 | + |
| 95 | + |
| 96 | +def test_cors_middleware_without_origin_header(app, clean_env, monkeypatch): |
| 97 | + """Test that requests without Origin header work normally.""" |
| 98 | + monkeypatch.setenv("TANGLE_CORS_ALLOWED_ORIGINS", "http://localhost:3000") |
| 99 | + middleware.setup_cors_middleware(app) |
| 100 | + client = TestClient(app) |
| 101 | + |
| 102 | + # Request without Origin header should still work |
| 103 | + response = client.get("/test") |
| 104 | + assert response.status_code == 200 |
| 105 | + assert response.json() == {"message": "success"} |
| 106 | + |
| 107 | + |
| 108 | +def test_cors_middleware_rejects_invalid_url_formats(app, clean_env, monkeypatch, caplog): |
| 109 | + """Test that invalid URL formats are rejected and logged.""" |
| 110 | + # Mix of valid and invalid origins |
| 111 | + origins = "http://localhost:3000,invalid-url,ftp://wrong-scheme.com,http://example.com/path" |
| 112 | + monkeypatch.setenv("TANGLE_CORS_ALLOWED_ORIGINS", origins) |
| 113 | + |
| 114 | + middleware.setup_cors_middleware(app) |
| 115 | + client = TestClient(app) |
| 116 | + |
| 117 | + # Valid origin should work |
| 118 | + response = client.get("/test", headers={"Origin": "http://localhost:3000"}) |
| 119 | + assert response.status_code == 200 |
| 120 | + assert response.headers.get("access-control-allow-origin") == "http://localhost:3000" |
| 121 | + |
| 122 | + # Check that invalid origins were logged |
| 123 | + assert "Invalid CORS origins found and ignored" in caplog.text |
| 124 | + assert "invalid-url" in caplog.text |
| 125 | + |
| 126 | + |
| 127 | +def test_cors_middleware_validates_scheme_http_https_only(app, clean_env, monkeypatch): |
| 128 | + """Test that only http and https schemes are accepted.""" |
| 129 | + origins = "http://localhost:3000,ftp://example.com,ws://example.com" |
| 130 | + monkeypatch.setenv("TANGLE_CORS_ALLOWED_ORIGINS", origins) |
| 131 | + middleware.setup_cors_middleware(app) |
| 132 | + client = TestClient(app) |
| 133 | + |
| 134 | + # http should work |
| 135 | + response = client.get("/test", headers={"Origin": "http://localhost:3000"}) |
| 136 | + assert response.status_code == 200 |
| 137 | + assert response.headers.get("access-control-allow-origin") == "http://localhost:3000" |
| 138 | + |
| 139 | + # ftp and ws should not be in allowed origins |
| 140 | + response = client.get("/test", headers={"Origin": "ftp://example.com"}) |
| 141 | + assert response.headers.get("access-control-allow-origin") != "ftp://example.com" |
| 142 | + |
| 143 | + |
| 144 | +def test_cors_middleware_rejects_origins_with_paths(app, clean_env, monkeypatch): |
| 145 | + """Test that origins with paths are rejected.""" |
| 146 | + origins = "http://localhost:3000,http://example.com/api,http://example.com/path/to/resource" |
| 147 | + monkeypatch.setenv("TANGLE_CORS_ALLOWED_ORIGINS", origins) |
| 148 | + middleware.setup_cors_middleware(app) |
| 149 | + client = TestClient(app) |
| 150 | + |
| 151 | + # Origin without path should work |
| 152 | + response = client.get("/test", headers={"Origin": "http://localhost:3000"}) |
| 153 | + assert response.status_code == 200 |
| 154 | + assert response.headers.get("access-control-allow-origin") == "http://localhost:3000" |
| 155 | + |
| 156 | + # Origins with paths should not work |
| 157 | + response = client.get("/test", headers={"Origin": "http://example.com/api"}) |
| 158 | + assert response.headers.get("access-control-allow-origin") != "http://example.com/api" |
| 159 | + |
| 160 | + |
| 161 | +def test_cors_middleware_empty_origins_string(app, clean_env, monkeypatch, caplog): |
| 162 | + """Test behavior when TANGLE_CORS_ALLOWED_ORIGINS is set to empty string.""" |
| 163 | + monkeypatch.setenv("TANGLE_CORS_ALLOWED_ORIGINS", "") |
| 164 | + middleware.setup_cors_middleware(app) |
| 165 | + |
| 166 | + # Should log warning about no valid origins |
| 167 | + assert "No valid CORS origins found" in caplog.text |
| 168 | + |
| 169 | + |
| 170 | +def test_cors_middleware_allows_credentials(app, clean_env, monkeypatch): |
| 171 | + """Test that CORS middleware is configured to allow credentials.""" |
| 172 | + monkeypatch.setenv("TANGLE_CORS_ALLOWED_ORIGINS", "http://localhost:3000") |
| 173 | + middleware.setup_cors_middleware(app) |
| 174 | + client = TestClient(app) |
| 175 | + |
| 176 | + response = client.get("/test", headers={"Origin": "http://localhost:3000"}) |
| 177 | + assert response.status_code == 200 |
| 178 | + # FastAPI's CORSMiddleware should set this header |
| 179 | + assert "access-control-allow-credentials" in response.headers |
| 180 | + |
| 181 | + |
| 182 | +def test_cors_middleware_allows_all_methods_and_headers(app, clean_env, monkeypatch): |
| 183 | + """Test that CORS middleware allows all methods and headers.""" |
| 184 | + monkeypatch.setenv("TANGLE_CORS_ALLOWED_ORIGINS", "http://localhost:3000") |
| 185 | + middleware.setup_cors_middleware(app) |
| 186 | + client = TestClient(app) |
| 187 | + |
| 188 | + # Preflight request |
| 189 | + response = client.options( |
| 190 | + "/test", |
| 191 | + headers={ |
| 192 | + "Origin": "http://localhost:3000", |
| 193 | + "Access-Control-Request-Method": "POST", |
| 194 | + "Access-Control-Request-Headers": "X-Custom-Header", |
| 195 | + }, |
| 196 | + ) |
| 197 | + |
| 198 | + assert response.status_code == 200 |
| 199 | + assert "access-control-allow-methods" in response.headers |
| 200 | + assert "access-control-allow-headers" in response.headers |
| 201 | + |
| 202 | + |
| 203 | +def test_is_valid_origin_helper(): |
| 204 | + """Test the _is_valid_origin helper function directly.""" |
| 205 | + # Valid origins |
| 206 | + assert middleware._is_valid_origin("http://localhost:3000") == True |
| 207 | + assert middleware._is_valid_origin("https://example.com") == True |
| 208 | + assert middleware._is_valid_origin("http://127.0.0.1:8080") == True |
| 209 | + assert middleware._is_valid_origin("https://subdomain.example.com:443") == True |
| 210 | + |
| 211 | + # Invalid origins - no scheme |
| 212 | + assert middleware._is_valid_origin("localhost:3000") == False |
| 213 | + assert middleware._is_valid_origin("example.com") == False |
| 214 | + |
| 215 | + # Invalid origins - wrong scheme |
| 216 | + assert middleware._is_valid_origin("ftp://example.com") == False |
| 217 | + assert middleware._is_valid_origin("ws://example.com") == False |
| 218 | + assert middleware._is_valid_origin("file:///path/to/file") == False |
| 219 | + |
| 220 | + # Invalid origins - has path |
| 221 | + assert middleware._is_valid_origin("http://example.com/api") == False |
| 222 | + assert middleware._is_valid_origin("https://example.com/path/to/resource") == False |
| 223 | + |
| 224 | + # Invalid origins - malformed |
| 225 | + assert middleware._is_valid_origin("not-a-url") == False |
| 226 | + assert middleware._is_valid_origin("http://") == False |
| 227 | + assert middleware._is_valid_origin("") == False |
| 228 | + |
| 229 | + |
| 230 | +def test_setup_cors_middleware_integration(app, clean_env, monkeypatch): |
| 231 | + """Test that setup_cors_middleware works end-to-end.""" |
| 232 | + monkeypatch.setenv("TANGLE_CORS_ALLOWED_ORIGINS", "http://localhost:3000") |
| 233 | + |
| 234 | + # Call setup_cors_middleware |
| 235 | + middleware.setup_cors_middleware(app) |
| 236 | + client = TestClient(app) |
| 237 | + |
| 238 | + # Verify CORS is working |
| 239 | + response = client.get("/test", headers={"Origin": "http://localhost:3000"}) |
| 240 | + assert response.status_code == 200 |
| 241 | + assert response.headers.get("access-control-allow-origin") == "http://localhost:3000" |
| 242 | + |
| 243 | + |
| 244 | +if __name__ == "__main__": |
| 245 | + pytest.main([__file__, "-v"]) |
| 246 | + |
0 commit comments