-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_socket_alert_category.py
More file actions
63 lines (49 loc) · 2.48 KB
/
test_socket_alert_category.py
File metadata and controls
63 lines (49 loc) · 2.48 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
"""
Unit tests for lenient SocketCategory parsing in SocketAlert.from_dict.
Regression coverage for
https://github.com/SocketDev/socket-sdk-python/issues/78: the Socket API can
emit category values the SDK does not yet know about (e.g. ``"other"``). Strict
enum parsing turned that into a hard failure that took down every consumer
(notably socketsecurity CI runs) whenever a diff included one of those alerts.
These tests pin the fallback behavior so the SDK stays forward-compatible with
new server-side categories.
"""
import logging
import unittest
from socketdev.fullscans import SocketAlert, SocketCategory, SocketIssueSeverity
class TestSocketAlertCategoryParsing(unittest.TestCase):
"""SocketAlert.from_dict should tolerate unknown category values."""
def _base_payload(self, category: str) -> dict:
return {
"key": "alert-key",
"type": "someAlertType",
"severity": "low",
"category": category,
}
def test_known_category_is_preserved(self):
alert = SocketAlert.from_dict(self._base_payload("supplyChainRisk"))
self.assertEqual(alert.category, SocketCategory.SUPPLY_CHAIN_RISK)
self.assertEqual(alert.severity, SocketIssueSeverity.LOW)
def test_unknown_category_falls_back_to_miscellaneous(self):
alert = SocketAlert.from_dict(self._base_payload("other"))
self.assertEqual(alert.category, SocketCategory.MISCELLANEOUS)
def test_unknown_category_does_not_raise(self):
# Explicit regression assertion: no ValueError for brand-new categories.
try:
SocketAlert.from_dict(self._base_payload("somethingCompletelyNew"))
except ValueError as exc:
self.fail(f"SocketAlert.from_dict raised ValueError for unknown category: {exc}")
def test_unknown_category_emits_warning(self):
with self.assertLogs("socketdev", level=logging.WARNING) as captured:
SocketAlert.from_dict(self._base_payload("other"))
self.assertTrue(
any("Unknown SocketCategory" in message for message in captured.output),
f"expected a warning about the unknown category, got: {captured.output}",
)
def test_every_known_category_round_trips(self):
for category in SocketCategory:
with self.subTest(category=category):
alert = SocketAlert.from_dict(self._base_payload(category.value))
self.assertEqual(alert.category, category)
if __name__ == "__main__":
unittest.main()