45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
import unittest
|
|
|
|
from sanitize import assert_publishable, sanitize_text
|
|
|
|
|
|
class SanitizeTests(unittest.TestCase):
|
|
def test_redacts_private_identifiers_and_credentials(self):
|
|
raw = """Connect to 10.1.2.3 or fd00::12 as admin@example.test.
|
|
SSH root@private.internal and use password=FakeSecret123.
|
|
Read /Users/example/private/config and call https://hidden.internal/api.
|
|
Phone +1 555 123 4567; token: abcdefghijklmnop.
|
|
"""
|
|
cleaned = sanitize_text(raw, allowed_hosts={"anwarpy.ai"})
|
|
for secret in (
|
|
"10.1.2.3",
|
|
"fd00::12",
|
|
"admin@example.test",
|
|
"root@private.internal",
|
|
"FakeSecret123",
|
|
"/Users/example/private/config",
|
|
"hidden.internal",
|
|
"+1 555 123 4567",
|
|
"abcdefghijklmnop",
|
|
):
|
|
self.assertNotIn(secret, cleaned)
|
|
assert_publishable(cleaned, allowed_hosts={"anwarpy.ai"})
|
|
|
|
def test_preserves_approved_public_urls(self):
|
|
text = "راجع https://anwarpy.ai/posts/example للمزيد."
|
|
self.assertEqual(sanitize_text(text, allowed_hosts={"anwarpy.ai"}), text)
|
|
assert_publishable(text, allowed_hosts={"anwarpy.ai"})
|
|
|
|
def test_redacts_unapproved_public_hosts(self):
|
|
cleaned = sanitize_text("Source: https://unapproved.example/article", allowed_hosts={"anwarpy.ai"})
|
|
self.assertNotIn("unapproved.example", cleaned)
|
|
assert_publishable(cleaned, allowed_hosts={"anwarpy.ai"})
|
|
|
|
def test_assert_publishable_raises_without_using_assert_statement(self):
|
|
with self.assertRaises(ValueError):
|
|
assert_publishable("internal address 192.168.1.10", allowed_hosts={"anwarpy.ai"})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|