@@ -104,6 +104,119 @@ def test_get_authentication_obtain_client_credentials(self, mock_request):
104104 )
105105 rest_client .close ()
106106
107+ @patch .object (rest .RESTClientObject , "request" )
108+ def test_get_authentication_obtain_client_credentials_with_scopes_list (self , mock_request ):
109+ """
110+ Test getting authentication header when method is client credentials with scopes as list
111+ """
112+ response_body = """
113+ {
114+ "expires_in": 120,
115+ "access_token": "AABBCCDD"
116+ }
117+ """
118+ mock_request .return_value = mock_response (response_body , 200 )
119+
120+ credentials = Credentials (
121+ method = "client_credentials" ,
122+ configuration = CredentialConfiguration (
123+ client_id = "myclientid" ,
124+ client_secret = "mysecret" ,
125+ api_issuer = "issuer.fga.example" ,
126+ api_audience = "myaudience" ,
127+ scopes = ["read" , "write" , "admin" ],
128+ ),
129+ )
130+ rest_client = rest .RESTClientObject (Configuration ())
131+ current_time = datetime .now ()
132+ client = OAuth2Client (credentials )
133+ auth_header = client .get_authentication_header (rest_client )
134+ self .assertEqual (auth_header , {"Authorization" : "Bearer AABBCCDD" })
135+ self .assertEqual (client ._access_token , "AABBCCDD" )
136+ self .assertGreaterEqual (
137+ client ._access_expiry_time , current_time + timedelta (seconds = 120 )
138+ )
139+ expected_header = urllib3 .response .HTTPHeaderDict (
140+ {
141+ "Accept" : "application/json" ,
142+ "Content-Type" : "application/x-www-form-urlencoded" ,
143+ "User-Agent" : "openfga-sdk (python) 0.9.5" ,
144+ }
145+ )
146+ mock_request .assert_called_once_with (
147+ method = "POST" ,
148+ url = "https://issuer.fga.example/oauth/token" ,
149+ headers = expected_header ,
150+ query_params = None ,
151+ body = None ,
152+ _preload_content = True ,
153+ _request_timeout = None ,
154+ post_params = {
155+ "client_id" : "myclientid" ,
156+ "client_secret" : "mysecret" ,
157+ "audience" : "myaudience" ,
158+ "grant_type" : "client_credentials" ,
159+ "scope" : "read write admin" ,
160+ },
161+ )
162+ rest_client .close ()
163+
164+ @patch .object (rest .RESTClientObject , "request" )
165+ def test_get_authentication_obtain_client_credentials_with_scopes_string (self , mock_request ):
166+ """
167+ Test getting authentication header when method is client credentials with scopes as string
168+ """
169+ response_body = """
170+ {
171+ "expires_in": 120,
172+ "access_token": "AABBCCDD"
173+ }
174+ """
175+ mock_request .return_value = mock_response (response_body , 200 )
176+
177+ credentials = Credentials (
178+ method = "client_credentials" ,
179+ configuration = CredentialConfiguration (
180+ client_id = "myclientid" ,
181+ client_secret = "mysecret" ,
182+ api_issuer = "issuer.fga.example" ,
183+ api_audience = "myaudience" ,
184+ scopes = "read write admin" ,
185+ ),
186+ )
187+ rest_client = rest .RESTClientObject (Configuration ())
188+ current_time = datetime .now ()
189+ client = OAuth2Client (credentials )
190+ auth_header = client .get_authentication_header (rest_client )
191+ self .assertEqual (auth_header , {"Authorization" : "Bearer AABBCCDD" })
192+ self .assertEqual (client ._access_token , "AABBCCDD" )
193+ self .assertGreaterEqual (
194+ client ._access_expiry_time , current_time + timedelta (seconds = 120 )
195+ )
196+ expected_header = urllib3 .response .HTTPHeaderDict (
197+ {
198+ "Accept" : "application/json" ,
199+ "Content-Type" : "application/x-www-form-urlencoded" ,
200+ "User-Agent" : "openfga-sdk (python) 0.9.5" ,
201+ }
202+ )
203+ mock_request .assert_called_once_with (
204+ method = "POST" ,
205+ url = "https://issuer.fga.example/oauth/token" ,
206+ headers = expected_header ,
207+ query_params = None ,
208+ body = None ,
209+ _preload_content = True ,
210+ _request_timeout = None ,
211+ post_params = {
212+ "client_id" : "myclientid" ,
213+ "client_secret" : "mysecret" ,
214+ "audience" : "myaudience" ,
215+ "grant_type" : "client_credentials" ,
216+ "scope" : "read write admin" ,
217+ },
218+ )
219+
107220 @patch .object (rest .RESTClientObject , "request" )
108221 def test_get_authentication_obtain_client_credentials_failed (self , mock_request ):
109222 """
0 commit comments