Catch network error in FTW

This commit is contained in:
Ken Roberts 2015-01-13 11:03:05 -08:00
parent 5bfb5b5426
commit 9551920821
2 changed files with 34 additions and 1 deletions

View File

@ -182,7 +182,18 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
self.config = ConfigParser() self.config = ConfigParser()
user_agent = 'OpenLP/' + Registry().get('application').applicationVersion() user_agent = 'OpenLP/' + Registry().get('application').applicationVersion()
self.application.process_events() self.application.process_events()
try:
web_config = get_web_page('%s%s' % (self.web, 'download.cfg'), header=('User-Agent', user_agent)) web_config = get_web_page('%s%s' % (self.web, 'download.cfg'), header=('User-Agent', user_agent))
except (urllib.error.URLError, ConnectionError) as err:
msg = QtGui.QMessageBox()
title = translate('OpenLP.FirstTimeWizard', 'Network Error')
msg.setText('{} {}'.format(title, err.code if hasattr(err, 'code') else ''))
msg.setInformativeText(translate('OpenLP.FirstTimeWizard',
'There was a network error attempting to\n'
'connect to retrieve initial configuration inforamtion'))
msg.setStandardButtons(msg.Ok)
ans = msg.exec_()
web_config = False
if web_config: if web_config:
files = web_config.read() files = web_config.read()
try: try:

View File

@ -30,6 +30,7 @@
Package to test the openlp.core.ui.firsttimeform package. Package to test the openlp.core.ui.firsttimeform package.
""" """
import os import os
import urllib
from unittest import TestCase from unittest import TestCase
from openlp.core.common import Registry from openlp.core.common import Registry
@ -214,3 +215,24 @@ class TestFirstTimeForm(TestCase, TestMixin):
# THEN: The First Time Form should not have web access # THEN: The First Time Form should not have web access
self.assertFalse(first_time_form.web_access, 'There should not be web access with an invalid config file') self.assertFalse(first_time_form.web_access, 'There should not be web access with an invalid config file')
@patch('openlp.core.ui.firsttimeform.get_web_page')
@patch('openlp.core.ui.firsttimeform.QtGui.QMessageBox')
def network_error_test(self, mocked_message_box, mocked_get_web_page):
"""
Test we catch a network error in First Time Wizard - bug 1409627
"""
# GIVEN: Initial setup and mocks
first_time_form = FirstTimeForm(None)
first_time_form.initialize(MagicMock())
mocked_get_web_page.side_effect = urllib.error.HTTPError(url='http//localhost',
code=407,
msg='Network proxy error',
hdrs=None,
fp=None)
# WHEN: the First Time Wizard calls to get the initial configuration
first_time_form._download_index()
# THEN: the critical_error_message_box should have been called
self.assertEquals(mocked_message_box.mock_calls[1][1][0], 'Network Error 407',
'first_time_form should have caught Network Error')