@@ -25,6 +25,21 @@ def _test_token_cache(self, app):
2525 "Should have expected client_id" )
2626 self .assertEqual ("managed_identity" , at ["realm" ], "Should have expected realm" )
2727
28+ def _test_happy_path (self , app , mocked_http ):
29+ result = app .acquire_token (resource = "R" )
30+ mocked_http .assert_called_once ()
31+ self .assertEqual ({
32+ "access_token" : "AT" ,
33+ "expires_in" : 1234 ,
34+ "resource" : "R" ,
35+ "token_type" : "Bearer" ,
36+ }, result , "Should obtain a token response" )
37+ self .assertEqual (
38+ result ["access_token" ],
39+ app .acquire_token (resource = "R" ).get ("access_token" ),
40+ "Should hit the same token from cache" )
41+ self ._test_token_cache (app )
42+
2843
2944class VmTestCase (ManagedIdentityTestCase ):
3045
@@ -34,19 +49,7 @@ def test_happy_path(self):
3449 status_code = 200 ,
3550 text = '{"access_token": "AT", "expires_in": "1234", "resource": "R"}' ,
3651 )) as mocked_method :
37- result = app .acquire_token (resource = "R" )
38- mocked_method .assert_called_once ()
39- self .assertEqual ({
40- "access_token" : "AT" ,
41- "expires_in" : 1234 ,
42- "resource" : "R" ,
43- "token_type" : "Bearer" ,
44- }, result , "Should obtain a token response" )
45- self .assertEqual (
46- result ["access_token" ],
47- app .acquire_token (resource = "R" ).get ("access_token" ),
48- "Should hit the same token from cache" )
49- self ._test_token_cache (app )
52+ self ._test_happy_path (app , mocked_method )
5053
5154 def test_vm_error_should_be_returned_as_is (self ):
5255 raw_error = '{"raw": "error format is undefined"}'
@@ -63,26 +66,13 @@ def test_vm_error_should_be_returned_as_is(self):
6366class AppServiceTestCase (ManagedIdentityTestCase ):
6467
6568 def test_happy_path (self ):
66- # TODO: Combine this with VM's test case, and move it into base class
6769 app = ManagedIdentity (requests .Session (), token_cache = TokenCache ())
68- now = int (time .time ())
6970 with patch .object (app ._http_client , "get" , return_value = MinimalResponse (
7071 status_code = 200 ,
71- text = '{"access_token": "AT", "expires_on": "%s", "resource": "R"}' % (now + 100 ),
72+ text = '{"access_token": "AT", "expires_on": "%s", "resource": "R"}' % (
73+ int (time .time ()) + 1234 ),
7274 )) as mocked_method :
73- result = app .acquire_token (resource = "R" )
74- mocked_method .assert_called_once ()
75- self .assertEqual ({
76- "access_token" : "AT" ,
77- "expires_in" : 100 ,
78- "resource" : "R" ,
79- "token_type" : "Bearer" ,
80- }, result , "Should obtain a token response" )
81- self .assertEqual (
82- result ["access_token" ],
83- app .acquire_token (resource = "R" ).get ("access_token" ),
84- "Should hit the same token from cache" )
85- self ._test_token_cache (app )
75+ self ._test_happy_path (app , mocked_method )
8676
8777 def test_app_service_error_should_be_normalized (self ):
8878 raw_error = '{"statusCode": 500, "message": "error content is undefined"}'
@@ -97,3 +87,44 @@ def test_app_service_error_should_be_normalized(self):
9787 }, app .acquire_token (resource = "R" ))
9888 self .assertEqual ({}, app ._token_cache ._cache )
9989
90+ @patch .dict (os .environ , {
91+ "IDENTITY_ENDPOINT" : "http://localhost" ,
92+ "IDENTITY_HEADER" : "foo" ,
93+ "IDENTITY_SERVER_THUMBPRINT" : "bar" ,
94+ })
95+ class ServiceFabricTestCase (ManagedIdentityTestCase ):
96+
97+ def _test_happy_path (self , app ):
98+ with patch .object (app ._http_client , "get" , return_value = MinimalResponse (
99+ status_code = 200 ,
100+ text = '{"access_token": "AT", "expires_on": %s, "resource": "R", "token_type": "Bearer"}' % (
101+ int (time .time ()) + 1234 ),
102+ )) as mocked_method :
103+ super (ServiceFabricTestCase , self )._test_happy_path (app , mocked_method )
104+
105+ def test_happy_path (self ):
106+ self ._test_happy_path (ManagedIdentity (
107+ requests .Session (), token_cache = TokenCache ()))
108+
109+ def test_unified_api_service_should_ignore_unnecessary_client_id (self ):
110+ self ._test_happy_path (ManagedIdentity (
111+ requests .Session (), client_id = "foo" , token_cache = TokenCache ()))
112+
113+ def test_app_service_error_should_be_normalized (self ):
114+ raw_error = '''
115+ {"error": {
116+ "correlationId": "foo",
117+ "code": "SecretHeaderNotFound",
118+ "message": "Secret is not found in the request headers."
119+ }}''' # https://learn.microsoft.com/en-us/azure/service-fabric/how-to-managed-identity-service-fabric-app-code#error-handling
120+ app = ManagedIdentity (requests .Session (), token_cache = TokenCache ())
121+ with patch .object (app ._http_client , "get" , return_value = MinimalResponse (
122+ status_code = 404 ,
123+ text = raw_error ,
124+ )) as mocked_method :
125+ self .assertEqual ({
126+ "error" : "unauthorized_client" ,
127+ "error_description" : raw_error ,
128+ }, app .acquire_token (resource = "R" ))
129+ self .assertEqual ({}, app ._token_cache ._cache )
130+
0 commit comments