Skip to content

Commit c1ead1c

Browse files
authored
Update ROPC broker related tests (#714)
* Update ROPC broker tests * Get test account and password from .env * update
1 parent 18174ed commit c1ead1c

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

tests/broker-test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
we can use this script to test it with a given version of MSAL Python.
77
"""
88
import msal
9+
import getpass
10+
import os
11+
try:
12+
from dotenv import load_dotenv # Use this only in local dev machine
13+
load_dotenv() # take environment variables from .env.
14+
except:
15+
pass
916

1017
_AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
1118
SCOPE_ARM = "https://management.azure.com/.default"
@@ -46,6 +53,16 @@ def interactive_and_silent(scopes, auth_scheme, data, expected_token_type):
4653
)
4754
_assert(result, expected_token_type)
4855

56+
def test_broker_username_password(scopes, expected_token_type):
57+
print("Testing broker username password flows by using accounts in local .env")
58+
username = os.getenv("BROKER_TEST_ACCOUNT") or input("Input test account for broker test: ")
59+
password = os.getenv("BROKER_TEST_ACCOUNT_PASSWORD") or getpass.getpass("Input test account's password: ")
60+
assert(username and password, "You need to provide a test account and its password")
61+
result = pca.acquire_token_by_username_password(username, password, scopes)
62+
_assert(result, expected_token_type)
63+
assert(result.get("token_source") == "broker")
64+
print("Username password test succeeds.")
65+
4966
def _assert(result, expected_token_type):
5067
assert result.get("access_token"), f"We should obtain a token. Got {result} instead."
5168
assert result.get("token_source") == "broker", "Token should be obtained via broker"
@@ -64,3 +81,4 @@ def _assert(result, expected_token_type):
6481
expected_token_type="ssh-cert",
6582
)
6683

84+
test_broker_username_password(scopes=[SCOPE_ARM], expected_token_type="bearer")

tests/test_account_source.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,19 @@ def test_device_flow_and_its_silent_call_should_bypass_broker(self, _, mocked_br
4646
mocked_broker_ats.assert_not_called()
4747
self.assertEqual(result["token_source"], "identity_provider")
4848

49-
def test_ropc_flow_and_its_silent_call_should_bypass_broker(self, _, mocked_broker_ats):
49+
def test_ropc_flow_and_its_silent_call_should_invoke_broker(self, _, mocked_broker_ats):
5050
app = msal.PublicClientApplication("client_id", enable_broker_on_windows=True)
51-
with patch.object(app.authority, "user_realm_discovery", return_value={}):
51+
with patch("msal.broker._signin_silently", return_value=dict(TOKEN_RESPONSE, _account_id="placeholder")):
5252
result = app.acquire_token_by_username_password(
5353
"username", "placeholder", [SCOPE], post=_mock_post)
54-
self.assertEqual(result["token_source"], "identity_provider")
54+
self.assertEqual(result["token_source"], "broker")
5555

5656
account = app.get_accounts()[0]
57-
self.assertEqual(account["account_source"], "password")
57+
self.assertEqual(account["account_source"], "broker")
5858

5959
result = app.acquire_token_silent_with_error(
6060
[SCOPE], account, force_refresh=True, post=_mock_post)
61-
mocked_broker_ats.assert_not_called()
62-
self.assertEqual(result["token_source"], "identity_provider")
61+
self.assertEqual(result["token_source"], "broker")
6362

6463
def test_interactive_flow_and_its_silent_call_should_invoke_broker(self, _, mocked_broker_ats):
6564
app = msal.PublicClientApplication("client_id", enable_broker_on_windows=True)

0 commit comments

Comments
 (0)