|
| 1 | +from tests import unittest |
| 2 | +import msal |
| 3 | +import sys |
| 4 | + |
| 5 | + |
| 6 | +if sys.platform not in ("win32", "darwin"): |
| 7 | + raise unittest.SkipTest(f"Our broker does not support {sys.platform}") |
| 8 | + |
| 9 | +SCOPES = ["https://management.azure.com/.default"] |
| 10 | +_AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46" |
| 11 | +pca = msal.PublicClientApplication( |
| 12 | + _AZURE_CLI, |
| 13 | + authority="https://login.microsoftonline.com/organizations", |
| 14 | + enable_broker_on_mac=True, |
| 15 | + enable_broker_on_windows=True, |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +class ForceRefreshTestCase(unittest.TestCase): |
| 20 | + def test_silent_with_force_refresh_should_return_a_new_token(self): |
| 21 | + result = pca.acquire_token_interactive( |
| 22 | + scopes=SCOPES, |
| 23 | + prompt="select_account", |
| 24 | + parent_window_handle=pca.CONSOLE_WINDOW_HANDLE, |
| 25 | + enable_msa_passthrough=True, |
| 26 | + ) |
| 27 | + accounts = pca.get_accounts() |
| 28 | + self.assertNotEqual( |
| 29 | + [], accounts, |
| 30 | + "Interactive flow should have established a logged-in account") |
| 31 | + account = accounts[0] |
| 32 | + old_token = result.get("access_token") |
| 33 | + |
| 34 | + result = pca.acquire_token_silent(SCOPES, account) |
| 35 | + assertion = "This token should have been received from cache" |
| 36 | + self.assertEqual(result.get("access_token"), old_token, assertion) |
| 37 | + self.assertEqual(result.get("token_source"), "cache", assertion) |
| 38 | + |
| 39 | + result = pca.acquire_token_silent(SCOPES, account, force_refresh=True) |
| 40 | + assertion = "A new token should have been received from broker" |
| 41 | + self.assertNotEqual(result.get("access_token"), old_token, assertion) |
| 42 | + self.assertEqual(result.get("token_source"), "broker", assertion) |
| 43 | + |
0 commit comments