def test_does_not_filter_invalid_emails(self): text_with_invalid_emails = [ 'Contact me here: [email protected]', 'And more: [email protected]', '*****@*****.**', '[email protected]', 'Whaa? [email protected]', ] for text in text_with_invalid_emails: self.assertEqual(text, solution.PrivacyFilter(text).filtered()) self.assertEqual(text, solution.PrivacyFilter(text).filtered()) self.assertEqual(text, self.filter_email_usernames(text)) self.assertEqual(text, self.partially_filter_email_usernames(text))
def test_does_not_filter_invalid_phone_numbers(self): invalid_phone_numbers = { 'Reach me at: 0885123': 'Reach me at: [PHONE]', '0005551234569': '0005551234569', '+1555 123, 55555': '+1555 123, 55555', '95551212': '95551212', } for text, filtered in invalid_phone_numbers.items(): self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
def test_separates_preserved_country_code_from_filtered_phone_with_a_space(self): filtered_phones_with_a_space = { 'Phone: 0025 (55) 12 12255': 'Phone: 0025 [FILTERED]', 'Phone: 0025(55) 12 12255': 'Phone: 0025 [FILTERED]', 'Phone: +25( 55 )12 12255': 'Phone: +25 [FILTERED]', } for text, filtered in filtered_phones_with_a_space.items(): filter = solution.PrivacyFilter(text) filter.preserve_phone_country_code = True self.assertEqual(filtered, filter.filtered())
def test_filters_more_complex_phone_numbers(self): more_complex_phone_numbers = { 'Reach me at: 0885123123': 'Reach me at: [PHONE]', '+155512345699': '[PHONE]', '+1 555 123-456': '[PHONE]', '+1 (555) 123-456-99': '[PHONE]', '004412125543': '[PHONE]', '0044 1 21 25 543': '[PHONE]', } for text, filtered in more_complex_phone_numbers.items(): self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
def test_obfuscates_more_complicated_emails(self): complicated_emails = { '*****@*****.**': '[EMAIL]', '*****@*****.**': '[EMAIL]', '*****@*****.**': '[EMAIL]', '*****@*****.**': '[EMAIL]', '*****@*****.**': '[EMAIL]', 'Contact:[email protected]': 'Contact:[EMAIL]', 'Contact: [email protected],[email protected]': 'Contact: [EMAIL],[EMAIL]', } for text, filtered in complicated_emails.items(): self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
def test_allows_country_code_to_be_preserved_for_internationally_formatted_phone_numbers(self): internationally_formatted_phone_numbers = { 'Phone: +359 2 555-1212': 'Phone: +359 [FILTERED]', 'Phone: +35925551212': 'Phone: +359 [FILTERED]', 'Phone: 08825551212': 'Phone: [PHONE]', 'Phone: 0 88 255-512 12 !': 'Phone: [PHONE] !', 'Phone: 0025 5512 12255': 'Phone: 0025 [FILTERED]', } for text, filtered in internationally_formatted_phone_numbers.items(): filter = solution.PrivacyFilter(text) filter.preserve_phone_country_code = True self.assertEqual(filtered, filter.filtered())
def test_filters_more_than_one_phone_or_email(self): text = """ Contacts Phones:_+1_(555) 123-456-99 or 004412125543 Email: [email protected] or [email protected] """ filtered = """ Contacts Phones:_+1_(555) 123-456-99 or [PHONE] Email: [EMAIL] or [EMAIL] """ self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
def test_preserves_whitespace_around_phones(self): self.assertEqual(' [PHONE] or...', solution.PrivacyFilter(' +359881212-12-1 2 or...').filtered())
def filter_email_usernames(self, text): filter = solution.PrivacyFilter(text) filter.preserve_email_hostname = True return filter.filtered()
def test_obfuscates_simple_emails(self): self.assertEqual('Contact: [EMAIL]', solution.PrivacyFilter('Contact: [email protected]').filtered())
def test_works_with_blank_or_whitespace_strings_and_preserves_whitespace(self): self.assertEqual('', solution.PrivacyFilter('').filtered()) self.assertEqual(' ', solution.PrivacyFilter(' ').filtered()) self.assertEqual(" \n \n ", solution.PrivacyFilter(" \n \n ").filtered())
def partially_filter_email_usernames(self, text): filter = solution.PrivacyFilter(text) filter.partially_preserve_email_username = True return filter.filtered()