Skip to content

PYTHON-4672 Clarify Reauthentication and Speculative Authentication combination behavior #1802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 23, 2024
51 changes: 46 additions & 5 deletions test/auth_oidc/test_auth_oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@
from pymongo.errors import AutoReconnect, ConfigurationError, OperationFailure
from pymongo.hello import HelloCompat
from pymongo.operations import InsertOne
from pymongo.synchronous.auth_oidc import (
OIDCCallback,
OIDCCallbackContext,
OIDCCallbackResult,
)
from pymongo.synchronous.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult
from pymongo.uri_parser import parse_uri

ROOT = Path(__file__).parent.parent.resolve()
Expand Down Expand Up @@ -1019,6 +1015,51 @@ def fetch(self, _):
# Close the client.
client.close()

def test_4_4_speculative_authentication_should_be_ignored_on_reauthentication(self):
# Create an OIDC configured client that can listen for `SaslStart` commands.
listener = EventListener()
client = self.create_client(event_listeners=[listener])

# Preload the *Client Cache* with a valid access token to enforce Speculative Authentication.
client2 = self.create_client()
client2.test.test.find_one()
client.options.pool_options._credentials.cache.data = (
client2.options.pool_options._credentials.cache.data
)
client2.close()
self.request_called = 0

# Perform an `insert` operation that succeeds.
client.test.test.insert_one({})

# Assert that the callback was not called.
self.assertEqual(self.request_called, 0)

# Assert there were no `SaslStart` commands executed.
assert not any(
event.command_name.lower() == "saslstart" for event in listener.started_events
)
listener.reset()

# Set a fail point for `insert` commands of the form:
with self.fail_point(
{
"mode": {"times": 1},
"data": {"failCommands": ["insert"], "errorCode": 391},
}
):
# Perform an `insert` operation that succeeds.
client.test.test.insert_one({})

# Assert that the callback was called once.
self.assertEqual(self.request_called, 1)

# Assert there were `SaslStart` commands executed.
assert any(event.command_name.lower() == "saslstart" for event in listener.started_events)

# Close the client.
client.close()

def test_5_1_azure_with_no_username(self):
if ENVIRON != "azure":
raise unittest.SkipTest("Test is only supported on Azure")
Expand Down
Loading