Skip to content

Update ROPC broker related tests #714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/broker-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
we can use this script to test it with a given version of MSAL Python.
"""
import msal
import getpass
import os
try:
from dotenv import load_dotenv # Use this only in local dev machine
load_dotenv() # take environment variables from .env.
except:
pass

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

def test_broker_username_password(scopes, expected_token_type):
print("Testing broker username password flows by using accounts in local .env")
username = os.getenv("BROKER_TEST_ACCOUNT") or input("Input test account for broker test: ")
password = os.getenv("BROKER_TEST_ACCOUNT_PASSWORD") or getpass.getpass("Input test account's password: ")
assert(username and password, "You need to provide a test account and its password")
result = pca.acquire_token_by_username_password(username, password, scopes)
_assert(result, expected_token_type)
assert(result.get("token_source") == "broker")
print("Username password test succeeds.")

def _assert(result, expected_token_type):
assert result.get("access_token"), f"We should obtain a token. Got {result} instead."
assert result.get("token_source") == "broker", "Token should be obtained via broker"
Expand All @@ -64,3 +81,4 @@ def _assert(result, expected_token_type):
expected_token_type="ssh-cert",
)

test_broker_username_password(scopes=[SCOPE_ARM], expected_token_type="bearer")
11 changes: 5 additions & 6 deletions tests/test_account_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,19 @@ def test_device_flow_and_its_silent_call_should_bypass_broker(self, _, mocked_br
mocked_broker_ats.assert_not_called()
self.assertEqual(result["token_source"], "identity_provider")

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

account = app.get_accounts()[0]
self.assertEqual(account["account_source"], "password")
self.assertEqual(account["account_source"], "broker")

result = app.acquire_token_silent_with_error(
[SCOPE], account, force_refresh=True, post=_mock_post)
mocked_broker_ats.assert_not_called()
self.assertEqual(result["token_source"], "identity_provider")
self.assertEqual(result["token_source"], "broker")

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