Skip to content

PYTHON-4256 OIDC - Convert two unified tests to prose tests #1612

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 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 0 additions & 167 deletions test/auth/unified/mongodb-oidc-no-retry.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,173 +416,6 @@
}
}
]
},
{
"description": "Read commands should fail if reauthentication fails",
"operations": [
{
"name": "find",
"object": "collection0",
"arguments": {
"filter": {}
},
"expectResult": []
},
{
"name": "failPoint",
"object": "testRunner",
"arguments": {
"client": "failPointClient",
"failPoint": {
"configureFailPoint": "failCommand",
"mode": {
"times": 2
},
"data": {
"failCommands": [
"find",
"saslStart"
],
"errorCode": 391
}
}
}
},
{
"name": "find",
"object": "collection0",
"arguments": {
"filter": {}
},
"expectError": {
"errorCode": 391
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"find": "collName",
"filter": {}
}
}
},
{
"commandSucceededEvent": {
"commandName": "find"
}
},
{
"commandStartedEvent": {
"command": {
"find": "collName",
"filter": {}
}
}
},
{
"commandFailedEvent": {
"commandName": "find"
}
}
]
}
]
},
{
"description": "Write commands should fail if reauthentication fails",
"operations": [
{
"name": "insertOne",
"object": "collection0",
"arguments": {
"document": {
"_id": 1,
"x": 1
}
}
},
{
"name": "failPoint",
"object": "testRunner",
"arguments": {
"client": "failPointClient",
"failPoint": {
"configureFailPoint": "failCommand",
"mode": {
"times": 2
},
"data": {
"failCommands": [
"insert",
"saslStart"
],
"errorCode": 391
}
}
}
},
{
"name": "insertOne",
"object": "collection0",
"arguments": {
"document": {
"_id": 2,
"x": 2
}
},
"expectError": {
"errorCode": 391
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"insert": "collName",
"documents": [
{
"_id": 1,
"x": 1
}
]
}
}
},
{
"commandSucceededEvent": {
"commandName": "insert"
}
},
{
"commandStartedEvent": {
"command": {
"insert": "collName",
"documents": [
{
"_id": 2,
"x": 2
}
]
}
}
},
{
"commandFailedEvent": {
"commandName": "insert"
}
}
]
}
]
}
]
}
80 changes: 79 additions & 1 deletion test/auth_oidc/test_auth_oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ def test_3_3_unexpected_error_code_does_not_clear_cache(self):
# Close the client.
client.close()

def test_4_reauthentication(self):
def test_4_1_reauthentication_succeds(self):
# Create a ``MongoClient`` configured with a custom OIDC callback that
# implements the provider logic.
client = self.create_client()
Expand All @@ -942,6 +942,84 @@ def test_4_reauthentication(self):
# Close the client.
client.close()

def test_4_2_read_commands_fail_if_reauthentication_fails(self):
# Create a ``MongoClient`` whose OIDC callback returns one good token and then
# bad tokens after the first call.
get_token = self.get_token

class CustomCallback(OIDCCallback):
count = 0

def fetch(self, _):
self.count += 1
if self.count == 1:
access_token = get_token()
else:
access_token = "bad value"
return OIDCCallbackResult(access_token=access_token)

callback = CustomCallback()
client = self.create_client(request_cb=callback)

# Perform a read operation that succeeds.
client.test.test.find_one()

# Set a fail point for the find command.
with self.fail_point(
{
"mode": {"times": 1},
"data": {"failCommands": ["find"], "errorCode": 391},
}
):
# Perform a ``find`` operation that fails.
with self.assertRaises(OperationFailure):
client.test.test.find_one()

# Verify that the callback was called 2 times.
self.assertEqual(callback.count, 2)

# Close the client.
client.close()

def test_4_3_write_commands_fail_if_reauthentication_fails(self):
# Create a ``MongoClient`` whose OIDC callback returns one good token and then
# bad token after the first call.
get_token = self.get_token

class CustomCallback(OIDCCallback):
count = 0

def fetch(self, _):
self.count += 1
if self.count == 1:
access_token = get_token()
else:
access_token = "bad value"
return OIDCCallbackResult(access_token=access_token)

callback = CustomCallback()
client = self.create_client(request_cb=callback)

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

# Set a fail point for the find command.
with self.fail_point(
{
"mode": {"times": 1},
"data": {"failCommands": ["insert"], "errorCode": 391},
}
):
# Perform a ``insert`` operation that fails.
with self.assertRaises(OperationFailure):
client.test.test.insert_one({})

# Verify that the callback was called 2 times.
self.assertEqual(callback.count, 2)

# 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