46
46
ROOT = Path (__file__ ).parent .parent .resolve ()
47
47
TEST_PATH = ROOT / "auth" / "unified"
48
48
ENVIRON = os .environ .get ("OIDC_ENV" , "test" )
49
-
49
+ DOMAIN = os .environ .get ("OIDC_DOMAIN" , "" )
50
+ TOKEN_DIR = os .environ .get ("OIDC_TOKEN_DIR" , "" )
51
+ TOKEN_FILE = os .environ .get ("OIDC_TOKEN_FILE" , "" )
50
52
51
53
# Generate unified tests.
52
54
globals ().update (generate_test_classes (str (TEST_PATH ), module = __name__ ))
@@ -56,7 +58,7 @@ class OIDCTestBase(unittest.TestCase):
56
58
@classmethod
57
59
def setUpClass (cls ):
58
60
cls .uri_single = os .environ ["MONGODB_URI_SINGLE" ]
59
- cls .uri_multiple = os .environ [ "MONGODB_URI_MULTI" ]
61
+ cls .uri_multiple = os .environ . get ( "MONGODB_URI_MULTI" )
60
62
cls .uri_admin = os .environ ["MONGODB_URI" ]
61
63
62
64
def setUp (self ):
@@ -65,8 +67,10 @@ def setUp(self):
65
67
def get_token (self , username = None ):
66
68
"""Get a token for the current provider."""
67
69
if ENVIRON == "test" :
68
- token_dir = os .environ ["OIDC_TOKEN_DIR" ]
69
- token_file = os .path .join (token_dir , username ).replace (os .sep , "/" )
70
+ if username is None :
71
+ token_file = TOKEN_FILE
72
+ else :
73
+ token_file = os .path .join (TOKEN_DIR , username )
70
74
with open (token_file ) as fid :
71
75
return fid .read ()
72
76
elif ENVIRON == "azure" :
@@ -95,6 +99,8 @@ class TestAuthOIDCHuman(OIDCTestBase):
95
99
def setUpClass (cls ):
96
100
if ENVIRON != "test" :
97
101
raise unittest .SkipTest ("Human workflows are only tested with the test environment" )
102
+ if DOMAIN is None :
103
+ raise ValueError ("Missing OIDC_DOMAIN" )
98
104
super ().setUpClass ()
99
105
100
106
def create_request_cb (self , username = "test_user1" , sleep = 0 ):
@@ -121,11 +127,14 @@ def fetch(self, context):
121
127
122
128
def create_client (self , * args , ** kwargs ):
123
129
username = kwargs .get ("username" , "test_user1" )
130
+ if kwargs .get ("username" ):
131
+ kwargs ["username" ] = f"{ username } @{ DOMAIN } "
124
132
request_cb = kwargs .pop ("request_cb" , self .create_request_cb (username = username ))
125
133
props = kwargs .pop ("authmechanismproperties" , {"OIDC_HUMAN_CALLBACK" : request_cb })
126
134
kwargs ["retryReads" ] = False
127
135
if not len (args ):
128
136
args = [self .uri_single ]
137
+
129
138
return MongoClient (* args , authmechanismproperties = props , ** kwargs )
130
139
131
140
def test_1_1_single_principal_implicit_username (self ):
@@ -145,6 +154,8 @@ def test_1_2_single_principal_explicit_username(self):
145
154
client .close ()
146
155
147
156
def test_1_3_multiple_principal_user_1 (self ):
157
+ if not self .uri_multiple :
158
+ raise unittest .SkipTest ("Test Requires Server with Multiple Workflow IdPs" )
148
159
# Create a client with MONGODB_URI_MULTI, a username of test_user1, authMechanism=MONGODB-OIDC, and the OIDC human callback.
149
160
client = self .create_client (self .uri_multiple , username = "test_user1" )
150
161
# Perform a find operation that succeeds.
@@ -153,6 +164,8 @@ def test_1_3_multiple_principal_user_1(self):
153
164
client .close ()
154
165
155
166
def test_1_4_multiple_principal_user_2 (self ):
167
+ if not self .uri_multiple :
168
+ raise unittest .SkipTest ("Test Requires Server with Multiple Workflow IdPs" )
156
169
# Create a human callback that reads in the generated test_user2 token file.
157
170
# Create a client with MONGODB_URI_MULTI, a username of test_user2, authMechanism=MONGODB-OIDC, and the OIDC human callback.
158
171
client = self .create_client (self .uri_multiple , username = "test_user2" )
@@ -162,6 +175,8 @@ def test_1_4_multiple_principal_user_2(self):
162
175
client .close ()
163
176
164
177
def test_1_5_multiple_principal_no_user (self ):
178
+ if not self .uri_multiple :
179
+ raise unittest .SkipTest ("Test Requires Server with Multiple Workflow IdPs" )
165
180
# Create a client with MONGODB_URI_MULTI, no username, authMechanism=MONGODB-OIDC, and the OIDC human callback.
166
181
client = self .create_client (self .uri_multiple )
167
182
# Assert that a find operation fails.
@@ -632,15 +647,8 @@ class TestAuthOIDCMachine(OIDCTestBase):
632
647
633
648
def setUp (self ):
634
649
self .request_called = 0
635
- if ENVIRON == "test" :
636
- self .default_username = "test_user1"
637
- else :
638
- self .default_username = None
639
650
640
651
def create_request_cb (self , username = None , sleep = 0 ):
641
- if username is None :
642
- username = self .default_username
643
-
644
652
def request_token (context ):
645
653
assert isinstance (context .timeout_seconds , int )
646
654
assert context .version == 1
0 commit comments