@@ -1475,6 +1475,40 @@ def test_api_package_viewset_aboutcode_files_action(self):
14751475 'attachment; filename="package1.zip_about.zip"' , response ["content-disposition" ]
14761476 )
14771477
1478+ @mock .patch ("dejacode_toolkit.scancodeio.ScanCodeIO.get_scan_results" )
1479+ @mock .patch ("dejacode_toolkit.scancodeio.ScanCodeIO.fetch_scan_data" )
1480+ @mock .patch ("dejacode_toolkit.scancodeio.ScanCodeIO.is_available" )
1481+ def test_api_package_viewset_download_scan_data_action (
1482+ self , mock_is_available , mock_fetch_scan_data , mock_get_scan_results
1483+ ):
1484+ scan_data_url = reverse ("api_v2:package-download-scan-data" , args = [self .package1 .uuid ])
1485+ mock_fetch_scan_data .return_value = {}
1486+ mock_get_scan_results .return_value = None
1487+
1488+ response = self .client .get (scan_data_url )
1489+ self .assertEqual (403 , response .status_code )
1490+ response = self .client .post (scan_data_url )
1491+ self .assertEqual (403 , response .status_code )
1492+
1493+ self .client .login (username = self .base_user .username , password = "secret" )
1494+ mock_is_available .return_value = False
1495+ response = self .client .get (scan_data_url )
1496+ self .assertEqual (status .HTTP_400_BAD_REQUEST , response .status_code )
1497+ expected = {"error" : "The ScanCode.io service is not available" }
1498+ self .assertEqual (expected , response .data )
1499+
1500+ mock_is_available .return_value = True
1501+ response = self .client .get (scan_data_url )
1502+ self .assertEqual (status .HTTP_400_BAD_REQUEST , response .status_code )
1503+ expected = {"error" : "Scan results are not available" }
1504+ self .assertEqual (expected , response .data )
1505+
1506+ mock_get_scan_results .return_value = {"url" : "" }
1507+ response = self .client .get (scan_data_url )
1508+ self .assertEqual (200 , response .status_code )
1509+ self .assertEqual ("application/zip" , response ["content-type" ])
1510+ self .assertEqual ('attachment; filename="package1.zip"' , response ["content-disposition" ])
1511+
14781512 def test_api_package_protected_fields_as_read_only (self ):
14791513 policy = UsagePolicy .objects .create (
14801514 label = "PackagePolicy" ,
0 commit comments