|
17 | 17 | from django.test import override_settings |
18 | 18 | from django.urls import reverse |
19 | 19 |
|
| 20 | +import requests |
20 | 21 | from rest_framework import status |
21 | 22 | from rest_framework.exceptions import ErrorDetail |
22 | 23 | from rest_framework.test import APIClient |
@@ -1523,19 +1524,32 @@ def test_api_package_viewset_scan_info_action(self, mock_is_available, mock_get_ |
1523 | 1524 | self.assertEqual(project_info, response.data) |
1524 | 1525 |
|
1525 | 1526 | @mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.get_project_info") |
1526 | | - @mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.fetch_scan_data") |
| 1527 | + @mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.stream_scan_data") |
1527 | 1528 | @mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.is_available") |
1528 | 1529 | def test_api_package_viewset_scan_results_action( |
1529 | | - self, mock_is_available, mock_fetch_scan_data, mock_get_project_info |
| 1530 | + self, mock_is_available, mock_stream_scan_data, mock_get_project_info |
1530 | 1531 | ): |
1531 | 1532 | self.client.login(username=self.base_user.username, password="secret") |
1532 | 1533 | action_url = reverse("api_v2:package-scan-results", args=[self.package1.uuid]) |
1533 | 1534 | mock_is_available.return_value = True |
1534 | 1535 | mock_get_project_info.return_value = {"uuid": "abcdef"} |
1535 | | - mock_fetch_scan_data.return_value = {"results": ""} |
| 1536 | + |
| 1537 | + mock_stream_scan_data.side_effect = requests.RequestException |
| 1538 | + response = self.client.get(action_url) |
| 1539 | + self.assertEqual(400, response.status_code) |
| 1540 | + error = {"detail": ErrorDetail(string="Could not fetch scan data", code="error")} |
| 1541 | + self.assertEqual(error, response.data) |
| 1542 | + |
| 1543 | + mock_response = mock.Mock() |
| 1544 | + mock_response.iter_content.return_value = iter([b'{"results": ""}']) |
| 1545 | + mock_response.headers = {"Content-Type": "application/json"} |
| 1546 | + mock_stream_scan_data.side_effect = None |
| 1547 | + mock_stream_scan_data.return_value = mock_response |
| 1548 | + |
1536 | 1549 | response = self.client.get(action_url) |
1537 | 1550 | self.assertEqual(200, response.status_code) |
1538 | | - self.assertEqual({"results": ""}, response.data) |
| 1551 | + self.assertEqual(b'{"results": ""}', b"".join(response.streaming_content)) |
| 1552 | + self.assertEqual("application/json", response.headers["Content-Type"]) |
1539 | 1553 |
|
1540 | 1554 | @mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.get_project_info") |
1541 | 1555 | @mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.fetch_scan_data") |
|
0 commit comments