@@ -25,6 +25,21 @@ def _test_token_cache(self, app):
25
25
"Should have expected client_id" )
26
26
self .assertEqual ("managed_identity" , at ["realm" ], "Should have expected realm" )
27
27
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
+
28
43
29
44
class VmTestCase (ManagedIdentityTestCase ):
30
45
@@ -34,19 +49,7 @@ def test_happy_path(self):
34
49
status_code = 200 ,
35
50
text = '{"access_token": "AT", "expires_in": "1234", "resource": "R"}' ,
36
51
)) 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 )
50
53
51
54
def test_vm_error_should_be_returned_as_is (self ):
52
55
raw_error = '{"raw": "error format is undefined"}'
@@ -63,26 +66,13 @@ def test_vm_error_should_be_returned_as_is(self):
63
66
class AppServiceTestCase (ManagedIdentityTestCase ):
64
67
65
68
def test_happy_path (self ):
66
- # TODO: Combine this with VM's test case, and move it into base class
67
69
app = ManagedIdentity (requests .Session (), token_cache = TokenCache ())
68
- now = int (time .time ())
69
70
with patch .object (app ._http_client , "get" , return_value = MinimalResponse (
70
71
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 ),
72
74
)) 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 )
86
76
87
77
def test_app_service_error_should_be_normalized (self ):
88
78
raw_error = '{"statusCode": 500, "message": "error content is undefined"}'
@@ -97,3 +87,44 @@ def test_app_service_error_should_be_normalized(self):
97
87
}, app .acquire_token (resource = "R" ))
98
88
self .assertEqual ({}, app ._token_cache ._cache )
99
89
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