Files

43 lines
2.1 KiB
Python
Raw Permalink Normal View History

2026-09-03 11:15:54 +02:00
import unittest
from app.contact_extractor import extract_contacts
class ContactExtractorTests(unittest.TestCase):
def test_extracts_public_mailto_obfuscated_phone_and_ignores_false_positives(self):
html = '''<html><body>
<a href="mailto:sales@acme.test">Email Sales</a>
<span>info [at] acme.test</span><span>+27 (12) 345-6789</span>
<a href="https://wa.me/27123456789">WhatsApp</a>
<script>const token = 'abc@example.com';</script>
<img src="https://cdn.thirdparty.test/x?email=bad@thirdparty.test">
<span>john@example.com</span>
</body></html>'''
result = extract_contacts(html, 'https://acme.test/contact')
values = {(x['kind'], x['value']) for x in result}
self.assertIn(('email', 'sales@acme.test'), values)
self.assertIn(('email', 'info@acme.test'), values)
self.assertIn(('phone', '+27123456789'), values)
self.assertIn(('whatsapp', '+27123456789'), values)
self.assertNotIn(('email', 'abc@example.com'), values)
self.assertNotIn(('email', 'bad@thirdparty.test'), values)
self.assertNotIn(('email', 'john@example.com'), values)
def test_excludes_credentials_adjacent_to_email_like_values(self):
result = extract_contacts('<p>API key: foo@acme.test password: bar@acme.test</p>', 'https://acme.test/')
self.assertEqual(result, [])
def test_classifies_role_named_free_mail_and_suppression(self):
html = '<p>support@acme.test alice@acme.test bob@gmail.com</p>'
result = extract_contacts(html, 'https://acme.test/', suppressions=[{'kind':'email','value':'support@acme.test'}])
by = {x['value']: x for x in result}
self.assertEqual(by['support@acme.test']['classification'], 'role')
self.assertTrue(by['support@acme.test']['do_not_contact'])
self.assertTrue(by['support@acme.test']['suppressed'])
self.assertEqual(by['alice@acme.test']['classification'], 'named')
self.assertEqual(by['bob@gmail.com']['classification'], 'free_mail')
self.assertEqual(by['alice@acme.test']['mx_status'], 'unknown')
if __name__ == '__main__':
unittest.main()