|
40 | 40 | from pymongo.errors import AutoReconnect, ConfigurationError, OperationFailure
|
41 | 41 | from pymongo.hello import HelloCompat
|
42 | 42 | 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 |
48 | 44 | from pymongo.uri_parser import parse_uri
|
49 | 45 |
|
50 | 46 | ROOT = Path(__file__).parent.parent.resolve()
|
@@ -1019,6 +1015,51 @@ def fetch(self, _):
|
1019 | 1015 | # Close the client.
|
1020 | 1016 | client.close()
|
1021 | 1017 |
|
| 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 | + |
1022 | 1063 | def test_5_1_azure_with_no_username(self):
|
1023 | 1064 | if ENVIRON != "azure":
|
1024 | 1065 | raise unittest.SkipTest("Test is only supported on Azure")
|
|
0 commit comments