Skip to content

Commit c7f4fef

Browse files
committed
Separate and rename tests to match prose
1 parent abd9eb7 commit c7f4fef

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

test/test_uri_parser.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,12 +554,15 @@ def test_port_with_whitespace(self):
554554
with self.assertRaisesRegex(ValueError, r"Port contains whitespace character: '\\n'"):
555555
parse_uri("mongodb://localhost:27\n017")
556556

557-
def test_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self):
557+
# Initial DNS Seedlist Discovery prose tests
558+
# https://github.com/mongodb/specifications/blob/0a7a8b5/source/initial-dns-seedlist-discovery/tests/README.md#prose-tests
559+
560+
def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self):
558561
with patch("dns.resolver.resolve"):
559562
parse_uri("mongodb+srv://localhost/")
560563
parse_uri("mongodb+srv://mongo.local/")
561564

562-
def test_error_when_return_address_does_not_end_with_srv_domain(self):
565+
def test_2_throw_when_return_address_does_not_end_with_srv_domain(self):
563566
test_cases = [
564567
{
565568
"query": "_mongodb._tcp.localhost",
@@ -576,6 +579,27 @@ def test_error_when_return_address_does_not_end_with_srv_domain(self):
576579
"mock_target": "test_1.evil.com",
577580
"expected_error": "Invalid SRV host",
578581
},
582+
]
583+
for case in test_cases:
584+
with patch("dns.resolver.resolve") as mock_resolver:
585+
586+
def mock_resolve(query, record_type, *args, **kwargs):
587+
mock_srv = MagicMock()
588+
mock_srv.target.to_text.return_value = case["mock_target"]
589+
return [mock_srv]
590+
591+
mock_resolver.side_effect = mock_resolve
592+
domain = case["query"].split("._tcp.")[1]
593+
connection_string = f"mongodb+srv://{domain}"
594+
try:
595+
parse_uri(connection_string)
596+
except ConfigurationError as e:
597+
self.assertIn(case["expected_error"], str(e))
598+
else:
599+
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
600+
601+
def test_3_throw_when_return_address_is_identical_to_srv_hostname(self):
602+
test_cases = [
579603
{
580604
"query": "_mongodb._tcp.localhost",
581605
"mock_target": "localhost",
@@ -586,6 +610,28 @@ def test_error_when_return_address_does_not_end_with_srv_domain(self):
586610
"mock_target": "mongo.local",
587611
"expected_error": "Invalid SRV host",
588612
},
613+
]
614+
for case in test_cases:
615+
with patch("dns.resolver.resolve") as mock_resolver:
616+
617+
def mock_resolve(query, record_type, *args, **kwargs):
618+
mock_srv = MagicMock()
619+
mock_srv.target.to_text.return_value = case["mock_target"]
620+
return [mock_srv]
621+
622+
mock_resolver.side_effect = mock_resolve
623+
domain = case["query"].split("._tcp.")[1]
624+
connection_string = f"mongodb+srv://{domain}"
625+
try:
626+
parse_uri(connection_string)
627+
except ConfigurationError as e:
628+
self.assertIn(case["expected_error"], str(e))
629+
else:
630+
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
631+
632+
def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part_of_domain(self):
633+
test_cases = [
634+
589635
{
590636
"query": "_mongodb._tcp.localhost",
591637
"mock_target": "test_1.cluster_1localhost",

0 commit comments

Comments
 (0)