diff --git a/README.md b/README.md index 36003fca5..8a66f7a33 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ pypi: https://pypi.org/project/aio.api.github #### [aio.api.nist](aio.api.nist) -version: 0.0.4.dev0 +version: 0.0.4 pypi: https://pypi.org/project/aio.api.nist diff --git a/aio.api.nist/VERSION b/aio.api.nist/VERSION index c08daabd5..81340c7e7 100644 --- a/aio.api.nist/VERSION +++ b/aio.api.nist/VERSION @@ -1 +1 @@ -0.0.4-dev +0.0.4 diff --git a/aio.api.nist/aio/api/nist/BUILD b/aio.api.nist/aio/api/nist/BUILD index 3d6d5fe7e..0a31e039f 100644 --- a/aio.api.nist/aio/api/nist/BUILD +++ b/aio.api.nist/aio/api/nist/BUILD @@ -5,6 +5,7 @@ toolshed_library( "//deps:reqs#abstracts", "//deps:reqs#aio.core", "//deps:reqs#aiohttp", + "//deps:reqs#packaging", ], sources=[ "abstract/__init__.py", diff --git a/aio.api.nist/aio/api/nist/abstract/downloader.py b/aio.api.nist/aio/api/nist/abstract/downloader.py index d1e539537..b60bd3cea 100644 --- a/aio.api.nist/aio/api/nist/abstract/downloader.py +++ b/aio.api.nist/aio/api/nist/abstract/downloader.py @@ -16,7 +16,7 @@ from aio.core.functional import async_property, QueryDict from aio.core.tasks import concurrent -from aio.api.nist import abstract, interface, typing +from aio.api.nist import abstract, exceptions, interface, typing logger = logging.getLogger(__name__) @@ -166,6 +166,9 @@ async def download_and_parse( download = None if not data: download = await self.download(url) + if download.status != 200: + raise exceptions.CVEDownloadError( + f"Download failed {url}: {download.reason}") data = await download.read() self.add(*await self.parse(url, data)) logger.debug(f"CVE data saved: {url}") diff --git a/aio.api.nist/aio/api/nist/exceptions.py b/aio.api.nist/aio/api/nist/exceptions.py index 27d2b5bc7..bf41cca42 100644 --- a/aio.api.nist/aio/api/nist/exceptions.py +++ b/aio.api.nist/aio/api/nist/exceptions.py @@ -13,3 +13,7 @@ class CVEError(NISTError): class CVECheckError(NISTError): pass + + +class CVEDownloadError(NISTError): + pass diff --git a/aio.api.nist/tests/test_abstract_downloader.py b/aio.api.nist/tests/test_abstract_downloader.py index 99b37ff32..e33b608a8 100644 --- a/aio.api.nist/tests/test_abstract_downloader.py +++ b/aio.api.nist/tests/test_abstract_downloader.py @@ -284,42 +284,72 @@ def test_downloader_add(patches): == [(cpe_revmap, ), {}]) -async def test_downloader_download_and_parse(patches): +@pytest.mark.parametrize("data", [True, False]) +@pytest.mark.parametrize("raises", [True, False]) +async def test_downloader_download_and_parse(patches, data, raises): downloader = DummyNISTDownloader("URLS", "TRACKED_CPES") patched = patches( "logger", - ("ANISTDownloader.session", - dict(new_callable=PropertyMock)), + "ANISTDownloader.download", "ANISTDownloader.add", "ANISTDownloader.parse", + "Predownload", prefix="aio.api.nist.abstract.downloader") url = MagicMock() + _data = MagicMock() + kwargs = ( + dict(data=_data) + if data + else {}) - with patched as (m_logger, m_session, m_add, m_parse): + with patched as (m_logger, m_download, m_add, m_parse, m_pre): + m_download.return_value.status = ( + 200 + if not raises + else 500) m_parse.return_value = "FOO", "BAR" - m_session.return_value.get = AsyncMock() + if raises and not data: + with pytest.raises(nist.exceptions.CVEDownloadError) as e: + await downloader.download_and_parse(url, **kwargs) + else: + assert ( + await downloader.download_and_parse(url, **kwargs) + == (m_download.return_value + if not data + else m_pre.return_value)) + + if data: + assert not m_download.called + else: + assert not m_pre.called assert ( - await downloader.download_and_parse(url) - == m_session.return_value.get.return_value) - + m_download.call_args + == [(url, ), {}]) + if not raises: + assert ( + m_download.return_value.read.call_args + == [(), {}]) + else: + assert ( + e.value.args[0] + == f"Download failed {url}: {m_download.return_value.reason}") + assert not m_add.called + assert not m_parse.called + assert not m_logger.debug.called + return assert ( - m_session.return_value.get.call_args - == [(url, ), {}]) - assert ( - m_logger.debug.call_args_list - == [[(f"Downloading CVE data: {url}", ), {}], - [(f"CVE data saved: {url}", ), {}]]) + m_logger.debug.call_args + == [(f"CVE data saved: {url}", ), {}]) assert ( m_add.call_args == [("FOO", "BAR"), {}]) assert ( m_parse.call_args == [(url, - m_session.return_value.get.return_value.read.return_value, ), + (m_download.return_value.read.return_value + if not data + else _data), ), {}]) - assert ( - m_session.return_value.get.return_value.read.call_args - == [(), {}]) async def test_downloader_parse(patches):