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
@@ -186,12 +187,6 @@ def create_tests(cls):
186
187
187
188
class TestParsingErrors (AsyncPyMongoTestCase ):
188
189
async def test_invalid_host (self ):
189
- with self .assertRaisesRegex (ConfigurationError , "Invalid URI host: mongodb is not" ):
190
- client = self .simple_client ("mongodb+srv://mongodb" )
191
- await client .aconnect ()
192
- with self .assertRaisesRegex (ConfigurationError , "Invalid URI host: mongodb.com is not" ):
193
- client = self .simple_client ("mongodb+srv://mongodb.com" )
194
- await client .aconnect ()
195
190
with self .assertRaisesRegex (ConfigurationError , "Invalid URI host: an IP address is not" ):
196
191
client = self .simple_client ("mongodb+srv://127.0.0.1" )
197
192
await client .aconnect ()
@@ -207,5 +202,93 @@ async def test_connect_case_insensitive(self):
207
202
self .assertGreater (len (client .topology_description .server_descriptions ()), 1 )
208
203
209
204
205
+ class TestInitialDnsSeedlistDiscovery (AsyncPyMongoTestCase ):
206
+ """
207
+ Initial DNS Seedlist Discovery prose tests
208
+ https://github.com/mongodb/specifications/blob/0a7a8b5/source/initial-dns-seedlist-discovery/tests/README.md#prose-tests
209
+ """
210
+
211
+ async def run_initial_dns_seedlist_discovery_prose_tests (self , test_cases ):
212
+ for case in test_cases :
213
+ with patch ("dns.asyncresolver.resolve" ) as mock_resolver :
214
+
215
+ async def mock_resolve (query , record_type , * args , ** kwargs ):
216
+ mock_srv = MagicMock ()
217
+ mock_srv .target .to_text .return_value = case ["mock_target" ]
218
+ return [mock_srv ]
219
+
220
+ mock_resolver .side_effect = mock_resolve
221
+ domain = case ["query" ].split ("._tcp." )[1 ]
222
+ connection_string = f"mongodb+srv://{ domain } "
223
+ try :
224
+ await parse_uri (connection_string )
225
+ except ConfigurationError as e :
226
+ self .assertIn (case ["expected_error" ], str (e ))
227
+ else :
228
+ self .fail (f"ConfigurationError was not raised for query: { case ['query' ]} " )
229
+
230
+ async def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts (self ):
231
+ with patch ("dns.asyncresolver.resolve" ):
232
+ await parse_uri ("mongodb+srv://localhost/" )
233
+ await parse_uri ("mongodb+srv://mongo.local/" )
234
+
235
+ async def test_2_throw_when_return_address_does_not_end_with_srv_domain (self ):
236
+ test_cases = [
237
+ {
238
+ "query" : "_mongodb._tcp.localhost" ,
239
+ "mock_target" : "localhost.mongodb" ,
240
+ "expected_error" : "Invalid SRV host" ,
241
+ },
242
+ {
243
+ "query" : "_mongodb._tcp.blogs.mongodb.com" ,
244
+ "mock_target" : "blogs.evil.com" ,
245
+ "expected_error" : "Invalid SRV host" ,
246
+ },
247
+ {
248
+ "query" : "_mongodb._tcp.blogs.mongo.local" ,
249
+ "mock_target" : "test_1.evil.com" ,
250
+ "expected_error" : "Invalid SRV host" ,
251
+ },
252
+ ]
253
+ await self .run_initial_dns_seedlist_discovery_prose_tests (test_cases )
254
+
255
+ async def test_3_throw_when_return_address_is_identical_to_srv_hostname (self ):
256
+ test_cases = [
257
+ {
258
+ "query" : "_mongodb._tcp.localhost" ,
259
+ "mock_target" : "localhost" ,
260
+ "expected_error" : "Invalid SRV host" ,
261
+ },
262
+ {
263
+ "query" : "_mongodb._tcp.mongo.local" ,
264
+ "mock_target" : "mongo.local" ,
265
+ "expected_error" : "Invalid SRV host" ,
266
+ },
267
+ ]
268
+ await self .run_initial_dns_seedlist_discovery_prose_tests (test_cases )
269
+
270
+ async def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part_of_domain (
271
+ self
272
+ ):
273
+ test_cases = [
274
+ {
275
+ "query" : "_mongodb._tcp.localhost" ,
276
+ "mock_target" : "test_1.cluster_1localhost" ,
277
+ "expected_error" : "Invalid SRV host" ,
278
+ },
279
+ {
280
+ "query" : "_mongodb._tcp.mongo.local" ,
281
+ "mock_target" : "test_1.my_hostmongo.local" ,
282
+ "expected_error" : "Invalid SRV host" ,
283
+ },
284
+ {
285
+ "query" : "_mongodb._tcp.blogs.mongodb.com" ,
286
+ "mock_target" : "cluster.testmongodb.com" ,
287
+ "expected_error" : "Invalid SRV host" ,
288
+ },
289
+ ]
290
+ await self .run_initial_dns_seedlist_discovery_prose_tests (test_cases )
291
+
292
+
210
293
if __name__ == "__main__" :
211
294
unittest .main ()
0 commit comments