|
29 | 29 |
|
30 | 30 | from django.apps import apps |
31 | 31 | from django.core.exceptions import SuspiciousFileOperation |
| 32 | +from django.http import FileResponse |
32 | 33 | from django.http.response import Http404 |
33 | 34 | from django.test import TestCase |
34 | 35 | from django.test import override_settings |
@@ -1325,3 +1326,88 @@ def test_scanpipe_policies_broken_policies_project_details(self): |
1325 | 1326 | response = self.client.get(url) |
1326 | 1327 | self.assertEqual(200, response.status_code) |
1327 | 1328 | self.assertContains(response, "Policies file format error") |
| 1329 | + |
| 1330 | + def test_scanpipe_views_export_json_returns_valid_response(self): |
| 1331 | + url = reverse("project_resources", args=[self.project1.slug]) |
| 1332 | + response = self.client.get(url + "?export_json=True") |
| 1333 | + |
| 1334 | + self.assertIsInstance(response, FileResponse) |
| 1335 | + self.assertEqual(response.get("Content-Type"), "application/json") |
| 1336 | + self.assertTrue(response.get("Content-Disposition").startswith("attachment")) |
| 1337 | + |
| 1338 | + def test_scanpipe_views_export_json_correct_filename(self): |
| 1339 | + url = reverse("project_resources", args=[self.project1.slug]) |
| 1340 | + response = self.client.get(url + "?export_json=True") |
| 1341 | + |
| 1342 | + actual_filename = response.get("Content-Disposition") |
| 1343 | + expected_filename = ( |
| 1344 | + f'attachment; filename="{self.project1.name}_codebaseresource.json"' |
| 1345 | + ) |
| 1346 | + self.assertEqual(actual_filename, expected_filename) |
| 1347 | + |
| 1348 | + def test_scanpipe_views_export_json_contains_expected_fields(self): |
| 1349 | + make_resource_file(self.project1, "file1.txt") |
| 1350 | + url = reverse("project_resources", args=[self.project1.slug]) |
| 1351 | + response = self.client.get(url + "?export_json=True") |
| 1352 | + |
| 1353 | + file_content = b"".join(response.streaming_content).decode("utf-8") |
| 1354 | + json_data = json.loads(file_content) |
| 1355 | + |
| 1356 | + expected_fields = [ |
| 1357 | + "path", |
| 1358 | + "type", |
| 1359 | + "name", |
| 1360 | + "status", |
| 1361 | + "for_packages", |
| 1362 | + "tag", |
| 1363 | + "extension", |
| 1364 | + "size", |
| 1365 | + "md5", |
| 1366 | + "sha1", |
| 1367 | + "sha256", |
| 1368 | + "sha512", |
| 1369 | + "mime_type", |
| 1370 | + "file_type", |
| 1371 | + "programming_language", |
| 1372 | + "is_binary", |
| 1373 | + "is_text", |
| 1374 | + "is_archive", |
| 1375 | + "is_media", |
| 1376 | + "is_legal", |
| 1377 | + "is_manifest", |
| 1378 | + "is_readme", |
| 1379 | + "is_top_level", |
| 1380 | + "is_key_file", |
| 1381 | + "detected_license_expression", |
| 1382 | + "detected_license_expression_spdx", |
| 1383 | + "percentage_of_license_text", |
| 1384 | + "compliance_alert", |
| 1385 | + "copyrights", |
| 1386 | + "holders", |
| 1387 | + "authors", |
| 1388 | + "emails", |
| 1389 | + "urls", |
| 1390 | + ] |
| 1391 | + |
| 1392 | + for field in expected_fields: |
| 1393 | + self.assertIn(field, json_data) |
| 1394 | + |
| 1395 | + def test_scanpipe_views_export_json_excludes_fields(self): |
| 1396 | + make_resource_file(self.project1, "file1.txt") |
| 1397 | + url = reverse("project_resources", args=[self.project1.slug]) |
| 1398 | + response = self.client.get(url + "?export_json=True") |
| 1399 | + |
| 1400 | + file_content = b"".join(response.streaming_content).decode("utf-8") |
| 1401 | + json_data = json.loads(file_content) |
| 1402 | + |
| 1403 | + excluded_fields = [ |
| 1404 | + "extra_data", |
| 1405 | + "package_data", |
| 1406 | + "license_detections", |
| 1407 | + "other_license_detections", |
| 1408 | + "license_clues", |
| 1409 | + "affected_by_vulnerabilities", |
| 1410 | + ] |
| 1411 | + |
| 1412 | + for field in excluded_fields: |
| 1413 | + self.assertNotIn(field, json_data) |
0 commit comments