Merge pull request #2696 from alphagov/doc-dl-errors

handle document download errors properly
This commit is contained in:
Leo Hemsted
2020-01-20 14:20:43 +00:00
committed by GitHub
5 changed files with 27 additions and 27 deletions

View File

@@ -1,4 +1,3 @@
import requests
import requests_mock
import pytest
@@ -33,26 +32,27 @@ def test_upload_document(document_download):
assert resp == 'https://document-download/services/service-id/documents/uploaded-url'
def test_should_raise_for_status(document_download):
def test_should_raise_400s_as_DocumentDownloadErrors(document_download):
with pytest.raises(DocumentDownloadError) as excinfo, requests_mock.Mocker() as request_mock:
request_mock.post('https://document-download/services/service-id/documents', json={
'error': 'Invalid encoding'
}, status_code=403)
'error': 'Invalid mime type'
}, status_code=400)
document_download.upload_document('service-id', 'abababab')
assert excinfo.value.message == 'Invalid encoding'
assert excinfo.value.status_code == 403
assert excinfo.value.message == 'Invalid mime type'
assert excinfo.value.status_code == 400
def test_should_raise_for_connection_errors(document_download):
with pytest.raises(DocumentDownloadError) as excinfo, requests_mock.Mocker() as request_mock:
def test_should_raise_non_400_statuses_as_exceptions(document_download):
with pytest.raises(Exception) as excinfo, requests_mock.Mocker() as request_mock:
request_mock.post(
'https://document-download/services/service-id/documents',
exc=requests.exceptions.ConnectTimeout
json={'error': 'Auth Error Of Some Kind'},
status_code=403
)
document_download.upload_document('service-id', 'abababab')
assert excinfo.value.message == 'connection error'
assert excinfo.value.status_code == 503
assert type(excinfo.value) == Exception # make sure it's a base exception, so will be handled as a 500 by v2 api
assert str(excinfo.value) == 'Unhandled document download error: {"error": "Auth Error Of Some Kind"}'

View File

@@ -250,7 +250,7 @@ def create_notification(
updated_at = updated_at or datetime.utcnow()
if not one_off and (job is None and api_key is None):
# we didn't specify in test - lets create it
# we did not specify in test - lets create it
api_key = ApiKey.query.filter(ApiKey.service == template.service, ApiKey.key_type == key_type).first()
if not api_key:
api_key = create_api_key(template.service, key_type=key_type)

View File

@@ -703,7 +703,7 @@ def test_get_pdf_for_notification_returns_400_if_pdf_not_found(
@pytest.mark.parametrize('status, expected_message', [
('virus-scan-failed', 'Document did not pass the virus scan'),
('virus-scan-failed', 'File did not pass the virus scan'),
('technical-failure', 'PDF not available for letters in status technical-failure'),
])
def test_get_pdf_for_notification_only_returns_pdf_content_if_right_status(