1
+ from tests import unittest
2
+ import msal
3
+ import logging
4
+ import sys
5
+
6
+ # from tests.test_e2e import LabBasedTestCase
7
+
8
+ if not sys .platform .startswith ("win" ):
9
+ raise unittest .SkipTest ("Currently, our broker supports Windows" )
10
+
11
+ SCOPE_ARM = "https://management.azure.com/.default"
12
+ _AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
13
+ pca = msal .PublicClientApplication (
14
+ _AZURE_CLI ,
15
+ authority = "https://login.microsoftonline.com/organizations" ,
16
+ enable_broker_on_mac = True ,
17
+ enable_broker_on_windows = True )
18
+
19
+
20
+ # class ForceRefreshTestCase(LabBasedTestCase):
21
+ # def test_silent_with_force_refresh(self):
22
+ # # acquire token using username and password
23
+ # print("Testing silent flow with force_refresh=True")
24
+ # config = self.get_lab_user(usertype="cloud")
25
+ # config["password"] = self.get_lab_user_secret(config["lab_name"])
26
+ # result = pca.acquire_token_by_username_password(username=config["lab_name"], password=config["password"], scopes=config["scope"])
27
+ # # assert username and password, "You need to provide a test account and its password"
28
+
29
+ # ropcToken = result.get("access_token")
30
+ # accounts = pca.get_accounts()
31
+ # account = accounts[0]
32
+ # assert account, "The logged in account should have been established by interactive flow"
33
+
34
+ # result = pca.acquire_token_silent(
35
+ # config["scope"],
36
+ # account=account,
37
+ # force_refresh=False,
38
+ # auth_scheme=None, data=None)
39
+
40
+ # assert result.get("access_token") == ropcToken, "Token should not be refreshed"
41
+
42
+
43
+ class ForceRefreshTestCase (unittest .TestCase ):
44
+ def test_silent_with_force_refresh (self ):
45
+ # acquire token using username and password
46
+ print ("Testing silent flow with force_refresh=True" )
47
+ result = pca .acquire_token_interactive (scopes = [SCOPE_ARM ], prompt = "select_account" , parent_window_handle = pca .CONSOLE_WINDOW_HANDLE , enable_msa_passthrough = True )
48
+ accounts = pca .get_accounts ()
49
+ account = accounts [0 ]
50
+ assert account , "The logged in account should have been established by interactive flow"
51
+ oldToken = result .get ("access_token" )
52
+
53
+
54
+ result = pca .acquire_token_silent (
55
+ scopes = [SCOPE_ARM ],
56
+ account = account ,
57
+ force_refresh = False )
58
+
59
+ # This token should be recieved from cache
60
+ assert result .get ("access_token" ) == oldToken , "Token should not be refreshed"
61
+
62
+
63
+ result = pca .acquire_token_silent (
64
+ scopes = [SCOPE_ARM ],
65
+ account = account ,
66
+ force_refresh = True )
67
+
68
+ # Token will be different proving it is not from cache and was renewed
69
+ assert result .get ("access_token" ) != oldToken , "Token should be refreshed"
0 commit comments