Files
guarddog-nexus/tests/test_nexus.py

152 lines
4.6 KiB
Python

"""Tests for Nexus package info extractors."""
from guarddog_nexus.core.nexus import (
extract_go_info,
extract_npm_info,
extract_package_info,
extract_pypi_info,
)
class TestPyPIExtractor:
def test_simple(self):
assert extract_pypi_info("/packages/requests/2.31.0/requests-2.31.0.tar.gz") == (
"requests",
"2.31.0",
)
def test_leading_slash(self):
assert extract_pypi_info("packages/numpy/1.24.0/numpy-1.24.0.tar.gz") == (
"numpy",
"1.24.0",
)
def test_non_package(self):
assert extract_pypi_info("/some/path") is None
def test_too_short(self):
assert extract_pypi_info("/packages/pkg") is None
class TestGoExtractor:
def test_simple(self):
assert extract_go_info("packages/github.com/gorilla/mux/@v/v1.8.0.zip") == (
"github.com/gorilla/mux",
"v1.8.0",
)
def test_long_module(self):
assert extract_go_info("/packages/github.com/gin-gonic/gin/@v/v1.9.0.zip") == (
"github.com/gin-gonic/gin",
"v1.9.0",
)
def test_no_at_v(self):
assert extract_go_info("packages/some/pkg/v1.0.0.zip") is None
def test_empty(self):
assert extract_go_info("") is None
class TestNpmExtractor:
def test_simple(self):
assert extract_npm_info("packages/lodash/-/lodash-4.17.21.tgz") == (
"lodash",
"4.17.21",
)
def test_scoped_package(self):
assert extract_npm_info("packages/@scope/name/-/name-1.0.0.tgz") == (
"@scope/name",
"1.0.0",
)
def test_scoped_angular_core(self):
assert extract_npm_info("packages/@angular/core/-/core-18.0.0.tgz") == (
"@angular/core",
"18.0.0",
)
def test_not_packages(self):
assert extract_npm_info("/other/lodash/-/lodash-4.17.21.tgz") is None
def test_short_path(self):
assert extract_npm_info("packages/lodash") is None
class TestDispatchExtractor:
def test_pypi(self):
assert extract_package_info("/packages/requests/2.31.0/requests-2.31.0.tar.gz", "pypi") == (
"requests",
"2.31.0",
)
def test_go(self):
assert extract_package_info("github.com/gorilla/mux/@v/v1.8.0.zip", "go") == (
"github.com/gorilla/mux",
"v1.8.0",
)
def test_npm(self):
assert extract_package_info("packages/lodash/-/lodash-4.17.21.tgz", "npm") == (
"lodash",
"4.17.21",
)
def test_unknown_ecosystem(self):
assert extract_package_info("/packages/pkg/1.0/file.tar.gz", "unknown") == ("pkg", "1.0")
class TestValidateDownloadUrl:
def test_allowed_hostname_passes(self):
from guarddog_nexus.core.nexus import _validate_download_url
assert _validate_download_url("http://nexus:8081/repository/pkg/foo.tar.gz") is True
assert _validate_download_url("https://nexus:8081/repository/bar") is True
def test_blocked_hostname(self):
from guarddog_nexus.core.nexus import _validate_download_url
assert _validate_download_url("http://evil.com/malware.tar.gz") is False
assert _validate_download_url("https://169.254.169.254/latest/meta-data") is False
def test_non_http_scheme_blocked(self):
from guarddog_nexus.core.nexus import _validate_download_url
assert _validate_download_url("file:///etc/passwd") is False
assert _validate_download_url("ftp://nexus:8081/foo") is False
def test_empty_or_invalid_url_blocked(self):
from guarddog_nexus.core.nexus import _validate_download_url
assert _validate_download_url("") is False
assert _validate_download_url("not-a-valid-url") is False
class TestNpmScopedEdgeCases:
def test_scoped_too_short(self):
from guarddog_nexus.core.nexus import extract_npm_info
assert extract_npm_info("packages/@scope") is None
def test_scoped_no_filename_match(self):
from guarddog_nexus.core.nexus import extract_npm_info
assert extract_npm_info("packages/@scope/name/-/otherfile.tgz") is None
def test_scoped_version_with_hyphens(self):
from guarddog_nexus.core.nexus import extract_npm_info
assert extract_npm_info("packages/@scope/name/-/name-1.0.0-beta.1.tgz") == (
"@scope/name",
"1.0.0-beta.1",
)
def test_scoped_tar_gz_extension(self):
from guarddog_nexus.core.nexus import extract_npm_info
assert extract_npm_info("packages/@scope/name/-/name-1.0.0.tar.gz") == (
"@scope/name",
"1.0.0",
)