From d31183db47a934e5e81c7954c3d9f1ee0c49c803 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Aug 14 2019 18:22:58 +0000 Subject: PR#1223: Unit test download_file Merges #1223 https://pagure.io/koji/pull-request/1223 --- diff --git a/docs/source/writing_koji_code.rst b/docs/source/writing_koji_code.rst index 41d6692..759d0b9 100644 --- a/docs/source/writing_koji_code.rst +++ b/docs/source/writing_koji_code.rst @@ -799,6 +799,7 @@ You will need to install the following packages to actually run the tests. * ``python-mock`` * ``python-psycopg2`` * ``python-requests`` + * ``python-requests-mock`` * ``python-qpid-proton`` Please note that it is currently not supported to use *virtualenv* when hacking diff --git a/tests/test_cli/test_download_file.py b/tests/test_cli/test_download_file.py index 4abdf71..3f96d10 100644 --- a/tests/test_cli/test_download_file.py +++ b/tests/test_cli/test_download_file.py @@ -3,6 +3,9 @@ import mock import six import shutil import tempfile +import os +import requests_mock +import requests try: import unittest2 as unittest except ImportError: @@ -153,5 +156,31 @@ class TestDownloadProgress(unittest.TestCase): self.assertMultiLineEqual(actual, expected) +class TestDownloadFileError(unittest.TestCase): + """Check error status code and text in download_file.""" + filename = '/tmp/tmp-download' + + @requests_mock.Mocker() + def test_handle_download_file_error_404(self, m): + m.get("http://url", text='Not Found\n', status_code=404) + with self.assertRaises(requests.HTTPError): + download_file("http://url", self.filename) + try: + self.assertFalse(os.path.exists(self.filename)) + except AssertionError: + os.unlink(self.filename) + raise + + @requests_mock.Mocker() + def test_handle_download_file_error_500(self, m): + m.get("http://url", text='Internal Server Error\n', status_code=500) + with self.assertRaises(requests.HTTPError): + download_file("http://url", self.filename) + try: + self.assertFalse(os.path.exists(self.filename)) + except AssertionError: + os.unlink(self.filename) + raise + if __name__ == '__main__': unittest.main()