Skip to content

Commit a565ba2

Browse files
committed
E2e attempts broker albeit PyMsalRuntime init fails
1 parent 7f15c39 commit a565ba2

File tree

4 files changed

+56
-37
lines changed

4 files changed

+56
-37
lines changed

msal/application.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,8 @@ def _decide_broker(self, allow_broker, enable_pii_log):
675675
"allow_broker is deprecated. "
676676
"Please use PublicClientApplication(..., "
677677
"enable_broker_on_windows=True, "
678-
"enable_broker_on_mac=...)",
678+
# No need to mention non-Windows platforms, because allow_broker is only for Windows
679+
"...)",
679680
DeprecationWarning)
680681
opted_in_for_broker = (
681682
self._enable_broker # True means Opted-in from PCA
@@ -697,7 +698,7 @@ def _decide_broker(self, allow_broker, enable_pii_log):
697698
_init_broker(enable_pii_log)
698699
except RuntimeError:
699700
self._enable_broker = False
700-
logger.exception(
701+
logger.warning( # It is common on Mac and Linux where broker is not built-in
701702
"Broker is unavailable on this platform. "
702703
"We will fallback to non-broker.")
703704
logger.debug("Broker enabled? %s", self._enable_broker)

tests/broker_util.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import logging
2+
3+
4+
logger = logging.getLogger(__name__)
5+
6+
7+
def is_pymsalruntime_installed() -> bool:
8+
try:
9+
import pymsalruntime
10+
logger.info("PyMsalRuntime installed and initialized")
11+
return True
12+
except ImportError:
13+
logger.info("PyMsalRuntime not installed")
14+
return False
15+
except RuntimeError:
16+
logger.warning(
17+
"PyMsalRuntime installed but failed to initialize the real broker. "
18+
"This may happen on Mac and Linux where broker is not built-in. "
19+
"Test cases shall attempt broker and test its fallback behavior."
20+
)
21+
return True

tests/test_account_source.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
from unittest.mock import patch
44
except:
55
from mock import patch
6-
try:
7-
import pymsalruntime
8-
broker_available = True
9-
except ImportError:
10-
broker_available = False
116
import msal
127
from tests import unittest
138
from tests.test_token_cache import build_response
149
from tests.http_client import MinimalResponse
10+
from tests.broker_util import is_pymsalruntime_installed
1511

1612

1713
SCOPE = "scope_foo"
@@ -24,54 +20,62 @@
2420
def _mock_post(url, headers=None, *args, **kwargs):
2521
return MinimalResponse(status_code=200, text=json.dumps(TOKEN_RESPONSE))
2622

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]")
2824
@patch("msal.broker._acquire_token_silently", return_value=dict(
29-
TOKEN_RESPONSE, _account_id="placeholder"))
25+
TOKEN_RESPONSE, _account_id="placeholder"))
3026
@patch.object(msal.authority, "tenant_discovery", return_value={
3127
"authorization_endpoint": "https://contoso.com/placeholder",
3228
"token_endpoint": "https://contoso.com/placeholder",
3329
}) # Otherwise it would fail on OIDC discovery
3430
class TestAccountSourceBehavior(unittest.TestCase):
3531

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+
3643
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)
3945
self.assertEqual(result["token_source"], "identity_provider")
4046

41-
account = app.get_accounts()[0]
47+
account = self.app.get_accounts()[0]
4248
self.assertEqual(account["account_source"], "urn:ietf:params:oauth:grant-type:device_code")
4349

44-
result = app.acquire_token_silent_with_error(
50+
result = self.app.acquire_token_silent_with_error(
4551
[SCOPE], account, force_refresh=True, post=_mock_post)
4652
mocked_broker_ats.assert_not_called()
4753
self.assertEqual(result["token_source"], "identity_provider")
4854

4955
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)
5156
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(
5358
"username", "placeholder", [SCOPE], post=_mock_post)
5459
self.assertEqual(result["token_source"], "broker")
5560

56-
account = app.get_accounts()[0]
61+
account = self.app.get_accounts()[0]
5762
self.assertEqual(account["account_source"], "broker")
5863

59-
result = app.acquire_token_silent_with_error(
64+
result = self.app.acquire_token_silent_with_error(
6065
[SCOPE], account, force_refresh=True, post=_mock_post)
6166
self.assertEqual(result["token_source"], "broker")
6267

6368
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(
6670
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)
6973
self.assertEqual(result["token_source"], "broker")
7074

71-
account = app.get_accounts()[0]
75+
account = self.app.get_accounts()[0]
7276
self.assertEqual(account["account_source"], "broker")
7377

74-
result = app.acquire_token_silent_with_error(
78+
result = self.app.acquire_token_silent_with_error(
7579
[SCOPE], account, force_refresh=True, post=_mock_post)
7680
mocked_broker_ats.assert_called_once()
7781
self.assertEqual(result["token_source"], "broker")

tests/test_e2e.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@
2929
from tests.http_client import MinimalHttpClient, MinimalResponse
3030
from msal.oauth2cli import AuthCodeReceiver
3131
from msal.oauth2cli.oidc import decode_part
32+
from tests.broker_util import is_pymsalruntime_installed
33+
3234

33-
try:
34-
import pymsalruntime
35-
broker_available = True
36-
except ImportError:
37-
broker_available = False
3835
logger = logging.getLogger(__name__)
3936
logging.basicConfig(level=logging.DEBUG if "-v" in sys.argv else logging.INFO)
4037

@@ -44,6 +41,7 @@
4441
except ImportError:
4542
logger.warn("Run pip install -r requirements.txt for optional dependency")
4643

44+
_PYMSALRUNTIME_INSTALLED = is_pymsalruntime_installed()
4745
_AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
4846

4947
def _get_app_and_auth_code(
@@ -187,19 +185,14 @@ def _build_app(cls,
187185
http_client=http_client or MinimalHttpClient(),
188186
)
189187
else:
190-
# Reuse same test cases, by run them with and without broker
191-
try:
192-
import pymsalruntime
193-
broker_available = True
194-
except ImportError:
195-
broker_available = False
188+
# Reuse same test cases, by running them with and without PyMsalRuntime installed
196189
return msal.PublicClientApplication(
197190
client_id,
198191
authority=authority,
199192
oidc_authority=oidc_authority,
200193
http_client=http_client or MinimalHttpClient(),
201-
enable_broker_on_windows=broker_available,
202-
enable_broker_on_mac=broker_available,
194+
enable_broker_on_windows=_PYMSALRUNTIME_INSTALLED,
195+
enable_broker_on_mac=_PYMSALRUNTIME_INSTALLED,
203196
)
204197

205198
def _test_username_password(self,
@@ -1307,7 +1300,7 @@ def test_acquire_token_silent_with_an_empty_cache_should_return_none(self):
13071300
# it means MSAL Python is not affected by that.
13081301

13091302

1310-
@unittest.skipUnless(broker_available, "AT POP feature is only supported by using broker")
1303+
@unittest.skipUnless(_PYMSALRUNTIME_INSTALLED, "AT POP feature is only supported by using broker")
13111304
class PopTestCase(LabBasedTestCase):
13121305
def test_at_pop_should_contain_pop_scheme_content(self):
13131306
auth_scheme = msal.PopAuthScheme(

0 commit comments

Comments
 (0)