Skip to content

Commit bc7d2ea

Browse files
committed
Add package-first tests for gitlab v2 importer #1903
Signed-off-by: Michael Ehab Mikhail <michael.ehab@hotmail.com>
1 parent 24c4569 commit bc7d2ea

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

vulnerabilities/tests/pipelines/test_gitlab_v2_importer.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
#
77

88
from pathlib import Path
9+
from unittest import mock
910
from unittest.mock import MagicMock
1011
from unittest.mock import patch
1112

1213
import pytest
14+
import saneyaml
15+
from packageurl import PackageURL
1316

1417
from vulnerabilities.importer import AdvisoryData
18+
from vulnerabilities.pipelines.v2_importers.gitlab_importer import GitLabImporterPipeline
19+
from vulnerabilities.tests import util_tests
20+
21+
TEST_DATA = Path(__file__).parent.parent / "test_data" / "gitlab"
1522

1623

1724
@pytest.fixture
@@ -57,8 +64,6 @@ def mock_gitlab_yaml(tmp_path):
5764

5865

5966
def test_clone(mock_fetch_via_vcs, mock_vcs_response):
60-
from vulnerabilities.pipelines.v2_importers.gitlab_importer import GitLabImporterPipeline
61-
6267
pipeline = GitLabImporterPipeline()
6368
pipeline.clone()
6469

@@ -67,8 +72,6 @@ def test_clone(mock_fetch_via_vcs, mock_vcs_response):
6772

6873

6974
def test_advisories_count(mock_gitlab_yaml, mock_vcs_response, mock_fetch_via_vcs):
70-
from vulnerabilities.pipelines.v2_importers.gitlab_importer import GitLabImporterPipeline
71-
7275
mock_vcs_response.dest_dir = str(mock_gitlab_yaml)
7376

7477
pipeline = GitLabImporterPipeline()
@@ -80,8 +83,6 @@ def test_advisories_count(mock_gitlab_yaml, mock_vcs_response, mock_fetch_via_vc
8083

8184

8285
def test_collect_advisories(mock_gitlab_yaml, mock_vcs_response, mock_fetch_via_vcs):
83-
from vulnerabilities.pipelines.v2_importers.gitlab_importer import GitLabImporterPipeline
84-
8586
mock_vcs_response.dest_dir = str(mock_gitlab_yaml)
8687

8788
pipeline = GitLabImporterPipeline()
@@ -101,8 +102,6 @@ def test_collect_advisories(mock_gitlab_yaml, mock_vcs_response, mock_fetch_via_
101102

102103

103104
def test_clean_downloads(mock_vcs_response):
104-
from vulnerabilities.pipelines.v2_importers.gitlab_importer import GitLabImporterPipeline
105-
106105
pipeline = GitLabImporterPipeline()
107106
pipeline.vcs_response = mock_vcs_response
108107

@@ -111,8 +110,6 @@ def test_clean_downloads(mock_vcs_response):
111110

112111

113112
def test_on_failure(mock_vcs_response):
114-
from vulnerabilities.pipelines.v2_importers.gitlab_importer import GitLabImporterPipeline
115-
116113
pipeline = GitLabImporterPipeline()
117114
pipeline.vcs_response = mock_vcs_response
118115

@@ -124,8 +121,6 @@ def test_on_failure(mock_vcs_response):
124121
def test_collect_advisories_with_invalid_yaml(
125122
mock_gitlab_yaml, mock_vcs_response, mock_fetch_via_vcs
126123
):
127-
from vulnerabilities.pipelines.v2_importers.gitlab_importer import GitLabImporterPipeline
128-
129124
# Add an invalid YAML file
130125
invalid_file = Path(mock_gitlab_yaml) / "pypi" / "package_name" / "invalid.yml"
131126
invalid_file.write_text(":::invalid_yaml")
@@ -141,8 +136,6 @@ def test_collect_advisories_with_invalid_yaml(
141136

142137

143138
def test_advisories_count_empty(mock_vcs_response, mock_fetch_via_vcs, tmp_path):
144-
from vulnerabilities.pipelines.v2_importers.gitlab_importer import GitLabImporterPipeline
145-
146139
mock_vcs_response.dest_dir = str(tmp_path)
147140

148141
pipeline = GitLabImporterPipeline()
@@ -151,3 +144,32 @@ def test_advisories_count_empty(mock_vcs_response, mock_fetch_via_vcs, tmp_path)
151144

152145
count = pipeline.advisories_count()
153146
assert count == 0
147+
148+
149+
@mock.patch(
150+
"vulnerabilities.pipelines.v2_importers.gitlab_importer.fetch_gitlab_advisories_for_purl"
151+
)
152+
def test_gitlab_importer_package_first_mode_found_with_version(mock_fetch):
153+
pkg_type = "pypi"
154+
response_file = TEST_DATA / f"{pkg_type}.yaml"
155+
expected_file = TEST_DATA / f"{pkg_type}-single-mode-expected-v2.json"
156+
157+
with open(response_file) as f:
158+
advisory_dict = saneyaml.load(f)
159+
160+
mock_fetch.return_value = [advisory_dict]
161+
purl = PackageURL(type="pypi", name="flask", version="0.9")
162+
pipeline = GitLabImporterPipeline(purl=purl)
163+
advisories = list(pipeline.collect_advisories())
164+
util_tests.check_results_against_json(advisories[0].to_dict(), expected_file)
165+
166+
167+
@mock.patch(
168+
"vulnerabilities.pipelines.v2_importers.gitlab_importer.fetch_gitlab_advisories_for_purl"
169+
)
170+
def test_gitlab_importer_package_first_mode_none_found(mock_fetch):
171+
mock_fetch.return_value = []
172+
purl = PackageURL(type="pypi", name="flask", version="1.2")
173+
pipeline = GitLabImporterPipeline(purl=purl)
174+
advisories = list(pipeline.collect_advisories())
175+
assert advisories == []
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"aliases": ["CVE-2019-1010083"],
3+
"summary": "Denial of service\nDenial of Service due to unexpected memory usage in the Pallets Project Flask",
4+
"affected_packages": [
5+
{
6+
"package": {
7+
"type": "pypi",
8+
"namespace": "",
9+
"name": "flask",
10+
"version": "",
11+
"qualifiers": "",
12+
"subpath": ""
13+
},
14+
"affected_version_range": "vers:pypi/<1.0",
15+
"fixed_version": "1.0"
16+
}
17+
],
18+
"references": [],
19+
"date_published": "2019-07-17T00:00:00+00:00",
20+
"weaknesses": [1035, 937],
21+
"url": "https://gitlab.com/gitlab-org/advisories-community/-/blob/main/pypi/Flask/CVE-2019-1010083.yml"
22+
}

0 commit comments

Comments
 (0)