aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_config.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/test_config.py b/tests/test_config.py
index 1815993..f62960b 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -281,5 +281,63 @@ class TestConfig(unittest.TestCase):
)
+class ConfiguredDestinations(unittest.TestCase):
+ def _load(self, text):
+ with tempfile.TemporaryDirectory() as tmp:
+ path = pathlib.Path(tmp) / "config.toml"
+ general = '[general]\ntrusted_relays = ["192.0.2.0/24"]\n'
+ # A bare top-level assignment (no leading "[") is only valid TOML
+ # before the first table header, so it must precede [general]
+ # rather than follow it.
+ combined = text + general if text and not text.startswith("[") \
+ else general + text
+ path.write_text(combined)
+ return config.load(path)
+
+ def test_no_sections_means_nothing_configured(self):
+ self.assertEqual(self._load("").destinations, set())
+
+ def test_a_vendor_with_a_key_is_configured(self):
+ settings = self._load('[abusedb]\napi_key = "k"\n')
+ self.assertEqual(settings.destinations, {"abusedb"})
+
+ def test_a_vendor_section_with_no_key_is_not_configured(self):
+ # Absent reads as not-configured, which is the whole convention.
+ self.assertEqual(self._load("[urlhaus]\n").destinations, set())
+
+ def test_an_empty_key_is_not_configured(self):
+ # api_key = "" is the trap this file exists to refuse: it reads as
+ # configured-and-broken and produces an auth error much later.
+ self.assertEqual(self._load('[urlhaus]\napi_key = ""\n').destinations,
+ set())
+
+ def test_misp_needs_both_halves(self):
+ both = '[misp]\nurl = "https://misp.example.invalid"\napi_key = "k"\n'
+ self.assertEqual(self._load(both).destinations, {"misp"})
+
+ def test_half_a_misp_pair_is_reported_separately_not_configured(self):
+ # Not an exception here: parse and contacts load this same file and
+ # read neither key. report refuses instead, with a sentence.
+ settings = self._load('[misp]\nurl = "https://misp.example.invalid"\n')
+ self.assertEqual(settings.destinations, set())
+ self.assertEqual(settings.incomplete, {"misp": "api_key"})
+
+ def test_the_other_half_is_reported_too(self):
+ settings = self._load('[misp]\napi_key = "k"\n')
+ self.assertEqual(settings.destinations, set())
+ self.assertEqual(settings.incomplete, {"misp": "url"})
+
+ def test_a_non_string_key_is_rejected_naming_the_file(self):
+ with self.assertRaises(ValueError) as caught:
+ self._load("[abusedb]\napi_key = 42\n")
+ self.assertIn("abusedb.api_key", str(caught.exception))
+ self.assertIn("config.toml", str(caught.exception))
+
+ def test_a_non_table_section_is_rejected(self):
+ with self.assertRaises(ValueError) as caught:
+ self._load('abusedb = "nope"\n')
+ self.assertIn("abusedb", str(caught.exception))
+
+
if __name__ == "__main__":
unittest.main()