4
4
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
5
5
6
6
import os
7
+ from mock import MagicMock
7
8
import pytest
8
9
from unittest import TestCase , mock
9
10
21
22
)
22
23
from ads .common .auth import (
23
24
SecurityToken ,
24
- TokenExpiredError ,
25
+ SecurityTokenError ,
25
26
api_keys ,
26
27
resource_principal ,
27
28
security_token ,
@@ -539,10 +540,12 @@ class TestSecurityToken(TestCase):
539
540
540
541
@mock .patch ("oci.auth.signers.SecurityTokenSigner.__init__" )
541
542
@mock .patch ("oci.signer.load_private_key_from_file" )
543
+ @mock .patch ("ads.common.auth.SecurityToken._read_security_token_file" )
542
544
@mock .patch ("ads.common.auth.SecurityToken._validate_and_refresh_token" )
543
545
def test_security_token (
544
546
self ,
545
547
mock_validate_and_refresh_token ,
548
+ mock_read_security_token_file ,
546
549
mock_load_private_key_from_file ,
547
550
mock_security_token_signer
548
551
):
@@ -571,8 +574,9 @@ def test_security_token(
571
574
client_kwargs = {"test_client_key" :"test_client_value" }
572
575
)
573
576
574
- mock_validate_and_refresh_token .assert_called_with ("test_security_token" )
575
- mock_load_private_key_from_file .assert_called_with ("test_key_file" )
577
+ mock_validate_and_refresh_token .assert_called_with (config )
578
+ mock_read_security_token_file .assert_called_with ("test_security_token" )
579
+ mock_load_private_key_from_file .assert_called_with ("test_key_file" , None )
576
580
assert signer ["client_kwargs" ] == {"test_client_key" : "test_client_value" }
577
581
assert "additional_user_agent" in signer ["config" ]
578
582
assert signer ["config" ]["fingerprint" ] == "test_fingerprint"
@@ -582,7 +586,7 @@ def test_security_token(
582
586
assert signer ["config" ]["key_file" ] == "test_key_file"
583
587
assert isinstance (signer ["signer" ], SecurityTokenSigner )
584
588
585
- @mock .patch ("os.system " )
589
+ @mock .patch ("ads.common.auth.SecurityToken._refresh_security_token " )
586
590
@mock .patch ("oci.auth.security_token_container.SecurityTokenContainer.get_jwt" )
587
591
@mock .patch ("time.time" )
588
592
@mock .patch ("oci.auth.security_token_container.SecurityTokenContainer.valid" )
@@ -595,7 +599,7 @@ def test_validate_and_refresh_token(
595
599
mock_valid ,
596
600
mock_time ,
597
601
mock_get_jwt ,
598
- mock_system
602
+ mock_refresh_security_token
599
603
):
600
604
security_token = SecurityToken (
601
605
args = {
@@ -606,24 +610,94 @@ def test_validate_and_refresh_token(
606
610
mock_security_token_container .return_value = None
607
611
608
612
mock_valid .return_value = False
613
+ configuration = {
614
+ "fingerprint" : "test_fingerprint" ,
615
+ "tenancy" : "test_tenancy" ,
616
+ "region" : "us-ashburn-1" ,
617
+ "key_file" : "test_key_file" ,
618
+ "security_token_file" : "test_security_token" ,
619
+ "generic_headers" : [1 ,2 ,3 ],
620
+ "body_headers" : [4 ,5 ,6 ]
621
+ }
609
622
with pytest .raises (
610
- TokenExpiredError ,
623
+ SecurityTokenError ,
611
624
match = "Security token has expired. Call `oci session authenticate` to generate new session."
612
625
):
613
- security_token ._validate_and_refresh_token ("test_security_token" )
626
+ security_token ._validate_and_refresh_token (configuration )
614
627
615
628
616
629
mock_valid .return_value = True
617
630
mock_time .return_value = 1
618
631
mock_get_jwt .return_value = {"exp" : 1 }
619
632
620
- security_token ._validate_and_refresh_token ("test_security_token" )
633
+ security_token ._validate_and_refresh_token (configuration )
621
634
622
635
mock_read_security_token_file .assert_called_with ("test_security_token" )
623
636
mock_security_token_container .assert_called ()
624
637
mock_time .assert_called ()
625
638
mock_get_jwt .assert_called ()
626
- mock_system .assert_called_with ("oci session refresh --profile test_profile" )
639
+ mock_refresh_security_token .assert_called_with (configuration )
640
+
641
+ @mock .patch ("oci_cli.cli_util.apply_user_only_access_permissions" )
642
+ @mock .patch ("json.loads" )
643
+ @mock .patch ("requests.post" )
644
+ @mock .patch ("json.dumps" )
645
+ @mock .patch ("oci.auth.signers.SecurityTokenSigner.__init__" )
646
+ @mock .patch ("oci.signer.load_private_key_from_file" )
647
+ @mock .patch ("builtins.open" )
648
+ def test_refresh_security_token (
649
+ self ,
650
+ mock_open ,
651
+ mock_load_private_key_from_file ,
652
+ mock_security_token_signer ,
653
+ mock_dumps ,
654
+ mock_post ,
655
+ mock_loads ,
656
+ mock_apply_user_only_access_permissions
657
+ ):
658
+ security_token = SecurityToken (args = {})
659
+ configuration = {
660
+ "fingerprint" : "test_fingerprint" ,
661
+ "tenancy" : "test_tenancy" ,
662
+ "region" : "us-ashburn-1" ,
663
+ "key_file" : "test_key_file" ,
664
+ "security_token_file" : "test_security_token" ,
665
+ "generic_headers" : [1 ,2 ,3 ],
666
+ "body_headers" : [4 ,5 ,6 ]
667
+ }
668
+ mock_security_token_signer .return_value = None
669
+ mock_loads .return_value = {
670
+ "token" : "test_token"
671
+ }
672
+
673
+ response = MagicMock ()
674
+ response .status_code = 401
675
+ mock_post .return_value = response
676
+ with pytest .raises (
677
+ SecurityTokenError ,
678
+ match = "Security token has expired. Call `oci session authenticate` to generate new session."
679
+ ):
680
+ security_token ._refresh_security_token (configuration )
681
+
682
+ response .status_code = 500
683
+ mock_post .return_value = response
684
+ with pytest .raises (
685
+ SecurityTokenError ,
686
+ ):
687
+ security_token ._refresh_security_token (configuration )
688
+
689
+ response .status_code = 200
690
+ response .content = bytes ("test_content" , encoding = 'utf8' )
691
+ mock_post .return_value = response
692
+ security_token ._refresh_security_token (configuration )
693
+
694
+ mock_open .assert_called ()
695
+ mock_load_private_key_from_file .assert_called_with ("test_key_file" , None )
696
+ mock_security_token_signer .assert_called ()
697
+ mock_dumps .assert_called ()
698
+ mock_post .assert_called ()
699
+ mock_loads .assert_called ()
700
+ mock_apply_user_only_access_permissions .assert_called ()
627
701
628
702
@mock .patch ("builtins.open" )
629
703
@mock .patch ("os.path.isfile" )
0 commit comments