66#
77
88from pathlib import Path
9+ from unittest import mock
910from unittest .mock import MagicMock
1011from unittest .mock import patch
1112
1213import pytest
14+ import saneyaml
15+ from packageurl import PackageURL
1316
1417from 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
5966def 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
6974def 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
8285def 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
103104def 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
113112def 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):
124121def 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
143138def 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 == []
0 commit comments