Skip to content

Commit 58c1d2f

Browse files
committed
fixup tests
1 parent 24f9fa7 commit 58c1d2f

File tree

1 file changed

+3
-38
lines changed

1 file changed

+3
-38
lines changed

test/auth_oidc/test_auth_oidc.py

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,9 @@ def create_client(self, *args, **kwargs):
763763
kwargs["retryReads"] = False
764764
if not len(args):
765765
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
767769

768770
def test_1_1_callback_is_called_during_reauthentication(self):
769771
# Create a ``MongoClient`` configured with a custom OIDC callback that
@@ -773,8 +775,6 @@ def test_1_1_callback_is_called_during_reauthentication(self):
773775
client.test.test.find_one()
774776
# Assert that the callback was called 1 time.
775777
self.assertEqual(self.request_called, 1)
776-
# Close the client.
777-
client.close()
778778

779779
def test_1_2_callback_is_called_once_for_multiple_connections(self):
780780
# Create a ``MongoClient`` configured with a custom OIDC callback that
@@ -795,8 +795,6 @@ def target():
795795
thread.join()
796796
# Assert that the callback was called 1 time.
797797
self.assertEqual(self.request_called, 1)
798-
# Close the client.
799-
client.close()
800798

801799
def test_2_1_valid_callback_inputs(self):
802800
# 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):
805803
client.test.test.find_one()
806804
# Assert that the OIDC callback was called with the appropriate inputs, including the timeout parameter if possible. Ensure that there are no unexpected fields.
807805
self.assertEqual(self.request_called, 1)
808-
# Close the client.
809-
client.close()
810806

811807
def test_2_2_oidc_callback_returns_null(self):
812808
# Create a MongoClient configured with an OIDC callback that returns null.
@@ -818,8 +814,6 @@ def fetch(self, a):
818814
# Perform a find operation that fails.
819815
with self.assertRaises(ValueError):
820816
client.test.test.find_one()
821-
# Close the client.
822-
client.close()
823817

824818
def test_2_3_oidc_callback_returns_missing_data(self):
825819
# 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):
834828
# Perform a find operation that fails.
835829
with self.assertRaises(ValueError):
836830
client.test.test.find_one()
837-
# Close the client.
838-
client.close()
839831

840832
def test_2_4_invalid_client_configuration_with_callback(self):
841833
# 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
875867
client.test.test.find_one()
876868
# Verify that the callback was called 1 time.
877869
self.assertEqual(self.request_called, 1)
878-
# Close the client.
879-
client.close()
880870

881871
def test_3_2_authentication_failures_without_cached_tokens_returns_an_error(self):
882872
# 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):
894884
client.test.test.find_one()
895885
# Verify that the callback was called 1 time.
896886
self.assertEqual(callback.count, 1)
897-
# Close the client.
898-
client.close()
899887

900888
def test_3_3_unexpected_error_code_does_not_clear_cache(self):
901889
# 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):
921909
# Assert that the callback has been called once.
922910
self.assertEqual(self.request_called, 1)
923911

924-
# Close the client.
925-
client.close()
926-
927912
def test_4_1_reauthentication_succeds(self):
928913
# Create a ``MongoClient`` configured with a custom OIDC callback that
929914
# implements the provider logic.
@@ -943,9 +928,6 @@ def test_4_1_reauthentication_succeds(self):
943928
# handshake, and again during reauthentication).
944929
self.assertEqual(self.request_called, 2)
945930

946-
# Close the client.
947-
client.close()
948-
949931
def test_4_2_read_commands_fail_if_reauthentication_fails(self):
950932
# Create a ``MongoClient`` whose OIDC callback returns one good token and then
951933
# bad tokens after the first call.
@@ -982,9 +964,6 @@ def fetch(self, _):
982964
# Verify that the callback was called 2 times.
983965
self.assertEqual(callback.count, 2)
984966

985-
# Close the client.
986-
client.close()
987-
988967
def test_4_3_write_commands_fail_if_reauthentication_fails(self):
989968
# Create a ``MongoClient`` whose OIDC callback returns one good token and then
990969
# bad token after the first call.
@@ -1021,9 +1000,6 @@ def fetch(self, _):
10211000
# Verify that the callback was called 2 times.
10221001
self.assertEqual(callback.count, 2)
10231002

1024-
# Close the client.
1025-
client.close()
1026-
10271003
def test_4_4_speculative_authentication_should_be_ignored_on_reauthentication(self):
10281004
# Create an OIDC configured client that can listen for `SaslStart` commands.
10291005
listener = OvertCommandListener()
@@ -1066,9 +1042,6 @@ def test_4_4_speculative_authentication_should_be_ignored_on_reauthentication(se
10661042
# Assert there were `SaslStart` commands executed.
10671043
assert any(event.command_name.lower() == "saslstart" for event in listener.started_events)
10681044

1069-
# Close the client.
1070-
client.close()
1071-
10721045
def test_5_1_azure_with_no_username(self):
10731046
if ENVIRON != "azure":
10741047
raise unittest.SkipTest("Test is only supported on Azure")
@@ -1078,7 +1051,6 @@ def test_5_1_azure_with_no_username(self):
10781051
props = dict(TOKEN_RESOURCE=resource, ENVIRONMENT="azure")
10791052
client = self.create_client(authMechanismProperties=props)
10801053
client.test.test.find_one()
1081-
client.close()
10821054

10831055
def test_5_2_azure_with_bad_username(self):
10841056
if ENVIRON != "azure":
@@ -1091,7 +1063,6 @@ def test_5_2_azure_with_bad_username(self):
10911063
client = self.create_client(username="bad", authmechanismproperties=props)
10921064
with self.assertRaises(ValueError):
10931065
client.test.test.find_one()
1094-
client.close()
10951066

10961067
def test_speculative_auth_success(self):
10971068
client1 = self.create_client()
@@ -1113,10 +1084,6 @@ def test_speculative_auth_success(self):
11131084
# Perform a find operation.
11141085
client2.test.test.find_one()
11151086

1116-
# Close the clients.
1117-
client2.close()
1118-
client1.close()
1119-
11201087
def test_reauthentication_succeeds_multiple_connections(self):
11211088
client1 = self.create_client()
11221089
client2 = self.create_client()
@@ -1156,8 +1123,6 @@ def test_reauthentication_succeeds_multiple_connections(self):
11561123
client2.test.test.find_one()
11571124

11581125
self.assertEqual(self.request_called, 3)
1159-
client1.close()
1160-
client2.close()
11611126

11621127

11631128
if __name__ == "__main__":

0 commit comments

Comments
 (0)