|
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
|
@@ -1340,3 +1341,210 @@ def test_scanpipe_views_codebase_resource_details_get_matched_snippet_annotation
|
1340 | 1341 | results = CodebaseResourceDetailsView.get_matched_snippet_annotations(resource1)
|
1341 | 1342 | expected_results = [{"start_line": 1, "end_line": 6}]
|
1342 | 1343 | self.assertEqual(expected_results, results)
|
| 1344 | + |
| 1345 | + def test_project_packages_export_json(self): |
| 1346 | + make_package(self.project1, package_url="pkg:type/a") |
| 1347 | + |
| 1348 | + url = reverse("project_packages", args=[self.project1.slug]) |
| 1349 | + response = self.client.get(url + "?export_json=True") |
| 1350 | + |
| 1351 | + self.assertIsInstance(response, FileResponse) |
| 1352 | + self.assertEqual(response.get("Content-Type"), "application/json") |
| 1353 | + self.assertTrue(response.get("Content-Disposition").startswith("attachment")) |
| 1354 | + |
| 1355 | + file_content = b"".join(response.streaming_content).decode("utf-8") |
| 1356 | + json_data = json.loads(file_content) |
| 1357 | + |
| 1358 | + expected_fields = [ |
| 1359 | + "purl", |
| 1360 | + "type", |
| 1361 | + "namespace", |
| 1362 | + "name", |
| 1363 | + "version", |
| 1364 | + "qualifiers", |
| 1365 | + "subpath", |
| 1366 | + "tag", |
| 1367 | + "primary_language", |
| 1368 | + "description", |
| 1369 | + "notes", |
| 1370 | + "release_date", |
| 1371 | + "parties", |
| 1372 | + "keywords", |
| 1373 | + "homepage_url", |
| 1374 | + "download_url", |
| 1375 | + "bug_tracking_url", |
| 1376 | + "code_view_url", |
| 1377 | + "vcs_url", |
| 1378 | + "repository_homepage_url", |
| 1379 | + "repository_download_url", |
| 1380 | + "api_data_url", |
| 1381 | + "size", |
| 1382 | + "md5", |
| 1383 | + "sha1", |
| 1384 | + "sha256", |
| 1385 | + "sha512", |
| 1386 | + "copyright", |
| 1387 | + "holder", |
| 1388 | + "declared_license_expression", |
| 1389 | + "declared_license_expression_spdx", |
| 1390 | + "other_license_expression", |
| 1391 | + "other_license_expression_spdx", |
| 1392 | + "extracted_license_statement", |
| 1393 | + "compliance_alert", |
| 1394 | + "notice_text", |
| 1395 | + "source_packages", |
| 1396 | + "package_uid", |
| 1397 | + "is_private", |
| 1398 | + "is_virtual", |
| 1399 | + "datasource_ids", |
| 1400 | + "datafile_paths", |
| 1401 | + "file_references", |
| 1402 | + "missing_resources", |
| 1403 | + "modified_resources", |
| 1404 | + ] |
| 1405 | + |
| 1406 | + for field in expected_fields: |
| 1407 | + self.assertIn(field, json_data[0]) |
| 1408 | + |
| 1409 | + def test_project_dependencies_export_json(self): |
| 1410 | + make_resource_file(self.project1, "file.ext") |
| 1411 | + make_dependency(self.project1) |
| 1412 | + |
| 1413 | + url = reverse("project_dependencies", args=[self.project1.slug]) |
| 1414 | + response = self.client.get(url + "?export_json=True") |
| 1415 | + |
| 1416 | + self.assertIsInstance(response, FileResponse) |
| 1417 | + self.assertEqual(response.get("Content-Type"), "application/json") |
| 1418 | + self.assertTrue(response.get("Content-Disposition").startswith("attachment")) |
| 1419 | + |
| 1420 | + file_content = b"".join(response.streaming_content).decode("utf-8") |
| 1421 | + json_data = json.loads(file_content) |
| 1422 | + |
| 1423 | + expected_fields = [ |
| 1424 | + "purl", |
| 1425 | + "extracted_requirement", |
| 1426 | + "scope", |
| 1427 | + "is_runtime", |
| 1428 | + "is_optional", |
| 1429 | + "is_pinned", |
| 1430 | + "is_direct", |
| 1431 | + "dependency_uid", |
| 1432 | + "for_package_uid", |
| 1433 | + "resolved_to_package_uid", |
| 1434 | + "datafile_path", |
| 1435 | + "datasource_id", |
| 1436 | + "package_type", |
| 1437 | + ] |
| 1438 | + |
| 1439 | + for field in expected_fields: |
| 1440 | + self.assertIn(field, json_data[0]) |
| 1441 | + |
| 1442 | + def test_project_relations_export_json(self): |
| 1443 | + make_relation( |
| 1444 | + from_resource=make_resource_file(self.project1, "file1.ext"), |
| 1445 | + to_resource=make_resource_file(self.project1, "file2.ext"), |
| 1446 | + map_type="path", |
| 1447 | + ) |
| 1448 | + |
| 1449 | + url = reverse("project_relations", args=[self.project1.slug]) |
| 1450 | + response = self.client.get(url + "?export_json=True") |
| 1451 | + |
| 1452 | + self.assertIsInstance(response, FileResponse) |
| 1453 | + self.assertEqual(response.get("Content-Type"), "application/json") |
| 1454 | + self.assertTrue(response.get("Content-Disposition").startswith("attachment")) |
| 1455 | + |
| 1456 | + file_content = b"".join(response.streaming_content).decode("utf-8") |
| 1457 | + json_data = json.loads(file_content) |
| 1458 | + |
| 1459 | + expected_fields = [ |
| 1460 | + "to_resource", |
| 1461 | + "status", |
| 1462 | + "map_type", |
| 1463 | + "score", |
| 1464 | + "from_resource", |
| 1465 | + ] |
| 1466 | + |
| 1467 | + for field in expected_fields: |
| 1468 | + self.assertIn(field, json_data[0]) |
| 1469 | + |
| 1470 | + def test_project_messages_export_json(self): |
| 1471 | + self.project1.add_message("warning") |
| 1472 | + |
| 1473 | + url = reverse("project_messages", args=[self.project1.slug]) |
| 1474 | + response = self.client.get(url + "?export_json=True") |
| 1475 | + |
| 1476 | + self.assertIsInstance(response, FileResponse) |
| 1477 | + self.assertEqual(response.get("Content-Type"), "application/json") |
| 1478 | + self.assertTrue(response.get("Content-Disposition").startswith("attachment")) |
| 1479 | + |
| 1480 | + file_content = b"".join(response.streaming_content).decode("utf-8") |
| 1481 | + json_data = json.loads(file_content) |
| 1482 | + |
| 1483 | + expected_fields = [ |
| 1484 | + "uuid", |
| 1485 | + "severity", |
| 1486 | + "description", |
| 1487 | + "model", |
| 1488 | + "details", |
| 1489 | + "traceback", |
| 1490 | + "created_date", |
| 1491 | + ] |
| 1492 | + |
| 1493 | + for field in expected_fields: |
| 1494 | + self.assertIn(field, json_data[0]) |
| 1495 | + |
| 1496 | + def test_project_codebase_resources_export_json(self): |
| 1497 | + make_resource_file(self.project1, "file.ext") |
| 1498 | + |
| 1499 | + url = reverse("project_resources", args=[self.project1.slug]) |
| 1500 | + response = self.client.get(url + "?export_json=True") |
| 1501 | + |
| 1502 | + self.assertIsInstance(response, FileResponse) |
| 1503 | + self.assertEqual(response.get("Content-Type"), "application/json") |
| 1504 | + self.assertTrue(response.get("Content-Disposition").startswith("attachment")) |
| 1505 | + |
| 1506 | + file_content = b"".join(response.streaming_content).decode("utf-8") |
| 1507 | + json_data = json.loads(file_content) |
| 1508 | + |
| 1509 | + expected_fields = [ |
| 1510 | + "path", |
| 1511 | + "type", |
| 1512 | + "name", |
| 1513 | + "status", |
| 1514 | + "for_packages", |
| 1515 | + "tag", |
| 1516 | + "extension", |
| 1517 | + "size", |
| 1518 | + "mime_type", |
| 1519 | + "file_type", |
| 1520 | + "programming_language", |
| 1521 | + "detected_license_expression", |
| 1522 | + "detected_license_expression_spdx", |
| 1523 | + "license_detections", |
| 1524 | + "license_clues", |
| 1525 | + "percentage_of_license_text", |
| 1526 | + "compliance_alert", |
| 1527 | + "copyrights", |
| 1528 | + "holders", |
| 1529 | + "authors", |
| 1530 | + "package_data", |
| 1531 | + "emails", |
| 1532 | + "urls", |
| 1533 | + "md5", |
| 1534 | + "sha1", |
| 1535 | + "sha256", |
| 1536 | + "sha512", |
| 1537 | + "is_binary", |
| 1538 | + "is_text", |
| 1539 | + "is_archive", |
| 1540 | + "is_media", |
| 1541 | + "is_legal", |
| 1542 | + "is_manifest", |
| 1543 | + "is_readme", |
| 1544 | + "is_top_level", |
| 1545 | + "is_key_file", |
| 1546 | + "extra_data", |
| 1547 | + ] |
| 1548 | + |
| 1549 | + for field in expected_fields: |
| 1550 | + self.assertIn(field, json_data[0]) |
0 commit comments