|
1 | 1 | # Note: Since Aug 2019 we move all e2e tests into test_e2e.py,
|
2 | 2 | # so this test_application file contains only unit tests without dependency.
|
| 3 | +import json |
| 4 | +import logging |
3 | 5 | import sys
|
4 | 6 | import time
|
5 |
| -from msal.application import * |
6 |
| -from msal.application import _str2bytes |
| 7 | +from unittest.mock import patch, Mock |
7 | 8 | import msal
|
8 |
| -from msal.application import _merge_claims_challenge_and_capabilities |
| 9 | +from msal.application import ( |
| 10 | + extract_certs, |
| 11 | + ClientApplication, PublicClientApplication, ConfidentialClientApplication, |
| 12 | + _str2bytes, _merge_claims_challenge_and_capabilities, |
| 13 | +) |
9 | 14 | from tests import unittest
|
10 | 15 | from tests.test_token_cache import build_id_token, build_response
|
11 | 16 | from tests.http_client import MinimalHttpClient, MinimalResponse
|
@@ -722,3 +727,63 @@ def test_client_id_should_be_a_valid_scope(self):
|
722 | 727 | self._test_client_id_should_be_a_valid_scope("client_id", [])
|
723 | 728 | self._test_client_id_should_be_a_valid_scope("client_id", ["foo"])
|
724 | 729 |
|
| 730 | + |
| 731 | +@patch("sys.platform", new="darwin") # Pretend running on Mac. |
| 732 | +@patch("msal.authority.tenant_discovery", new=Mock(return_value={ |
| 733 | + "authorization_endpoint": "https://contoso.com/placeholder", |
| 734 | + "token_endpoint": "https://contoso.com/placeholder", |
| 735 | + })) |
| 736 | +@patch("msal.application._init_broker", new=Mock()) # Allow testing without pymsalruntime |
| 737 | +class TestBrokerFallback(unittest.TestCase): |
| 738 | + |
| 739 | + def test_broker_should_be_disabled_by_default(self): |
| 740 | + app = msal.PublicClientApplication( |
| 741 | + "client_id", |
| 742 | + authority="https://login.microsoftonline.com/common", |
| 743 | + ) |
| 744 | + self.assertFalse(app._enable_broker) |
| 745 | + |
| 746 | + def test_broker_should_be_enabled_when_opted_in(self): |
| 747 | + app = msal.PublicClientApplication( |
| 748 | + "client_id", |
| 749 | + authority="https://login.microsoftonline.com/common", |
| 750 | + enable_broker_on_mac=True, |
| 751 | + ) |
| 752 | + self.assertTrue(app._enable_broker) |
| 753 | + |
| 754 | + def test_should_fallback_to_non_broker_when_using_adfs(self): |
| 755 | + app = msal.PublicClientApplication( |
| 756 | + "client_id", |
| 757 | + authority="https://contoso.com/adfs", |
| 758 | + #instance_discovery=False, # Automatically skipped when detected ADFS |
| 759 | + enable_broker_on_mac=True, |
| 760 | + ) |
| 761 | + self.assertFalse(app._enable_broker) |
| 762 | + |
| 763 | + def test_should_fallback_to_non_broker_when_using_b2c(self): |
| 764 | + app = msal.PublicClientApplication( |
| 765 | + "client_id", |
| 766 | + authority="https://contoso.b2clogin.com/contoso/policy", |
| 767 | + #instance_discovery=False, # Automatically skipped when detected B2C |
| 768 | + enable_broker_on_mac=True, |
| 769 | + ) |
| 770 | + self.assertFalse(app._enable_broker) |
| 771 | + |
| 772 | + def test_should_use_broker_when_disabling_instance_discovery(self): |
| 773 | + app = msal.PublicClientApplication( |
| 774 | + "client_id", |
| 775 | + authority="https://contoso.com/path", |
| 776 | + instance_discovery=False, # Need this for a generic authority url |
| 777 | + enable_broker_on_mac=True, |
| 778 | + ) |
| 779 | + # TODO: Shall we bypass broker when opted out of instance discovery? |
| 780 | + self.assertTrue(app._enable_broker) # Current implementation enables broker |
| 781 | + |
| 782 | + def test_should_fallback_to_non_broker_when_using_oidc_authority(self): |
| 783 | + app = msal.PublicClientApplication( |
| 784 | + "client_id", |
| 785 | + oidc_authority="https://contoso.com/path", |
| 786 | + enable_broker_on_mac=True, |
| 787 | + ) |
| 788 | + self.assertFalse(app._enable_broker) |
| 789 | + |
0 commit comments