11
11
12
12
from tests .http_client import MinimalResponse
13
13
from msal import (
14
+ ConfidentialClientApplication ,
14
15
SystemAssignedManagedIdentity , UserAssignedManagedIdentity ,
15
- ManagedIdentityClient )
16
+ )
16
17
17
18
18
19
class ManagedIdentityTestCase (unittest .TestCase ):
@@ -39,26 +40,22 @@ class ClientTestCase(unittest.TestCase):
39
40
maxDiff = None
40
41
41
42
def setUp (self ):
42
- self .app = ManagedIdentityClient (
43
- { # Here we test it with the raw dict form, to test that
44
- # the client has no hard dependency on ManagedIdentity object
45
- "ManagedIdentityIdType" : "SystemAssigned" , "Id" : None ,
46
- },
47
- requests .Session (),
48
- )
43
+ system_assigned = {"ManagedIdentityIdType" : "SystemAssigned" , "Id" : None }
44
+ self .app = ConfidentialClientApplication (client_id = system_assigned )
49
45
50
46
def _test_token_cache (self , app ):
51
- cache = app ._token_cache ._cache
47
+ cache = app .token_cache ._cache
52
48
self .assertEqual (1 , len (cache .get ("AccessToken" , [])), "Should have 1 AT" )
53
49
at = list (cache ["AccessToken" ].values ())[0 ]
54
50
self .assertEqual (
55
- app ._managed_identity .get ("Id" , "SYSTEM_ASSIGNED_MANAGED_IDENTITY" ),
51
+ app .client_id .get ("Id" , "SYSTEM_ASSIGNED_MANAGED_IDENTITY" ),
56
52
at ["client_id" ],
57
53
"Should have expected client_id" )
58
54
self .assertEqual ("managed_identity" , at ["realm" ], "Should have expected realm" )
59
55
60
56
def _test_happy_path (self , app , mocked_http ):
61
- result = app .acquire_token_for_client (resource = "R" )
57
+ #result = app.acquire_token_for_client(resource="R")
58
+ result = app .acquire_token_for_client (["R" ])
62
59
mocked_http .assert_called ()
63
60
self .assertEqual ({
64
61
"access_token" : "AT" ,
@@ -68,29 +65,29 @@ def _test_happy_path(self, app, mocked_http):
68
65
}, result , "Should obtain a token response" )
69
66
self .assertEqual (
70
67
result ["access_token" ],
71
- app .acquire_token_for_client (resource = "R" ).get ("access_token" ),
68
+ app .acquire_token_for_client ([ "R" ] ).get ("access_token" ),
72
69
"Should hit the same token from cache" )
73
70
self ._test_token_cache (app )
74
71
75
72
76
73
class VmTestCase (ClientTestCase ):
77
74
78
75
def test_happy_path (self ):
79
- with patch .object (self .app ._http_client , "get" , return_value = MinimalResponse (
76
+ with patch .object (self .app .http_client , "get" , return_value = MinimalResponse (
80
77
status_code = 200 ,
81
78
text = '{"access_token": "AT", "expires_in": "1234", "resource": "R"}' ,
82
79
)) as mocked_method :
83
80
self ._test_happy_path (self .app , mocked_method )
84
81
85
82
def test_vm_error_should_be_returned_as_is (self ):
86
83
raw_error = '{"raw": "error format is undefined"}'
87
- with patch .object (self .app ._http_client , "get" , return_value = MinimalResponse (
84
+ with patch .object (self .app .http_client , "get" , return_value = MinimalResponse (
88
85
status_code = 400 ,
89
86
text = raw_error ,
90
87
)) as mocked_method :
91
88
self .assertEqual (
92
- json .loads (raw_error ), self .app .acquire_token_for_client (resource = "R" ))
93
- self .assertEqual ({}, self .app ._token_cache ._cache )
89
+ json .loads (raw_error ), self .app .acquire_token_for_client ([ "R" ] ))
90
+ self .assertEqual ({}, self .app .token_cache ._cache )
94
91
95
92
96
93
@patch .dict (os .environ , {"IDENTITY_ENDPOINT" : "http://localhost" , "IDENTITY_HEADER" : "foo" })
0 commit comments