Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add utils to check for WebApp user agents

Roland Geiderec90a24eae0a
diff --git a/wger/utils/tests/test_user_agents.py b/wger/utils/tests/test_user_agents.py
new file mode 100644
index 000000000..0445d0725
--- /dev/null
+++ b/wger/utils/tests/test_user_agents.py
@@ -0,0 +1,88 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+
+from wger.manager.tests.testcase import WorkoutManagerTestCase
+
+from wger.utils import user_agents
+
+
+class AmazonUserAgentTestCase(WorkoutManagerTestCase):
+ '''
+ Tests the amazon WebApp testcase
+ '''
+
+ def test_user_agent_3rd_generation(self):
+ '''
+ Tests 3rd generation user agents
+ '''
+
+ agent = "Mozilla/5.0 (Linux; Android 4.2.2; <device> Build/<build>) AppleWebKit/ "\
+ "<webkit> (KHTML, like Gecko) Chrome/<chrome> Mobile Safari/<safari> "\
+ "AmazonWebAppPlatform/<version> "
+ self.assertTrue(user_agents.is_amazon_webview(agent))
+
+ def test_user_agent_2nd_generation(self):
+ '''
+ Tests 2nd generation user agents
+ '''
+
+ agent = "Mozilla/5.0 (Linux; Android 4.0.3; <device> Build/<build>) AppleWebKit/<webkit>" \
+ "(KHTML, like Gecko) Chrome/<chrome> Mobile Safari/<safari> AmazonWebAppPlatform"\
+ "/<version>"
+ self.assertTrue(user_agents.is_amazon_webview(agent))
+
+ def test_user_agent_1st_generation(self):
+ '''
+ Tests 1st generation user agents
+ '''
+
+ agent = "Mozilla/5.0 (Linux; U; Android 2.3.4; <locale>; Kindle Fire Build/GINGERBREAD) "\
+ "AppleWebKit/<webkit> (KHTML, like Gecko) Version/4.0 Safari/<safari> "\
+ "AmazonWebAppPlatform/<version> "
+ self.assertTrue(user_agents.is_amazon_webview(agent))
+
+ def test_user_agent_generic_android(self):
+ '''
+ Tests generic android, from amazon app store
+ '''
+
+ agent = "Mozilla/5.0 (Linux; U; Android <version>; <locale>; Build/<build>) AppleWebKit/"\
+ "<webkit> (KHTML, like Gecko) Version/4.0 Safari/<safari> AmazonWebAppPlatform/"\
+ "<version>"
+ self.assertTrue(user_agents.is_amazon_webview(agent))
+
+ def test_user_agent_firefox(self):
+ '''
+ Tests vanilla firefox user agent
+ '''
+
+ agent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0"
+ self.assertFalse(user_agents.is_amazon_webview(agent))
+
+ agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0"
+ self.assertFalse(user_agents.is_amazon_webview(agent))
+
+ def test_user_agent_chrome(self):
+ '''
+ Tests vanilla chrome user agent
+ '''
+
+ agent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"\
+ "Chrome/32.0.1667.0 Safari/537.36"
+ self.assertFalse(user_agents.is_amazon_webview(agent))
+
+ agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like "\
+ "Gecko) Chrome/32.0.1664.3 Safari/537.36"
+ self.assertFalse(user_agents.is_amazon_webview(agent))
diff --git a/wger/utils/user_agents.py b/wger/utils/user_agents.py
new file mode 100644
index 000000000..d529c96b6
--- /dev/null
+++ b/wger/utils/user_agents.py
@@ -0,0 +1,46 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+import logging
+
+logger = logging.getLogger('wger.custom')
+
+
+def is_amazon_webview(user_agent):
+ '''
+ Check if the client is an Amazon WebView.
+
+ See the amazon developer page for specific user agents:
+ https://developer.amazon.com/sdk/webapps/faq.html#distribution
+
+ :param user_agent A string with the user agent to check
+ :return boolean
+ '''
+ if 'amazonwebapp' in user_agent.lower():
+ return True
+ else:
+ return False
+
+
+def check_request_amazon(request):
+ '''
+
+ :param request:
+ :return: boolean
+ '''
+ print request.META.get('HTTP_USER_AGENT')
+ if request.META.get('HTTP_USER_AGENT'):
+ return is_amazon_webview(request.META.get('HTTP_USER_AGENT'))
+ else:
+ return False