Skip to content

Commit

Permalink
Handle unconventional status codes
Browse files Browse the repository at this point in the history
- Add test
  • Loading branch information
DavisRayM committed Jan 15, 2020
1 parent e7ae49c commit d96a644
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
32 changes: 32 additions & 0 deletions onadata/apps/api/tests/viewsets/test_xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from mock import Mock, patch
from rest_framework import status
from rest_framework.viewsets import ModelViewSet
from http.client import BadStatusLine

from onadata.apps.api.tests.mocked_data import (
enketo_error500_mock, enketo_error502_mock, enketo_error_mock, enketo_mock,
Expand Down Expand Up @@ -72,6 +73,13 @@ def fixtures_path(filepath):
'rb')


def raise_bad_status_line(arg):
"""
Raises http.client BadStatusLine
"""
raise BadStatusLine('RANDOM STATUS')


ROLES = [ReadOnlyRole,
DataEntryRole,
EditorRole,
Expand Down Expand Up @@ -4581,3 +4589,27 @@ def test_csv_xls_import_errors(self):
self.assertEqual(response.status_code, 400)
self.assertEqual(
response.data.get('error'), 'csv_file not a csv file')

@patch(
'onadata.apps.main.forms.urlopen', side_effect=raise_bad_status_line)
def test_error_raised_xform_url_upload_urllib_error(self, mock_urlopen):
"""
Test that the BadStatusLine error thrown by urlopen when a status
code is not understood is handled properly
"""
view = XFormViewSet.as_view({
'post': 'create'
})

xls_url = 'http://localhost:2000'

post_data = {'xls_url': xls_url}
request = self.factory.post('/', data=post_data, **self.extra)
response = view(request)
error_msg = ('An error occurred while publishing the form. '
'Please try again.')

self.assertEqual(response.status_code, 400)
self.assertEqual(
response.data.get('text'),
error_msg)
3 changes: 2 additions & 1 deletion onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from django.utils import timezone
from django.utils.encoding import DjangoUnicodeDecodeError
from django.utils.translation import ugettext as _
from http.client import BadStatusLine
from modilabs.utils.subprocess_timeout import ProcessTimedOut
from multidb.pinning import use_master
from pyxform.errors import PyXFormError
Expand Down Expand Up @@ -495,7 +496,7 @@ def publish_form(callback):
'type': 'alert-error',
'text': _(u'Form validation timeout, please try again.'),
}
except (MemoryError, OSError):
except (MemoryError, OSError, BadStatusLine):
return {
'type': 'alert-error',
'text': _((u'An error occurred while publishing the form. '
Expand Down

0 comments on commit d96a644

Please sign in to comment.