Skip to content

Commit 79fb19c

Browse files
authored
PYTHON-4256 OIDC - Convert two unified tests to prose tests (mongodb#1612)
1 parent a053a6d commit 79fb19c

File tree

2 files changed

+79
-168
lines changed

2 files changed

+79
-168
lines changed

test/auth/unified/mongodb-oidc-no-retry.json

Lines changed: 0 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -416,173 +416,6 @@
416416
}
417417
}
418418
]
419-
},
420-
{
421-
"description": "Read commands should fail if reauthentication fails",
422-
"operations": [
423-
{
424-
"name": "find",
425-
"object": "collection0",
426-
"arguments": {
427-
"filter": {}
428-
},
429-
"expectResult": []
430-
},
431-
{
432-
"name": "failPoint",
433-
"object": "testRunner",
434-
"arguments": {
435-
"client": "failPointClient",
436-
"failPoint": {
437-
"configureFailPoint": "failCommand",
438-
"mode": {
439-
"times": 2
440-
},
441-
"data": {
442-
"failCommands": [
443-
"find",
444-
"saslStart"
445-
],
446-
"errorCode": 391
447-
}
448-
}
449-
}
450-
},
451-
{
452-
"name": "find",
453-
"object": "collection0",
454-
"arguments": {
455-
"filter": {}
456-
},
457-
"expectError": {
458-
"errorCode": 391
459-
}
460-
}
461-
],
462-
"expectEvents": [
463-
{
464-
"client": "client0",
465-
"events": [
466-
{
467-
"commandStartedEvent": {
468-
"command": {
469-
"find": "collName",
470-
"filter": {}
471-
}
472-
}
473-
},
474-
{
475-
"commandSucceededEvent": {
476-
"commandName": "find"
477-
}
478-
},
479-
{
480-
"commandStartedEvent": {
481-
"command": {
482-
"find": "collName",
483-
"filter": {}
484-
}
485-
}
486-
},
487-
{
488-
"commandFailedEvent": {
489-
"commandName": "find"
490-
}
491-
}
492-
]
493-
}
494-
]
495-
},
496-
{
497-
"description": "Write commands should fail if reauthentication fails",
498-
"operations": [
499-
{
500-
"name": "insertOne",
501-
"object": "collection0",
502-
"arguments": {
503-
"document": {
504-
"_id": 1,
505-
"x": 1
506-
}
507-
}
508-
},
509-
{
510-
"name": "failPoint",
511-
"object": "testRunner",
512-
"arguments": {
513-
"client": "failPointClient",
514-
"failPoint": {
515-
"configureFailPoint": "failCommand",
516-
"mode": {
517-
"times": 2
518-
},
519-
"data": {
520-
"failCommands": [
521-
"insert",
522-
"saslStart"
523-
],
524-
"errorCode": 391
525-
}
526-
}
527-
}
528-
},
529-
{
530-
"name": "insertOne",
531-
"object": "collection0",
532-
"arguments": {
533-
"document": {
534-
"_id": 2,
535-
"x": 2
536-
}
537-
},
538-
"expectError": {
539-
"errorCode": 391
540-
}
541-
}
542-
],
543-
"expectEvents": [
544-
{
545-
"client": "client0",
546-
"events": [
547-
{
548-
"commandStartedEvent": {
549-
"command": {
550-
"insert": "collName",
551-
"documents": [
552-
{
553-
"_id": 1,
554-
"x": 1
555-
}
556-
]
557-
}
558-
}
559-
},
560-
{
561-
"commandSucceededEvent": {
562-
"commandName": "insert"
563-
}
564-
},
565-
{
566-
"commandStartedEvent": {
567-
"command": {
568-
"insert": "collName",
569-
"documents": [
570-
{
571-
"_id": 2,
572-
"x": 2
573-
}
574-
]
575-
}
576-
}
577-
},
578-
{
579-
"commandFailedEvent": {
580-
"commandName": "insert"
581-
}
582-
}
583-
]
584-
}
585-
]
586419
}
587420
]
588421
}

test/auth_oidc/test_auth_oidc.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ def test_3_3_unexpected_error_code_does_not_clear_cache(self):
920920
# Close the client.
921921
client.close()
922922

923-
def test_4_reauthentication(self):
923+
def test_4_1_reauthentication_succeds(self):
924924
# Create a ``MongoClient`` configured with a custom OIDC callback that
925925
# implements the provider logic.
926926
client = self.create_client()
@@ -942,6 +942,84 @@ def test_4_reauthentication(self):
942942
# Close the client.
943943
client.close()
944944

945+
def test_4_2_read_commands_fail_if_reauthentication_fails(self):
946+
# Create a ``MongoClient`` whose OIDC callback returns one good token and then
947+
# bad tokens after the first call.
948+
get_token = self.get_token
949+
950+
class CustomCallback(OIDCCallback):
951+
count = 0
952+
953+
def fetch(self, _):
954+
self.count += 1
955+
if self.count == 1:
956+
access_token = get_token()
957+
else:
958+
access_token = "bad value"
959+
return OIDCCallbackResult(access_token=access_token)
960+
961+
callback = CustomCallback()
962+
client = self.create_client(request_cb=callback)
963+
964+
# Perform a read operation that succeeds.
965+
client.test.test.find_one()
966+
967+
# Set a fail point for the find command.
968+
with self.fail_point(
969+
{
970+
"mode": {"times": 1},
971+
"data": {"failCommands": ["find"], "errorCode": 391},
972+
}
973+
):
974+
# Perform a ``find`` operation that fails.
975+
with self.assertRaises(OperationFailure):
976+
client.test.test.find_one()
977+
978+
# Verify that the callback was called 2 times.
979+
self.assertEqual(callback.count, 2)
980+
981+
# Close the client.
982+
client.close()
983+
984+
def test_4_3_write_commands_fail_if_reauthentication_fails(self):
985+
# Create a ``MongoClient`` whose OIDC callback returns one good token and then
986+
# bad token after the first call.
987+
get_token = self.get_token
988+
989+
class CustomCallback(OIDCCallback):
990+
count = 0
991+
992+
def fetch(self, _):
993+
self.count += 1
994+
if self.count == 1:
995+
access_token = get_token()
996+
else:
997+
access_token = "bad value"
998+
return OIDCCallbackResult(access_token=access_token)
999+
1000+
callback = CustomCallback()
1001+
client = self.create_client(request_cb=callback)
1002+
1003+
# Perform an insert operation that succeeds.
1004+
client.test.test.insert_one({})
1005+
1006+
# Set a fail point for the find command.
1007+
with self.fail_point(
1008+
{
1009+
"mode": {"times": 1},
1010+
"data": {"failCommands": ["insert"], "errorCode": 391},
1011+
}
1012+
):
1013+
# Perform a ``insert`` operation that fails.
1014+
with self.assertRaises(OperationFailure):
1015+
client.test.test.insert_one({})
1016+
1017+
# Verify that the callback was called 2 times.
1018+
self.assertEqual(callback.count, 2)
1019+
1020+
# Close the client.
1021+
client.close()
1022+
9451023
def test_5_1_azure_with_no_username(self):
9461024
if ENVIRON != "azure":
9471025
raise unittest.SkipTest("Test is only supported on Azure")

0 commit comments

Comments
 (0)