Ejemplo n.º 1
0
    def test_download_failed(self):
        """
        The content-length is corrected since Nectar does not download anything if
        it receives a non-200 response.
        """
        mock_report = Mock()
        mock_report.error_report = {'response_code': '418', 'response_msg': 'I am a teapot.'}
        mock_report.url = 'https://example.com/teapot/'
        listener = StreamerListener(Mock(), Mock())

        listener.download_failed(mock_report)
        self.assertEqual(('Connection', 'close'),
                         listener.request.setHeader.call_args_list[0][0])
        self.assertEqual(('Content-Length', '0'),
                         listener.request.setHeader.call_args_list[1][0])
        listener.request.setResponseCode.assert_called_once_with('418')
Ejemplo n.º 2
0
    def test_download_failed(self):
        """
        The content-length is corrected since Nectar does not download anything if
        it receives a non-200 response.
        """
        mock_report = Mock()
        mock_report.error_report = {
            'response_code': '418',
            'response_msg': 'I am a teapot.'
        }
        mock_report.url = 'https://example.com/teapot/'
        listener = StreamerListener(Mock(), Mock())

        listener.download_failed(mock_report)
        self.assertEqual(('Connection', 'close'),
                         listener.request.setHeader.call_args_list[0][0])
        self.assertEqual(('Content-Length', '0'),
                         listener.request.setHeader.call_args_list[1][0])
        listener.request.setResponseCode.assert_called_once_with('418')
Ejemplo n.º 3
0
class TestStreamerListener(unittest.TestCase):
    def setUp(self):
        self.mock_config = Mock()
        self.mock_config.get.return_value = "1"
        self.mock_report = Mock()
        self.mock_report.headers = {}
        self.listener = StreamerListener(Mock(), self.mock_config, Mock(unit_id="abc", unit_type_id="123"))

    def test_download_headers_cache_control(self):
        """
        The cache-control headers are set appropriately.
        """
        self.listener.download_headers(self.mock_report)
        self.listener.request.setHeader.assert_called_once_with("Cache-Control", "public, s-maxage=1, max-age=1")

    def test_download_headers_hop(self):
        """
        The cache-control headers are set appropriately.
        """
        self.mock_report.headers = {
            "Content-Length": "1234",
            "Connection": "close-if-you-want",
            "Keep-Alive": "timeout=UINT_MAX, max=UINT_MAX",
            "Public": "True",
            "Proxy-Authenticate": "username=alot, password=hunter2",
            "Transfer-Encoding": "Braille",
            "Upgrade": "True",
            "Some-Header": "value",
        }
        expected_headers = (
            ("Content-Length", "1234"),
            ("Some-Header", "value"),
            ("Cache-Control", "public, s-maxage=1, max-age=1"),
        )

        self.listener.download_headers(self.mock_report)
        actual_headers = [c[0] for c in self.listener.request.setHeader.call_args_list]
        for expected, actual in zip(expected_headers, actual_headers):
            self.assertEqual(expected, actual)

    def test_download_failed(self):
        """
        The content-length is corrected since Nectar does not download anything if
        it receives a non-200 response.
        """
        self.mock_report.error_report = {"response_code": "418", "response_msg": "I am a teapot."}
        self.mock_report.url = "https://example.com/teapot/"

        self.listener.download_failed(self.mock_report)
        self.assertEqual(("Content-Length", "0"), self.listener.request.setHeader.call_args_list[0][0])
        self.listener.request.setResponseCode.assert_called_once_with("418")

    def test_download_failed_no_response(self):
        """
        The content-length is corrected since Nectar does not download anything if
        it receives a non-200 response.
        """
        self.mock_report.error_report = {}
        self.mock_report.url = "https://example.com/no_response_for_you"

        self.listener.download_failed(self.mock_report)
        self.assertEqual(("Content-Length", "0"), self.listener.request.setHeader.call_args_list[0][0])
        self.listener.request.setResponseCode.assert_called_once_with(SERVICE_UNAVAILABLE)

    @patch(MODULE_PREFIX + "model.DeferredDownload")
    def test_download_succeeded(self, mock_deferred_download):
        """Assert a deferred download entry is made."""
        self.listener.download_succeeded(None)
        mock_deferred_download.assert_called_once_with(unit_id="abc", unit_type_id="123")
        mock_deferred_download.return_value.save.assert_called_once_with()

    @patch(MODULE_PREFIX + "model.DeferredDownload")
    def test_download_succeeded_entry_not_unique(self, mock_deferred_download):
        """Assert NotUniqueError exceptions are ignored."""
        # Setup
        mock_deferred_download.return_value.save.side_effect = NotUniqueError()

        # Test
        self.listener.download_succeeded(None)
        mock_deferred_download.return_value.save.assert_called_once_with()

    @patch(MODULE_PREFIX + "model.DeferredDownload")
    def test_download_succeeded_pulp_request(self, mock_deferred_download):
        # Setup
        self.listener.pulp_request = True

        # Test
        self.listener.download_succeeded(None)
        self.assertEqual(0, mock_deferred_download.call_count)
Ejemplo n.º 4
0
class TestStreamerListener(unittest.TestCase):

    def setUp(self):
        self.mock_config = Mock()
        self.mock_config.get.return_value = '1'
        self.mock_report = Mock()
        self.mock_report.headers = {}
        self.listener = StreamerListener(
            Mock(),
            self.mock_config,
            Mock(unit_id='abc', unit_type_id='123'),
        )

    def test_download_headers_cache_control(self):
        """
        The cache-control headers are set appropriately.
        """
        self.listener.download_headers(self.mock_report)
        self.listener.request.setHeader.assert_called_once_with(
            'Cache-Control',
            'public, s-maxage=1, max-age=1',
        )

    def test_download_headers_hop(self):
        """
        The cache-control headers are set appropriately.
        """
        self.mock_report.headers = {
            'Content-Length': '1234',
            'Connection': 'close-if-you-want',
            'Keep-Alive': 'timeout=UINT_MAX, max=UINT_MAX',
            'Public': 'True',
            'Proxy-Authenticate': 'username=alot, password=hunter2',
            'Transfer-Encoding': 'Braille',
            'Upgrade': 'True',
            'Some-Header': 'value'
        }
        expected_headers = (
            ('Content-Length', '1234'),
            ('Some-Header', 'value'),
            ('Cache-Control', 'public, s-maxage=1, max-age=1'),
        )

        self.listener.download_headers(self.mock_report)
        actual_headers = [c[0] for c in self.listener.request.setHeader.call_args_list]
        for expected, actual in zip(expected_headers, actual_headers):
            self.assertEqual(expected, actual)

    def test_download_failed(self):
        """
        The content-length is corrected since Nectar does not download anything if
        it receives a non-200 response.
        """
        self.mock_report.error_report = {'response_code': '418', 'response_msg': 'I am a teapot.'}
        self.mock_report.url = 'https://example.com/teapot/'

        self.listener.download_failed(self.mock_report)
        self.assertEqual(('Content-Length', '0'),
                         self.listener.request.setHeader.call_args_list[0][0])
        self.listener.request.setResponseCode.assert_called_once_with('418')

    def test_download_failed_no_response(self):
        """
        The content-length is corrected since Nectar does not download anything if
        it receives a non-200 response.
        """
        self.mock_report.error_report = {}
        self.mock_report.url = 'https://example.com/no_response_for_you'

        self.listener.download_failed(self.mock_report)
        self.assertEqual(('Content-Length', '0'),
                         self.listener.request.setHeader.call_args_list[0][0])
        self.listener.request.setResponseCode.assert_called_once_with(SERVICE_UNAVAILABLE)

    @patch(MODULE_PREFIX + 'model.DeferredDownload')
    def test_download_succeeded(self, mock_deferred_download):
        """Assert a deferred download entry is made."""
        self.listener.download_succeeded(None)
        mock_deferred_download.assert_called_once_with(unit_id='abc', unit_type_id='123')
        mock_deferred_download.return_value.save.assert_called_once_with()

    @patch(MODULE_PREFIX + 'model.DeferredDownload')
    def test_download_succeeded_entry_not_unique(self, mock_deferred_download):
        """Assert NotUniqueError exceptions are ignored."""
        # Setup
        mock_deferred_download.return_value.save.side_effect = NotUniqueError()

        # Test
        self.listener.download_succeeded(None)
        mock_deferred_download.return_value.save.assert_called_once_with()

    @patch(MODULE_PREFIX + 'model.DeferredDownload')
    def test_download_succeeded_pulp_request(self, mock_deferred_download):
        # Setup
        self.listener.pulp_request = True

        # Test
        self.listener.download_succeeded(None)
        self.assertEqual(0, mock_deferred_download.call_count)
Ejemplo n.º 5
0
class TestStreamerListener(unittest.TestCase):

    def setUp(self):
        self.mock_config = Mock()
        self.mock_config.get.return_value = '1'
        self.mock_report = Mock()
        self.mock_report.headers = {}
        self.listener = StreamerListener(
            Mock(),
            self.mock_config,
            Mock(unit_id='abc', unit_type_id='123'),
        )

    def test_download_headers_cache_control(self):
        """
        The cache-control headers are set appropriately.
        """
        self.listener.download_headers(self.mock_report)
        self.listener.request.setHeader.assert_called_once_with(
            'Cache-Control',
            'public, s-maxage=1, max-age=1',
        )

    def test_download_headers_hop(self):
        """
        The cache-control headers are set appropriately.
        """
        self.mock_report.headers = {
            'Content-Length': '1234',
            'Connection': 'close-if-you-want',
            'Keep-Alive': 'timeout=UINT_MAX, max=UINT_MAX',
            'Public': 'True',
            'Proxy-Authenticate': 'username=alot, password=hunter2',
            'Transfer-Encoding': 'Braille',
            'Upgrade': 'True',
            'Some-Header': 'value'
        }
        expected_headers = (
            ('Content-Length', '1234'),
            ('Some-Header', 'value'),
            ('Cache-Control', 'public, s-maxage=1, max-age=1'),
        )

        self.listener.download_headers(self.mock_report)
        actual_headers = [c[0] for c in self.listener.request.setHeader.call_args_list]
        for expected, actual in zip(expected_headers, actual_headers):
            self.assertEqual(expected, actual)

    def test_download_failed(self):
        """
        The content-length is corrected since Nectar does not download anything if
        it receives a non-200 response.
        """
        self.mock_report.error_report = {'response_code': '418', 'response_msg': 'I am a teapot.'}
        self.mock_report.url = 'https://example.com/teapot/'

        self.listener.download_failed(self.mock_report)
        self.assertEqual(('Content-Length', '0'),
                         self.listener.request.setHeader.call_args_list[0][0])
        self.listener.request.setResponseCode.assert_called_once_with('418')

    @patch(MODULE_PREFIX + 'model.DeferredDownload', autospec=True)
    def test_download_succeeded(self, mock_deferred_download):
        """Assert a deferred download entry is made."""
        self.listener.download_succeeded(None)
        mock_deferred_download.assert_called_once_with(unit_id='abc', unit_type_id='123')
        mock_deferred_download.return_value.save.assert_called_once_with()

    @patch(MODULE_PREFIX + 'model.DeferredDownload', autospec=True)
    def test_download_succeeded_entry_not_unique(self, mock_deferred_download):
        """Assert NotUniqueError exceptions are ignored."""
        # Setup
        mock_deferred_download.return_value.save.side_effect = NotUniqueError()

        # Test
        self.listener.download_succeeded(None)
        mock_deferred_download.return_value.save.assert_called_once_with()

    @patch(MODULE_PREFIX + 'model.DeferredDownload', autospec=True)
    def test_download_succeeded_pulp_request(self, mock_deferred_download):
        # Setup
        self.listener.pulp_request = True

        # Test
        self.listener.download_succeeded(None)
        self.assertEqual(0, mock_deferred_download.call_count)