Repository navigation
Expand file tree
/
Copy pathtest_messages.py
More file actions
122 lines (88 loc) · 3.56 KB
/
Copy pathtest_messages.py
File metadata and controls
122 lines (88 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from socketsecurity.core.classes import Diff, Issue
from socketsecurity.core.messages import Messages
def _issue(**kwargs):
values = {
"pkg_type": "npm",
"pkg_name": "example-lib",
"pkg_version": "1.4.2",
"type": "highCVE",
"severity": "high",
"title": "High CVE",
"description": "A vulnerable dependency.",
"suggestion": "Upgrade to a patched release.",
"purl": "pkg:npm/example-lib@1.4.2",
"url": "https://socket.dev/npm/package/example-lib/overview/1.4.2",
"manifests": "package-lock.json",
"introduced_by": [["example-lib", "package-lock.json"]],
"error": True,
}
values.update(kwargs)
return Issue(**values)
def test_console_security_alert_table_includes_patched_version():
diff = Diff(
new_alerts=[
_issue(props={"firstPatchedVersionIdentifier": "1.5.0"}),
]
)
table = Messages.create_console_security_alert_table(diff)
assert table.field_names == [
"Alert",
"Package",
"Patched Version",
"url",
"Introduced by",
"Manifest File",
"CI Status",
]
assert table.rows[0][2] == "1.5.0"
def test_console_security_alert_table_leaves_missing_patched_version_blank():
diff = Diff(
new_alerts=[
_issue(),
_issue(props={}),
_issue(props={"firstPatchedVersionIdentifier": None}),
]
)
table = Messages.create_console_security_alert_table(diff)
assert [row[2] for row in table.rows] == ["", "", ""]
def test_security_comment_includes_patched_version_when_available():
diff = Diff(
new_alerts=[
_issue(props={"firstPatchedVersionIdentifier": "1.5.0"}),
],
diff_url="https://socket.dev/dashboard/org/acme/diff/before/after",
)
comment = Messages.security_comment_template(diff)
assert "<strong>Patched version:</strong> <code>1.5.0</code>" in comment
def test_security_comment_omits_missing_patched_version():
diff = Diff(
new_alerts=[_issue(props={})],
diff_url="https://socket.dev/dashboard/org/acme/diff/before/after",
)
comment = Messages.security_comment_template(diff)
assert "Patched version:" not in comment
class _LinkConfig:
scm = "github"
branch = "main"
repository_url = "https://github.com/acme/widgets"
def test_manifest_link_uses_the_full_repository_url():
url = Messages.get_manifest_file_url(Diff(), "app/package.json", _LinkConfig())
assert url == "https://github.com/acme/widgets/blob/main/app/package.json"
def test_manifest_link_falls_back_to_the_report_without_a_repository_url():
"""A bare slug has no owner, so the link goes to the Socket report instead."""
config = _LinkConfig()
config.repository_url = None
diff = Diff(report_url="https://socket.dev/report/123")
url = Messages.get_manifest_file_url(diff, "app/package.json", config)
assert url == "https://socket.dev/report/123?tab=files&file=app%2Fpackage.json"
def test_manifest_link_does_not_guess_the_scm_from_the_diff_url():
"""Without an explicit scm, a GitHub-looking diff_url falls back to the report."""
class _NoScmConfig:
branch = "main"
repository_url = "https://github.com/acme/widgets"
diff = Diff(
diff_url="https://github.com/acme/widgets/pull/1",
report_url="https://socket.dev/report/123",
)
url = Messages.get_manifest_file_url(diff, "app/package.json", _NoScmConfig())
assert url == "https://socket.dev/report/123?tab=files&file=app%2Fpackage.json"