|
30 | 30 | unittest,
|
31 | 31 | )
|
32 | 32 | from test.utils_shared import async_wait_until
|
| 33 | +from unittest.mock import MagicMock, patch |
33 | 34 |
|
34 | 35 | from pymongo.asynchronous.uri_parser import parse_uri
|
35 | 36 | from pymongo.common import validate_read_preference_tags
|
|
39 | 40 | _IS_SYNC = False
|
40 | 41 |
|
41 | 42 |
|
| 43 | +def run_initial_dns_seedlist_discovery_prose_tests(self, test_cases): |
| 44 | + for case in test_cases: |
| 45 | + with patch("dns.resolver.resolve") as mock_resolver: |
| 46 | + |
| 47 | + def mock_resolve(query, record_type, *args, **kwargs): |
| 48 | + mock_srv = MagicMock() |
| 49 | + mock_srv.target.to_text.return_value = case["mock_target"] |
| 50 | + return [mock_srv] |
| 51 | + |
| 52 | + mock_resolver.side_effect = mock_resolve |
| 53 | + domain = case["query"].split("._tcp.")[1] |
| 54 | + connection_string = f"mongodb+srv://{domain}" |
| 55 | + try: |
| 56 | + parse_uri(connection_string) |
| 57 | + except ConfigurationError as e: |
| 58 | + self.assertIn(case["expected_error"], str(e)) |
| 59 | + else: |
| 60 | + self.fail(f"ConfigurationError was not raised for query: {case['query']}") |
| 61 | + |
| 62 | + |
42 | 63 | class TestDNSRepl(AsyncPyMongoTestCase):
|
43 | 64 | if _IS_SYNC:
|
44 | 65 | TEST_PATH = os.path.join(
|
@@ -201,5 +222,74 @@ async def test_connect_case_insensitive(self):
|
201 | 222 | self.assertGreater(len(client.topology_description.server_descriptions()), 1)
|
202 | 223 |
|
203 | 224 |
|
| 225 | +class TestInitialDnsSeedlistDiscovery(AsyncPyMongoTestCase): |
| 226 | + """ |
| 227 | + Initial DNS Seedlist Discovery prose tests |
| 228 | + https://github.com/mongodb/specifications/blob/0a7a8b5/source/initial-dns-seedlist-discovery/tests/README.md#prose-tests |
| 229 | + """ |
| 230 | + |
| 231 | + def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self): |
| 232 | + with patch("dns.resolver.resolve"): |
| 233 | + parse_uri("mongodb+srv://localhost/") |
| 234 | + parse_uri("mongodb+srv://mongo.local/") |
| 235 | + |
| 236 | + def test_2_throw_when_return_address_does_not_end_with_srv_domain(self): |
| 237 | + test_cases = [ |
| 238 | + { |
| 239 | + "query": "_mongodb._tcp.localhost", |
| 240 | + "mock_target": "localhost.mongodb", |
| 241 | + "expected_error": "Invalid SRV host", |
| 242 | + }, |
| 243 | + { |
| 244 | + "query": "_mongodb._tcp.blogs.mongodb.com", |
| 245 | + "mock_target": "blogs.evil.com", |
| 246 | + "expected_error": "Invalid SRV host", |
| 247 | + }, |
| 248 | + { |
| 249 | + "query": "_mongodb._tcp.blogs.mongo.local", |
| 250 | + "mock_target": "test_1.evil.com", |
| 251 | + "expected_error": "Invalid SRV host", |
| 252 | + }, |
| 253 | + ] |
| 254 | + run_initial_dns_seedlist_discovery_prose_tests(self, test_cases) |
| 255 | + |
| 256 | + def test_3_throw_when_return_address_is_identical_to_srv_hostname(self): |
| 257 | + test_cases = [ |
| 258 | + { |
| 259 | + "query": "_mongodb._tcp.localhost", |
| 260 | + "mock_target": "localhost", |
| 261 | + "expected_error": "Invalid SRV host", |
| 262 | + }, |
| 263 | + { |
| 264 | + "query": "_mongodb._tcp.mongo.local", |
| 265 | + "mock_target": "mongo.local", |
| 266 | + "expected_error": "Invalid SRV host", |
| 267 | + }, |
| 268 | + ] |
| 269 | + run_initial_dns_seedlist_discovery_prose_tests(self, test_cases) |
| 270 | + |
| 271 | + def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part_of_domain( |
| 272 | + self |
| 273 | + ): |
| 274 | + test_cases = [ |
| 275 | + { |
| 276 | + "query": "_mongodb._tcp.localhost", |
| 277 | + "mock_target": "test_1.cluster_1localhost", |
| 278 | + "expected_error": "Invalid SRV host", |
| 279 | + }, |
| 280 | + { |
| 281 | + "query": "_mongodb._tcp.mongo.local", |
| 282 | + "mock_target": "test_1.my_hostmongo.local", |
| 283 | + "expected_error": "Invalid SRV host", |
| 284 | + }, |
| 285 | + { |
| 286 | + "query": "_mongodb._tcp.blogs.mongodb.com", |
| 287 | + "mock_target": "cluster.testmongodb.com", |
| 288 | + "expected_error": "Invalid SRV host", |
| 289 | + }, |
| 290 | + ] |
| 291 | + run_initial_dns_seedlist_discovery_prose_tests(self, test_cases) |
| 292 | + |
| 293 | + |
204 | 294 | if __name__ == "__main__":
|
205 | 295 | unittest.main()
|
0 commit comments