Skip to content

Commit 7ee08dd

Browse files
blink1073Jibola
andauthored
PYTHON-4672 Clarify Reauthentication and Speculative Authentication combination behavior (#1802)
Co-authored-by: Jib <[email protected]>
1 parent 4eae7d2 commit 7ee08dd

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

test/auth_oidc/test_auth_oidc.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@
4040
from pymongo.errors import AutoReconnect, ConfigurationError, OperationFailure
4141
from pymongo.hello import HelloCompat
4242
from pymongo.operations import InsertOne
43-
from pymongo.synchronous.auth_oidc import (
44-
OIDCCallback,
45-
OIDCCallbackContext,
46-
OIDCCallbackResult,
47-
)
43+
from pymongo.synchronous.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult
4844
from pymongo.uri_parser import parse_uri
4945

5046
ROOT = Path(__file__).parent.parent.resolve()
@@ -1019,6 +1015,51 @@ def fetch(self, _):
10191015
# Close the client.
10201016
client.close()
10211017

1018+
def test_4_4_speculative_authentication_should_be_ignored_on_reauthentication(self):
1019+
# Create an OIDC configured client that can listen for `SaslStart` commands.
1020+
listener = EventListener()
1021+
client = self.create_client(event_listeners=[listener])
1022+
1023+
# Preload the *Client Cache* with a valid access token to enforce Speculative Authentication.
1024+
client2 = self.create_client()
1025+
client2.test.test.find_one()
1026+
client.options.pool_options._credentials.cache.data = (
1027+
client2.options.pool_options._credentials.cache.data
1028+
)
1029+
client2.close()
1030+
self.request_called = 0
1031+
1032+
# Perform an `insert` operation that succeeds.
1033+
client.test.test.insert_one({})
1034+
1035+
# Assert that the callback was not called.
1036+
self.assertEqual(self.request_called, 0)
1037+
1038+
# Assert there were no `SaslStart` commands executed.
1039+
assert not any(
1040+
event.command_name.lower() == "saslstart" for event in listener.started_events
1041+
)
1042+
listener.reset()
1043+
1044+
# Set a fail point for `insert` commands of the form:
1045+
with self.fail_point(
1046+
{
1047+
"mode": {"times": 1},
1048+
"data": {"failCommands": ["insert"], "errorCode": 391},
1049+
}
1050+
):
1051+
# Perform an `insert` operation that succeeds.
1052+
client.test.test.insert_one({})
1053+
1054+
# Assert that the callback was called once.
1055+
self.assertEqual(self.request_called, 1)
1056+
1057+
# Assert there were `SaslStart` commands executed.
1058+
assert any(event.command_name.lower() == "saslstart" for event in listener.started_events)
1059+
1060+
# Close the client.
1061+
client.close()
1062+
10221063
def test_5_1_azure_with_no_username(self):
10231064
if ENVIRON != "azure":
10241065
raise unittest.SkipTest("Test is only supported on Azure")

0 commit comments

Comments
 (0)