|
3 | 3 | from unittest.mock import patch
|
4 | 4 | except:
|
5 | 5 | from mock import patch
|
6 |
| -try: |
7 |
| - import pymsalruntime |
8 |
| - broker_available = True |
9 |
| -except ImportError: |
10 |
| - broker_available = False |
11 | 6 | import msal
|
12 | 7 | from tests import unittest
|
13 | 8 | from tests.test_token_cache import build_response
|
14 | 9 | from tests.http_client import MinimalResponse
|
| 10 | +from tests.broker_util import is_pymsalruntime_installed |
15 | 11 |
|
16 | 12 |
|
17 | 13 | SCOPE = "scope_foo"
|
|
24 | 20 | def _mock_post(url, headers=None, *args, **kwargs):
|
25 | 21 | return MinimalResponse(status_code=200, text=json.dumps(TOKEN_RESPONSE))
|
26 | 22 |
|
27 |
| -@unittest.skipUnless(broker_available, "These test cases need pip install msal[broker]") |
| 23 | +@unittest.skipUnless(is_pymsalruntime_installed(), "These test cases need pip install msal[broker]") |
28 | 24 | @patch("msal.broker._acquire_token_silently", return_value=dict(
|
29 |
| - TOKEN_RESPONSE, _account_id="placeholder")) |
| 25 | + TOKEN_RESPONSE, _account_id="placeholder")) |
30 | 26 | @patch.object(msal.authority, "tenant_discovery", return_value={
|
31 | 27 | "authorization_endpoint": "https://contoso.com/placeholder",
|
32 | 28 | "token_endpoint": "https://contoso.com/placeholder",
|
33 | 29 | }) # Otherwise it would fail on OIDC discovery
|
34 | 30 | class TestAccountSourceBehavior(unittest.TestCase):
|
35 | 31 |
|
| 32 | + def setUp(self): |
| 33 | + self.app = msal.PublicClientApplication( |
| 34 | + "client_id", |
| 35 | + enable_broker_on_windows=True, |
| 36 | + ) |
| 37 | + if not self.app._enable_broker: |
| 38 | + self.skipTest( |
| 39 | + "These test cases require patching msal.broker which is only possible " |
| 40 | + "when broker enabled successfully i.e. no RuntimeError") |
| 41 | + return super().setUp() |
| 42 | + |
36 | 43 | def test_device_flow_and_its_silent_call_should_bypass_broker(self, _, mocked_broker_ats):
|
37 |
| - app = msal.PublicClientApplication("client_id", enable_broker_on_windows=True) |
38 |
| - result = app.acquire_token_by_device_flow({"device_code": "123"}, post=_mock_post) |
| 44 | + result = self.app.acquire_token_by_device_flow({"device_code": "123"}, post=_mock_post) |
39 | 45 | self.assertEqual(result["token_source"], "identity_provider")
|
40 | 46 |
|
41 |
| - account = app.get_accounts()[0] |
| 47 | + account = self.app.get_accounts()[0] |
42 | 48 | self.assertEqual(account["account_source"], "urn:ietf:params:oauth:grant-type:device_code")
|
43 | 49 |
|
44 |
| - result = app.acquire_token_silent_with_error( |
| 50 | + result = self.app.acquire_token_silent_with_error( |
45 | 51 | [SCOPE], account, force_refresh=True, post=_mock_post)
|
46 | 52 | mocked_broker_ats.assert_not_called()
|
47 | 53 | self.assertEqual(result["token_source"], "identity_provider")
|
48 | 54 |
|
49 | 55 | def test_ropc_flow_and_its_silent_call_should_invoke_broker(self, _, mocked_broker_ats):
|
50 |
| - app = msal.PublicClientApplication("client_id", enable_broker_on_windows=True) |
51 | 56 | with patch("msal.broker._signin_silently", return_value=dict(TOKEN_RESPONSE, _account_id="placeholder")):
|
52 |
| - result = app.acquire_token_by_username_password( |
| 57 | + result = self.app.acquire_token_by_username_password( |
53 | 58 | "username", "placeholder", [SCOPE], post=_mock_post)
|
54 | 59 | self.assertEqual(result["token_source"], "broker")
|
55 | 60 |
|
56 |
| - account = app.get_accounts()[0] |
| 61 | + account = self.app.get_accounts()[0] |
57 | 62 | self.assertEqual(account["account_source"], "broker")
|
58 | 63 |
|
59 |
| - result = app.acquire_token_silent_with_error( |
| 64 | + result = self.app.acquire_token_silent_with_error( |
60 | 65 | [SCOPE], account, force_refresh=True, post=_mock_post)
|
61 | 66 | self.assertEqual(result["token_source"], "broker")
|
62 | 67 |
|
63 | 68 | def test_interactive_flow_and_its_silent_call_should_invoke_broker(self, _, mocked_broker_ats):
|
64 |
| - app = msal.PublicClientApplication("client_id", enable_broker_on_windows=True) |
65 |
| - with patch.object(app, "_acquire_token_interactive_via_broker", return_value=dict( |
| 69 | + with patch.object(self.app, "_acquire_token_interactive_via_broker", return_value=dict( |
66 | 70 | TOKEN_RESPONSE, _account_id="placeholder")):
|
67 |
| - result = app.acquire_token_interactive( |
68 |
| - [SCOPE], parent_window_handle=app.CONSOLE_WINDOW_HANDLE) |
| 71 | + result = self.app.acquire_token_interactive( |
| 72 | + [SCOPE], parent_window_handle=self.app.CONSOLE_WINDOW_HANDLE) |
69 | 73 | self.assertEqual(result["token_source"], "broker")
|
70 | 74 |
|
71 |
| - account = app.get_accounts()[0] |
| 75 | + account = self.app.get_accounts()[0] |
72 | 76 | self.assertEqual(account["account_source"], "broker")
|
73 | 77 |
|
74 |
| - result = app.acquire_token_silent_with_error( |
| 78 | + result = self.app.acquire_token_silent_with_error( |
75 | 79 | [SCOPE], account, force_refresh=True, post=_mock_post)
|
76 | 80 | mocked_broker_ats.assert_called_once()
|
77 | 81 | self.assertEqual(result["token_source"], "broker")
|
|
0 commit comments