Skip to content

Commit

Permalink
Add safe domain names (joke2k#1209)
Browse files Browse the repository at this point in the history
* Add safe_domain_name() to Internet Provider

* Apply DRY to safe domain generation

* Add a test for safe email generation
  • Loading branch information
crd authored and fcurella committed Aug 4, 2020
1 parent b9ca6f3 commit 4cc3dd5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
16 changes: 7 additions & 9 deletions faker/providers/internet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class _IPv4Constants:


class Provider(BaseProvider):
safe_email_tlds = ('org', 'com', 'net')
safe_domain_names = ('example.org', 'example.com', 'example.net')
free_email_domains = ('gmail.com', 'yahoo.com', 'hotmail.com')
tlds = (
'com', 'com', 'com', 'com', 'com', 'com', 'biz', 'info', 'net', 'org',
Expand Down Expand Up @@ -127,11 +127,13 @@ def email(self, domain=None):
email = "".join(self.generator.parse(pattern).split(" "))
return email

@lowercase
def safe_domain_name(self):
return self.random_element(self.safe_domain_names)

@lowercase
def safe_email(self):
return '{}@example.{}'.format(
self.user_name(), self.random_element(self.safe_email_tlds),
)
return self.user_name() + '@' + self.safe_domain_name()

@lowercase
def free_email(self):
Expand All @@ -154,11 +156,7 @@ def ascii_email(self):

@lowercase
def ascii_safe_email(self):
return self._to_ascii(
self.user_name() +
'@example.' +
self.random_element(self.safe_email_tlds),
)
return self._to_ascii(self.user_name() + '@' + self.safe_domain_name())

@lowercase
def ascii_free_email(self):
Expand Down
12 changes: 12 additions & 0 deletions tests/providers/test_internet.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ def test_email_with_domain(self, faker):
email = faker.email(domain=domain)
assert email.split('@')[1] == domain

def test_safe_email(self, faker, num_samples):
expected_domains = ['example.com', 'example.org', 'example.net']
for _ in range(num_samples):
email = faker.safe_email()
assert email.split('@')[1] in expected_domains

def test_safe_domain_names(self, faker, num_samples):
expected_domains = ['example.com', 'example.org', 'example.net']
for _ in range(num_samples):
safe_domain_name = faker.safe_domain_name()
assert safe_domain_name in expected_domains

@patch(
'faker.providers.internet.Provider.image_placeholder_services',
{'https://dummyimage.com/{width}x{height}'},
Expand Down

0 comments on commit 4cc3dd5

Please sign in to comment.