|
11 | 11 |
|
12 | 12 | def forbids(get_response, request): |
13 | 13 | response = ForbidMiddleware(get_response)(request) |
| 14 | + client_ip = request.META["HTTP_X_FORWARDED_FOR"] |
14 | 15 | if response.status_code == 302: |
15 | 16 | request = wsgi.post({"CLIENT_TZ": "Europe/London"}) |
| 17 | + request.META["HTTP_X_FORWARDED_FOR"] = client_ip |
16 | 18 | response = ForbidMiddleware(get_response)(request) |
17 | 19 | return response.status_code == 403 |
18 | 20 |
|
@@ -49,9 +51,32 @@ def test_should_forbid_users_when_country_in_territories_blacklist(get_response) |
49 | 51 |
|
50 | 52 | @override_settings(DJANGO_FORBID={"COUNTRIES": ["GB"], "OPTIONS": {"VPN": True}}) |
51 | 53 | def test_should_allow_users_when_country_in_countries_whitelist(get_response): |
| 54 | + """Should allow access to users from countries in whitelist.""" |
52 | 55 | for ip_address in IP.all: |
53 | 56 | request.META["HTTP_X_FORWARDED_FOR"] = ip_address |
54 | 57 | if ip_address == IP.ip_london: |
55 | 58 | assert not forbids(get_response, request) |
56 | 59 | continue |
57 | 60 | assert forbids(get_response, request) |
| 61 | + |
| 62 | + |
| 63 | +@override_settings(DJANGO_FORBID={"OPTIONS": {"VPN": True}}) |
| 64 | +def test_should_allow_users_only_from_great_britain_with_shared_session(get_response): |
| 65 | + """It should give access to the user from Great Britain when session is shared""" |
| 66 | + # Get access from London |
| 67 | + request.META["HTTP_X_FORWARDED_FOR"] = IP.ip_london |
| 68 | + assert not forbids(get_response, request) |
| 69 | + # Turn on VPN temporary |
| 70 | + request.META["HTTP_X_FORWARDED_FOR"] = IP.ip_zurich |
| 71 | + assert forbids(get_response, request) |
| 72 | + request.META["HTTP_X_FORWARDED_FOR"] = IP.ip_cobain |
| 73 | + assert forbids(get_response, request) |
| 74 | + # Turn off VPN - back to London |
| 75 | + request.META["HTTP_X_FORWARDED_FOR"] = IP.ip_london |
| 76 | + assert not forbids(get_response, request) |
| 77 | + # Turn on VPN temporary |
| 78 | + request.META["HTTP_X_FORWARDED_FOR"] = IP.ip_cobain |
| 79 | + assert forbids(get_response, request) |
| 80 | + # Turn off VPN - back to London |
| 81 | + request.META["HTTP_X_FORWARDED_FOR"] = IP.ip_london |
| 82 | + assert not forbids(get_response, request) |
0 commit comments