From a71680c0744a5008dcebcd6e0f45e231b0f77b8c Mon Sep 17 00:00:00 2001 From: Derrick Petzold Date: Thu, 11 Sep 2014 16:35:28 -0700 Subject: [PATCH 01/10] Added test. --- user_agents/tests.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/user_agents/tests.py b/user_agents/tests.py index 0a8eab2..cc3f728 100644 --- a/user_agents/tests.py +++ b/user_agents/tests.py @@ -1,3 +1,5 @@ +# flake8: noqa + import unittest from ua_parser import user_agent_parser @@ -25,6 +27,7 @@ google_bot_ua_string = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' nokia_n97_ua_string = 'Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/12.0.024; Profile/MIDP-2.1 Configuration/CLDC-1.1; en-us) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.12344' android_firefox_aurora_ua_string = 'Mozilla/5.0 (Android; Mobile; rv:27.0) Gecko/27.0 Firefox/27.0' +samsung_galaxy_s_ua_string = 'Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG SCH-I545 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36' iphone_ua = parse(iphone_ua_string) ipad_ua = parse(ipad_ua_string) @@ -47,6 +50,7 @@ google_bot_ua = parse(google_bot_ua_string) nokia_n97_ua = parse(nokia_n97_ua_string) android_firefox_aurora_ua = parse(android_firefox_aurora_ua_string) +samsung_galaxy_s_ua = parse(samsung_galaxy_s_ua_string) class UserAgentsTest(unittest.TestCase): @@ -61,7 +65,7 @@ def test_user_agent_object_assignments(self): iphone_ua.browser.version, (int(ua_dict['user_agent']['major']), int(ua_dict['user_agent']['minor'])) ) - + # Ensure os attributes are assigned correctly self.assertEqual(iphone_ua.os.family, ua_dict['os']['family']) self.assertEqual( @@ -93,6 +97,7 @@ def test_is_tablet_property(self): self.assertTrue(kindle_fire_ua.is_tablet) self.assertTrue(nexus_7_ua.is_tablet) self.assertFalse(android_firefox_aurora_ua.is_tablet) + self.assertFalse(samsung_galaxy_s_ua.is_tablet) def test_is_mobile_property(self): self.assertTrue(iphone_ua.is_mobile) @@ -114,6 +119,7 @@ def test_is_mobile_property(self): self.assertFalse(ubuntu_firefox_ua.is_mobile) self.assertFalse(google_bot_ua.is_mobile) self.assertTrue(android_firefox_aurora_ua.is_mobile) + self.assertTrue(samsung_galaxy_s_ua.is_mobile) def test_is_touch_property(self): self.assertTrue(iphone_ua.is_touch_capable) @@ -135,6 +141,7 @@ def test_is_touch_property(self): self.assertFalse(google_bot_ua.is_touch_capable) self.assertFalse(nokia_n97_ua.is_touch_capable) self.assertTrue(android_firefox_aurora_ua.is_touch_capable) + self.assertTrue(samsung_galaxy_s_ua.is_touch_capable) def test_is_pc(self): self.assertFalse(iphone_ua.is_pc) @@ -156,6 +163,7 @@ def test_is_pc(self): self.assertTrue(ie_touch_ua.is_pc) self.assertTrue(ie_ua.is_pc) self.assertFalse(android_firefox_aurora_ua.is_pc) + self.assertFalse(samsung_galaxy_s_ua.is_pc) def test_is_bot(self): self.assertTrue(google_bot_ua.is_bot) @@ -177,6 +185,7 @@ def test_is_bot(self): self.assertFalse(ie_ua.is_bot) self.assertFalse(nokia_n97_ua.is_bot) self.assertFalse(android_firefox_aurora_ua.is_bot) + self.assertFalse(samsung_galaxy_s_ua.is_bot) def test_strings(self): self.assertEqual(str(iphone_ua), "iPhone / iOS 5.1 / Mobile Safari 5.1") From b2d428d5fd1fbd068c76b5092953e7bf25b91372 Mon Sep 17 00:00:00 2001 From: Derrick Petzold Date: Thu, 11 Sep 2014 16:43:34 -0700 Subject: [PATCH 02/10] pep8 --- user_agents/parsers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/user_agents/parsers.py b/user_agents/parsers.py index eb62121..425098b 100644 --- a/user_agents/parsers.py +++ b/user_agents/parsers.py @@ -22,14 +22,14 @@ MOBILE_OS_FAMILIES = ( 'Windows Phone', - 'Windows Phone OS', # Earlier versions of ua-parser returns Windows Phone OS + 'Windows Phone OS', # Earlier versions of ua-parser returns Windows Phone OS 'Symbian OS', ) TABLET_DEVICE_FAMILIES = ( 'iPad', 'BlackBerry Playbook', - 'Blackberry Playbook', # Earlier versions of ua-parser returns "Blackberry" instead of "BlackBerry" + 'Blackberry Playbook', # Earlier versions of ua-parser returns "Blackberry" instead of "BlackBerry" 'Kindle', 'Kindle Fire', ) @@ -130,9 +130,9 @@ def _is_blackberry_touch_capable_device(self): # Blackberry Bold Touch series begins with 99XX if 'Blackberry 99' in self.device.family: return True - if 'Blackberry 95' in self.device.family: # BB Storm devices + if 'Blackberry 95' in self.device.family: # BB Storm devices return True - if 'Blackberry 95' in self.device.family: # BB Torch devices + if 'Blackberry 95' in self.device.family: # BB Torch devices return True return False From 907ae15da7f4317756e588ba42be8e4c1828b6e9 Mon Sep 17 00:00:00 2001 From: Derrick Petzold Date: Thu, 11 Sep 2014 16:44:03 -0700 Subject: [PATCH 03/10] Update tablet device family. --- user_agents/parsers.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/user_agents/parsers.py b/user_agents/parsers.py index 425098b..040e5c8 100644 --- a/user_agents/parsers.py +++ b/user_agents/parsers.py @@ -32,6 +32,10 @@ 'Blackberry Playbook', # Earlier versions of ua-parser returns "Blackberry" instead of "BlackBerry" 'Kindle', 'Kindle Fire', + 'Kindle Fire HD', + 'Galaxy Tab', + 'Xoom', + 'Dell Streak', ) TOUCH_CAPABLE_OS_FAMILIES = ( From 873da2772af4da5790548bacf29e404d5717d16d Mon Sep 17 00:00:00 2001 From: Derrick Petzold Date: Thu, 11 Sep 2014 17:00:15 -0700 Subject: [PATCH 04/10] The SCH- check was erroneously setting Galaxy S phones to tablets. --- user_agents/parsers.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/user_agents/parsers.py b/user_agents/parsers.py index 040e5c8..4ab8a20 100644 --- a/user_agents/parsers.py +++ b/user_agents/parsers.py @@ -125,8 +125,6 @@ def _is_android_tablet(self): if ('Mobile Safari' not in self.ua_string and self.browser.family != "Firefox Mobile"): return True - if 'SCH-' in self.ua_string: - return True return False def _is_blackberry_touch_capable_device(self): From 1aad434543d63592494bcd1b0e9011903113d83c Mon Sep 17 00:00:00 2001 From: Derrick Petzold Date: Mon, 22 Sep 2014 15:13:52 -0700 Subject: [PATCH 05/10] Added support for PlayStation, PlayStation Vita, and SAMSUNG-GT-S8530. --- user_agents/parsers.py | 2 ++ user_agents/tests.py | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/user_agents/parsers.py b/user_agents/parsers.py index 4ab8a20..5085c9e 100644 --- a/user_agents/parsers.py +++ b/user_agents/parsers.py @@ -18,12 +18,14 @@ 'iPod', 'Generic Smartphone', 'Generic Feature Phone', + 'PlayStation Vita', ) MOBILE_OS_FAMILIES = ( 'Windows Phone', 'Windows Phone OS', # Earlier versions of ua-parser returns Windows Phone OS 'Symbian OS', + 'Bada', ) TABLET_DEVICE_FAMILIES = ( diff --git a/user_agents/tests.py b/user_agents/tests.py index cc3f728..1e6b599 100644 --- a/user_agents/tests.py +++ b/user_agents/tests.py @@ -28,6 +28,10 @@ nokia_n97_ua_string = 'Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/12.0.024; Profile/MIDP-2.1 Configuration/CLDC-1.1; en-us) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.12344' android_firefox_aurora_ua_string = 'Mozilla/5.0 (Android; Mobile; rv:27.0) Gecko/27.0 Firefox/27.0' samsung_galaxy_s_ua_string = 'Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG SCH-I545 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36' +playstation_vita_ua_string = 'Mozilla/5.0 (PlayStation Vita 3.12) AppleWebKit/536.26 (KHTML, like Gecko) Silk/3.2' +playstation_ua_string = 'Mozilla/5.0 (PLAYSTATION 3 4.60) AppleWebKit/531.22.8 (KHTML, like Gecko)' +samsung_gt_ua_string = 'Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S8530/S8530DDLC2; U; Bada/2.0; en-us) AppleWebKit/534.20 (KHTML, like Gecko) Dolfin/3.0 Mobile WVGA SMM-MMS/1.2.0 OPN-B' + iphone_ua = parse(iphone_ua_string) ipad_ua = parse(ipad_ua_string) @@ -51,6 +55,9 @@ nokia_n97_ua = parse(nokia_n97_ua_string) android_firefox_aurora_ua = parse(android_firefox_aurora_ua_string) samsung_galaxy_s_ua = parse(samsung_galaxy_s_ua_string) +playstation_vita_ua = parse(playstation_vita_ua_string) +playstation_ua = parse(playstation_ua_string) +samsung_gt_ua = parse(samsung_gt_ua_string) class UserAgentsTest(unittest.TestCase): @@ -98,6 +105,10 @@ def test_is_tablet_property(self): self.assertTrue(nexus_7_ua.is_tablet) self.assertFalse(android_firefox_aurora_ua.is_tablet) self.assertFalse(samsung_galaxy_s_ua.is_tablet) + self.assertFalse(playstation_vita_ua.is_tablet) + self.assertFalse(playstation_ua.is_tablet) + self.assertFalse(samsung_gt_ua.is_tablet) + def test_is_mobile_property(self): self.assertTrue(iphone_ua.is_mobile) @@ -120,6 +131,10 @@ def test_is_mobile_property(self): self.assertFalse(google_bot_ua.is_mobile) self.assertTrue(android_firefox_aurora_ua.is_mobile) self.assertTrue(samsung_galaxy_s_ua.is_mobile) + self.assertTrue(playstation_vita_ua.is_mobile) + self.assertFalse(playstation_ua.is_mobile) + self.assertTrue(samsung_gt_ua.is_mobile) + def test_is_touch_property(self): self.assertTrue(iphone_ua.is_touch_capable) @@ -142,6 +157,10 @@ def test_is_touch_property(self): self.assertFalse(nokia_n97_ua.is_touch_capable) self.assertTrue(android_firefox_aurora_ua.is_touch_capable) self.assertTrue(samsung_galaxy_s_ua.is_touch_capable) + self.assertFalse(playstation_vita_ua.is_touch_capable) + self.assertFalse(playstation_ua.is_touch_capable) + self.assertFalse(samsung_gt_ua.is_touch_capable) + def test_is_pc(self): self.assertFalse(iphone_ua.is_pc) @@ -152,7 +171,7 @@ def test_is_pc(self): self.assertFalse(nexus_7_ua.is_pc) self.assertFalse(windows_phone_ua.is_pc) self.assertFalse(blackberry_bold_touch_ua.is_pc) - self.assertFalse(blackberry_torch_ua.is_pc) + self.assertFalse(blackberry_torch_ua.is_pc) self.assertFalse(blackberry_bold_ua.is_pc) self.assertFalse(j2me_opera_ua.is_pc) self.assertFalse(google_bot_ua.is_pc) @@ -164,6 +183,10 @@ def test_is_pc(self): self.assertTrue(ie_ua.is_pc) self.assertFalse(android_firefox_aurora_ua.is_pc) self.assertFalse(samsung_galaxy_s_ua.is_pc) + self.assertFalse(playstation_vita_ua.is_pc) + self.assertFalse(playstation_ua.is_pc) + self.assertFalse(samsung_gt_ua.is_pc) + def test_is_bot(self): self.assertTrue(google_bot_ua.is_bot) @@ -175,9 +198,9 @@ def test_is_bot(self): self.assertFalse(nexus_7_ua.is_bot) self.assertFalse(windows_phone_ua.is_bot) self.assertFalse(blackberry_bold_touch_ua.is_bot) - self.assertFalse(blackberry_torch_ua.is_bot) + self.assertFalse(blackberry_torch_ua.is_bot) self.assertFalse(blackberry_bold_ua.is_bot) - self.assertFalse(j2me_opera_ua.is_bot) + self.assertFalse(j2me_opera_ua.is_bot) self.assertFalse(mac_safari_ua.is_bot) self.assertFalse(windows_ie_ua.is_bot) self.assertFalse(ubuntu_firefox_ua.is_bot) @@ -186,6 +209,10 @@ def test_is_bot(self): self.assertFalse(nokia_n97_ua.is_bot) self.assertFalse(android_firefox_aurora_ua.is_bot) self.assertFalse(samsung_galaxy_s_ua.is_bot) + self.assertFalse(playstation_vita_ua.is_bot) + self.assertFalse(playstation_ua.is_bot) + self.assertFalse(samsung_gt_ua.is_bot) + def test_strings(self): self.assertEqual(str(iphone_ua), "iPhone / iOS 5.1 / Mobile Safari 5.1") From e00e0cf0de73d067a8c74545dff5e16f9abb21c9 Mon Sep 17 00:00:00 2001 From: Derrick Petzold Date: Thu, 11 Sep 2014 16:35:28 -0700 Subject: [PATCH 06/10] The SCH- check was erroneously setting Galaxy S phones to tablets. Added support for PlayStation, PlayStation Vita, and SAMSUNG-GT-S8530. --- user_agents/parsers.py | 16 +++++++++------ user_agents/tests.py | 44 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/user_agents/parsers.py b/user_agents/parsers.py index eb62121..5085c9e 100644 --- a/user_agents/parsers.py +++ b/user_agents/parsers.py @@ -18,20 +18,26 @@ 'iPod', 'Generic Smartphone', 'Generic Feature Phone', + 'PlayStation Vita', ) MOBILE_OS_FAMILIES = ( 'Windows Phone', - 'Windows Phone OS', # Earlier versions of ua-parser returns Windows Phone OS + 'Windows Phone OS', # Earlier versions of ua-parser returns Windows Phone OS 'Symbian OS', + 'Bada', ) TABLET_DEVICE_FAMILIES = ( 'iPad', 'BlackBerry Playbook', - 'Blackberry Playbook', # Earlier versions of ua-parser returns "Blackberry" instead of "BlackBerry" + 'Blackberry Playbook', # Earlier versions of ua-parser returns "Blackberry" instead of "BlackBerry" 'Kindle', 'Kindle Fire', + 'Kindle Fire HD', + 'Galaxy Tab', + 'Xoom', + 'Dell Streak', ) TOUCH_CAPABLE_OS_FAMILIES = ( @@ -121,8 +127,6 @@ def _is_android_tablet(self): if ('Mobile Safari' not in self.ua_string and self.browser.family != "Firefox Mobile"): return True - if 'SCH-' in self.ua_string: - return True return False def _is_blackberry_touch_capable_device(self): @@ -130,9 +134,9 @@ def _is_blackberry_touch_capable_device(self): # Blackberry Bold Touch series begins with 99XX if 'Blackberry 99' in self.device.family: return True - if 'Blackberry 95' in self.device.family: # BB Storm devices + if 'Blackberry 95' in self.device.family: # BB Storm devices return True - if 'Blackberry 95' in self.device.family: # BB Torch devices + if 'Blackberry 95' in self.device.family: # BB Torch devices return True return False diff --git a/user_agents/tests.py b/user_agents/tests.py index 0a8eab2..1e6b599 100644 --- a/user_agents/tests.py +++ b/user_agents/tests.py @@ -1,3 +1,5 @@ +# flake8: noqa + import unittest from ua_parser import user_agent_parser @@ -25,6 +27,11 @@ google_bot_ua_string = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' nokia_n97_ua_string = 'Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/12.0.024; Profile/MIDP-2.1 Configuration/CLDC-1.1; en-us) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.12344' android_firefox_aurora_ua_string = 'Mozilla/5.0 (Android; Mobile; rv:27.0) Gecko/27.0 Firefox/27.0' +samsung_galaxy_s_ua_string = 'Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG SCH-I545 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36' +playstation_vita_ua_string = 'Mozilla/5.0 (PlayStation Vita 3.12) AppleWebKit/536.26 (KHTML, like Gecko) Silk/3.2' +playstation_ua_string = 'Mozilla/5.0 (PLAYSTATION 3 4.60) AppleWebKit/531.22.8 (KHTML, like Gecko)' +samsung_gt_ua_string = 'Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S8530/S8530DDLC2; U; Bada/2.0; en-us) AppleWebKit/534.20 (KHTML, like Gecko) Dolfin/3.0 Mobile WVGA SMM-MMS/1.2.0 OPN-B' + iphone_ua = parse(iphone_ua_string) ipad_ua = parse(ipad_ua_string) @@ -47,6 +54,10 @@ google_bot_ua = parse(google_bot_ua_string) nokia_n97_ua = parse(nokia_n97_ua_string) android_firefox_aurora_ua = parse(android_firefox_aurora_ua_string) +samsung_galaxy_s_ua = parse(samsung_galaxy_s_ua_string) +playstation_vita_ua = parse(playstation_vita_ua_string) +playstation_ua = parse(playstation_ua_string) +samsung_gt_ua = parse(samsung_gt_ua_string) class UserAgentsTest(unittest.TestCase): @@ -61,7 +72,7 @@ def test_user_agent_object_assignments(self): iphone_ua.browser.version, (int(ua_dict['user_agent']['major']), int(ua_dict['user_agent']['minor'])) ) - + # Ensure os attributes are assigned correctly self.assertEqual(iphone_ua.os.family, ua_dict['os']['family']) self.assertEqual( @@ -93,6 +104,11 @@ def test_is_tablet_property(self): self.assertTrue(kindle_fire_ua.is_tablet) self.assertTrue(nexus_7_ua.is_tablet) self.assertFalse(android_firefox_aurora_ua.is_tablet) + self.assertFalse(samsung_galaxy_s_ua.is_tablet) + self.assertFalse(playstation_vita_ua.is_tablet) + self.assertFalse(playstation_ua.is_tablet) + self.assertFalse(samsung_gt_ua.is_tablet) + def test_is_mobile_property(self): self.assertTrue(iphone_ua.is_mobile) @@ -114,6 +130,11 @@ def test_is_mobile_property(self): self.assertFalse(ubuntu_firefox_ua.is_mobile) self.assertFalse(google_bot_ua.is_mobile) self.assertTrue(android_firefox_aurora_ua.is_mobile) + self.assertTrue(samsung_galaxy_s_ua.is_mobile) + self.assertTrue(playstation_vita_ua.is_mobile) + self.assertFalse(playstation_ua.is_mobile) + self.assertTrue(samsung_gt_ua.is_mobile) + def test_is_touch_property(self): self.assertTrue(iphone_ua.is_touch_capable) @@ -135,6 +156,11 @@ def test_is_touch_property(self): self.assertFalse(google_bot_ua.is_touch_capable) self.assertFalse(nokia_n97_ua.is_touch_capable) self.assertTrue(android_firefox_aurora_ua.is_touch_capable) + self.assertTrue(samsung_galaxy_s_ua.is_touch_capable) + self.assertFalse(playstation_vita_ua.is_touch_capable) + self.assertFalse(playstation_ua.is_touch_capable) + self.assertFalse(samsung_gt_ua.is_touch_capable) + def test_is_pc(self): self.assertFalse(iphone_ua.is_pc) @@ -145,7 +171,7 @@ def test_is_pc(self): self.assertFalse(nexus_7_ua.is_pc) self.assertFalse(windows_phone_ua.is_pc) self.assertFalse(blackberry_bold_touch_ua.is_pc) - self.assertFalse(blackberry_torch_ua.is_pc) + self.assertFalse(blackberry_torch_ua.is_pc) self.assertFalse(blackberry_bold_ua.is_pc) self.assertFalse(j2me_opera_ua.is_pc) self.assertFalse(google_bot_ua.is_pc) @@ -156,6 +182,11 @@ def test_is_pc(self): self.assertTrue(ie_touch_ua.is_pc) self.assertTrue(ie_ua.is_pc) self.assertFalse(android_firefox_aurora_ua.is_pc) + self.assertFalse(samsung_galaxy_s_ua.is_pc) + self.assertFalse(playstation_vita_ua.is_pc) + self.assertFalse(playstation_ua.is_pc) + self.assertFalse(samsung_gt_ua.is_pc) + def test_is_bot(self): self.assertTrue(google_bot_ua.is_bot) @@ -167,9 +198,9 @@ def test_is_bot(self): self.assertFalse(nexus_7_ua.is_bot) self.assertFalse(windows_phone_ua.is_bot) self.assertFalse(blackberry_bold_touch_ua.is_bot) - self.assertFalse(blackberry_torch_ua.is_bot) + self.assertFalse(blackberry_torch_ua.is_bot) self.assertFalse(blackberry_bold_ua.is_bot) - self.assertFalse(j2me_opera_ua.is_bot) + self.assertFalse(j2me_opera_ua.is_bot) self.assertFalse(mac_safari_ua.is_bot) self.assertFalse(windows_ie_ua.is_bot) self.assertFalse(ubuntu_firefox_ua.is_bot) @@ -177,6 +208,11 @@ def test_is_bot(self): self.assertFalse(ie_ua.is_bot) self.assertFalse(nokia_n97_ua.is_bot) self.assertFalse(android_firefox_aurora_ua.is_bot) + self.assertFalse(samsung_galaxy_s_ua.is_bot) + self.assertFalse(playstation_vita_ua.is_bot) + self.assertFalse(playstation_ua.is_bot) + self.assertFalse(samsung_gt_ua.is_bot) + def test_strings(self): self.assertEqual(str(iphone_ua), "iPhone / iOS 5.1 / Mobile Safari 5.1") From 01e1534df9a0c405cd5484c217d15a9bafd535f0 Mon Sep 17 00:00:00 2001 From: Derrick Petzold Date: Mon, 22 Sep 2014 18:01:43 -0700 Subject: [PATCH 07/10] Json based tests. --- user_agents/devices.json | 227 ++++++++++++++++++++++++++++++++++++ user_agents/tests.py | 242 +++++---------------------------------- 2 files changed, 255 insertions(+), 214 deletions(-) create mode 100644 user_agents/devices.json diff --git a/user_agents/devices.json b/user_agents/devices.json new file mode 100644 index 0000000..a12f188 --- /dev/null +++ b/user_agents/devices.json @@ -0,0 +1,227 @@ +{ + "android_firefox_aurora": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (Android; Mobile; rv:27.0) Gecko/27.0 Firefox/27.0", + "str": "Other / Android / Firefox Mobile 27" + }, + "blackberry_bold": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "BlackBerry9700/5.0.0.862 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/331 UNTRUSTED/1.0 3gpp-gba", + "str": "BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700" + }, + "blackberry_bold_touch": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Mozilla/5.0 (BlackBerry; U; BlackBerry 9930; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.241 Mobile Safari/534.11+", + "str": "BlackBerry 9930 / BlackBerry OS 7 / BlackBerry WebKit 7" + }, + "blackberry_torch": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; zh-TW) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.448 Mobile Safari/534.8+", + "str": "BlackBerry 9800 / BlackBerry OS 6 / BlackBerry WebKit 6" + }, + "galaxy_s3": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", + "str": "GT-I9300 / Android 4.0.4 / Android 4.0.4" + }, + "galaxy_tab": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (Linux; U; Android 2.2; en-us; SCH-I800 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1", + "str": "SCH-I800 / Android 2.2 / Android 2.2" + }, + "google_bot": { + "is_bot": true, + "is_mobile": false, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", + "str": "Spider / Other / Googlebot 2.1" + }, + "ie": { + "is_bot": false, + "is_mobile": false, + "is_pc": true, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)", + "str": "PC / Windows 8 / IE 10" + }, + "ie_touch": { + "is_bot": false, + "is_mobile": false, + "is_pc": true, + "is_tablet": false, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)", + "str": "PC / Windows 8 / IE 10" + }, + "ipad": { + "is_bot": false, + "is_mobile": false, + "is_pc": false, + "is_tablet": true, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10", + "str": "iPad / iOS 3.2 / Mobile Safari 4.0.4" + }, + "iphone": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3", + "str": "iPhone / iOS 5.1 / Mobile Safari 5.1" + }, + "j2me_opera": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Opera/9.80 (J2ME/MIDP; Opera Mini/9.80 (J2ME/22.478; U; en) Presto/2.5.25 Version/10.54", + "str": "Other / Other / Opera Mini 9.80" + }, + "kindle_fire": { + "is_bot": false, + "is_mobile": false, + "is_pc": false, + "is_tablet": true, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-80) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true", + "str": "Kindle Fire / Android / Amazon Silk 1.1.0-80" + }, + "mac_safari": { + "is_bot": false, + "is_mobile": false, + "is_pc": true, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2", + "str": "PC / Mac OS X 10.6.8 / WebKit Nightly 537.13" + }, + "nexus_7": { + "is_bot": false, + "is_mobile": false, + "is_pc": false, + "is_tablet": true, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19", + "str": "Nexus 7 / Android 4.1.1 / Chrome 18.0.1025" + }, + "nokia_n97": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/12.0.024; Profile/MIDP-2.1 Configuration/CLDC-1.1; en-us) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.12344", + "str": "Nokia N97 / Symbian OS 9.4 / Nokia Browser 7.1.12344" + }, + "playbook": { + "is_bot": false, + "is_mobile": false, + "is_pc": false, + "is_tablet": true, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.0.1; en-US) AppleWebKit/535.8+ (KHTML, like Gecko) Version/7.2.0.1 Safari/535.8+", + "str": "BlackBerry Playbook / BlackBerry Tablet OS 2.0.1 / BlackBerry WebKit 2.0.1" + }, + "playstation": { + "is_bot": false, + "is_mobile": false, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Mozilla/5.0 (PLAYSTATION 3 4.60) AppleWebKit/531.22.8 (KHTML, like Gecko)", + "str": "PlayStation 3 / Other / NetFront NX" + }, + "playstation_vita": {"is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Mozilla/5.0 (PlayStation Vita 3.12) AppleWebKit/536.26 (KHTML, like Gecko) Silk/3.2", + "str": "PlayStation Vita / Other / NetFront NX" + }, + "samsung_galaxy_s": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG SCH-I545 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36", + "str": "SAMSUNG SCH-I545 / Android 4.4.2 / Chrome Mobile 28.0.1500" + }, + "samsung_gt": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S8530/S8530DDLC2; U; Bada/2.0; en-us) AppleWebKit/534.20 (KHTML, like Gecko) Dolfin/3.0 Mobile WVGA SMM-MMS/1.2.0 OPN-B", + "str": "Samsung GT-S8530 / Bada 2 / Dolfin 3" + }, + "ubuntu_firefox": { + "is_bot": false, + "is_mobile": false, + "is_pc": true, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1", + "str": "PC / Ubuntu / Firefox 15.0.1" + }, + "windows_ie": { + "is_bot": false, + "is_mobile": false, + "is_pc": true, + "is_tablet": false, + "is_touch_capable": false, + "ua_string": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)", + "str": "PC / Windows 7 / IE 9" + }, + "windows_phone": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; SGH-i917)", + "str": "Samsung SGH-i917 / Windows Phone 7.5 / IE Mobile 9" + }, + "windows_rt": { + "is_bot": false, + "is_mobile": false, + "is_pc": true, + "is_tablet": true, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; ARM; Trident/6.0)", + "str": "PC / Windows RT / IE 10" + } +} + diff --git a/user_agents/tests.py b/user_agents/tests.py index 1e6b599..89a7307 100644 --- a/user_agents/tests.py +++ b/user_agents/tests.py @@ -1,76 +1,25 @@ -# flake8: noqa - +import json +import os +import path import unittest from ua_parser import user_agent_parser -from .parsers import parse, UserAgent - - -iphone_ua_string = 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3' -ipad_ua_string = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10' -galaxy_tab_ua_string = 'Mozilla/5.0 (Linux; U; Android 2.2; en-us; SCH-I800 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' -galaxy_s3_ua_string = 'Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30' -kindle_fire_ua_string = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-80) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true' -playbook_ua_string = 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.0.1; en-US) AppleWebKit/535.8+ (KHTML, like Gecko) Version/7.2.0.1 Safari/535.8+' -nexus_7_ua_string = 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19' -windows_phone_ua_string = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; SGH-i917)' -blackberry_torch_ua_string = 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; zh-TW) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.448 Mobile Safari/534.8+' -blackberry_bold_ua_string = 'BlackBerry9700/5.0.0.862 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/331 UNTRUSTED/1.0 3gpp-gba' -blackberry_bold_touch_ua_string = 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9930; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.241 Mobile Safari/534.11+' -windows_rt_ua_string = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; ARM; Trident/6.0)' -j2me_opera_ua_string = 'Opera/9.80 (J2ME/MIDP; Opera Mini/9.80 (J2ME/22.478; U; en) Presto/2.5.25 Version/10.54' -ie_ua_string = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)' -ie_touch_ua_string = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)' -mac_safari_ua_string = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2' -windows_ie_ua_string = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)' -ubuntu_firefox_ua_string = 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1' -google_bot_ua_string = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' -nokia_n97_ua_string = 'Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/12.0.024; Profile/MIDP-2.1 Configuration/CLDC-1.1; en-us) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.12344' -android_firefox_aurora_ua_string = 'Mozilla/5.0 (Android; Mobile; rv:27.0) Gecko/27.0 Firefox/27.0' -samsung_galaxy_s_ua_string = 'Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG SCH-I545 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36' -playstation_vita_ua_string = 'Mozilla/5.0 (PlayStation Vita 3.12) AppleWebKit/536.26 (KHTML, like Gecko) Silk/3.2' -playstation_ua_string = 'Mozilla/5.0 (PLAYSTATION 3 4.60) AppleWebKit/531.22.8 (KHTML, like Gecko)' -samsung_gt_ua_string = 'Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S8530/S8530DDLC2; U; Bada/2.0; en-us) AppleWebKit/534.20 (KHTML, like Gecko) Dolfin/3.0 Mobile WVGA SMM-MMS/1.2.0 OPN-B' - - -iphone_ua = parse(iphone_ua_string) -ipad_ua = parse(ipad_ua_string) -galaxy_tab = parse(galaxy_tab_ua_string) -galaxy_s3_ua = parse(galaxy_s3_ua_string) -kindle_fire_ua = parse(kindle_fire_ua_string) -playbook_ua = parse(playbook_ua_string) -nexus_7_ua = parse(nexus_7_ua_string) -windows_phone_ua = parse(windows_phone_ua_string) -windows_rt_ua = parse(windows_rt_ua_string) -blackberry_torch_ua = parse(blackberry_torch_ua_string) -blackberry_bold_ua = parse(blackberry_bold_ua_string) -blackberry_bold_touch_ua = parse(blackberry_bold_touch_ua_string) -j2me_opera_ua = parse(j2me_opera_ua_string) -ie_ua = parse(ie_ua_string) -ie_touch_ua = parse(ie_touch_ua_string) -mac_safari_ua = parse(mac_safari_ua_string) -windows_ie_ua = parse(windows_ie_ua_string) -ubuntu_firefox_ua = parse(ubuntu_firefox_ua_string) -google_bot_ua = parse(google_bot_ua_string) -nokia_n97_ua = parse(nokia_n97_ua_string) -android_firefox_aurora_ua = parse(android_firefox_aurora_ua_string) -samsung_galaxy_s_ua = parse(samsung_galaxy_s_ua_string) -playstation_vita_ua = parse(playstation_vita_ua_string) -playstation_ua = parse(playstation_ua_string) -samsung_gt_ua = parse(samsung_gt_ua_string) +from .parsers import parse class UserAgentsTest(unittest.TestCase): def test_user_agent_object_assignments(self): - ua_dict = user_agent_parser.Parse(iphone_ua_string) + ua_dict = user_agent_parser.Parse(devices['iphone']['ua_string']) + iphone_ua = devices['iphone']['user_agent'] # Ensure browser attributes are assigned correctly self.assertEqual(iphone_ua.browser.family, ua_dict['user_agent']['family']) self.assertEqual( iphone_ua.browser.version, - (int(ua_dict['user_agent']['major']), int(ua_dict['user_agent']['minor'])) + (int(ua_dict['user_agent']['major']), + int(ua_dict['user_agent']['minor'])) ) # Ensure os attributes are assigned correctly @@ -84,168 +33,33 @@ def test_user_agent_object_assignments(self): self.assertEqual(iphone_ua.device.family, ua_dict['device']['family']) - def test_is_tablet_property(self): - self.assertFalse(iphone_ua.is_tablet) - self.assertFalse(galaxy_s3_ua.is_tablet) - self.assertFalse(blackberry_torch_ua.is_tablet) - self.assertFalse(blackberry_bold_ua.is_tablet) - self.assertFalse(windows_phone_ua.is_tablet) - self.assertFalse(ie_ua.is_tablet) - self.assertFalse(ie_touch_ua.is_tablet) - self.assertFalse(mac_safari_ua.is_tablet) - self.assertFalse(windows_ie_ua.is_tablet) - self.assertFalse(ubuntu_firefox_ua.is_tablet) - self.assertFalse(j2me_opera_ua.is_tablet) - self.assertFalse(google_bot_ua.is_tablet) - self.assertFalse(nokia_n97_ua.is_tablet) - self.assertTrue(windows_rt_ua.is_tablet) - self.assertTrue(ipad_ua.is_tablet) - self.assertTrue(playbook_ua.is_tablet) - self.assertTrue(kindle_fire_ua.is_tablet) - self.assertTrue(nexus_7_ua.is_tablet) - self.assertFalse(android_firefox_aurora_ua.is_tablet) - self.assertFalse(samsung_galaxy_s_ua.is_tablet) - self.assertFalse(playstation_vita_ua.is_tablet) - self.assertFalse(playstation_ua.is_tablet) - self.assertFalse(samsung_gt_ua.is_tablet) - - - def test_is_mobile_property(self): - self.assertTrue(iphone_ua.is_mobile) - self.assertTrue(galaxy_s3_ua.is_mobile) - self.assertTrue(blackberry_torch_ua.is_mobile) - self.assertTrue(blackberry_bold_ua.is_mobile) - self.assertTrue(windows_phone_ua.is_mobile) - self.assertTrue(j2me_opera_ua.is_mobile) - self.assertTrue(nokia_n97_ua.is_mobile) - self.assertFalse(windows_rt_ua.is_mobile) - self.assertFalse(ipad_ua.is_mobile) - self.assertFalse(playbook_ua.is_mobile) - self.assertFalse(kindle_fire_ua.is_mobile) - self.assertFalse(nexus_7_ua.is_mobile) - self.assertFalse(ie_ua.is_mobile) - self.assertFalse(ie_touch_ua.is_mobile) - self.assertFalse(mac_safari_ua.is_mobile) - self.assertFalse(windows_ie_ua.is_mobile) - self.assertFalse(ubuntu_firefox_ua.is_mobile) - self.assertFalse(google_bot_ua.is_mobile) - self.assertTrue(android_firefox_aurora_ua.is_mobile) - self.assertTrue(samsung_galaxy_s_ua.is_mobile) - self.assertTrue(playstation_vita_ua.is_mobile) - self.assertFalse(playstation_ua.is_mobile) - self.assertTrue(samsung_gt_ua.is_mobile) - - - def test_is_touch_property(self): - self.assertTrue(iphone_ua.is_touch_capable) - self.assertTrue(galaxy_s3_ua.is_touch_capable) - self.assertTrue(ipad_ua.is_touch_capable) - self.assertTrue(playbook_ua.is_touch_capable) - self.assertTrue(kindle_fire_ua.is_touch_capable) - self.assertTrue(nexus_7_ua.is_touch_capable) - self.assertTrue(windows_phone_ua.is_touch_capable) - self.assertTrue(ie_touch_ua.is_touch_capable) - self.assertTrue(blackberry_bold_touch_ua.is_mobile) - self.assertTrue(blackberry_torch_ua.is_mobile) - self.assertFalse(j2me_opera_ua.is_touch_capable) - self.assertFalse(ie_ua.is_touch_capable) - self.assertFalse(blackberry_bold_ua.is_touch_capable) - self.assertFalse(mac_safari_ua.is_touch_capable) - self.assertFalse(windows_ie_ua.is_touch_capable) - self.assertFalse(ubuntu_firefox_ua.is_touch_capable) - self.assertFalse(google_bot_ua.is_touch_capable) - self.assertFalse(nokia_n97_ua.is_touch_capable) - self.assertTrue(android_firefox_aurora_ua.is_touch_capable) - self.assertTrue(samsung_galaxy_s_ua.is_touch_capable) - self.assertFalse(playstation_vita_ua.is_touch_capable) - self.assertFalse(playstation_ua.is_touch_capable) - self.assertFalse(samsung_gt_ua.is_touch_capable) - - - def test_is_pc(self): - self.assertFalse(iphone_ua.is_pc) - self.assertFalse(galaxy_s3_ua.is_pc) - self.assertFalse(ipad_ua.is_pc) - self.assertFalse(playbook_ua.is_pc) - self.assertFalse(kindle_fire_ua.is_pc) - self.assertFalse(nexus_7_ua.is_pc) - self.assertFalse(windows_phone_ua.is_pc) - self.assertFalse(blackberry_bold_touch_ua.is_pc) - self.assertFalse(blackberry_torch_ua.is_pc) - self.assertFalse(blackberry_bold_ua.is_pc) - self.assertFalse(j2me_opera_ua.is_pc) - self.assertFalse(google_bot_ua.is_pc) - self.assertFalse(nokia_n97_ua.is_pc) - self.assertTrue(mac_safari_ua.is_pc) - self.assertTrue(windows_ie_ua.is_pc) - self.assertTrue(ubuntu_firefox_ua.is_pc) - self.assertTrue(ie_touch_ua.is_pc) - self.assertTrue(ie_ua.is_pc) - self.assertFalse(android_firefox_aurora_ua.is_pc) - self.assertFalse(samsung_galaxy_s_ua.is_pc) - self.assertFalse(playstation_vita_ua.is_pc) - self.assertFalse(playstation_ua.is_pc) - self.assertFalse(samsung_gt_ua.is_pc) - - - def test_is_bot(self): - self.assertTrue(google_bot_ua.is_bot) - self.assertFalse(iphone_ua.is_bot) - self.assertFalse(galaxy_s3_ua.is_bot) - self.assertFalse(ipad_ua.is_bot) - self.assertFalse(playbook_ua.is_bot) - self.assertFalse(kindle_fire_ua.is_bot) - self.assertFalse(nexus_7_ua.is_bot) - self.assertFalse(windows_phone_ua.is_bot) - self.assertFalse(blackberry_bold_touch_ua.is_bot) - self.assertFalse(blackberry_torch_ua.is_bot) - self.assertFalse(blackberry_bold_ua.is_bot) - self.assertFalse(j2me_opera_ua.is_bot) - self.assertFalse(mac_safari_ua.is_bot) - self.assertFalse(windows_ie_ua.is_bot) - self.assertFalse(ubuntu_firefox_ua.is_bot) - self.assertFalse(ie_touch_ua.is_bot) - self.assertFalse(ie_ua.is_bot) - self.assertFalse(nokia_n97_ua.is_bot) - self.assertFalse(android_firefox_aurora_ua.is_bot) - self.assertFalse(samsung_galaxy_s_ua.is_bot) - self.assertFalse(playstation_vita_ua.is_bot) - self.assertFalse(playstation_ua.is_bot) - self.assertFalse(samsung_gt_ua.is_bot) - - - def test_strings(self): - self.assertEqual(str(iphone_ua), "iPhone / iOS 5.1 / Mobile Safari 5.1") - self.assertEqual(str(ipad_ua), "iPad / iOS 3.2 / Mobile Safari 4.0.4") - self.assertEqual(str(galaxy_tab), "SCH-I800 / Android 2.2 / Android 2.2") - self.assertEqual(str(galaxy_s3_ua), "GT-I9300 / Android 4.0.4 / Android 4.0.4") - self.assertEqual(str(kindle_fire_ua), "Kindle Fire / Android / Amazon Silk 1.1.0-80") - self.assertEqual(str(playbook_ua), "BlackBerry Playbook / BlackBerry Tablet OS 2.0.1 / BlackBerry WebKit 2.0.1") - self.assertEqual(str(nexus_7_ua), "Nexus 7 / Android 4.1.1 / Chrome 18.0.1025") - self.assertEqual(str(windows_phone_ua), "Samsung SGH-i917 / Windows Phone 7.5 / IE Mobile 9") - self.assertEqual(str(windows_rt_ua), "PC / Windows RT / IE 10") - self.assertEqual(str(blackberry_torch_ua), "BlackBerry 9800 / BlackBerry OS 6 / BlackBerry WebKit 6") - self.assertEqual(str(blackberry_bold_ua), "BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700") - self.assertEqual(str(blackberry_bold_touch_ua), "BlackBerry 9930 / BlackBerry OS 7 / BlackBerry WebKit 7") - self.assertEqual(str(j2me_opera_ua), "Other / Other / Opera Mini 9.80") - self.assertEqual(str(ie_ua), "PC / Windows 8 / IE 10") - self.assertEqual(str(ie_touch_ua), "PC / Windows 8 / IE 10") - self.assertEqual(str(mac_safari_ua), "PC / Mac OS X 10.6.8 / WebKit Nightly 537.13") - self.assertEqual(str(windows_ie_ua), "PC / Windows 7 / IE 9") - self.assertEqual(str(ubuntu_firefox_ua), "PC / Ubuntu / Firefox 15.0.1") - self.assertEqual(str(google_bot_ua), "Spider / Other / Googlebot 2.1") - self.assertEqual(str(nokia_n97_ua), "Nokia N97 / Symbian OS 9.4 / Nokia Browser 7.1.12344") - self.assertEqual(str(android_firefox_aurora_ua), "Other / Android / Firefox Mobile 27") - def test_unicode_strings(self): try: # Python 2 - unicode_ua_str = unicode(iphone_ua) + unicode_ua_str = unicode(devices['iphone']['user_agent']) self.assertEqual(unicode_ua_str, u"iPhone / iOS 5.1 / Mobile Safari 5.1") self.assertTrue(isinstance(unicode_ua_str, unicode)) except NameError: # Python 3 - unicode_ua_str = str(iphone_ua) + unicode_ua_str = str(devices['iphone']['user_agent']) self.assertEqual(unicode_ua_str, "iPhone / iOS 5.1 / Mobile Safari 5.1") + + +with open(os.path.join(path.path(__file__).dirname(), 'devices.json')) as f: + devices = json.load(f) + + +def test_wrapper(items): + def test_func(self): + attrs = ('is_bot', 'is_mobile', + 'is_pc', 'is_tablet', 'is_touch_capable') + for attr in attrs: + self.assertEqual(getattr(items['user_agent'], attr), items[attr]) + self.assertEqual(str(items['user_agent']), items['str']) + return test_func + +for device, items in devices.iteritems(): + items['user_agent'] = parse(items['ua_string']) + setattr(UserAgentsTest, 'test_' + device, test_wrapper(items)) From 20c703619f77f52f87f4c5cc46edadd9b2f42519 Mon Sep 17 00:00:00 2001 From: Derrick Petzold Date: Tue, 23 Sep 2014 14:06:10 -0700 Subject: [PATCH 08/10] Added test for Samsung I8190 Galaxy S III mini. Added test for Samsung Galaxy S5. Added test for Nexus 5. --- user_agents/devices.json | 29 ++++++++++++++++++++++++++++- user_agents/tests.py | 3 ++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/user_agents/devices.json b/user_agents/devices.json index a12f188..d9f446d 100644 --- a/user_agents/devices.json +++ b/user_agents/devices.json @@ -125,6 +125,15 @@ "ua_string": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2", "str": "PC / Mac OS X 10.6.8 / WebKit Nightly 537.13" }, + "nexus_5": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (Linux; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.117 Mobile Safari/537.36", + "str": "Nexus 5 / Android 4.4.4 / Chrome Mobile 37.0.2062" + }, "nexus_7": { "is_bot": false, "is_mobile": false, @@ -178,7 +187,25 @@ "ua_string": "Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG SCH-I545 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36", "str": "SAMSUNG SCH-I545 / Android 4.4.2 / Chrome Mobile 28.0.1500" }, - "samsung_gt": { + "samsung_galaxy_s_iii_mini": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (Linux; Android 4.1.2; GT-I8190 Build/JZO54K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.117 Mobile Safari/537.36", + "str": "GT-I8190 / Android 4.1.2 / Chrome Mobile 37.0.2062" + }, + "samsung_sm_galaxy_s5": { + "is_bot": false, + "is_mobile": true, + "is_pc": false, + "is_tablet": false, + "is_touch_capable": true, + "ua_string": "Mozilla/5.0 (Linux; Android 4.4.2; en-gb; SAMSUNG SM-G900F Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.6 Chrome/28.0.1500.94 Mobile Safari/537.36", + "str": "SAMSUNG SM-G900F / Android 4.4.2 / Chrome Mobile 28.0.1500" + }, + "samsung_wave_ii": { "is_bot": false, "is_mobile": true, "is_pc": false, diff --git a/user_agents/tests.py b/user_agents/tests.py index 89a7307..846e8ef 100644 --- a/user_agents/tests.py +++ b/user_agents/tests.py @@ -56,7 +56,8 @@ def test_func(self): attrs = ('is_bot', 'is_mobile', 'is_pc', 'is_tablet', 'is_touch_capable') for attr in attrs: - self.assertEqual(getattr(items['user_agent'], attr), items[attr]) + self.assertEqual( + getattr(items['user_agent'], attr), items[attr], msg=attr) self.assertEqual(str(items['user_agent']), items['str']) return test_func From 95e26f51ee5f6211d9efd8af1c0ccc0014d7e4a3 Mon Sep 17 00:00:00 2001 From: Derrick Petzold Date: Tue, 23 Sep 2014 14:41:31 -0700 Subject: [PATCH 09/10] Fix import. --- user_agents/tests.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/user_agents/tests.py b/user_agents/tests.py index 846e8ef..7b4025c 100644 --- a/user_agents/tests.py +++ b/user_agents/tests.py @@ -1,6 +1,5 @@ import json import os -import path import unittest from ua_parser import user_agent_parser @@ -47,7 +46,7 @@ def test_unicode_strings(self): "iPhone / iOS 5.1 / Mobile Safari 5.1") -with open(os.path.join(path.path(__file__).dirname(), 'devices.json')) as f: +with open(os.path.join(os.path.dirname(__file__), 'devices.json')) as f: devices = json.load(f) From 8c8e6386fb7ab865ef2bcdd0e34bbcb63f8e2408 Mon Sep 17 00:00:00 2001 From: Derrick Petzold Date: Wed, 24 Sep 2014 10:46:17 -0700 Subject: [PATCH 10/10] Python 3 compatibility. --- user_agents/compat.py | 10 ++++++++++ user_agents/tests.py | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 user_agents/compat.py diff --git a/user_agents/compat.py b/user_agents/compat.py new file mode 100644 index 0000000..f241f43 --- /dev/null +++ b/user_agents/compat.py @@ -0,0 +1,10 @@ +import sys + +PY3 = sys.version_info[0] == 3 + +if PY3: + def iteritems(d, **kw): + return iter(d.items(**kw)) +else: + def iteritems(d, **kw): + return iter(d.iteritems(**kw)) diff --git a/user_agents/tests.py b/user_agents/tests.py index 7b4025c..e61619b 100644 --- a/user_agents/tests.py +++ b/user_agents/tests.py @@ -4,6 +4,7 @@ from ua_parser import user_agent_parser from .parsers import parse +from . import compat class UserAgentsTest(unittest.TestCase): @@ -60,6 +61,6 @@ def test_func(self): self.assertEqual(str(items['user_agent']), items['str']) return test_func -for device, items in devices.iteritems(): +for device, items in compat.iteritems(devices): items['user_agent'] = parse(items['ua_string']) setattr(UserAgentsTest, 'test_' + device, test_wrapper(items))