|
| 1 | +import pytest |
| 2 | +from unittest.mock import Mock, patch |
| 3 | + |
| 4 | +from patchwork.steps.ScanSonar.ScanSonar import ScanSonar |
| 5 | +from patchwork.common.client.sonar import SonarClient, SonarVuln |
| 6 | + |
| 7 | +def test_scan_sonar(): |
| 8 | + inputs = { |
| 9 | + "sonarqube_project_key": "test-project", |
| 10 | + "sonarqube_access_token": "test-token", |
| 11 | + "sonarqube_base_url": "https://sonarcloud.io" |
| 12 | + } |
| 13 | + |
| 14 | + mock_vulns = { |
| 15 | + "src/file1.py": [ |
| 16 | + SonarVuln( |
| 17 | + start=10, |
| 18 | + end=15, |
| 19 | + cwe="CWE-79", |
| 20 | + bug_msg="Test vulnerability" |
| 21 | + ) |
| 22 | + ] |
| 23 | + } |
| 24 | + |
| 25 | + with patch.object(SonarClient, 'find_vulns', return_value=mock_vulns): |
| 26 | + step = ScanSonar(inputs) |
| 27 | + result = step.run() |
| 28 | + |
| 29 | + assert "files_to_patch" in result |
| 30 | + vulns = result["files_to_patch"] |
| 31 | + assert len(vulns) == 1 |
| 32 | + |
| 33 | + vuln = vulns[0] |
| 34 | + assert vuln["uri"] == "src/file1.py" |
| 35 | + assert vuln["startLine"] == 10 |
| 36 | + assert vuln["endLine"] == 15 |
| 37 | + assert vuln["cwe"] == "CWE-79" |
| 38 | + assert vuln["description"] == "Test vulnerability" |
| 39 | + |
| 40 | +def test_scan_sonar_error(): |
| 41 | + inputs = { |
| 42 | + "sonarqube_project_key": "test-project", |
| 43 | + "sonarqube_access_token": "test-token", |
| 44 | + "sonarqube_base_url": "https://sonarcloud.io" |
| 45 | + } |
| 46 | + |
| 47 | + with patch.object(SonarClient, 'find_vulns', side_effect=Exception("Test error")): |
| 48 | + step = ScanSonar(inputs) |
| 49 | + with pytest.raises(Exception) as exc_info: |
| 50 | + step.run() |
| 51 | + assert str(exc_info.value) == "Test error" |
0 commit comments