|
| 1 | +from django.test import RequestFactory |
1 | 2 | from django.test import override_settings |
2 | 3 |
|
3 | 4 | from django_forbid.access import grants_access |
4 | 5 |
|
| 6 | +factory = RequestFactory() |
| 7 | +request = factory.get("/") |
| 8 | +request.session = {} |
| 9 | + |
| 10 | +LOCALHOST = "localhost" |
| 11 | +IP_LOCAL1 = "0.0.0.0" |
| 12 | +IP_LOCAL2 = "127.0.0.1" |
| 13 | +IP_LONDON = "212.102.63.59" |
| 14 | + |
5 | 15 |
|
6 | 16 | def test_access_without_configuration(): |
7 | 17 | """If no configuration is provided, access is granted everywhere.""" |
8 | | - assert grants_access("doesnt-matter") |
| 18 | + assert grants_access(request, LOCALHOST) |
| 19 | + |
| 20 | + |
| 21 | +@override_settings(FORBID_VPN=True) |
| 22 | +def test_access_forbid_vpn(): |
| 23 | + """If VPN detection is enabled, access is granted everywhere.""" |
| 24 | + assert grants_access(request, LOCALHOST) |
9 | 25 |
|
10 | 26 |
|
11 | 27 | @override_settings(WHITELIST_COUNTRIES=["US"], DEBUG=True) |
12 | 28 | def test_access_from_localhost_development_mode(): |
13 | 29 | """In development mode, access is granted from localhost.""" |
14 | | - assert grants_access("127.0.0.1") |
15 | | - assert grants_access("localhost") |
| 30 | + assert grants_access(request, IP_LOCAL1) |
| 31 | + assert grants_access(request, IP_LOCAL2) |
| 32 | + assert grants_access(request, LOCALHOST) |
16 | 33 |
|
17 | 34 |
|
18 | 35 | @override_settings(WHITELIST_COUNTRIES=["US"]) |
19 | 36 | def test_access_from_localhost_production_mode(): |
20 | 37 | """In production mode, access is not granted from localhost.""" |
21 | | - assert not grants_access("127.0.0.1") |
22 | | - assert not grants_access("localhost") |
| 38 | + assert not grants_access(request, IP_LOCAL1) |
| 39 | + assert not grants_access(request, IP_LOCAL2) |
| 40 | + assert not grants_access(request, LOCALHOST) |
23 | 41 |
|
24 | 42 |
|
25 | 43 | @override_settings(WHITELIST_COUNTRIES=["GB"]) |
26 | 44 | def test_access_from_gb_when_gb_in_countries_whitelist(): |
27 | 45 | """Access is granted from GB when GB is in the counties' whitelist.""" |
28 | | - assert grants_access("212.102.63.59") |
| 46 | + assert grants_access(request, IP_LONDON) |
29 | 47 |
|
30 | 48 |
|
31 | 49 | @override_settings(WHITELIST_COUNTRIES=["US"]) |
32 | 50 | def test_access_from_gb_when_gb_not_in_countries_whitelist(): |
33 | 51 | """Access is not granted from GB when GB is not in the counties' whitelist.""" |
34 | | - assert not grants_access("212.102.63.59") |
| 52 | + assert not grants_access(request, IP_LONDON) |
35 | 53 |
|
36 | 54 |
|
37 | 55 | @override_settings(WHITELIST_TERRITORIES=["EU"]) |
38 | 56 | def test_access_from_gb_when_eu_in_continent_whitelist(): |
39 | 57 | """Access is granted from GB when EU is in the continents' whitelist.""" |
40 | | - assert grants_access("212.102.63.59") |
| 58 | + assert grants_access(request, IP_LONDON) |
41 | 59 |
|
42 | 60 |
|
43 | 61 | @override_settings(WHITELIST_TERRITORIES=["US"]) |
44 | 62 | def test_access_from_gb_when_gb_not_in_continent_whitelist(): |
45 | 63 | """Access is not granted from GB when EU is not in the continents' whitelist.""" |
46 | | - assert not grants_access("212.102.63.59") |
| 64 | + assert not grants_access(request, IP_LONDON) |
47 | 65 |
|
48 | 66 |
|
49 | 67 | @override_settings(FORBIDDEN_COUNTRIES=["GB"]) |
50 | 68 | def test_access_from_gb_when_gb_in_forbidden_countries(): |
51 | 69 | """Access is not granted from GB when GB is in the forbidden list.""" |
52 | | - assert not grants_access("212.102.63.59") |
| 70 | + assert not grants_access(request, IP_LONDON) |
53 | 71 |
|
54 | 72 |
|
55 | 73 | @override_settings(FORBIDDEN_COUNTRIES=["RU"]) |
56 | 74 | def test_access_from_gb_when_gb_not_in_forbidden_countries(): |
57 | 75 | """Access is granted from GB when GB is not in the forbidden list.""" |
58 | | - assert grants_access("212.102.63.59") |
| 76 | + assert grants_access(request, IP_LONDON) |
59 | 77 |
|
60 | 78 |
|
61 | 79 | @override_settings(FORBIDDEN_TERRITORIES=["EU"]) |
62 | 80 | def test_access_from_gb_when_eu_in_forbidden_territories(): |
63 | 81 | """Access is not granted from GB when EU is in the forbidden list.""" |
64 | | - assert not grants_access("212.102.63.59") |
| 82 | + assert not grants_access(request, IP_LONDON) |
65 | 83 |
|
66 | 84 |
|
67 | 85 | @override_settings(FORBIDDEN_TERRITORIES=["AS"]) |
68 | 86 | def test_access_from_gb_when_eu_not_in_forbidden_territories(): |
69 | 87 | """Access is granted from GB when EU is not in the forbidden list.""" |
70 | | - assert grants_access("212.102.63.59") |
| 88 | + assert grants_access(request, IP_LONDON) |
71 | 89 |
|
72 | 90 |
|
73 | 91 | @override_settings(WHITELIST_TERRITORIES=["EU"], FORBIDDEN_COUNTRIES=["GB"]) |
74 | 92 | def test_mix_config_access_from_gb_when_eu_in_whitelist_but_gb_is_forbidden(): |
75 | 93 | """Access is not granted from GB when EU is in the whitelist but GB is forbidden.""" |
76 | | - assert not grants_access("212.102.63.59") |
| 94 | + assert not grants_access(request, IP_LONDON) |
77 | 95 |
|
78 | 96 |
|
79 | 97 | @override_settings(WHITELIST_COUNTRIES=["GB"], FORBIDDEN_COUNTRIES=["GB"]) |
80 | 98 | def test_mix_config_access_from_gb_when_gb_in_both_lists(): |
81 | 99 | """Access is not granted from GB when GB is in both lists.""" |
82 | | - assert not grants_access("212.102.63.59") |
| 100 | + assert not grants_access(request, IP_LONDON) |
0 commit comments