|
| 1 | +from django.test import override_settings |
| 2 | + |
| 3 | +from django_forbid.device import detect_device |
| 4 | +from django_forbid.device import device_forbidden |
| 5 | + |
| 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 | +devices = ( |
| 23 | + peripheral_ua, smartphone_ua, wearable_ua, phablet_ua, desktop_ua, console_ua, |
| 24 | + display_ua, speaker_ua, camera_ua, tablet_ua, player_ua, phone_ua, car_ua, tv_ua, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +@override_settings(DJANGO_FORBID={"DEVICES": []}) |
| 29 | +def test_access_with_empty_list_of_devices(): |
| 30 | + """Should allow access to all devices if the list is empty, even if the user agent is unknown.""" |
| 31 | + for device_ua in devices + (unknown_ua,): |
| 32 | + device_type = detect_device(device_ua) |
| 33 | + assert not device_forbidden(device_type) |
| 34 | + |
| 35 | + |
| 36 | +@override_settings(DJANGO_FORBID={"DEVICES": ["desktop", "smartphone", "console", "tablet", "tv"]}) |
| 37 | +def test_access_desktops_smartphones_consoles_tablets_and_tvs(): |
| 38 | + """Should allow access to desktops, smartphones, consoles, tablets and TVs.""" |
| 39 | + for device_ua in devices + (unknown_ua,): |
| 40 | + device_type = detect_device(device_ua) |
| 41 | + if device_type not in ("desktop", "smartphone", "console", "tablet", "tv"): |
| 42 | + # Forbid access to all non-listed devices. |
| 43 | + assert device_forbidden(device_type) |
| 44 | + continue |
| 45 | + assert not device_forbidden(device_type) |
| 46 | + |
| 47 | + |
| 48 | +@override_settings(DJANGO_FORBID={"DEVICES": ["!car", "!speaker", "!wearable"]}) |
| 49 | +def test_forbid_access_to_cars_speakers_and_wearables(): |
| 50 | + """Should forbid access to cars, speakers and wearables.""" |
| 51 | + for device_ua in devices: |
| 52 | + device_type = detect_device(device_ua) |
| 53 | + if device_type in ("car", "speaker", "wearable"): |
| 54 | + # Forbid access to cars, speakers and wearables. |
| 55 | + assert device_forbidden(device_type) |
| 56 | + continue |
| 57 | + assert not device_forbidden(device_type) |
| 58 | + |
| 59 | + |
| 60 | +@override_settings(DJANGO_FORBID={"DEVICES": ["!phablet", "tablet", "phablet"]}) |
| 61 | +def test_forbid_access_if_same_device_is_listed_as_permitted_and_forbidden(): |
| 62 | + """Should forbid access if the same device is listed as permitted and forbidden.""" |
| 63 | + for device_ua in devices + (unknown_ua,): |
| 64 | + device_type = detect_device(device_ua) |
| 65 | + if device_type != "tablet": |
| 66 | + # Forbid all non-tablet devices. |
| 67 | + assert device_forbidden(device_type) |
| 68 | + continue |
| 69 | + assert not device_forbidden(device_type) |
| 70 | + |
| 71 | + |
| 72 | +@override_settings(DJANGO_FORBID={"DEVICES": ["smartphone", "phablet", "tablet"]}) |
| 73 | +def test_forbid_access_unknown_devices_if_devices_are_set(): |
| 74 | + """Should forbid access to unknown devices if the list of devices is not empty.""" |
| 75 | + device_type = detect_device(unknown_ua) |
| 76 | + assert device_forbidden(device_type) |
0 commit comments