|
| 1 | +""" |
| 2 | +Unit tests for lenient SocketCategory parsing in SocketAlert.from_dict. |
| 3 | +
|
| 4 | +Regression coverage for |
| 5 | +https://github.com/SocketDev/socket-sdk-python/issues/78: the Socket API can |
| 6 | +emit category values the SDK does not yet know about (e.g. ``"other"``). Strict |
| 7 | +enum parsing turned that into a hard failure that took down every consumer |
| 8 | +(notably socketsecurity CI runs) whenever a diff included one of those alerts. |
| 9 | +
|
| 10 | +These tests pin the fallback behavior so the SDK stays forward-compatible with |
| 11 | +new server-side categories. |
| 12 | +""" |
| 13 | + |
| 14 | +import logging |
| 15 | +import unittest |
| 16 | + |
| 17 | +from socketdev.fullscans import SocketAlert, SocketCategory, SocketIssueSeverity |
| 18 | + |
| 19 | + |
| 20 | +class TestSocketAlertCategoryParsing(unittest.TestCase): |
| 21 | + """SocketAlert.from_dict should tolerate unknown category values.""" |
| 22 | + |
| 23 | + def _base_payload(self, category: str) -> dict: |
| 24 | + return { |
| 25 | + "key": "alert-key", |
| 26 | + "type": "someAlertType", |
| 27 | + "severity": "low", |
| 28 | + "category": category, |
| 29 | + } |
| 30 | + |
| 31 | + def test_known_category_is_preserved(self): |
| 32 | + alert = SocketAlert.from_dict(self._base_payload("supplyChainRisk")) |
| 33 | + self.assertEqual(alert.category, SocketCategory.SUPPLY_CHAIN_RISK) |
| 34 | + self.assertEqual(alert.severity, SocketIssueSeverity.LOW) |
| 35 | + |
| 36 | + def test_unknown_category_falls_back_to_miscellaneous(self): |
| 37 | + alert = SocketAlert.from_dict(self._base_payload("other")) |
| 38 | + self.assertEqual(alert.category, SocketCategory.MISCELLANEOUS) |
| 39 | + |
| 40 | + def test_unknown_category_does_not_raise(self): |
| 41 | + # Explicit regression assertion: no ValueError for brand-new categories. |
| 42 | + try: |
| 43 | + SocketAlert.from_dict(self._base_payload("somethingCompletelyNew")) |
| 44 | + except ValueError as exc: |
| 45 | + self.fail(f"SocketAlert.from_dict raised ValueError for unknown category: {exc}") |
| 46 | + |
| 47 | + def test_unknown_category_emits_warning(self): |
| 48 | + with self.assertLogs("socketdev", level=logging.WARNING) as captured: |
| 49 | + SocketAlert.from_dict(self._base_payload("other")) |
| 50 | + self.assertTrue( |
| 51 | + any("Unknown SocketCategory" in message for message in captured.output), |
| 52 | + f"expected a warning about the unknown category, got: {captured.output}", |
| 53 | + ) |
| 54 | + |
| 55 | + def test_every_known_category_round_trips(self): |
| 56 | + for category in SocketCategory: |
| 57 | + with self.subTest(category=category): |
| 58 | + alert = SocketAlert.from_dict(self._base_payload(category.value)) |
| 59 | + self.assertEqual(alert.category, category) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + unittest.main() |
0 commit comments