Skip to content

Commit 6ffae7b

Browse files
committed
Remove code duplications and merge cases with same configs
1 parent 1e11143 commit 6ffae7b

File tree

4 files changed

+197
-188
lines changed

4 files changed

+197
-188
lines changed

tests/__init__.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
from django.test import RequestFactory
2+
3+
4+
class IP:
5+
localhost = "localhost"
6+
ip_local1 = "0.0.0.0"
7+
ip_local2 = "127.0.0.1"
8+
ip_london = "212.102.63.59"
9+
ip_zurich = "146.70.99.178"
10+
ip_cobain = "104.129.57.189"
11+
12+
locals = [
13+
localhost,
14+
ip_local1,
15+
ip_local2,
16+
]
17+
18+
all = [
19+
*locals,
20+
ip_london,
21+
ip_zurich,
22+
ip_cobain,
23+
]
24+
25+
26+
class Header:
27+
accept_html = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
28+
accept_json = "application/json"
29+
30+
user_agent_unknown = "curl/7.47.0"
31+
user_agent_peripheral = (
32+
"Mozilla/5.0 (Linux; Android 7.0; SHTRIH-SMARTPOS-F2 Build/NRD90M; wv) AppleWebKit"
33+
"/537.36 (KHTML, like Gecko) Version/4.0 Chrome/51.0.2704.91 Mobile Safari/537.36"
34+
)
35+
user_agent_smartphone = (
36+
"SAMSUNG-GT-S3850/S3850CXKD1 SHP/VPP/R5 Dolfin/2.0 NexPlayer"
37+
"/3.0 SMM-MMS/1.2.0 profile/MIDP-2.1 configuration/CLDC-1.1 OPN-B"
38+
)
39+
user_agent_wearable = (
40+
"Mozilla/5.0 (Linux; Android 8.1.0; KidPhone4G Build/O11019; wv) AppleWebKit"
41+
"/537.36 (KHTML, like Gecko) Version/4.0 Chrome/58.0.3029.125 Mobile Safari/537.36"
42+
)
43+
user_agent_phablet = (
44+
"Mozilla/5.0 (Linux; Android 6.0; GI-626 Build/MRA58K) AppleWebKit"
45+
"/537.36 (KHTML, like Gecko) Chrome/53.0.2785.124 Mobile Safari/537.36"
46+
)
47+
user_agent_desktop = (
48+
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.28) Gecko/20130316 Songbird/1.12.1 (20140112193149)"
49+
)
50+
user_agent_console = (
51+
"Mozilla/5.0 (Linux; Android 4.1.1; ARCHOS GAMEPAD Build/JRO03H) "
52+
"AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19"
53+
)
54+
user_agent_display = (
55+
"Mozilla/5.0 (Linux; U; Android 4.0.4; fr-be; DA220HQL Build/IMM76D) "
56+
"AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30"
57+
)
58+
user_agent_speaker = (
59+
"AlexaMediaPlayer/2.0.201528.0 (Linux;Android 5.1.1) ExoPlayerLib/1.5.9"
60+
)
61+
user_agent_camera = (
62+
"Mozilla/5.0 (Linux; U; Android 2.3.3; ja-jp; COOLPIX S800c Build/CP01_WW) "
63+
"AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
64+
)
65+
user_agent_tablet = (
66+
"Mozilla/5.0 (iPad3,6; iPad; U; CPU OS 7_1 like Mac OS X; en_US) "
67+
"com.google.GooglePlus/33839 (KHTML, like Gecko) Mobile/P103AP (gzip)"
68+
)
69+
user_agent_player = (
70+
"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_2_1 like Mac OS X; ja-jp) "
71+
"AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8C148"
72+
)
73+
user_agent_phone = "lephone K10/Dorado WAP-Browser/1.0.0"
74+
user_agent_car = (
75+
"Mozilla/5.0 (Linux; Android 4.4.2; CarPad-II-P Build/KOT49H) AppleWebKit"
76+
"/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36"
77+
)
78+
user_agent_tv = (
79+
"Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, "
80+
"like Gecko) Chrome/53.0.2785.34 Safari/537.36 WebAppManager"
81+
)
82+
83+
all_user_agents = (
84+
user_agent_peripheral, user_agent_smartphone, user_agent_wearable, user_agent_phablet, user_agent_desktop,
85+
user_agent_console, user_agent_display, user_agent_speaker, user_agent_camera, user_agent_tablet,
86+
user_agent_player, user_agent_phone, user_agent_car, user_agent_tv, user_agent_unknown,
87+
)
88+
89+
90+
class SessionStore(dict):
91+
def has_key(self, key):
92+
return key in self
93+
94+
95+
class WSGIRequest:
96+
def __init__(self, ajax=False):
97+
self.session = SessionStore()
98+
if ajax:
99+
self.accept = Header.accept_json
100+
else:
101+
self.accept = Header.accept_html
102+
103+
def get(self):
104+
request = RequestFactory().get("/")
105+
request.session = self.session
106+
request.META["HTTP_ACCEPT"] = self.accept
107+
return request
108+
109+
def post(self, data):
110+
request = RequestFactory().post("/", data)
111+
request.session = self.session
112+
request.META["HTTP_ACCEPT"] = self.accept
113+
return request

tests/test_device_middleware.py

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
1-
from django.test import RequestFactory
21
from django.test import override_settings
3-
42
from django_forbid.skills.forbid_device import ForbidDeviceMiddleware
53

6-
unknown_ua = "curl/7.47.0"
7-
peripheral_ua = "Mozilla/5.0 (Linux; Android 7.0; SHTRIH-SMARTPOS-F2 Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/51.0.2704.91 Mobile Safari/537.36"
8-
smartphone_ua = "SAMSUNG-GT-S3850/S3850CXKD1 SHP/VPP/R5 Dolfin/2.0 NexPlayer/3.0 SMM-MMS/1.2.0 profile/MIDP-2.1 configuration/CLDC-1.1 OPN-B"
9-
wearable_ua = "Mozilla/5.0 (Linux; Android 8.1.0; KidPhone4G Build/O11019; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/58.0.3029.125 Mobile Safari/537.36"
10-
phablet_ua = "Mozilla/5.0 (Linux; Android 6.0; GI-626 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.124 Mobile Safari/537.36"
11-
desktop_ua = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.28) Gecko/20130316 Songbird/1.12.1 (20140112193149)"
12-
console_ua = "Mozilla/5.0 (Linux; Android 4.1.1; ARCHOS GAMEPAD Build/JRO03H) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19"
13-
display_ua = "Mozilla/5.0 (Linux; U; Android 4.0.4; fr-be; DA220HQL Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30"
14-
speaker_ua = "AlexaMediaPlayer/2.0.201528.0 (Linux;Android 5.1.1) ExoPlayerLib/1.5.9"
15-
camera_ua = "Mozilla/5.0 (Linux; U; Android 2.3.3; ja-jp; COOLPIX S800c Build/CP01_WW) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
16-
tablet_ua = "Mozilla/5.0 (iPad3,6; iPad; U; CPU OS 7_1 like Mac OS X; en_US) com.google.GooglePlus/33839 (KHTML, like Gecko) Mobile/P103AP (gzip)"
17-
player_ua = "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_2_1 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8C148"
18-
phone_ua = "lephone K10/Dorado WAP-Browser/1.0.0"
19-
car_ua = "Mozilla/5.0 (Linux; Android 4.4.2; CarPad-II-P Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36"
20-
tv_ua = "Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.34 Safari/537.36 WebAppManager"
21-
22-
factory = RequestFactory()
23-
request = factory.get("/")
24-
request.session = {}
4+
from tests import Header
5+
from tests import WSGIRequest
6+
7+
request = WSGIRequest().get()
258

269

2710
def forbids(get_response, user_agent):
@@ -31,54 +14,58 @@ def forbids(get_response, user_agent):
3114
return response.status_code == 403
3215

3316

34-
def test_access_with_empty_list_of_devices(get_response):
35-
"""Should allow access to all devices if no config is provided."""
36-
for device_ua in (peripheral_ua, smartphone_ua, wearable_ua, phablet_ua,
37-
desktop_ua, console_ua, display_ua, speaker_ua, camera_ua,
38-
tablet_ua, player_ua, phone_ua, car_ua, tv_ua, unknown_ua):
39-
assert not forbids(get_response, device_ua)
17+
def test_should_allow_all_when_no_config_provided(get_response):
18+
"""Should allow access to all_user_agents devices if no config is provided."""
19+
for user_agent in Header.all_user_agents:
20+
assert not forbids(get_response, user_agent)
4021

4122

4223
@override_settings(DJANGO_FORBID={"DEVICES": []})
43-
def test_access_with_empty_list_of_devices(get_response):
44-
"""Should allow access to all devices if the list is empty, even if the user agent is unknown."""
45-
for device_ua in (peripheral_ua, smartphone_ua, wearable_ua, phablet_ua,
46-
desktop_ua, console_ua, display_ua, speaker_ua, camera_ua,
47-
tablet_ua, player_ua, phone_ua, car_ua, tv_ua, unknown_ua):
48-
assert not forbids(get_response, device_ua)
24+
def test_should_allow_all_when_devices_are_empty(get_response):
25+
"""Should allow access to all_user_agents devices if the list is empty, even if the user agent is unknown."""
26+
for user_agent in Header.all_user_agents:
27+
assert not forbids(get_response, user_agent)
4928

5029

5130
@override_settings(DJANGO_FORBID={"DEVICES": ["desktop", "smartphone", "console", "tablet", "tv"]})
52-
def test_access_desktops_smartphones_consoles_tablets_and_tvs(get_response):
31+
def test_should_allow_desktops_smartphones_consoles_tablets_and_tvs(get_response):
5332
"""Should allow access to desktops, smartphones, consoles, tablets and TVs."""
54-
for device_ua in (peripheral_ua, wearable_ua, phablet_ua, display_ua,
55-
speaker_ua, camera_ua, player_ua, phone_ua, car_ua, unknown_ua):
56-
assert forbids(get_response, device_ua)
57-
for device_ua in (desktop_ua, smartphone_ua, console_ua, tablet_ua, tv_ua):
58-
assert not forbids(get_response, device_ua)
33+
for user_agent in (Header.user_agent_peripheral, Header.user_agent_wearable, Header.user_agent_phablet,
34+
Header.user_agent_display, Header.user_agent_speaker, Header.user_agent_camera,
35+
Header.user_agent_player, Header.user_agent_phone, Header.user_agent_car,
36+
Header.user_agent_unknown):
37+
assert forbids(get_response, user_agent)
38+
for user_agent in (Header.user_agent_desktop, Header.user_agent_smartphone, Header.user_agent_console,
39+
Header.user_agent_tablet, Header.user_agent_tv):
40+
assert not forbids(get_response, user_agent)
5941

6042

6143
@override_settings(DJANGO_FORBID={"DEVICES": ["!car", "!speaker", "!wearable"]})
62-
def test_forbid_access_to_cars_speakers_and_wearables(get_response):
44+
def test_should_forbid_cars_speakers_and_wearables(get_response):
6345
"""Should forbid access to cars, speakers and wearables."""
64-
for device_ua in (peripheral_ua, smartphone_ua, phablet_ua, desktop_ua, console_ua,
65-
display_ua, camera_ua, tablet_ua, player_ua, phone_ua, tv_ua):
66-
assert not forbids(get_response, device_ua)
67-
for device_ua in (car_ua, speaker_ua, wearable_ua, unknown_ua):
68-
assert forbids(get_response, device_ua)
46+
for user_agent in (Header.user_agent_peripheral, Header.user_agent_smartphone, Header.user_agent_phablet,
47+
Header.user_agent_desktop, Header.user_agent_console, Header.user_agent_display,
48+
Header.user_agent_camera, Header.user_agent_tablet, Header.user_agent_player,
49+
Header.user_agent_phone, Header.user_agent_tv):
50+
assert not forbids(get_response, user_agent)
51+
for user_agent in (Header.user_agent_car, Header.user_agent_speaker,
52+
Header.user_agent_wearable, Header.user_agent_unknown):
53+
assert forbids(get_response, user_agent)
6954

7055

7156
@override_settings(DJANGO_FORBID={"DEVICES": ["!phablet", "tablet", "phablet"]})
72-
def test_forbid_access_if_same_device_is_listed_as_permitted_and_forbidden(get_response):
57+
def test_should_forbid_device_when_sametime_permitted_and_forbidden(get_response):
7358
"""Should forbid access if the same device is listed as permitted and forbidden."""
74-
for device_ua in (peripheral_ua, smartphone_ua, phablet_ua, desktop_ua,
75-
console_ua, display_ua, camera_ua, player_ua, phone_ua,
76-
tv_ua, car_ua, speaker_ua, wearable_ua, unknown_ua):
77-
assert forbids(get_response, device_ua)
78-
assert not forbids(get_response, tablet_ua)
59+
for user_agent in (Header.user_agent_peripheral, Header.user_agent_smartphone, Header.user_agent_phablet,
60+
Header.user_agent_desktop, Header.user_agent_console, Header.user_agent_display,
61+
Header.user_agent_camera, Header.user_agent_player, Header.user_agent_phone,
62+
Header.user_agent_tv, Header.user_agent_car, Header.user_agent_speaker,
63+
Header.user_agent_wearable, Header.user_agent_unknown):
64+
assert forbids(get_response, user_agent)
65+
assert not forbids(get_response, Header.user_agent_tablet)
7966

8067

8168
@override_settings(DJANGO_FORBID={"DEVICES": ["smartphone", "phablet", "tablet"]})
82-
def test_forbid_access_unknown_devices_if_devices_are_set(get_response):
69+
def test_should_forbid_unknown_devices_when_config_provided(get_response):
8370
"""Should forbid access to unknown devices if the list of devices is not empty."""
84-
assert forbids(get_response, unknown_ua)
71+
assert forbids(get_response, Header.user_agent_unknown)

tests/test_location_middleware.py

Lines changed: 24 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
from django.test import RequestFactory
21
from django.test import override_settings
3-
42
from django_forbid.skills.forbid_location import ForbidLocationMiddleware
53

6-
factory = RequestFactory()
7-
request = factory.get("/")
8-
request.session = {}
4+
from tests import IP
5+
from tests import WSGIRequest
96

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-
IP_ZURICH = "146.70.99.178"
7+
request = WSGIRequest().get()
158

169

1710
def forbids(get_response, ip_address):
@@ -20,89 +13,49 @@ def forbids(get_response, ip_address):
2013
return response.status_code == 403
2114

2215

23-
def test_access_without_configuration(get_response):
16+
def test_should_allow_all_when_no_config_provided(get_response):
2417
"""If no configuration is provided, access is granted everywhere."""
25-
assert not forbids(get_response, LOCALHOST)
26-
27-
28-
def test_access_no_config(get_response):
29-
"""If no config is set, access is granted everywhere."""
30-
assert not forbids(get_response, LOCALHOST)
31-
assert not forbids(get_response, IP_LOCAL1)
32-
assert not forbids(get_response, IP_LOCAL2)
33-
assert not forbids(get_response, IP_LONDON)
34-
assert not forbids(get_response, IP_ZURICH)
35-
36-
37-
@override_settings(DJANGO_FORBID={"OPTIONS": {"VPN": True}})
38-
def test_access_forbid_vpn(get_response):
39-
"""If VPN detection is enabled, access is granted everywhere."""
40-
assert not forbids(get_response, LOCALHOST)
41-
assert not forbids(get_response, IP_LOCAL1)
42-
assert not forbids(get_response, IP_LOCAL2)
43-
assert not forbids(get_response, IP_LONDON)
44-
assert not forbids(get_response, IP_ZURICH)
18+
for ip_address in IP.all:
19+
assert not forbids(get_response, ip_address)
4520

4621

4722
@override_settings(DJANGO_FORBID={"COUNTRIES": ["US"], "OPTIONS": {"ACTION": "PERMIT"}}, DEBUG=True)
48-
def test_access_from_localhost_development_mode(get_response):
23+
def test_should_allow_all_when_development_mode(get_response):
4924
"""In development mode, access is granted from localhost."""
50-
assert not forbids(get_response, IP_LOCAL1)
51-
assert not forbids(get_response, IP_LOCAL2)
52-
assert not forbids(get_response, LOCALHOST)
25+
for ip_address in IP.locals:
26+
assert not forbids(get_response, ip_address)
5327

5428

5529
@override_settings(DJANGO_FORBID={"COUNTRIES": ["US"], "OPTIONS": {"ACTION": "PERMIT"}})
56-
def test_access_from_localhost_production_mode(get_response):
30+
def test_should_forbid_all_when_production_mode(get_response):
5731
"""In production mode, access is not granted from localhost."""
58-
assert forbids(get_response, IP_LOCAL1)
59-
assert forbids(get_response, IP_LOCAL2)
60-
assert forbids(get_response, LOCALHOST)
32+
for ip_address in IP.locals:
33+
assert forbids(get_response, ip_address)
6134

6235

6336
@override_settings(DJANGO_FORBID={"COUNTRIES": ["GB"], "OPTIONS": {"ACTION": "PERMIT"}})
64-
def test_access_from_gb_when_gb_in_countries_whitelist(get_response):
37+
def test_should_allow_country_when_country_in_countries_whitelist_otherwise_forbid(get_response):
6538
"""Access is granted from GB when GB is in the counties' whitelist."""
66-
assert not forbids(get_response, IP_LONDON)
67-
68-
69-
@override_settings(DJANGO_FORBID={"COUNTRIES": ["US"], "OPTIONS": {"ACTION": "PERMIT"}})
70-
def test_access_from_gb_when_gb_not_in_countries_whitelist(get_response):
71-
"""Access is not granted from GB when GB is not in the counties' whitelist."""
72-
assert forbids(get_response, IP_LONDON)
39+
assert not forbids(get_response, IP.ip_london)
40+
assert forbids(get_response, IP.ip_zurich)
7341

7442

7543
@override_settings(DJANGO_FORBID={"TERRITORIES": ["EU"], "OPTIONS": {"ACTION": "PERMIT"}})
76-
def test_access_from_gb_when_eu_in_continent_whitelist(get_response):
44+
def test_should_allow_country_when_country_in_territories_whitelist_otherwise_forbid(get_response):
7745
"""Access is granted from GB when EU is in the continents' whitelist."""
78-
assert not forbids(get_response, IP_LONDON)
79-
80-
81-
@override_settings(DJANGO_FORBID={"TERRITORIES": ["US"], "OPTIONS": {"ACTION": "PERMIT"}})
82-
def test_access_from_gb_when_gb_not_in_continent_whitelist(get_response):
83-
"""Access is not granted from GB when EU is not in the continents' whitelist."""
84-
assert forbids(get_response, IP_LONDON)
46+
assert not forbids(get_response, IP.ip_london)
47+
assert forbids(get_response, IP.ip_cobain)
8548

8649

8750
@override_settings(DJANGO_FORBID={"COUNTRIES": ["GB"], "OPTIONS": {"ACTION": "FORBID"}})
88-
def test_access_from_gb_when_gb_in_forbidden_countries(get_response):
51+
def test_should_forbid_country_when_country_in_countries_blacklist_otherwise_allow(get_response):
8952
"""Access is not granted from GB when GB is in the forbidden list."""
90-
assert forbids(get_response, IP_LONDON)
91-
92-
93-
@override_settings(DJANGO_FORBID={"COUNTRIES": ["RU"], "OPTIONS": {"ACTION": "FORBID"}})
94-
def test_access_from_gb_when_gb_not_in_forbidden_countries(get_response):
95-
"""Access is granted from GB when GB is not in the forbidden list."""
96-
assert not forbids(get_response, IP_LONDON)
53+
assert forbids(get_response, IP.ip_london)
54+
assert not forbids(get_response, IP.ip_cobain)
9755

9856

9957
@override_settings(DJANGO_FORBID={"TERRITORIES": ["EU"], "OPTIONS": {"ACTION": "FORBID"}})
100-
def test_access_from_gb_when_eu_in_forbidden_territories(get_response):
58+
def test_should_forbid_country_when_country_in_territories_blacklist_otherwise_allow(get_response):
10159
"""Access is not granted from GB when EU is in the forbidden list."""
102-
assert forbids(get_response, IP_LONDON)
103-
104-
105-
@override_settings(DJANGO_FORBID={"TERRITORIES": ["AS"], "OPTIONS": {"ACTION": "FORBID"}})
106-
def test_access_from_gb_when_eu_not_in_forbidden_territories(get_response):
107-
"""Access is granted from GB when EU is not in the forbidden list."""
108-
assert not forbids(get_response, IP_LONDON)
60+
assert forbids(get_response, IP.ip_london)
61+
assert not forbids(get_response, IP.ip_cobain)

0 commit comments

Comments
 (0)