|
6 | 6 |
|
7 | 7 | import msal |
8 | 8 | from msal.authority import * |
9 | | -from msal.authority import _CIAM_DOMAIN_SUFFIX, TRUSTED_ISSUER_HOSTS # Explicitly import private/new constants |
| 9 | +from msal.authority import _CIAM_DOMAIN_SUFFIX |
10 | 10 | from tests import unittest |
11 | 11 | from tests.http_client import MinimalHttpClient |
12 | 12 |
|
@@ -37,10 +37,95 @@ def _test_authority_builder(self, host, tenant): |
37 | 37 | c.close() |
38 | 38 |
|
39 | 39 | def test_wellknown_host_and_tenant(self): |
40 | | - # Assert all well known authority hosts are using their own "common" tenant |
| 40 | + # This test makes real HTTP calls to authority endpoints. |
| 41 | + # It is intentionally network-based to validate reachable hosts end-to-end. |
| 42 | + excluded_hosts = { |
| 43 | + DEPRECATED_AZURE_CHINA, |
| 44 | + "login.microsoftonline.de", # deprecated |
| 45 | + "login.microsoft.com", # issuer-only in this test context |
| 46 | + "login.windows.net", # issuer-only in this test context |
| 47 | + "sts.windows.net", # issuer-only in this test context |
| 48 | + "login.partner.microsoftonline.cn", # issuer-only in this test context |
| 49 | + "login.usgovcloudapi.net", # issuer-only in this test context |
| 50 | + AZURE_GOV_FR, # currently unreachable in this environment |
| 51 | + AZURE_GOV_DE, # currently unreachable in this environment |
| 52 | + AZURE_GOV_SG, # currently unreachable in this environment |
| 53 | + } |
41 | 54 | for host in WELL_KNOWN_AUTHORITY_HOSTS: |
42 | | - if host != AZURE_CHINA: # It is prone to ConnectionError |
43 | | - self._test_given_host_and_tenant(host, "common") |
| 55 | + if host in excluded_hosts: |
| 56 | + continue |
| 57 | + self._test_given_host_and_tenant(host, "common") |
| 58 | + |
| 59 | + def test_new_sovereign_hosts_should_be_known_authorities(self): |
| 60 | + self.assertIn(AZURE_GOV_FR, WELL_KNOWN_AUTHORITY_HOSTS) |
| 61 | + self.assertIn(AZURE_GOV_DE, WELL_KNOWN_AUTHORITY_HOSTS) |
| 62 | + self.assertIn(AZURE_GOV_SG, WELL_KNOWN_AUTHORITY_HOSTS) |
| 63 | + |
| 64 | + @patch("msal.authority._instance_discovery") |
| 65 | + @patch("msal.authority.tenant_discovery") |
| 66 | + def test_new_sovereign_hosts_should_build_authority_endpoints( |
| 67 | + self, tenant_discovery_mock, instance_discovery_mock): |
| 68 | + for host in (AZURE_GOV_FR, AZURE_GOV_DE, AZURE_GOV_SG): |
| 69 | + tenant_discovery_mock.return_value = { |
| 70 | + "authorization_endpoint": "https://{}/common/oauth2/v2.0/authorize".format(host), |
| 71 | + "token_endpoint": "https://{}/common/oauth2/v2.0/token".format(host), |
| 72 | + "issuer": "https://{}/common/v2.0".format(host), |
| 73 | + } |
| 74 | + instance_discovery_mock.return_value = { |
| 75 | + "tenant_discovery_endpoint": ( |
| 76 | + "https://{}/common/v2.0/.well-known/openid-configuration".format(host) |
| 77 | + ), |
| 78 | + } |
| 79 | + c = MinimalHttpClient() |
| 80 | + a = Authority(AuthorityBuilder(host, "common"), c) |
| 81 | + self.assertEqual( |
| 82 | + a.authorization_endpoint, |
| 83 | + "https://{}/common/oauth2/v2.0/authorize".format(host)) |
| 84 | + self.assertEqual( |
| 85 | + a.token_endpoint, |
| 86 | + "https://{}/common/oauth2/v2.0/token".format(host)) |
| 87 | + c.close() |
| 88 | + |
| 89 | + @patch("msal.authority._instance_discovery") |
| 90 | + @patch("msal.authority.tenant_discovery") |
| 91 | + def test_known_authority_should_use_same_host_and_skip_instance_discovery( |
| 92 | + self, tenant_discovery_mock, instance_discovery_mock): |
| 93 | + host = AZURE_US_GOVERNMENT |
| 94 | + tenant_discovery_mock.return_value = { |
| 95 | + "authorization_endpoint": "https://{}/common/oauth2/v2.0/authorize".format(host), |
| 96 | + "token_endpoint": "https://{}/common/oauth2/v2.0/token".format(host), |
| 97 | + "issuer": "https://{}/common/v2.0".format(host), |
| 98 | + } |
| 99 | + c = MinimalHttpClient() |
| 100 | + Authority("https://{}/common".format(host), c) |
| 101 | + c.close() |
| 102 | + |
| 103 | + instance_discovery_mock.assert_not_called() |
| 104 | + tenant_discovery_endpoint = tenant_discovery_mock.call_args[0][0] |
| 105 | + self.assertTrue( |
| 106 | + tenant_discovery_endpoint.startswith( |
| 107 | + "https://{}/common/v2.0/.well-known/openid-configuration".format(host))) |
| 108 | + |
| 109 | + @patch("msal.authority._instance_discovery") |
| 110 | + @patch("msal.authority.tenant_discovery") |
| 111 | + def test_unknown_authority_should_use_world_wide_instance_discovery_endpoint( |
| 112 | + self, tenant_discovery_mock, instance_discovery_mock): |
| 113 | + tenant_discovery_mock.return_value = { |
| 114 | + "authorization_endpoint": "https://example.com/tenant/oauth2/v2.0/authorize", |
| 115 | + "token_endpoint": "https://example.com/tenant/oauth2/v2.0/token", |
| 116 | + "issuer": "https://example.com/tenant/v2.0", |
| 117 | + } |
| 118 | + instance_discovery_mock.return_value = { |
| 119 | + "tenant_discovery_endpoint": "https://example.com/tenant/v2.0/.well-known/openid-configuration", |
| 120 | + } |
| 121 | + |
| 122 | + c = MinimalHttpClient() |
| 123 | + Authority("https://example.com/tenant", c) |
| 124 | + c.close() |
| 125 | + |
| 126 | + self.assertEqual( |
| 127 | + "https://{}/common/discovery/instance".format(WORLD_WIDE), |
| 128 | + instance_discovery_mock.call_args[0][2]) |
44 | 129 |
|
45 | 130 | def test_wellknown_host_and_tenant_using_new_authority_builder(self): |
46 | 131 | self._test_authority_builder(AZURE_PUBLIC, "consumers") |
|
0 commit comments