|
| 1 | +from socketsecurity.core.classes import Diff, Issue |
| 2 | +from socketsecurity.core.messages import Messages |
| 3 | + |
| 4 | + |
| 5 | +def _issue(**kwargs): |
| 6 | + values = { |
| 7 | + "pkg_type": "npm", |
| 8 | + "pkg_name": "example-lib", |
| 9 | + "pkg_version": "1.4.2", |
| 10 | + "type": "highCVE", |
| 11 | + "severity": "high", |
| 12 | + "title": "High CVE", |
| 13 | + "description": "A vulnerable dependency.", |
| 14 | + "suggestion": "Upgrade to a patched release.", |
| 15 | + "purl": "pkg:npm/example-lib@1.4.2", |
| 16 | + "url": "https://socket.dev/npm/package/example-lib/overview/1.4.2", |
| 17 | + "manifests": "package-lock.json", |
| 18 | + "introduced_by": [["example-lib", "package-lock.json"]], |
| 19 | + "error": True, |
| 20 | + } |
| 21 | + values.update(kwargs) |
| 22 | + return Issue(**values) |
| 23 | + |
| 24 | + |
| 25 | +def test_console_security_alert_table_includes_patched_version(): |
| 26 | + diff = Diff( |
| 27 | + new_alerts=[ |
| 28 | + _issue(props={"firstPatchedVersionIdentifier": "1.5.0"}), |
| 29 | + ] |
| 30 | + ) |
| 31 | + |
| 32 | + table = Messages.create_console_security_alert_table(diff) |
| 33 | + |
| 34 | + assert table.field_names == [ |
| 35 | + "Alert", |
| 36 | + "Package", |
| 37 | + "Patched Version", |
| 38 | + "url", |
| 39 | + "Introduced by", |
| 40 | + "Manifest File", |
| 41 | + "CI Status", |
| 42 | + ] |
| 43 | + assert table.rows[0][2] == "1.5.0" |
| 44 | + |
| 45 | + |
| 46 | +def test_console_security_alert_table_leaves_missing_patched_version_blank(): |
| 47 | + diff = Diff( |
| 48 | + new_alerts=[ |
| 49 | + _issue(), |
| 50 | + _issue(props={}), |
| 51 | + _issue(props={"firstPatchedVersionIdentifier": None}), |
| 52 | + ] |
| 53 | + ) |
| 54 | + |
| 55 | + table = Messages.create_console_security_alert_table(diff) |
| 56 | + |
| 57 | + assert [row[2] for row in table.rows] == ["", "", ""] |
| 58 | + |
| 59 | + |
| 60 | +def test_security_comment_includes_patched_version_when_available(): |
| 61 | + diff = Diff( |
| 62 | + new_alerts=[ |
| 63 | + _issue(props={"firstPatchedVersionIdentifier": "1.5.0"}), |
| 64 | + ], |
| 65 | + diff_url="https://socket.dev/dashboard/org/acme/diff/before/after", |
| 66 | + ) |
| 67 | + |
| 68 | + comment = Messages.security_comment_template(diff) |
| 69 | + |
| 70 | + assert "<strong>Patched version:</strong> <code>1.5.0</code>" in comment |
| 71 | + |
| 72 | + |
| 73 | +def test_security_comment_omits_missing_patched_version(): |
| 74 | + diff = Diff( |
| 75 | + new_alerts=[_issue(props={})], |
| 76 | + diff_url="https://socket.dev/dashboard/org/acme/diff/before/after", |
| 77 | + ) |
| 78 | + |
| 79 | + comment = Messages.security_comment_template(diff) |
| 80 | + |
| 81 | + assert "Patched version:" not in comment |
0 commit comments