|
9 | 9 |
|
10 | 10 | import json |
11 | 11 | from pathlib import Path |
| 12 | +from unittest.mock import patch |
12 | 13 |
|
13 | 14 | from commoncode import testcase |
14 | 15 | from packageurl import PackageURL |
|
20 | 21 | class TestDeps(testcase.FileBasedTesting): |
21 | 22 | test_data_dir = str(Path(__file__).resolve().parent / "test_data" / "oss_index") |
22 | 23 |
|
| 24 | + def test_fetch_json_response_uses_sonatype_guide_compatibility_api(self): |
| 25 | + coordinates = ["pkg:pypi/django@5.2.1"] |
| 26 | + datasource = oss_index.OSSDataSource() |
| 27 | + |
| 28 | + with patch.object(oss_index.requests, "post") as mock_post: |
| 29 | + mock_response = mock_post.return_value |
| 30 | + mock_response.raise_for_status.return_value = None |
| 31 | + mock_response.json.return_value = [] |
| 32 | + |
| 33 | + assert datasource.fetch_json_response(coordinates) == [] |
| 34 | + |
| 35 | + mock_post.assert_called_once_with( |
| 36 | + "https://api.guide.sonatype.com/api/v3/component-report", |
| 37 | + auth=None, |
| 38 | + json={"coordinates": coordinates}, |
| 39 | + ) |
| 40 | + |
| 41 | + def test_fetch_json_response_uses_authenticated_sonatype_guide_compatibility_api(self): |
| 42 | + coordinates = ["pkg:pypi/django@5.2.1"] |
| 43 | + datasource = oss_index.OSSDataSource() |
| 44 | + |
| 45 | + with patch.dict(oss_index.os.environ, {"OSS_USERNAME": "user", "OSS_TOKEN": "token"}): |
| 46 | + with patch.object(oss_index.requests, "post") as mock_post: |
| 47 | + mock_response = mock_post.return_value |
| 48 | + mock_response.raise_for_status.return_value = None |
| 49 | + mock_response.json.return_value = [] |
| 50 | + |
| 51 | + assert datasource.fetch_json_response(coordinates) == [] |
| 52 | + |
| 53 | + mock_post.assert_called_once_with( |
| 54 | + "https://api.guide.sonatype.com/api/v3/authorized/component-report", |
| 55 | + auth=("user", "token"), |
| 56 | + json={"coordinates": coordinates}, |
| 57 | + ) |
| 58 | + |
23 | 59 | def test_parse_advisory(self): |
24 | 60 | advisory_file = self.get_test_loc("advisory.json") |
25 | 61 | with open(advisory_file) as f: |
|
0 commit comments