← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~raoul-snyman/openlp/bug-1211049 into lp:openlp

 

Raoul Snyman has proposed merging lp:~raoul-snyman/openlp/bug-1211049 into lp:openlp.

Requested reviews:
  Tim Bentley (trb143)
  Andreas Preikschat (googol)
Related bugs:
  Bug #1211049 in OpenLP: "Can no long download bible verses"
  https://bugs.launchpad.net/openlp/+bug/1211049

For more details, see:
https://code.launchpad.net/~raoul-snyman/openlp/bug-1211049/+merge/199855

Fix bug #1211049: Add a list of valid user agents and a method to return an appropriate user agent for the current platform
-- 
https://code.launchpad.net/~raoul-snyman/openlp/bug-1211049/+merge/199855
Your team OpenLP Core is subscribed to branch lp:openlp.
=== modified file 'openlp/core/ui/firsttimeform.py'
--- openlp/core/ui/firsttimeform.py	2013-10-13 21:07:28 +0000
+++ openlp/core/ui/firsttimeform.py	2013-12-20 19:37:19 +0000
@@ -37,7 +37,7 @@
 import urllib.parse
 import urllib.error
 from tempfile import gettempdir
-from configparser import SafeConfigParser
+from configparser import ConfigParser
 
 from PyQt4 import QtCore, QtGui
 
@@ -68,7 +68,7 @@
             filename = config.get('theme_%s' % theme, 'filename')
             screenshot = config.get('theme_%s' % theme, 'screenshot')
             urllib.request.urlretrieve('%s%s' % (self.parent().web, screenshot),
-                os.path.join(gettempdir(), 'openlp', screenshot))
+                                       os.path.join(gettempdir(), 'openlp', screenshot))
             item = QtGui.QListWidgetItem(title, self.parent().themes_list_widget)
             item.setData(QtCore.Qt.UserRole, filename)
             item.setCheckState(QtCore.Qt.Unchecked)
@@ -90,14 +90,16 @@
         self.screens = screens
         # check to see if we have web access
         self.web = 'http://openlp.org/files/frw/'
-        self.config = SafeConfigParser()
-        self.web_access = get_web_page('%s%s' % (self.web, 'download.cfg'))
+        self.config = ConfigParser()
+        user_agent = 'OpenLP/' + Registry().get('application').applicationVersion()
+        self.web_access = get_web_page('%s%s' % (self.web, 'download.cfg'), header=('User-Agent', user_agent))
         if self.web_access:
             files = self.web_access.read()
             self.config.read_string(files.decode())
         self.update_screen_list_combo()
         self.was_download_cancelled = False
         self.theme_screenshot_thread = None
+        self.has_run_wizard = False
         self.downloading = translate('OpenLP.FirstTimeWizard', 'Downloading %s...')
         self.cancel_button.clicked.connect(self.on_cancel_button_clicked)
         self.no_internet_finish_button.clicked.connect(self.on_no_internet_finish_button_clicked)

=== modified file 'openlp/core/utils/__init__.py'
--- openlp/core/utils/__init__.py	2013-10-13 21:07:28 +0000
+++ openlp/core/utils/__init__.py	2013-12-20 19:37:19 +0000
@@ -40,6 +40,7 @@
 import urllib.request
 import urllib.error
 import urllib.parse
+from random import randint
 
 from PyQt4 import QtGui, QtCore
 
@@ -61,10 +62,29 @@
 IMAGES_FILTER = None
 ICU_COLLATOR = None
 UNO_CONNECTION_TYPE = 'pipe'
-#UNO_CONNECTION_TYPE = u'socket'
 CONTROL_CHARS = re.compile(r'[\x00-\x1F\x7F-\x9F]', re.UNICODE)
 INVALID_FILE_CHARS = re.compile(r'[\\/:\*\?"<>\|\+\[\]%]', re.UNICODE)
 DIGITS_OR_NONDIGITS = re.compile(r'\d+|\D+', re.UNICODE)
+USER_AGENTS = {
+    'win32': [
+        'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
+        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
+        'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36'
+    ],
+    'darwin': [
+        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31',
+        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11',
+        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11',
+    ],
+    'linux2': [
+        'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1364.160 Chrome/25.0.1364.160 Safari/537.22',
+        'Mozilla/5.0 (X11; CrOS armv7l 2913.260.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.99 Safari/537.11',
+        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.27 (KHTML, like Gecko) Chrome/26.0.1389.0 Safari/537.27'
+    ],
+    'default': [
+        'Mozilla/5.0 (X11; NetBSD amd64; rv:18.0) Gecko/20130120 Firefox/18.0'
+    ]
+}
 
 
 class VersionThread(QtCore.QThread):
@@ -298,6 +318,17 @@
         return False
 
 
+def _get_user_agent():
+    """
+    Return a user agent customised for the platform the user is on.
+    """
+    browser_list = USER_AGENTS.get(sys.platform, None)
+    if not browser_list:
+        browser_list = USER_AGENTS['default']
+    random_index = randint(0, len(browser_list) - 1)
+    return browser_list[random_index]
+
+
 def get_web_page(url, header=None, update_openlp=False):
     """
     Attempts to download the webpage at url and returns that page or None.
@@ -318,6 +349,9 @@
     if not url:
         return None
     req = urllib.request.Request(url)
+    if not header or header[0].lower() != 'user-agent':
+        user_agent = _get_user_agent()
+        req.add_header('User-Agent', user_agent)
     if header:
         req.add_header(header[0], header[1])
     page = None

=== modified file 'tests/functional/__init__.py'
--- tests/functional/__init__.py	2013-09-20 09:35:50 +0000
+++ tests/functional/__init__.py	2013-12-20 19:37:19 +0000
@@ -11,9 +11,9 @@
 from PyQt4 import QtGui
 
 if sys.version_info[1] >= 3:
-    from unittest.mock import patch, MagicMock
+    from unittest.mock import MagicMock, patch, mock_open
 else:
-    from mock import patch, MagicMock
+    from mock import MagicMock, patch, mock_open
 
 # Only one QApplication can be created. Use QtGui.QApplication.instance() when you need to "create" a  QApplication.
 application = QtGui.QApplication([])

=== modified file 'tests/functional/openlp_core_utils/test_utils.py'
--- tests/functional/openlp_core_utils/test_utils.py	2013-10-14 04:55:20 +0000
+++ tests/functional/openlp_core_utils/test_utils.py	2013-12-20 19:37:19 +0000
@@ -32,14 +32,74 @@
 from unittest import TestCase
 
 from openlp.core.utils import clean_filename, get_filesystem_encoding, get_locale_key, \
-    get_natural_key, split_filename
-from tests.functional import patch
+    get_natural_key, split_filename, _get_user_agent, get_web_page, get_uno_instance, add_actions
+from tests.functional import MagicMock, patch
 
 
 class TestUtils(TestCase):
     """
     A test suite to test out various methods around the AppLocation class.
     """
+    def add_actions_empty_list_test(self):
+        """
+        Test that no actions are added when the list is empty
+        """
+        # GIVEN: a mocked action list, and an empty list
+        mocked_target = MagicMock()
+        empty_list = []
+
+        # WHEN: The empty list is added to the mocked target
+        add_actions(mocked_target, empty_list)
+
+        # THEN: The add method on the mocked target is never called
+        self.assertEqual(0, mocked_target.addSeparator.call_count, 'addSeparator method should not have been called')
+        self.assertEqual(0, mocked_target.addAction.call_count, 'addAction method should not have been called')
+
+    def add_actions_none_action_test(self):
+        """
+        Test that a separator is added when a None action is in the list
+        """
+        # GIVEN: a mocked action list, and a list with None in it
+        mocked_target = MagicMock()
+        separator_list = [None]
+
+        # WHEN: The list is added to the mocked target
+        add_actions(mocked_target, separator_list)
+
+        # THEN: The addSeparator method is called, but the addAction method is never called
+        mocked_target.addSeparator.assert_called_with()
+        self.assertEqual(0, mocked_target.addAction.call_count, 'addAction method should not have been called')
+
+    def add_actions_add_action_test(self):
+        """
+        Test that an action is added when a valid action is in the list
+        """
+        # GIVEN: a mocked action list, and a list with an action in it
+        mocked_target = MagicMock()
+        action_list = ['action']
+
+        # WHEN: The list is added to the mocked target
+        add_actions(mocked_target, action_list)
+
+        # THEN: The addSeparator method is not called, and the addAction method is called
+        self.assertEqual(0, mocked_target.addSeparator.call_count, 'addSeparator method should not have been called')
+        mocked_target.addAction.assert_called_with('action')
+
+    def add_actions_action_and_none_test(self):
+        """
+        Test that an action and a separator are added when a valid action and None are in the list
+        """
+        # GIVEN: a mocked action list, and a list with an action and None in it
+        mocked_target = MagicMock()
+        action_list = ['action', None]
+
+        # WHEN: The list is added to the mocked target
+        add_actions(mocked_target, action_list)
+
+        # THEN: The addSeparator method is called, and the addAction method is called
+        mocked_target.addSeparator.assert_called_with()
+        mocked_target.addAction.assert_called_with('action')
+
     def get_filesystem_encoding_sys_function_not_called_test(self):
         """
         Test the get_filesystem_encoding() function does not call the sys.getdefaultencoding() function
@@ -153,3 +213,211 @@
 
             # THEN: We get a properly sorted list
             self.assertEqual(['1st item', 'item 3b', 'item 10a'], sorted_list, 'Numbers should be sorted naturally')
+
+    def get_uno_instance_pipe_test(self):
+        """
+        Test that when the UNO connection type is "pipe" the resolver is given the "pipe" URI
+        """
+        # GIVEN: A mock resolver object and UNO_CONNECTION_TYPE is "pipe"
+        mock_resolver = MagicMock()
+
+        # WHEN: get_uno_instance() is called
+        get_uno_instance(mock_resolver)
+
+        # THEN: the resolve method is called with the correct argument
+        mock_resolver.resolve.assert_called_with('uno:pipe,name=openlp_pipe;urp;StarOffice.ComponentContext')
+
+    def get_user_agent_linux_test(self):
+        """
+        Test that getting a user agent on Linux returns a user agent suitable for Linux
+        """
+        with patch('openlp.core.utils.sys') as mocked_sys:
+
+            # GIVEN: The system is Linux
+            mocked_sys.platform = 'linux2'
+
+            # WHEN: We call _get_user_agent()
+            user_agent = _get_user_agent()
+
+            # THEN: The user agent is a Linux (or ChromeOS) user agent
+            result = 'Linux' in user_agent or 'CrOS' in user_agent
+            self.assertTrue(result, u'The user agent should be a valid Linux user agent')
+
+    def get_user_agent_windows_test(self):
+        """
+        Test that getting a user agent on Windows returns a user agent suitable for Windows
+        """
+        with patch('openlp.core.utils.sys') as mocked_sys:
+
+            # GIVEN: The system is Linux
+            mocked_sys.platform = 'win32'
+
+            # WHEN: We call _get_user_agent()
+            user_agent = _get_user_agent()
+
+            # THEN: The user agent is a Linux (or ChromeOS) user agent
+            self.assertIn('Windows', user_agent, u'The user agent should be a valid Windows user agent')
+
+    def get_user_agent_macos_test(self):
+        """
+        Test that getting a user agent on OS X returns a user agent suitable for OS X
+        """
+        with patch('openlp.core.utils.sys') as mocked_sys:
+
+            # GIVEN: The system is Linux
+            mocked_sys.platform = 'darwin'
+
+            # WHEN: We call _get_user_agent()
+            user_agent = _get_user_agent()
+
+            # THEN: The user agent is a Linux (or ChromeOS) user agent
+            self.assertIn('Mac OS X', user_agent, u'The user agent should be a valid OS X user agent')
+
+    def get_user_agent_default_test(self):
+        """
+        Test that getting a user agent on a non-Linux/Windows/OS X platform returns the default user agent
+        """
+        with patch('openlp.core.utils.sys') as mocked_sys:
+
+            # GIVEN: The system is Linux
+            mocked_sys.platform = 'freebsd'
+
+            # WHEN: We call _get_user_agent()
+            user_agent = _get_user_agent()
+
+            # THEN: The user agent is a Linux (or ChromeOS) user agent
+            self.assertIn('NetBSD', user_agent, u'The user agent should be the default user agent')
+
+    def get_web_page_no_url_test(self):
+        """
+        Test that sending a URL of None to the get_web_page method returns None
+        """
+        # GIVEN: A None url
+        test_url = None
+
+        # WHEN: We try to get the test URL
+        result = get_web_page(test_url)
+
+        # THEN: None should be returned
+        self.assertIsNone(result, 'The return value of get_web_page should be None')
+
+    def get_web_page_test(self):
+        """
+        Test that the get_web_page method works correctly
+        """
+        with patch('openlp.core.utils.urllib.request.Request') as MockRequest, \
+                patch('openlp.core.utils.urllib.request.urlopen') as mock_urlopen, \
+                patch('openlp.core.utils._get_user_agent') as mock_get_user_agent, \
+                patch('openlp.core.utils.Registry') as MockRegistry:
+            # GIVEN: Mocked out objects and a fake URL
+            mocked_request_object = MagicMock()
+            MockRequest.return_value = mocked_request_object
+            mocked_page_object = MagicMock()
+            mock_urlopen.return_value = mocked_page_object
+            mock_get_user_agent.return_value = 'user_agent'
+            fake_url = 'this://is.a.fake/url'
+
+            # WHEN: The get_web_page() method is called
+            returned_page = get_web_page(fake_url)
+
+            # THEN: The correct methods are called with the correct arguments and a web page is returned
+            MockRequest.assert_called_with(fake_url)
+            mocked_request_object.add_header.assert_called_with('User-Agent', 'user_agent')
+            self.assertEqual(1, mocked_request_object.add_header.call_count,
+                             'There should only be 1 call to add_header')
+            mock_get_user_agent.assert_called_with()
+            mock_urlopen.assert_called_with(mocked_request_object)
+            mocked_page_object.geturl.assert_called_with()
+            self.assertEqual(0, MockRegistry.call_count, 'The Registry() object should have never been called')
+            self.assertEqual(mocked_page_object, returned_page, 'The returned page should be the mock object')
+
+    def get_web_page_with_header_test(self):
+        """
+        Test that adding a header to the call to get_web_page() adds the header to the request
+        """
+        with patch('openlp.core.utils.urllib.request.Request') as MockRequest, \
+                patch('openlp.core.utils.urllib.request.urlopen') as mock_urlopen, \
+                patch('openlp.core.utils._get_user_agent') as mock_get_user_agent:
+            # GIVEN: Mocked out objects, a fake URL and a fake header
+            mocked_request_object = MagicMock()
+            MockRequest.return_value = mocked_request_object
+            mocked_page_object = MagicMock()
+            mock_urlopen.return_value = mocked_page_object
+            mock_get_user_agent.return_value = 'user_agent'
+            fake_url = 'this://is.a.fake/url'
+            fake_header = ('Fake-Header', 'fake value')
+
+            # WHEN: The get_web_page() method is called
+            returned_page = get_web_page(fake_url, header=fake_header)
+
+            # THEN: The correct methods are called with the correct arguments and a web page is returned
+            MockRequest.assert_called_with(fake_url)
+            mocked_request_object.add_header.assert_called_with(fake_header[0], fake_header[1])
+            self.assertEqual(2, mocked_request_object.add_header.call_count,
+                             'There should only be 2 calls to add_header')
+            mock_get_user_agent.assert_called_with()
+            mock_urlopen.assert_called_with(mocked_request_object)
+            mocked_page_object.geturl.assert_called_with()
+            self.assertEqual(mocked_page_object, returned_page, 'The returned page should be the mock object')
+
+    def get_web_page_with_user_agent_in_headers_test(self):
+        """
+        Test that adding a user agent in the header when calling get_web_page() adds that user agent to the request
+        """
+        with patch('openlp.core.utils.urllib.request.Request') as MockRequest, \
+                patch('openlp.core.utils.urllib.request.urlopen') as mock_urlopen, \
+                patch('openlp.core.utils._get_user_agent') as mock_get_user_agent:
+            # GIVEN: Mocked out objects, a fake URL and a fake header
+            mocked_request_object = MagicMock()
+            MockRequest.return_value = mocked_request_object
+            mocked_page_object = MagicMock()
+            mock_urlopen.return_value = mocked_page_object
+            fake_url = 'this://is.a.fake/url'
+            user_agent_header = ('User-Agent', 'OpenLP/2.1.0')
+
+            # WHEN: The get_web_page() method is called
+            returned_page = get_web_page(fake_url, header=user_agent_header)
+
+            # THEN: The correct methods are called with the correct arguments and a web page is returned
+            MockRequest.assert_called_with(fake_url)
+            mocked_request_object.add_header.assert_called_with(user_agent_header[0], user_agent_header[1])
+            self.assertEqual(1, mocked_request_object.add_header.call_count,
+                             'There should only be 1 call to add_header')
+            self.assertEqual(0, mock_get_user_agent.call_count, '_get_user_agent should not have been called')
+            mock_urlopen.assert_called_with(mocked_request_object)
+            mocked_page_object.geturl.assert_called_with()
+            self.assertEqual(mocked_page_object, returned_page, 'The returned page should be the mock object')
+
+    def get_web_page_update_openlp_test(self):
+        """
+        Test that passing "update_openlp" as true to get_web_page calls Registry().get('app').process_events()
+        """
+        with patch('openlp.core.utils.urllib.request.Request') as MockRequest, \
+                patch('openlp.core.utils.urllib.request.urlopen') as mock_urlopen, \
+                patch('openlp.core.utils._get_user_agent') as mock_get_user_agent, \
+                patch('openlp.core.utils.Registry') as MockRegistry:
+            # GIVEN: Mocked out objects, a fake URL
+            mocked_request_object = MagicMock()
+            MockRequest.return_value = mocked_request_object
+            mocked_page_object = MagicMock()
+            mock_urlopen.return_value = mocked_page_object
+            mock_get_user_agent.return_value = 'user_agent'
+            mocked_registry_object = MagicMock()
+            mocked_application_object = MagicMock()
+            mocked_registry_object.get.return_value = mocked_application_object
+            MockRegistry.return_value = mocked_registry_object
+            fake_url = 'this://is.a.fake/url'
+
+            # WHEN: The get_web_page() method is called
+            returned_page = get_web_page(fake_url, update_openlp=True)
+
+            # THEN: The correct methods are called with the correct arguments and a web page is returned
+            MockRequest.assert_called_with(fake_url)
+            mocked_request_object.add_header.assert_called_with('User-Agent', 'user_agent')
+            self.assertEqual(1, mocked_request_object.add_header.call_count,
+                             'There should only be 1 call to add_header')
+            mock_urlopen.assert_called_with(mocked_request_object)
+            mocked_page_object.geturl.assert_called_with()
+            mocked_registry_object.get.assert_called_with('application')
+            mocked_application_object.process_events.assert_called_with()
+            self.assertEqual(mocked_page_object, returned_page, 'The returned page should be the mock object')

=== modified file 'tests/functional/openlp_plugins/remotes/test_router.py'
--- tests/functional/openlp_plugins/remotes/test_router.py	2013-11-14 20:33:46 +0000
+++ tests/functional/openlp_plugins/remotes/test_router.py	2013-12-20 19:37:19 +0000
@@ -37,8 +37,7 @@
 
 from openlp.core.common import Settings
 from openlp.plugins.remotes.lib.httpserver import HttpRouter
-from tests.functional import MagicMock, patch
-from mock import mock_open
+from tests.functional import MagicMock, patch, mock_open
 
 __default_settings__ = {
     'remotes/twelve hour': True,
@@ -53,6 +52,7 @@
 
 TEST_PATH = os.path.abspath(os.path.dirname(__file__))
 
+
 class TestRouter(TestCase):
     """
     Test the functions in the :mod:`lib` module.
@@ -91,7 +91,7 @@
 
         # THEN: the function should return the correct password
         self.assertEqual(router.auth, test_value,
-            'The result for make_sha_hash should return the correct encrypted password')
+                         'The result for make_sha_hash should return the correct encrypted password')
 
     def process_http_request_test(self):
         """
@@ -109,10 +109,8 @@
         function, args = router.process_http_request('/stage/api/poll', None)
 
         # THEN: the function should have been called only once
-        assert function['function'] == mocked_function, \
-            'The mocked function should match defined value.'
-        assert function['secure'] == False, \
-            'The mocked function should not require any security.'
+        self.assertEqual(mocked_function, function['function'], 'The mocked function should match defined value.')
+        self.assertFalse(function['secure'], 'The mocked function should not require any security.')
 
     def get_content_type_test(self):
         """
@@ -124,10 +122,12 @@
             ['test.gif', 'image/gif'], ['test.ico', 'image/x-icon'],
             ['test.png', 'image/png'], ['test.whatever', 'text/plain'],
             ['test', 'text/plain'], ['', 'text/plain'],
-            [os.path.join(TEST_PATH,'test.html'), 'text/html']]
+            [os.path.join(TEST_PATH, 'test.html'), 'text/html']]
+
         # WHEN: calling each file type
         for header in headers:
             ext, content_type = self.router.get_content_type(header[0])
+
             # THEN: all types should match
             self.assertEqual(content_type, header[1], 'Mismatch of content type')
 
@@ -142,13 +142,14 @@
         self.router.wfile = MagicMock()
         self.router.html_dir = os.path.normpath('test/dir')
         self.router.template_vars = MagicMock()
+
         # WHEN: call serve_file with no file_name
         self.router.serve_file()
+
         # THEN: it should return a 404
         self.router.send_response.assert_called_once_with(404)
         self.router.send_header.assert_called_once_with('Content-type','text/html')
-        self.assertEqual(self.router.end_headers.call_count, 1,
-            'end_headers called once')
+        self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once')
 
     def serve_file_with_valid_params_test(self):
         """
@@ -162,13 +163,13 @@
         self.router.html_dir = os.path.normpath('test/dir')
         self.router.template_vars = MagicMock()
         with patch('openlp.core.lib.os.path.exists') as mocked_exists, \
-            patch('builtins.open', mock_open(read_data='123')):
+                patch('builtins.open', mock_open(read_data='123')):
             mocked_exists.return_value = True
+
             # WHEN: call serve_file with an existing html file
             self.router.serve_file(os.path.normpath('test/dir/test.html'))
+
             # THEN: it should return a 200 and the file
             self.router.send_response.assert_called_once_with(200)
-            self.router.send_header.assert_called_once_with(
-                'Content-type','text/html')
-            self.assertEqual(self.router.end_headers.call_count, 1,
-                'end_headers called once')
+            self.router.send_header.assert_called_once_with('Content-type', 'text/html')
+            self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once')


Follow ups