|
| 1 | +from cycode.cli.consts import ( |
| 2 | + LICENSE_COMPLIANCE_POLICY_ID, |
| 3 | + PACKAGE_VULNERABILITY_POLICY_ID, |
| 4 | + UNMAINTAINED_PACKAGE_POLICY_ID, |
| 5 | +) |
| 6 | +from cycode.cli.printers.utils.sca_policy_details import get_sca_policy_details |
| 7 | +from cycode.cyclient.models import Detection |
| 8 | + |
| 9 | + |
| 10 | +def _make_detection(policy_id: str, **details: object) -> Detection: |
| 11 | + return Detection( |
| 12 | + detection_type_id=policy_id, |
| 13 | + type='sca', |
| 14 | + message='message', |
| 15 | + detection_details=dict(details), |
| 16 | + detection_rule_id='rule-id', |
| 17 | + severity='Medium', |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +def test_package_vulnerability_reports_the_patched_version() -> None: |
| 22 | + detection = _make_detection(PACKAGE_VULNERABILITY_POLICY_ID, alert={'first_patched_version': '4.17.21'}) |
| 23 | + |
| 24 | + assert get_sca_policy_details(detection) == [('First patched version', '4.17.21')] |
| 25 | + |
| 26 | + |
| 27 | +def test_package_vulnerability_without_a_patch() -> None: |
| 28 | + detection = _make_detection(PACKAGE_VULNERABILITY_POLICY_ID, alert={'first_patched_version': None}) |
| 29 | + |
| 30 | + assert get_sca_policy_details(detection) == [('First patched version', 'Not fixed')] |
| 31 | + |
| 32 | + |
| 33 | +def test_license_compliance_reports_the_license() -> None: |
| 34 | + detection = _make_detection(LICENSE_COMPLIANCE_POLICY_ID, license='GPL-3.0') |
| 35 | + |
| 36 | + assert get_sca_policy_details(detection) == [('License', 'GPL-3.0')] |
| 37 | + |
| 38 | + |
| 39 | +def test_license_compliance_without_a_license() -> None: |
| 40 | + detection = _make_detection(LICENSE_COMPLIANCE_POLICY_ID) |
| 41 | + |
| 42 | + assert get_sca_policy_details(detection) == [('License', 'N/A')] |
| 43 | + |
| 44 | + |
| 45 | +def test_unmaintained_package_reports_the_maintained_check_first() -> None: |
| 46 | + detection = _make_detection( |
| 47 | + UNMAINTAINED_PACKAGE_POLICY_ID, |
| 48 | + ossf={ |
| 49 | + 'score': 4.1, |
| 50 | + 'scorecard_report_url': 'https://scorecard.dev/viewer/?uri=github.com/a/b', |
| 51 | + 'checks': [{'name': 'Maintained', 'score': 0}], |
| 52 | + }, |
| 53 | + ) |
| 54 | + |
| 55 | + assert get_sca_policy_details(detection) == [ |
| 56 | + ('Maintained score', '0'), |
| 57 | + ('OSSF Scorecard score', '4.1'), |
| 58 | + ('Scorecard report', 'https://scorecard.dev/viewer/?uri=github.com/a/b'), |
| 59 | + ] |
| 60 | + |
| 61 | + |
| 62 | +def test_unmaintained_package_without_a_scorecard() -> None: |
| 63 | + detection = _make_detection(UNMAINTAINED_PACKAGE_POLICY_ID) |
| 64 | + |
| 65 | + assert get_sca_policy_details(detection) == [ |
| 66 | + ('Maintained score', 'N/A'), |
| 67 | + ('OSSF Scorecard score', 'N/A'), |
| 68 | + ('Scorecard report', 'N/A'), |
| 69 | + ] |
| 70 | + |
| 71 | + |
| 72 | +def test_an_unregistered_policy_contributes_nothing() -> None: |
| 73 | + """A policy with no entry must stay silent rather than borrow another policy's fields. |
| 74 | +
|
| 75 | + Before this was keyed by policy, an unmaintained detection fell through to the license branch and rendered |
| 76 | + an empty License row. A fourth policy would do the same. |
| 77 | + """ |
| 78 | + detection = _make_detection('00000000-0000-0000-0000-000000000000', license='GPL-3.0', alert={}) |
| 79 | + |
| 80 | + assert get_sca_policy_details(detection) == [] |
0 commit comments