Skip to content

Commit b8d7a4e

Browse files
committed
Added a few more tests to improve code coverage
1 parent 6bc4589 commit b8d7a4e

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

tests/mocked-dns-answers.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
"type": "A",
130130
"class": "IN"
131131
},
132-
"answer": []
132+
"answer": ["127.0.0.1"]
133133
},
134134
{
135135
"query": {
@@ -139,6 +139,30 @@
139139
},
140140
"answer": []
141141
},
142+
{
143+
"query": {
144+
"name": "ipv6only.joshdata.me",
145+
"type": "MX",
146+
"class": "IN"
147+
},
148+
"answer": []
149+
},
150+
{
151+
"query": {
152+
"name": "ipv6only.joshdata.me",
153+
"type": "A",
154+
"class": "IN"
155+
},
156+
"answer": []
157+
},
158+
{
159+
"query": {
160+
"name": "ipv6only.joshdata.me",
161+
"type": "AAAA",
162+
"class": "IN"
163+
},
164+
"answer": ["::1"]
165+
},
142166
{
143167
"query": {
144168
"name": "mail.example",

tests/test_deliverability.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_deliverability_found(domain: str, expected_response: str) -> None:
3535
# No MX or A/AAAA records, but some other DNS records must
3636
# exist such that the response is NOANSWER instead of NXDOMAIN.
3737
('justtxt.joshdata.me', 'The domain name {domain} does not accept email'),
38+
('ipv6only.joshdata.me', 'The domain name {domain} does not accept email'),
3839
],
3940
)
4041
def test_deliverability_fails(domain: str, error: str) -> None:
@@ -65,6 +66,11 @@ def test_deliverability_dns_timeout() -> None:
6566
assert response.get("unknown-deliverability") == "timeout"
6667

6768

69+
def test_timeout_and_resolver() -> None:
70+
with pytest.raises(ValueError, match="It's not valid to pass both timeout and dns_resolver."):
71+
validate_email_deliverability('timeout.com', 'timeout.com', timeout=1, dns_resolver=RESOLVER)
72+
73+
6874
@pytest.mark.network
6975
def test_caching_dns_resolver() -> None:
7076
class TestCache:
@@ -84,3 +90,4 @@ def put(self, key: Any, value: Any) -> Any:
8490

8591
validate_email("test@gmail.com", dns_resolver=resolver)
8692
assert len(cache.cache) == 1
93+

tests/test_main.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def test_dict_accessor() -> None:
1515
assert isinstance(valid_email.as_dict(), dict)
1616
assert valid_email.as_dict()["original"] == input_email
1717

18+
def test_dict_accessor_with_domain_address() -> None:
19+
input_email = "me@[127.0.0.1]"
20+
valid_email = validate_email(input_email, check_deliverability=False, allow_domain_literal=True)
21+
assert valid_email.domain == "[127.0.0.1]"
22+
assert isinstance(valid_email.as_dict(), dict)
23+
assert valid_email.as_dict()["domain_address"] == '"IPv4Address(\'127.0.0.1\')"'
24+
1825

1926
def test_main_single_good_input(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None:
2027
import json
@@ -65,3 +72,19 @@ def test_deprecation() -> None:
6572
valid_email = validate_email(input_email, check_deliverability=False)
6673
with pytest.deprecated_call():
6774
assert valid_email.email is not None
75+
76+
@pytest.mark.parametrize('invalid_email', [
77+
None,
78+
12345,
79+
[],
80+
{},
81+
lambda x: x,
82+
])
83+
def test_invalid_type(invalid_email) -> None:
84+
with pytest.raises(TypeError, match="email must be str or bytes"):
85+
validate_email(invalid_email, check_deliverability=False)
86+
87+
def test_invalid_ascii() -> None:
88+
invalid_email = b'\xd0\xba\xd0\xb2\xd1\x96\xd1\x82\xd0\xbe\xd1\x87\xd0\xba\xd0\xb0@\xd0\xbf\xd0\xbe\xd1\x88\xd1\x82\xd0\xb0.test'
89+
with pytest.raises(EmailSyntaxError, match="The email address is not valid ASCII."):
90+
validate_email(invalid_email, check_deliverability=False)

0 commit comments

Comments
 (0)