2929from requests import auth as request_auth
3030
3131from scanpipe .pipes import fetch
32+ from scanpipe .tests import make_mock_response
3233
3334
3435class ScanPipeFetchPipesTest (TestCase ):
@@ -41,6 +42,7 @@ def test_scanpipe_pipes_fetch_get_fetcher(self):
4142 git_http_url = "https://github.com/aboutcode-org/scancode.io.git"
4243 self .assertEqual (fetch .fetch_git_repo , fetch .get_fetcher (git_http_url ))
4344 self .assertEqual (fetch .fetch_git_repo , fetch .get_fetcher (git_http_url + "/" ))
45+ self .
assertEqual (
fetch .
fetch_package_url ,
fetch .
get_fetcher (
"pkg:npm/[email protected] " ))
4446
4547 with self .assertRaises (ValueError ) as cm :
4648 fetch .get_fetcher ("" )
@@ -71,28 +73,41 @@ def test_scanpipe_pipes_fetch_get_fetcher(self):
7173 def test_scanpipe_pipes_fetch_http (self , mock_get ):
7274 url = "https://example.com/filename.zip"
7375
74- mock_get .return_value = mock .Mock (
75- content = b"\x00 " , headers = {}, status_code = 200 , url = url
76- )
76+ mock_get .return_value = make_mock_response (url = url )
7777 downloaded_file = fetch .fetch_http (url )
7878 self .assertTrue (Path (downloaded_file .directory , "filename.zip" ).exists ())
7979
8080 url_with_spaces = "https://example.com/space%20in%20name.zip"
81- mock_get .return_value = mock .Mock (
82- content = b"\x00 " , headers = {}, status_code = 200 , url = url_with_spaces
83- )
81+ mock_get .return_value = make_mock_response (url = url_with_spaces )
8482 downloaded_file = fetch .fetch_http (url )
8583 self .assertTrue (Path (downloaded_file .directory , "space in name.zip" ).exists ())
8684
8785 headers = {
8886 "content-disposition" : 'attachment; filename="another_name.zip"' ,
8987 }
90- mock_get .return_value = mock .Mock (
91- content = b"\x00 " , headers = headers , status_code = 200 , url = url
92- )
88+ mock_get .return_value = make_mock_response (url = url , headers = headers )
9389 downloaded_file = fetch .fetch_http (url )
9490 self .assertTrue (Path (downloaded_file .directory , "another_name.zip" ).exists ())
9591
92+ @mock .patch ("requests.sessions.Session.get" )
93+ def test_scanpipe_pipes_fetch_package_url (self , mock_get ):
94+ package_url = "pkg:not_a_valid_purl"
95+ with self .assertRaises (ValueError ) as cm :
96+ fetch .fetch_package_url (package_url )
97+ expected = f"purl is missing the required type component: '{ package_url } '."
98+ self .assertEqual (expected , str (cm .exception ))
99+
100+ package_url = "pkg:generic/name@version"
101+ with self .assertRaises (ValueError ) as cm :
102+ fetch .fetch_package_url (package_url )
103+ expected = f"Could not resolve a download URL for { package_url } ."
104+ self .assertEqual (expected , str (cm .exception ))
105+
106+ package_url = "pkg:npm/[email protected] " 107+ mock_get .return_value = make_mock_response (url = "https://exa.com/filename.zip" )
108+ downloaded_file = fetch .fetch_package_url (package_url )
109+ self .assertTrue (Path (downloaded_file .directory , "filename.zip" ).exists ())
110+
96111 @mock .patch ("scanpipe.pipes.fetch.get_docker_image_platform" )
97112 @mock .patch ("scanpipe.pipes.fetch._get_skopeo_location" )
98113 @mock .patch ("scanpipe.pipes.fetch.run_command_safely" )
@@ -188,9 +203,7 @@ def test_scanpipe_pipes_fetch_fetch_urls(self, mock_get):
188203 "https://example.com/archive.tar.gz" ,
189204 ]
190205
191- mock_get .return_value = mock .Mock (
192- content = b"\x00 " , headers = {}, status_code = 200 , url = "mocked_url"
193- )
206+ mock_get .return_value = make_mock_response (url = "mocked_url" )
194207 downloads , errors = fetch .fetch_urls (urls )
195208 self .assertEqual (2 , len (downloads ))
196209 self .assertEqual (urls [0 ], downloads [0 ].uri )
0 commit comments