|
| 1 | +# ------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# ------------------------------------ |
| 5 | +"""Tests for policy override support in azure-identity pipelines.""" |
| 6 | + |
| 7 | +from unittest.mock import Mock |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from azure.core.pipeline.policies import ( |
| 12 | + ContentDecodePolicy, |
| 13 | + CustomHookPolicy, |
| 14 | + DistributedTracingPolicy, |
| 15 | + HeadersPolicy, |
| 16 | + HttpLoggingPolicy, |
| 17 | + NetworkTraceLoggingPolicy, |
| 18 | + ProxyPolicy, |
| 19 | + RetryPolicy, |
| 20 | + SansIOHTTPPolicy, |
| 21 | + UserAgentPolicy, |
| 22 | +) |
| 23 | + |
| 24 | +from azure.identity._internal.pipeline import ( |
| 25 | + _get_config, |
| 26 | + _get_policies, |
| 27 | + build_pipeline, |
| 28 | + build_async_pipeline, |
| 29 | +) |
| 30 | + |
| 31 | +CONFIG_POLICIES = [ |
| 32 | + ("custom_hook_policy", CustomHookPolicy), |
| 33 | + ("headers_policy", HeadersPolicy), |
| 34 | + ("http_logging_policy", HttpLoggingPolicy), |
| 35 | + ("logging_policy", NetworkTraceLoggingPolicy), |
| 36 | + ("proxy_policy", ProxyPolicy), |
| 37 | + ("user_agent_policy", UserAgentPolicy), |
| 38 | +] |
| 39 | + |
| 40 | + |
| 41 | +class TestGetConfigPolicyOverrides: |
| 42 | + """Tests that _get_config respects policy override kwargs.""" |
| 43 | + |
| 44 | + def test_default_policies_created_when_no_overrides(self): |
| 45 | + config = _get_config() |
| 46 | + for attr, cls in CONFIG_POLICIES: |
| 47 | + assert isinstance(getattr(config, attr), cls) |
| 48 | + |
| 49 | + @pytest.mark.parametrize("kwarg,cls", CONFIG_POLICIES) |
| 50 | + def test_single_policy_override(self, kwarg, cls): |
| 51 | + custom = Mock(spec=cls) |
| 52 | + config = _get_config(**{kwarg: custom}) |
| 53 | + assert getattr(config, kwarg) is custom |
| 54 | + |
| 55 | + @pytest.mark.parametrize("kwarg,cls", CONFIG_POLICIES) |
| 56 | + def test_non_overridden_policies_unaffected(self, kwarg, cls): |
| 57 | + """Overriding one policy should not affect others.""" |
| 58 | + custom = Mock(spec=cls) |
| 59 | + config = _get_config(**{kwarg: custom}) |
| 60 | + for other_attr, other_cls in CONFIG_POLICIES: |
| 61 | + if other_attr == kwarg: |
| 62 | + assert getattr(config, other_attr) is custom |
| 63 | + else: |
| 64 | + assert isinstance(getattr(config, other_attr), other_cls) |
| 65 | + |
| 66 | + |
| 67 | +class TestGetPoliciesOverrides: |
| 68 | + """Tests for per_call_policies and per_retry_policies in _get_policies.""" |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def _make_config(): |
| 72 | + config = _get_config() |
| 73 | + config.retry_policy = RetryPolicy() |
| 74 | + return config |
| 75 | + |
| 76 | + def test_default_policy_order(self): |
| 77 | + policies = _get_policies(self._make_config()) |
| 78 | + |
| 79 | + assert [type(p) for p in policies] == [ |
| 80 | + HeadersPolicy, |
| 81 | + UserAgentPolicy, |
| 82 | + ProxyPolicy, |
| 83 | + ContentDecodePolicy, |
| 84 | + RetryPolicy, |
| 85 | + CustomHookPolicy, |
| 86 | + NetworkTraceLoggingPolicy, |
| 87 | + DistributedTracingPolicy, |
| 88 | + HttpLoggingPolicy, |
| 89 | + ] |
| 90 | + |
| 91 | + @pytest.mark.parametrize("as_list", [False, True], ids=["single", "list"]) |
| 92 | + def test_per_call_policies_inserted_before_retry(self, as_list): |
| 93 | + custom_policies = [Mock(spec=SansIOHTTPPolicy) for _ in range(2 if as_list else 1)] |
| 94 | + arg = custom_policies if as_list else custom_policies[0] |
| 95 | + |
| 96 | + policies = _get_policies(self._make_config(), per_call_policies=arg) |
| 97 | + retry_idx = next(i for i, p in enumerate(policies) if isinstance(p, RetryPolicy)) |
| 98 | + for custom in custom_policies: |
| 99 | + assert policies.index(custom) < retry_idx |
| 100 | + |
| 101 | + @pytest.mark.parametrize("as_list", [False, True], ids=["single", "list"]) |
| 102 | + def test_per_retry_policies_inserted_after_retry(self, as_list): |
| 103 | + custom_policies = [Mock(spec=SansIOHTTPPolicy) for _ in range(2 if as_list else 1)] |
| 104 | + arg = custom_policies if as_list else custom_policies[0] |
| 105 | + |
| 106 | + policies = _get_policies(self._make_config(), per_retry_policies=arg) |
| 107 | + retry_idx = next(i for i, p in enumerate(policies) if isinstance(p, RetryPolicy)) |
| 108 | + for custom in custom_policies: |
| 109 | + assert policies.index(custom) > retry_idx |
| 110 | + |
| 111 | + def test_both_per_call_and_per_retry(self): |
| 112 | + per_call = Mock(spec=SansIOHTTPPolicy) |
| 113 | + per_retry = Mock(spec=SansIOHTTPPolicy) |
| 114 | + |
| 115 | + policies = _get_policies(self._make_config(), per_call_policies=per_call, per_retry_policies=per_retry) |
| 116 | + retry_idx = next(i for i, p in enumerate(policies) if isinstance(p, RetryPolicy)) |
| 117 | + assert policies.index(per_call) < retry_idx |
| 118 | + assert policies.index(per_retry) > retry_idx |
| 119 | + |
| 120 | + |
| 121 | +class TestBuildPipelineOverrides: |
| 122 | + """Tests for policy overrides in build_pipeline and build_async_pipeline.""" |
| 123 | + |
| 124 | + @pytest.mark.parametrize("builder", [build_pipeline, build_async_pipeline]) |
| 125 | + def test_retry_policy_override(self, builder): |
| 126 | + custom_retry = Mock(spec=RetryPolicy) |
| 127 | + pipeline = builder(retry_policy=custom_retry, transport=Mock()) |
| 128 | + assert custom_retry in pipeline._impl_policies |
| 129 | + |
| 130 | + def test_default_retry_policy_when_no_override(self): |
| 131 | + pipeline = build_pipeline(transport=Mock()) |
| 132 | + retry_policies = [p for p in pipeline._impl_policies if isinstance(p, RetryPolicy)] |
| 133 | + assert len(retry_policies) == 1 |
| 134 | + |
| 135 | + @pytest.mark.parametrize("builder", [build_pipeline, build_async_pipeline]) |
| 136 | + def test_policy_override_flows_through(self, builder): |
| 137 | + """Verify that config policy overrides reach the pipeline.""" |
| 138 | + custom_headers = Mock(spec=HeadersPolicy) |
| 139 | + pipeline = builder(headers_policy=custom_headers, transport=Mock()) |
| 140 | + wrapped_policies = [p._policy for p in pipeline._impl_policies if hasattr(p, "_policy")] |
| 141 | + assert custom_headers in wrapped_policies |
0 commit comments