|
5 | 5 | # See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
6 | 6 | # |
7 | 7 |
|
| 8 | +from datetime import datetime |
8 | 9 | from pathlib import Path |
9 | 10 | from unittest.mock import MagicMock |
10 | 11 | from unittest.mock import patch |
11 | 12 |
|
12 | 13 | import pytest |
| 14 | +import saneyaml |
13 | 15 |
|
14 | 16 | from vulnerabilities.importer import AdvisoryData |
| 17 | +from vulnerabilities.pipelines.v2_importers.gitlab_importer import parse_gitlab_advisory |
15 | 18 |
|
16 | 19 |
|
17 | 20 | @pytest.fixture |
@@ -151,3 +154,55 @@ def test_advisories_count_empty(mock_vcs_response, mock_fetch_via_vcs, tmp_path) |
151 | 154 |
|
152 | 155 | count = pipeline.advisories_count() |
153 | 156 | assert count == 0 |
| 157 | + |
| 158 | + |
| 159 | +@pytest.fixture |
| 160 | +def gitlab_advisory_yaml(tmp_path): |
| 161 | + content = { |
| 162 | + "identifier": "GMS-2018-26", |
| 163 | + "package_slug": "pypi/django", |
| 164 | + "title": "Incorrect header injection check", |
| 165 | + "description": "django isn't properly protected against HTTP header injection.", |
| 166 | + "pubdate": "2018-03-15", |
| 167 | + "affected_range": "<2.0.1", |
| 168 | + "fixed_versions": ["v2.0.1"], |
| 169 | + "urls": ["https://github.com/django/django/pull/123"], |
| 170 | + "cwe_ids": ["CWE-1035", "CWE-937"], |
| 171 | + "identifiers": ["GMS-2018-26"], |
| 172 | + } |
| 173 | + |
| 174 | + advisory_path = tmp_path / "GMS-2018-26.yaml" |
| 175 | + advisory_path.write_text(saneyaml.dump(content)) |
| 176 | + return advisory_path, content |
| 177 | + |
| 178 | + |
| 179 | +def test_parse_gitlab_advisory_with_no_purl(monkeypatch, gitlab_advisory_yaml): |
| 180 | + file_path, advisory_data = gitlab_advisory_yaml |
| 181 | + |
| 182 | + # Mock get_purl to always return None |
| 183 | + def mock_get_purl(package_slug, purl_type_by_gitlab_scheme, logger): |
| 184 | + return None |
| 185 | + |
| 186 | + # Patch the dependencies |
| 187 | + import vulnerabilities.pipelines.v2_importers.gitlab_importer as gitlab_module |
| 188 | + |
| 189 | + monkeypatch.setattr(gitlab_module, "get_purl", mock_get_purl) |
| 190 | + |
| 191 | + dummy_logger = lambda *args, **kwargs: None # Ignore logging in test |
| 192 | + |
| 193 | + result = parse_gitlab_advisory( |
| 194 | + file=file_path, |
| 195 | + base_path=file_path.parent, |
| 196 | + gitlab_scheme_by_purl_type={}, |
| 197 | + purl_type_by_gitlab_scheme={}, |
| 198 | + logger=dummy_logger, |
| 199 | + ) |
| 200 | + |
| 201 | + assert isinstance(result, AdvisoryData) |
| 202 | + assert result.advisory_id == "pypi/django/GMS-2018-26" |
| 203 | + assert result.aliases == ["GMS-2018-26"] |
| 204 | + assert result.summary.startswith("Incorrect header") |
| 205 | + assert result.url.startswith("https://gitlab.com/gitlab-org/advisories-community") |
| 206 | + assert isinstance(result.date_published, datetime) |
| 207 | + assert result.date_published.year == 2018 |
| 208 | + assert result.affected_packages == [] # Because get_purl was mocked to return None |
0 commit comments