def setUp(self): self.config = Mock() self.streamer = Streamer(self.config) self.request = Mock(spec=Request)
class TestStreamer(unittest.TestCase): def setUp(self): self.config = Mock() self.streamer = Streamer(self.config) self.request = Mock(spec=Request) @patch(MODULE_PREFIX + "reactor", autospec=True) def test_render_GET(self, mock_reactor): """ The handler for GET requests is invoked in a thread so that nectar is safe to use. """ self.streamer.render_GET(self.request) mock_reactor.callInThread.assert_called_once_with(self.streamer._handle_get, self.request) @patch(MODULE_PREFIX + "model") @patch(MODULE_PREFIX + "Responder.__exit__") @patch(MODULE_PREFIX + "Responder.__enter__") @patch(MODULE_PREFIX + "Streamer._download") def test_handle_get(self, mock_download, mock_enter, mock_exit, mock_model): """ When the streamer receives a request, the content is downloaded and a task is dispatched. """ # Setup self.streamer._add_deferred_download_entry = Mock() self.request.uri = "/a/resource?k=v" self.request.getHeader.return_value = None mock_catalog = mock_model.LazyCatalogEntry.objects.return_value.order_by("-plugin_id").first.return_value # Test self.streamer._handle_get(self.request) mock_exit.assert_called_once_with(None, None, None) mock_model.LazyCatalogEntry.objects.assert_called_once_with(path="/a/resource") query_set = mock_model.LazyCatalogEntry.objects.return_value self.assertEqual(1, query_set.order_by("importer_id").first.call_count) mock_download.assert_called_once_with(mock_catalog, self.request, mock_enter.return_value) @patch(MODULE_PREFIX + "repo_controller", autospec=True) @patch(MODULE_PREFIX + "model", Mock()) def test_handle_get_no_plugin(self, mock_repo_controller): """ When the _download helper method fails to find the plugin, it raises an exception. """ self.request.uri = "/a/resource?k=v" mock_repo_controller.get_importer_by_id.side_effect = PluginNotFound() self.streamer._handle_get(self.request) self.request.setResponseCode.assert_called_once_with(INTERNAL_SERVER_ERROR) @patch(MODULE_PREFIX + "logger") @patch(MODULE_PREFIX + "model") def test_handle_get_no_catalog(self, mock_model, mock_logger): """ When there is no catalog entry a DoesNotExist exception is raised and handled. """ self.request.uri = "/a/resource?k=v" mock_model.LazyCatalogEntry.objects.return_value.order_by("importer_id").first.return_value = None self.streamer._handle_get(self.request) mock_logger.error.assert_called_once_with("Failed to find a catalog entry " 'with path "/a/resource".') self.request.setResponseCode.assert_called_once_with(NOT_FOUND) @patch(MODULE_PREFIX + "logger") @patch(MODULE_PREFIX + "model") def test_handle_get_unexpected_failure(self, mock_model, mock_logger): """ When an unexpected exception occurs, the exception is logged. Further, an HTTP 500 is returned. """ self.request.uri = "/a/resource?k=v" mock_model.LazyCatalogEntry.objects.return_value.order_by("importer_id").first = OSError("Disaster.") self.streamer._handle_get(self.request) mock_logger.exception.assert_called_once_with("An unexpected error occurred while " "handling the request.") self.request.setResponseCode.assert_called_once_with(INTERNAL_SERVER_ERROR) @patch(MODULE_PREFIX + "content_container.ContentContainer") @patch(MODULE_PREFIX + "plugins_api.get_unit_model_by_id") @patch(MODULE_PREFIX + "repo_controller", autospec=True) def test_download(self, mock_repo_controller, mock_get_unit_model, mock_container): # Setup mock_catalog = Mock(importer_id="mock_id", url="http://dev.null/", working_dir="/tmp", data={"k": "v"}) mock_request = Mock() mock_responder = Mock() mock_importer = Mock() mock_importer_config = Mock() mock_db_importer = Mock() mock_repo_controller.get_importer_by_id.return_value = (mock_importer, mock_importer_config, mock_db_importer) mock_get_unit_model.return_value.unit_key_fields = tuple() # Test self.streamer._download(mock_catalog, mock_request, mock_responder) mock_repo_controller.get_importer_by_id.assert_called_once_with(mock_catalog.importer_id) mock_importer.get_downloader_for_db_importer.assert_called_once_with( mock_db_importer, mock_catalog.url, working_dir=mock_catalog.working_dir ) mock_container.return_value.download.assert_called_once()
class TestStreamer(unittest.TestCase): def setUp(self): self.config = Mock() self.streamer = Streamer(self.config) self.request = Mock(spec=Request) @patch(MODULE_PREFIX + 'reactor', autospec=True) def test_render_GET(self, mock_reactor): """ The handler for GET requests is invoked in a thread so that nectar is safe to use. """ self.streamer.render_GET(self.request) mock_reactor.callInThread.assert_called_once_with(self.streamer._handle_get, self.request) @patch(MODULE_PREFIX + 'model') @patch(MODULE_PREFIX + 'Responder.__exit__') @patch(MODULE_PREFIX + 'Responder.__enter__') @patch(MODULE_PREFIX + 'Streamer._download') def test_handle_get(self, mock_download, mock_enter, mock_exit, mock_model): """ When the streamer receives a request, the content is downloaded and a task is dispatched. """ # Setup self.streamer._add_deferred_download_entry = Mock() self.request.uri = '/a/resource?k=v' self.request.getHeader.return_value = None mock_catalog = mock_model.LazyCatalogEntry.objects.return_value.order_by( '-plugin_id').first.return_value # Test self.streamer._handle_get(self.request) mock_exit.assert_called_once_with(None, None, None) mock_model.LazyCatalogEntry.objects.assert_called_once_with(path='/a/resource') query_set = mock_model.LazyCatalogEntry.objects.return_value self.assertEqual(1, query_set.order_by('importer_id').first.call_count) mock_download.assert_called_once_with(mock_catalog, self.request, mock_enter.return_value) @patch(MODULE_PREFIX + 'repo_controller', autospec=True) @patch(MODULE_PREFIX + 'model', Mock()) def test_handle_get_no_plugin(self, mock_repo_controller): """ When the _download helper method fails to find the plugin, it raises an exception. """ self.request.uri = '/a/resource?k=v' mock_repo_controller.get_importer_by_id.side_effect = PluginNotFound() self.streamer._handle_get(self.request) self.request.setResponseCode.assert_called_once_with(INTERNAL_SERVER_ERROR) @patch(MODULE_PREFIX + 'logger') @patch(MODULE_PREFIX + 'model') def test_handle_get_no_catalog(self, mock_model, mock_logger): """ When there is no catalog entry a DoesNotExist exception is raised and handled. """ self.request.uri = '/a/resource?k=v' mock_model.LazyCatalogEntry.objects.return_value.\ order_by('importer_id').first.return_value = None self.streamer._handle_get(self.request) mock_logger.debug.assert_called_once_with('Failed to find a catalog entry ' 'with path "/a/resource".') self.request.setResponseCode.assert_called_once_with(NOT_FOUND) @patch(MODULE_PREFIX + 'logger') @patch(MODULE_PREFIX + 'model') def test_handle_get_unexpected_failure(self, mock_model, mock_logger): """ When an unexpected exception occurs, the exception is logged. Further, an HTTP 500 is returned. """ self.request.uri = '/a/resource?k=v' mock_model.LazyCatalogEntry.objects.return_value. \ order_by('importer_id').first = OSError('Disaster.') self.streamer._handle_get(self.request) mock_logger.exception.assert_called_once_with('An unexpected error occurred while ' 'handling the request.') self.request.setResponseCode.assert_called_once_with(INTERNAL_SERVER_ERROR) @patch(MODULE_PREFIX + 'nectar_request.DownloadRequest') @patch(MODULE_PREFIX + 'repo_controller', autospec=True) def test_download(self, mock_repo_controller, mock_dl_request): # Setup mock_catalog = Mock() mock_catalog.importer_id = 'mock_id' mock_catalog.url = 'http://dev.null/' mock_catalog.data = {'k': 'v'} mock_request = Mock() mock_data = {'catalog_entry': mock_catalog, 'client_request': mock_request} mock_responder = Mock() mock_importer = Mock() mock_importer_config = Mock() mock_downloader = mock_importer.get_downloader.return_value mock_repo_controller.get_importer_by_id.return_value = (mock_importer, mock_importer_config) # Test self.streamer._download(mock_catalog, mock_request, mock_responder) mock_repo_controller.get_importer_by_id.assert_called_once_with(mock_catalog.importer_id) mock_dl_request.assert_called_once_with(mock_catalog.url, mock_responder, data=mock_data) mock_importer.get_downloader.assert_called_once_with( mock_importer_config, mock_catalog.url, **mock_catalog.data) self.assertEqual(mock_request, mock_downloader.event_listener.request) self.assertEqual(self.config, mock_downloader.event_listener.streamer_config) mock_downloader.download_one.assert_called_once_with(mock_dl_request.return_value, events=True) mock_downloader.config.finalize.assert_called_once_with()
class TestStreamer(unittest.TestCase): def setUp(self): self.config = Mock() self.streamer = Streamer(self.config) self.request = Mock(spec=Request) @patch(MODULE_PREFIX + 'reactor', autospec=True) def test_render_GET(self, mock_reactor): """ The handler for GET requests is invoked in a thread so that nectar is safe to use. """ self.streamer.render_GET(self.request) mock_reactor.callInThread.assert_called_once_with(self.streamer._handle_get, self.request) @patch(MODULE_PREFIX + 'model') @patch(MODULE_PREFIX + 'Responder.__exit__') @patch(MODULE_PREFIX + 'Responder.__enter__') @patch(MODULE_PREFIX + 'Streamer._download') def test_handle_get(self, mock_download, mock_enter, mock_exit, mock_model): """ When the streamer receives a request, the content is downloaded and a task is dispatched. """ # Setup self.streamer._add_deferred_download_entry = Mock() self.request.uri = '/a/resource?k=v' self.request.getHeader.return_value = None mock_catalog = mock_model.LazyCatalogEntry.objects.return_value.order_by( '-plugin_id').first.return_value # Test self.streamer._handle_get(self.request) mock_exit.assert_called_once_with(None, None, None) mock_model.LazyCatalogEntry.objects.assert_called_once_with(path='/a/resource') query_set = mock_model.LazyCatalogEntry.objects.return_value self.assertEqual(1, query_set.order_by('importer_id').first.call_count) mock_download.assert_called_once_with(mock_catalog, self.request, mock_enter.return_value) @patch(MODULE_PREFIX + 'repo_controller', autospec=True) @patch(MODULE_PREFIX + 'model', Mock()) def test_handle_get_no_plugin(self, mock_repo_controller): """ When the _download helper method fails to find the plugin, it raises an exception. """ self.request.uri = '/a/resource?k=v' mock_repo_controller.get_importer_by_id.side_effect = PluginNotFound() self.streamer._handle_get(self.request) self.request.setResponseCode.assert_called_once_with(INTERNAL_SERVER_ERROR) @patch(MODULE_PREFIX + 'logger') @patch(MODULE_PREFIX + 'model') def test_handle_get_no_catalog(self, mock_model, mock_logger): """ When there is no catalog entry a DoesNotExist exception is raised and handled. """ self.request.uri = '/a/resource?k=v' mock_model.LazyCatalogEntry.objects.return_value.\ order_by('importer_id').first.return_value = None self.streamer._handle_get(self.request) mock_logger.error.assert_called_once_with('Failed to find a catalog entry ' 'with path "/a/resource".') self.request.setResponseCode.assert_called_once_with(NOT_FOUND) @patch(MODULE_PREFIX + 'logger') @patch(MODULE_PREFIX + 'model') def test_handle_get_unexpected_failure(self, mock_model, mock_logger): """ When an unexpected exception occurs, the exception is logged. Further, an HTTP 500 is returned. """ self.request.uri = '/a/resource?k=v' mock_model.LazyCatalogEntry.objects.return_value. \ order_by('importer_id').first = OSError('Disaster.') self.streamer._handle_get(self.request) mock_logger.exception.assert_called_once_with('An unexpected error occurred while ' 'handling the request.') self.request.setResponseCode.assert_called_once_with(INTERNAL_SERVER_ERROR) @patch(MODULE_PREFIX + 'content_container.ContentContainer') @patch(MODULE_PREFIX + 'plugins_api.get_unit_model_by_id') @patch(MODULE_PREFIX + 'repo_controller', autospec=True) def test_download(self, mock_repo_controller, mock_get_unit_model, mock_container): # Setup mock_catalog = Mock(importer_id='mock_id', url='http://dev.null/', working_dir='/tmp', data={'k': 'v'}) mock_request = Mock() mock_responder = Mock() mock_importer = Mock() mock_importer_config = Mock() mock_db_importer = Mock() mock_repo_controller.get_importer_by_id.return_value = ( mock_importer, mock_importer_config, mock_db_importer) mock_get_unit_model.return_value.unit_key_fields = tuple() # Test self.streamer._download(mock_catalog, mock_request, mock_responder) mock_repo_controller.get_importer_by_id.assert_called_once_with(mock_catalog.importer_id) mock_importer.get_downloader_for_db_importer.assert_called_once_with( mock_db_importer, mock_catalog.url, working_dir=mock_catalog.working_dir) mock_container.return_value.download.assert_called_once()