|
6 | 6 |
|
7 | 7 | @pytest.fixture(autouse=True) |
8 | 8 | def clear_environment(): |
9 | | - # Clear the environment variable before each test |
10 | | - os.environ.pop("AIKIDO_TRUST_PROXY", None) |
| 9 | + os.environ.pop("AIKIDO_TRUSTED_HOSTNAMES", None) |
| 10 | + yield |
| 11 | + os.environ.pop("AIKIDO_TRUSTED_HOSTNAMES", None) |
11 | 12 |
|
12 | 13 |
|
13 | | -def test_returns_false_if_server_url_is_empty(): |
14 | | - assert not is_request_to_itself("", "aikido.dev", 80) |
| 14 | +def test_returns_false_if_no_trusted_hostnames_configured(): |
| 15 | + assert not is_request_to_itself("aikido.dev") |
| 16 | + assert not is_request_to_itself("localhost") |
15 | 17 |
|
16 | 18 |
|
17 | | -def test_returns_false_if_server_url_is_invalid(): |
18 | | - assert not is_request_to_itself("http://", "aikido.dev", 80) |
| 19 | +def test_returns_false_if_hostname_not_in_trusted_list(monkeypatch): |
| 20 | + monkeypatch.setenv("AIKIDO_TRUSTED_HOSTNAMES", "myapp.com,api.myapp.com") |
| 21 | + assert not is_request_to_itself("aikido.dev") |
| 22 | + assert not is_request_to_itself("google.com") |
19 | 23 |
|
20 | 24 |
|
21 | | -def test_returns_false_if_port_is_different(): |
22 | | - assert not is_request_to_itself("http://aikido.dev:4000", "aikido.dev", 80) |
23 | | - assert not is_request_to_itself("https://aikido.dev:4000", "aikido.dev", 443) |
| 25 | +def test_returns_true_if_hostname_in_trusted_list(monkeypatch): |
| 26 | + monkeypatch.setenv("AIKIDO_TRUSTED_HOSTNAMES", "myapp.com,api.myapp.com") |
| 27 | + assert is_request_to_itself("myapp.com") |
| 28 | + assert is_request_to_itself("api.myapp.com") |
24 | 29 |
|
25 | 30 |
|
26 | | -def test_returns_false_if_hostname_is_different(): |
27 | | - assert not is_request_to_itself("http://aikido.dev", "google.com", 80) |
28 | | - assert not is_request_to_itself("http://aikido.dev:4000", "google.com", 4000) |
29 | | - assert not is_request_to_itself("https://aikido.dev", "google.com", 443) |
30 | | - assert not is_request_to_itself("https://aikido.dev:4000", "google.com", 443) |
| 31 | +def test_returns_true_for_single_trusted_hostname(monkeypatch): |
| 32 | + monkeypatch.setenv("AIKIDO_TRUSTED_HOSTNAMES", "aikido.dev") |
| 33 | + assert is_request_to_itself("aikido.dev") |
31 | 34 |
|
32 | 35 |
|
33 | | -def test_returns_true_if_server_does_request_to_itself(): |
34 | | - assert is_request_to_itself("https://aikido.dev", "aikido.dev", 443) |
35 | | - assert is_request_to_itself("http://aikido.dev:4000", "aikido.dev", 4000) |
36 | | - assert is_request_to_itself("http://aikido.dev", "aikido.dev", 80) |
37 | | - assert is_request_to_itself("https://aikido.dev:4000", "aikido.dev", 4000) |
| 36 | +def test_strips_whitespace_from_trusted_hostnames(monkeypatch): |
| 37 | + monkeypatch.setenv("AIKIDO_TRUSTED_HOSTNAMES", " myapp.com , api.myapp.com ") |
| 38 | + assert is_request_to_itself("myapp.com") |
| 39 | + assert is_request_to_itself("api.myapp.com") |
38 | 40 |
|
39 | 41 |
|
40 | | -def test_returns_true_for_special_case_http_to_https(): |
41 | | - assert is_request_to_itself("http://aikido.dev", "aikido.dev", 443) |
42 | | - assert is_request_to_itself("https://aikido.dev", "aikido.dev", 80) |
| 42 | +def test_returns_false_if_hostname_is_none(): |
| 43 | + assert not is_request_to_itself(None) |
43 | 44 |
|
44 | 45 |
|
45 | | -def test_returns_false_if_trust_proxy_is_false(monkeypatch): |
46 | | - monkeypatch.setenv("AIKIDO_TRUST_PROXY", "false") |
47 | | - assert not is_request_to_itself("https://aikido.dev", "aikido.dev", 443) |
48 | | - assert not is_request_to_itself("http://aikido.dev", "aikido.dev", 80) |
| 46 | +def test_returns_false_if_hostname_is_empty(): |
| 47 | + assert not is_request_to_itself("") |
49 | 48 |
|
50 | 49 |
|
51 | | -def test_returns_false_if_server_url_is_null(): |
52 | | - assert not is_request_to_itself(None, "aikido.dev", 80) |
53 | | - assert not is_request_to_itself(None, "aikido.dev", 443) |
54 | | - |
55 | | - |
56 | | -def test_returns_false_if_hostname_is_null(): |
57 | | - assert not is_request_to_itself("http://aikido.dev:4000", None, 80) |
58 | | - assert not is_request_to_itself("https://aikido.dev:4000", None, 443) |
59 | | - |
60 | | - |
61 | | -def test_returns_false_if_both_are_null(): |
62 | | - assert not is_request_to_itself(None, None, 80) |
63 | | - assert not is_request_to_itself(None, None, 443) |
| 50 | +def test_returns_false_if_hostname_is_not_a_string(): |
| 51 | + assert not is_request_to_itself(123) |
| 52 | + assert not is_request_to_itself(["myapp.com"]) |
0 commit comments