6
6
#
7
7
8
8
from pathlib import Path
9
+ from unittest import mock
9
10
from unittest .mock import MagicMock
10
11
from unittest .mock import patch
11
12
12
13
import pytest
14
+ import saneyaml
15
+ from packageurl import PackageURL
13
16
14
17
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"
15
22
16
23
17
24
@pytest .fixture
@@ -57,8 +64,6 @@ def mock_gitlab_yaml(tmp_path):
57
64
58
65
59
66
def test_clone (mock_fetch_via_vcs , mock_vcs_response ):
60
- from vulnerabilities .pipelines .v2_importers .gitlab_importer import GitLabImporterPipeline
61
-
62
67
pipeline = GitLabImporterPipeline ()
63
68
pipeline .clone ()
64
69
@@ -67,8 +72,6 @@ def test_clone(mock_fetch_via_vcs, mock_vcs_response):
67
72
68
73
69
74
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
-
72
75
mock_vcs_response .dest_dir = str (mock_gitlab_yaml )
73
76
74
77
pipeline = GitLabImporterPipeline ()
@@ -80,8 +83,6 @@ def test_advisories_count(mock_gitlab_yaml, mock_vcs_response, mock_fetch_via_vc
80
83
81
84
82
85
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
-
85
86
mock_vcs_response .dest_dir = str (mock_gitlab_yaml )
86
87
87
88
pipeline = GitLabImporterPipeline ()
@@ -101,8 +102,6 @@ def test_collect_advisories(mock_gitlab_yaml, mock_vcs_response, mock_fetch_via_
101
102
102
103
103
104
def test_clean_downloads (mock_vcs_response ):
104
- from vulnerabilities .pipelines .v2_importers .gitlab_importer import GitLabImporterPipeline
105
-
106
105
pipeline = GitLabImporterPipeline ()
107
106
pipeline .vcs_response = mock_vcs_response
108
107
@@ -111,8 +110,6 @@ def test_clean_downloads(mock_vcs_response):
111
110
112
111
113
112
def test_on_failure (mock_vcs_response ):
114
- from vulnerabilities .pipelines .v2_importers .gitlab_importer import GitLabImporterPipeline
115
-
116
113
pipeline = GitLabImporterPipeline ()
117
114
pipeline .vcs_response = mock_vcs_response
118
115
@@ -124,8 +121,6 @@ def test_on_failure(mock_vcs_response):
124
121
def test_collect_advisories_with_invalid_yaml (
125
122
mock_gitlab_yaml , mock_vcs_response , mock_fetch_via_vcs
126
123
):
127
- from vulnerabilities .pipelines .v2_importers .gitlab_importer import GitLabImporterPipeline
128
-
129
124
# Add an invalid YAML file
130
125
invalid_file = Path (mock_gitlab_yaml ) / "pypi" / "package_name" / "invalid.yml"
131
126
invalid_file .write_text (":::invalid_yaml" )
@@ -141,8 +136,6 @@ def test_collect_advisories_with_invalid_yaml(
141
136
142
137
143
138
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
-
146
139
mock_vcs_response .dest_dir = str (tmp_path )
147
140
148
141
pipeline = GitLabImporterPipeline ()
@@ -151,3 +144,32 @@ def test_advisories_count_empty(mock_vcs_response, mock_fetch_via_vcs, tmp_path)
151
144
152
145
count = pipeline .advisories_count ()
153
146
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