@@ -220,20 +220,24 @@ def setUp(self):
220220
221221 @patch .object (Authenticator , "conf" )
222222 @patch .object (Authenticator , "_configure_credentials" )
223- def test_setup_credentials_with_service_account (self , mock_configure_credentials , mock_conf ):
223+ def test_setup_credentials_with_service_account (
224+ self , mock_configure_credentials , mock_conf
225+ ):
224226 # Simulate `service_account` being set
225- mock_conf .return_value = ' service_account_value'
227+ mock_conf .return_value = " service_account_value"
226228
227229 self .authenticator ._setup_credentials ()
228230
229231 # Assert _configure_credentials was not called
230232 mock_configure_credentials .assert_not_called ()
231233 # Assert service_account is set correctly
232- self .assertEqual (self .authenticator .service_account , ' service_account_value' )
234+ self .assertEqual (self .authenticator .service_account , " service_account_value" )
233235
234236 @patch .object (Authenticator , "conf" )
235237 @patch .object (Authenticator , "_configure_credentials" )
236- def test_setup_credentials_without_service_account (self , mock_configure_credentials , mock_conf ):
238+ def test_setup_credentials_without_service_account (
239+ self , mock_configure_credentials , mock_conf
240+ ):
237241 # Simulate `service_account` not being set
238242 mock_conf .return_value = None
239243 mock_creds = Mock ()
@@ -283,7 +287,11 @@ def test_cleanup(self, mock_get_client):
283287 "test_domain" , "validation_name_test" , "validation_test"
284288 )
285289
286- @patch ("builtins.open" , new_callable = mock_open , read_data = '{"credentials": {"iss": "test_iss", "sub": "test_sub", "aud": "test_aud", "kid": "test_kid", "privateKey": "test_private_key"}}' )
290+ @patch (
291+ "builtins.open" ,
292+ new_callable = mock_open ,
293+ read_data = '{"credentials": {"iss": "test_iss", "sub": "test_sub", "aud": "test_aud", "kid": "test_kid", "privateKey": "test_private_key"}}' ,
294+ )
287295 @patch ("json.load" , lambda x : json .loads (x .read ()))
288296 def test_load_service_file (self , mock_load_service_file ):
289297 expected_credentials = {
@@ -307,70 +315,85 @@ def test_load_service_file_not_found(self, mock_log, mock_file):
307315 @patch ("jwt.encode" )
308316 def test_generate_jwt (self , mock_jwt_encode ):
309317 credentials = {
310- ' iss' : ' issuer' ,
311- ' sub' : ' subject' ,
312- ' aud' : ' audience' ,
313- ' kid' : ' key_id' ,
314- ' privateKey' : ' private_key'
318+ " iss" : " issuer" ,
319+ " sub" : " subject" ,
320+ " aud" : " audience" ,
321+ " kid" : " key_id" ,
322+ " privateKey" : " private_key" ,
315323 }
316324 self .authenticator ._generate_jwt (credentials )
317325 mock_jwt_encode .assert_called ()
318326
319327 def test_generate_jwt_fail (self ):
320328 credentials = {
321- ' iss' : ' issuer' ,
322- ' sub' : ' subject' ,
323- ' aud' : ' audience' ,
324- ' kid' : ' key_id' ,
325- ' privateKey' : ' not_a_valid_key'
329+ " iss" : " issuer" ,
330+ " sub" : " subject" ,
331+ " aud" : " audience" ,
332+ " kid" : " key_id" ,
333+ " privateKey" : " not_a_valid_key" ,
326334 }
327335 with self .assertRaises (jwt .exceptions .InvalidKeyError ):
328336 token = self .authenticator ._generate_jwt (credentials )
329337 self .assertIsNone (token )
330338
331- @patch (' requests.post' )
339+ @patch (" requests.post" )
332340 def test_request_access_token_success (self , mock_post ):
333341 mock_response = mock_post .return_value
334- mock_response .raise_for_status = lambda : None # Mock raise_for_status to do nothing
335- mock_response .json .return_value = {'access_token' : 'mocked_access_token' }
342+ mock_response .raise_for_status = (
343+ lambda : None
344+ ) # Mock raise_for_status to do nothing
345+ mock_response .json .return_value = {"access_token" : "mocked_access_token" }
336346
337- result = self .authenticator ._request_access_token (' jwt_token_example' )
347+ result = self .authenticator ._request_access_token (" jwt_token_example" )
338348
339349 # Assertions
340350 mock_post .assert_called_once_with (
341- 'https://service-account.api.stackit.cloud/token' ,
342- data = {'grant_type' : 'urn:ietf:params:oauth:grant-type:jwt-bearer' , 'assertion' : 'jwt_token_example' },
343- headers = {'Content-Type' : 'application/x-www-form-urlencoded' }
351+ "https://service-account.api.stackit.cloud/token" ,
352+ data = {
353+ "grant_type" : "urn:ietf:params:oauth:grant-type:jwt-bearer" ,
354+ "assertion" : "jwt_token_example" ,
355+ },
356+ headers = {"Content-Type" : "application/x-www-form-urlencoded" },
344357 )
345- self .assertEqual (result , ' mocked_access_token' )
358+ self .assertEqual (result , " mocked_access_token" )
346359
347- @patch (' requests.post' )
360+ @patch (" requests.post" )
348361 def test_request_access_token_failure_raises_http_error (self , mock_post ):
349362 mock_response = Response ()
350363 mock_response .status_code = 403
351364 mock_post .return_value = mock_response
352365 mock_response .raise_for_status = lambda : (_ for _ in ()).throw (HTTPError ())
353366
354367 with self .assertRaises (errors .PluginError ):
355- self .authenticator ._request_access_token (' jwt_token_example' )
368+ self .authenticator ._request_access_token (" jwt_token_example" )
356369
357370 mock_post .assert_called_once ()
358371
359- @patch ("builtins.open" , new_callable = mock_open , read_data = '{"credentials": {"iss": "test_iss", "sub": "test_sub", "aud": "test_aud", "kid": "test_kid", "privateKey": "test_private_key"}}' )
360- @patch .object (Authenticator , '_request_access_token' )
361- @patch .object (Authenticator , '_generate_jwt' )
362- @patch .object (Authenticator , '_load_service_file' )
363- def test_generate_jwt_token_success (self , mock_load_service_file , mock_generate_jwt , mock_request_access_token , mock_open ):
364- mock_load_service_file .return_value = {'dummy' : 'credentials' }
365- mock_generate_jwt .return_value = 'jwt_token_example'
366- mock_request_access_token .return_value = 'access_token_example'
367-
368- result = self .authenticator ._generate_jwt_token ('path/to/service/file' )
369-
370- self .assertEqual (result , 'access_token_example' )
371- mock_load_service_file .assert_called_once_with ('path/to/service/file' )
372- mock_generate_jwt .assert_called_once_with ({'dummy' : 'credentials' })
373- mock_request_access_token .assert_called_once_with ('jwt_token_example' )
372+ @patch (
373+ "builtins.open" ,
374+ new_callable = mock_open ,
375+ read_data = '{"credentials": {"iss": "test_iss", "sub": "test_sub", "aud": "test_aud", "kid": "test_kid", "privateKey": "test_private_key"}}' ,
376+ )
377+ @patch .object (Authenticator , "_request_access_token" )
378+ @patch .object (Authenticator , "_generate_jwt" )
379+ @patch .object (Authenticator , "_load_service_file" )
380+ def test_generate_jwt_token_success (
381+ self ,
382+ mock_load_service_file ,
383+ mock_generate_jwt ,
384+ mock_request_access_token ,
385+ mock_open ,
386+ ):
387+ mock_load_service_file .return_value = {"dummy" : "credentials" }
388+ mock_generate_jwt .return_value = "jwt_token_example"
389+ mock_request_access_token .return_value = "access_token_example"
390+
391+ result = self .authenticator ._generate_jwt_token ("path/to/service/file" )
392+
393+ self .assertEqual (result , "access_token_example" )
394+ mock_load_service_file .assert_called_once_with ("path/to/service/file" )
395+ mock_generate_jwt .assert_called_once_with ({"dummy" : "credentials" })
396+ mock_request_access_token .assert_called_once_with ("jwt_token_example" )
374397
375398
376399if __name__ == "__main__" :
0 commit comments