|
| 1 | +# SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +defmodule HttpCapabilityGateway.GatewayTest do |
| 3 | + use ExUnit.Case, async: false |
| 4 | + use Plug.Test |
| 5 | + |
| 6 | + alias HttpCapabilityGateway.{Gateway, PolicyCompiler} |
| 7 | + |
| 8 | + setup do |
| 9 | + # Clean up ETS tables |
| 10 | + try do |
| 11 | + :ets.delete(:gateway_rules) |
| 12 | + :ets.delete(:stealth_config) |
| 13 | + catch |
| 14 | + :error, :badarg -> :ok |
| 15 | + end |
| 16 | + |
| 17 | + # Compile default policy |
| 18 | + policy = %{ |
| 19 | + "dsl_version" => "1", |
| 20 | + "governance" => %{ |
| 21 | + "global_verbs" => ["GET", "POST"], |
| 22 | + "routes" => [ |
| 23 | + %{"path" => "/api/admin", "verbs" => ["GET"]}, |
| 24 | + %{"path" => "/api/users/[0-9]+", "verbs" => ["GET", "PUT", "DELETE"]}, |
| 25 | + %{"path" => "/health", "verbs" => ["GET"]} |
| 26 | + ] |
| 27 | + }, |
| 28 | + "stealth" => %{ |
| 29 | + "enabled" => true, |
| 30 | + "status_code" => 404 |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + PolicyCompiler.compile(policy) |
| 35 | + :ok |
| 36 | + end |
| 37 | + |
| 38 | + describe "HTTP verb enforcement" do |
| 39 | + test "allows global verbs on unspecified routes" do |
| 40 | + conn = conn(:get, "/api/public") |
| 41 | + conn = Gateway.call(conn, []) |
| 42 | + |
| 43 | + # Should pass through (not get 404/403) |
| 44 | + refute conn.status == 404 |
| 45 | + refute conn.status == 403 |
| 46 | + end |
| 47 | + |
| 48 | + test "denies non-global verbs on unspecified routes (stealth)" do |
| 49 | + conn = conn(:delete, "/api/public") |
| 50 | + conn = Gateway.call(conn, []) |
| 51 | + |
| 52 | + assert conn.status == 404 |
| 53 | + assert conn.halted |
| 54 | + end |
| 55 | + |
| 56 | + test "allows route-specific verbs" do |
| 57 | + conn = conn(:get, "/api/admin") |
| 58 | + conn = Gateway.call(conn, []) |
| 59 | + |
| 60 | + refute conn.status == 404 |
| 61 | + refute conn.status == 403 |
| 62 | + end |
| 63 | + |
| 64 | + test "denies verbs not allowed for route" do |
| 65 | + conn = conn(:post, "/api/admin") |
| 66 | + conn = Gateway.call(conn, []) |
| 67 | + |
| 68 | + assert conn.status == 404 |
| 69 | + assert conn.halted |
| 70 | + end |
| 71 | + |
| 72 | + test "handles regex route matching" do |
| 73 | + # Should match /api/users/[0-9]+ |
| 74 | + conn = conn(:put, "/api/users/123") |
| 75 | + conn = Gateway.call(conn, []) |
| 76 | + |
| 77 | + refute conn.status == 404 |
| 78 | + |
| 79 | + # Should not match (non-numeric ID) |
| 80 | + conn = conn(:put, "/api/users/abc") |
| 81 | + conn = Gateway.call(conn, []) |
| 82 | + |
| 83 | + assert conn.status == 404 |
| 84 | + end |
| 85 | + |
| 86 | + test "allows DELETE on specific routes" do |
| 87 | + conn = conn(:delete, "/api/users/456") |
| 88 | + conn = Gateway.call(conn, []) |
| 89 | + |
| 90 | + refute conn.status == 404 |
| 91 | + refute conn.status == 403 |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + describe "HTTP methods" do |
| 96 | + test "handles GET requests" do |
| 97 | + conn = conn(:get, "/health") |
| 98 | + conn = Gateway.call(conn, []) |
| 99 | + |
| 100 | + refute conn.status == 404 |
| 101 | + end |
| 102 | + |
| 103 | + test "handles POST requests" do |
| 104 | + conn = conn(:post, "/api/public") |
| 105 | + conn = Gateway.call(conn, []) |
| 106 | + |
| 107 | + refute conn.status == 404 |
| 108 | + end |
| 109 | + |
| 110 | + test "handles PUT requests" do |
| 111 | + conn = conn(:put, "/api/users/789") |
| 112 | + conn = Gateway.call(conn, []) |
| 113 | + |
| 114 | + refute conn.status == 404 |
| 115 | + end |
| 116 | + |
| 117 | + test "handles DELETE requests" do |
| 118 | + conn = conn(:delete, "/api/users/321") |
| 119 | + conn = Gateway.call(conn, []) |
| 120 | + |
| 121 | + refute conn.status == 404 |
| 122 | + end |
| 123 | + |
| 124 | + test "handles HEAD requests" do |
| 125 | + # HEAD not in global verbs, should be denied |
| 126 | + conn = conn(:head, "/api/public") |
| 127 | + conn = Gateway.call(conn, []) |
| 128 | + |
| 129 | + assert conn.status == 404 |
| 130 | + end |
| 131 | + |
| 132 | + test "handles OPTIONS requests" do |
| 133 | + # OPTIONS not in global verbs, should be denied |
| 134 | + conn = conn(:options, "/api/public") |
| 135 | + conn = Gateway.call(conn, []) |
| 136 | + |
| 137 | + assert conn.status == 404 |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + describe "stealth mode" do |
| 142 | + test "returns configured stealth status code" do |
| 143 | + conn = conn(:delete, "/api/forbidden") |
| 144 | + conn = Gateway.call(conn, []) |
| 145 | + |
| 146 | + assert conn.status == 404 # Stealth status code |
| 147 | + end |
| 148 | + |
| 149 | + test "halts connection on forbidden request" do |
| 150 | + conn = conn(:patch, "/api/admin") |
| 151 | + conn = Gateway.call(conn, []) |
| 152 | + |
| 153 | + assert conn.halted |
| 154 | + end |
| 155 | + |
| 156 | + test "returns empty body in stealth mode" do |
| 157 | + conn = conn(:delete, "/api/forbidden") |
| 158 | + conn = Gateway.call(conn, []) |
| 159 | + |
| 160 | + assert conn.resp_body == "" |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + describe "stealth disabled" do |
| 165 | + setup do |
| 166 | + policy = %{ |
| 167 | + "dsl_version" => "1", |
| 168 | + "governance" => %{ |
| 169 | + "global_verbs" => ["GET", "POST"] |
| 170 | + }, |
| 171 | + "stealth" => %{ |
| 172 | + "enabled" => false, |
| 173 | + "status_code" => 403 |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + PolicyCompiler.compile(policy) |
| 178 | + :ok |
| 179 | + end |
| 180 | + |
| 181 | + test "returns 403 when stealth disabled" do |
| 182 | + conn = conn(:delete, "/api/forbidden") |
| 183 | + conn = Gateway.call(conn, []) |
| 184 | + |
| 185 | + assert conn.status == 403 |
| 186 | + end |
| 187 | + end |
| 188 | + |
| 189 | + describe "request ID tracking" do |
| 190 | + test "preserves existing request ID" do |
| 191 | + conn = conn(:get, "/health") |
| 192 | + |> put_req_header("x-request-id", "test-123") |
| 193 | + |
| 194 | + conn = Gateway.call(conn, []) |
| 195 | + |
| 196 | + assert get_req_header(conn, "x-request-id") == ["test-123"] |
| 197 | + end |
| 198 | + |
| 199 | + test "generates request ID if missing" do |
| 200 | + conn = conn(:get, "/health") |
| 201 | + conn = Gateway.call(conn, []) |
| 202 | + |
| 203 | + # Should have a request ID header or assign |
| 204 | + assert is_binary(conn.assigns[:request_id]) or |
| 205 | + length(get_req_header(conn, "x-request-id")) > 0 |
| 206 | + end |
| 207 | + end |
| 208 | + |
| 209 | + describe "trust level evaluation" do |
| 210 | + test "extracts trust level from header" do |
| 211 | + conn = conn(:get, "/api/admin") |
| 212 | + |> put_req_header("x-trust-level", "high") |
| 213 | + |
| 214 | + conn = Gateway.call(conn, []) |
| 215 | + |
| 216 | + # Trust level should be evaluated |
| 217 | + assert conn.assigns[:trust_level] == "high" or |
| 218 | + conn.assigns[:trust_level] == :high |
| 219 | + end |
| 220 | + |
| 221 | + test "defaults to low trust when header missing" do |
| 222 | + conn = conn(:get, "/api/public") |
| 223 | + conn = Gateway.call(conn, []) |
| 224 | + |
| 225 | + # Should default to low trust |
| 226 | + assert conn.assigns[:trust_level] in ["low", :low, nil] |
| 227 | + end |
| 228 | + |
| 229 | + test "handles invalid trust level gracefully" do |
| 230 | + conn = conn(:get, "/api/public") |
| 231 | + |> put_req_header("x-trust-level", "invalid") |
| 232 | + |
| 233 | + conn = Gateway.call(conn, []) |
| 234 | + |
| 235 | + # Should not crash, should default or handle gracefully |
| 236 | + assert is_map(conn.assigns) |
| 237 | + end |
| 238 | + end |
| 239 | + |
| 240 | + describe "path matching edge cases" do |
| 241 | + test "handles paths with trailing slashes" do |
| 242 | + conn = conn(:get, "/health/") |
| 243 | + conn = Gateway.call(conn, []) |
| 244 | + |
| 245 | + # Should match /health |
| 246 | + refute conn.status == 404 |
| 247 | + end |
| 248 | + |
| 249 | + test "handles paths with query parameters" do |
| 250 | + conn = conn(:get, "/api/public?foo=bar") |
| 251 | + conn = Gateway.call(conn, []) |
| 252 | + |
| 253 | + refute conn.status == 404 |
| 254 | + end |
| 255 | + |
| 256 | + test "handles paths with fragments" do |
| 257 | + conn = conn(:get, "/api/public#section") |
| 258 | + conn = Gateway.call(conn, []) |
| 259 | + |
| 260 | + refute conn.status == 404 |
| 261 | + end |
| 262 | + |
| 263 | + test "handles root path" do |
| 264 | + conn = conn(:get, "/") |
| 265 | + conn = Gateway.call(conn, []) |
| 266 | + |
| 267 | + # Should match global verbs (GET allowed) |
| 268 | + refute conn.status == 404 |
| 269 | + end |
| 270 | + |
| 271 | + test "handles nested paths" do |
| 272 | + conn = conn(:get, "/api/v1/users/123/posts") |
| 273 | + conn = Gateway.call(conn, []) |
| 274 | + |
| 275 | + # Should use global verbs (GET allowed) |
| 276 | + refute conn.status == 404 |
| 277 | + end |
| 278 | + end |
| 279 | + |
| 280 | + describe "case sensitivity" do |
| 281 | + test "verb matching is case-sensitive" do |
| 282 | + conn = conn(:get, "/health") |
| 283 | + conn = Gateway.call(conn, []) |
| 284 | + |
| 285 | + refute conn.status == 404 |
| 286 | + |
| 287 | + # Lowercase verb should not match |
| 288 | + conn = %Plug.Conn{conn(:get, "/health") | method: "get"} |
| 289 | + conn = Gateway.call(conn, []) |
| 290 | + |
| 291 | + assert conn.status == 404 |
| 292 | + end |
| 293 | + end |
| 294 | + |
| 295 | + describe "concurrent requests" do |
| 296 | + test "handles multiple concurrent requests" do |
| 297 | + # Simulate 10 concurrent requests |
| 298 | + tasks = for i <- 1..10 do |
| 299 | + Task.async(fn -> |
| 300 | + conn = conn(:get, "/api/public") |
| 301 | + conn = Gateway.call(conn, []) |
| 302 | + {i, conn.status} |
| 303 | + end) |
| 304 | + end |
| 305 | + |
| 306 | + results = Task.await_many(tasks, 5000) |
| 307 | + |
| 308 | + # All requests should succeed (not get 404) |
| 309 | + assert Enum.all?(results, fn {_i, status} -> status != 404 end) |
| 310 | + end |
| 311 | + end |
| 312 | +end |
0 commit comments