@@ -763,7 +763,9 @@ def create_client(self, *args, **kwargs):
763
763
kwargs ["retryReads" ] = False
764
764
if not len (args ):
765
765
args = [self .uri_single ]
766
- return MongoClient (* args , authmechanismproperties = props , ** kwargs )
766
+ client = MongoClient (* args , authmechanismproperties = props , ** kwargs )
767
+ self .addCleanup (client .close )
768
+ return client
767
769
768
770
def test_1_1_callback_is_called_during_reauthentication (self ):
769
771
# Create a ``MongoClient`` configured with a custom OIDC callback that
@@ -773,8 +775,6 @@ def test_1_1_callback_is_called_during_reauthentication(self):
773
775
client .test .test .find_one ()
774
776
# Assert that the callback was called 1 time.
775
777
self .assertEqual (self .request_called , 1 )
776
- # Close the client.
777
- client .close ()
778
778
779
779
def test_1_2_callback_is_called_once_for_multiple_connections (self ):
780
780
# Create a ``MongoClient`` configured with a custom OIDC callback that
@@ -795,8 +795,6 @@ def target():
795
795
thread .join ()
796
796
# Assert that the callback was called 1 time.
797
797
self .assertEqual (self .request_called , 1 )
798
- # Close the client.
799
- client .close ()
800
798
801
799
def test_2_1_valid_callback_inputs (self ):
802
800
# Create a MongoClient configured with an OIDC callback that validates its inputs and returns a valid access token.
@@ -805,8 +803,6 @@ def test_2_1_valid_callback_inputs(self):
805
803
client .test .test .find_one ()
806
804
# Assert that the OIDC callback was called with the appropriate inputs, including the timeout parameter if possible. Ensure that there are no unexpected fields.
807
805
self .assertEqual (self .request_called , 1 )
808
- # Close the client.
809
- client .close ()
810
806
811
807
def test_2_2_oidc_callback_returns_null (self ):
812
808
# Create a MongoClient configured with an OIDC callback that returns null.
@@ -818,8 +814,6 @@ def fetch(self, a):
818
814
# Perform a find operation that fails.
819
815
with self .assertRaises (ValueError ):
820
816
client .test .test .find_one ()
821
- # Close the client.
822
- client .close ()
823
817
824
818
def test_2_3_oidc_callback_returns_missing_data (self ):
825
819
# Create a MongoClient configured with an OIDC callback that returns data not conforming to the OIDCCredential with missing fields.
@@ -834,8 +828,6 @@ def fetch(self, a):
834
828
# Perform a find operation that fails.
835
829
with self .assertRaises (ValueError ):
836
830
client .test .test .find_one ()
837
- # Close the client.
838
- client .close ()
839
831
840
832
def test_2_4_invalid_client_configuration_with_callback (self ):
841
833
# Create a MongoClient configured with an OIDC callback and auth mechanism property ENVIRONMENT:test.
@@ -875,8 +867,6 @@ def test_3_1_authentication_failure_with_cached_tokens_fetch_a_new_token_and_ret
875
867
client .test .test .find_one ()
876
868
# Verify that the callback was called 1 time.
877
869
self .assertEqual (self .request_called , 1 )
878
- # Close the client.
879
- client .close ()
880
870
881
871
def test_3_2_authentication_failures_without_cached_tokens_returns_an_error (self ):
882
872
# Create a MongoClient configured with retryReads=false and an OIDC callback that always returns invalid access tokens.
@@ -894,8 +884,6 @@ def fetch(self, a):
894
884
client .test .test .find_one ()
895
885
# Verify that the callback was called 1 time.
896
886
self .assertEqual (callback .count , 1 )
897
- # Close the client.
898
- client .close ()
899
887
900
888
def test_3_3_unexpected_error_code_does_not_clear_cache (self ):
901
889
# Create a ``MongoClient`` with a human callback that returns a valid token
@@ -921,9 +909,6 @@ def test_3_3_unexpected_error_code_does_not_clear_cache(self):
921
909
# Assert that the callback has been called once.
922
910
self .assertEqual (self .request_called , 1 )
923
911
924
- # Close the client.
925
- client .close ()
926
-
927
912
def test_4_1_reauthentication_succeds (self ):
928
913
# Create a ``MongoClient`` configured with a custom OIDC callback that
929
914
# implements the provider logic.
@@ -943,9 +928,6 @@ def test_4_1_reauthentication_succeds(self):
943
928
# handshake, and again during reauthentication).
944
929
self .assertEqual (self .request_called , 2 )
945
930
946
- # Close the client.
947
- client .close ()
948
-
949
931
def test_4_2_read_commands_fail_if_reauthentication_fails (self ):
950
932
# Create a ``MongoClient`` whose OIDC callback returns one good token and then
951
933
# bad tokens after the first call.
@@ -982,9 +964,6 @@ def fetch(self, _):
982
964
# Verify that the callback was called 2 times.
983
965
self .assertEqual (callback .count , 2 )
984
966
985
- # Close the client.
986
- client .close ()
987
-
988
967
def test_4_3_write_commands_fail_if_reauthentication_fails (self ):
989
968
# Create a ``MongoClient`` whose OIDC callback returns one good token and then
990
969
# bad token after the first call.
@@ -1021,9 +1000,6 @@ def fetch(self, _):
1021
1000
# Verify that the callback was called 2 times.
1022
1001
self .assertEqual (callback .count , 2 )
1023
1002
1024
- # Close the client.
1025
- client .close ()
1026
-
1027
1003
def test_4_4_speculative_authentication_should_be_ignored_on_reauthentication (self ):
1028
1004
# Create an OIDC configured client that can listen for `SaslStart` commands.
1029
1005
listener = OvertCommandListener ()
@@ -1066,9 +1042,6 @@ def test_4_4_speculative_authentication_should_be_ignored_on_reauthentication(se
1066
1042
# Assert there were `SaslStart` commands executed.
1067
1043
assert any (event .command_name .lower () == "saslstart" for event in listener .started_events )
1068
1044
1069
- # Close the client.
1070
- client .close ()
1071
-
1072
1045
def test_5_1_azure_with_no_username (self ):
1073
1046
if ENVIRON != "azure" :
1074
1047
raise unittest .SkipTest ("Test is only supported on Azure" )
@@ -1078,7 +1051,6 @@ def test_5_1_azure_with_no_username(self):
1078
1051
props = dict (TOKEN_RESOURCE = resource , ENVIRONMENT = "azure" )
1079
1052
client = self .create_client (authMechanismProperties = props )
1080
1053
client .test .test .find_one ()
1081
- client .close ()
1082
1054
1083
1055
def test_5_2_azure_with_bad_username (self ):
1084
1056
if ENVIRON != "azure" :
@@ -1091,7 +1063,6 @@ def test_5_2_azure_with_bad_username(self):
1091
1063
client = self .create_client (username = "bad" , authmechanismproperties = props )
1092
1064
with self .assertRaises (ValueError ):
1093
1065
client .test .test .find_one ()
1094
- client .close ()
1095
1066
1096
1067
def test_speculative_auth_success (self ):
1097
1068
client1 = self .create_client ()
@@ -1113,10 +1084,6 @@ def test_speculative_auth_success(self):
1113
1084
# Perform a find operation.
1114
1085
client2 .test .test .find_one ()
1115
1086
1116
- # Close the clients.
1117
- client2 .close ()
1118
- client1 .close ()
1119
-
1120
1087
def test_reauthentication_succeeds_multiple_connections (self ):
1121
1088
client1 = self .create_client ()
1122
1089
client2 = self .create_client ()
@@ -1156,8 +1123,6 @@ def test_reauthentication_succeeds_multiple_connections(self):
1156
1123
client2 .test .test .find_one ()
1157
1124
1158
1125
self .assertEqual (self .request_called , 3 )
1159
- client1 .close ()
1160
- client2 .close ()
1161
1126
1162
1127
1163
1128
if __name__ == "__main__" :
0 commit comments