|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +"""Tests for operation_context support on DataverseClient and User-Agent header.""" |
| 5 | + |
| 6 | +import unittest |
| 7 | +from unittest.mock import MagicMock |
| 8 | + |
| 9 | +from azure.core.credentials import TokenCredential |
| 10 | + |
| 11 | +from PowerPlatform.Dataverse.client import DataverseClient |
| 12 | +from PowerPlatform.Dataverse.core.config import DataverseConfig, OperationContext |
| 13 | +from PowerPlatform.Dataverse.data._odata import _ODataClient, _USER_AGENT |
| 14 | + |
| 15 | + |
| 16 | +class TestOperationContextValidation(unittest.TestCase): |
| 17 | + """Tests for OperationContext format validation and PII rejection.""" |
| 18 | + |
| 19 | + def test_valid_single_pair(self): |
| 20 | + ctx = OperationContext(user_agent_context="app=test/1.0") |
| 21 | + self.assertEqual(ctx.user_agent_context, "app=test/1.0") |
| 22 | + |
| 23 | + def test_valid_multiple_pairs(self): |
| 24 | + ctx = OperationContext(user_agent_context="app=test/1.0;skill=dv-data;agent=claude-code") |
| 25 | + self.assertEqual(ctx.user_agent_context, "app=test/1.0;skill=dv-data;agent=claude-code") |
| 26 | + |
| 27 | + def test_valid_with_dots_slashes_hyphens(self): |
| 28 | + ctx = OperationContext(user_agent_context="app=dataverse-skills/1.2.1") |
| 29 | + self.assertEqual(ctx.user_agent_context, "app=dataverse-skills/1.2.1") |
| 30 | + |
| 31 | + def test_reject_empty(self): |
| 32 | + with self.assertRaises(ValueError): |
| 33 | + OperationContext(user_agent_context="") |
| 34 | + |
| 35 | + def test_reject_email(self): |
| 36 | + with self.assertRaises(ValueError): |
| 37 | + OperationContext(user_agent_context="myname@email.com") |
| 38 | + |
| 39 | + def test_reject_freeform_text(self): |
| 40 | + with self.assertRaises(ValueError): |
| 41 | + OperationContext(user_agent_context="my bank password is 1234") |
| 42 | + |
| 43 | + def test_reject_control_chars(self): |
| 44 | + for bad in ["has\rnewline", "has\nnewline", "has\x00null"]: |
| 45 | + with self.assertRaises(ValueError): |
| 46 | + OperationContext(user_agent_context=bad) |
| 47 | + |
| 48 | + def test_reject_spaces(self): |
| 49 | + with self.assertRaises(ValueError): |
| 50 | + OperationContext(user_agent_context="app=my app") |
| 51 | + |
| 52 | + def test_reject_no_equals(self): |
| 53 | + with self.assertRaises(ValueError): |
| 54 | + OperationContext(user_agent_context="justaplainstring") |
| 55 | + |
| 56 | + |
| 57 | +class TestOperationContextConfig(unittest.TestCase): |
| 58 | + """Tests for operation_context on DataverseConfig.""" |
| 59 | + |
| 60 | + def test_default_is_none(self): |
| 61 | + config = DataverseConfig.from_env() |
| 62 | + self.assertIsNone(config.operation_context) |
| 63 | + |
| 64 | + def test_explicit_value(self): |
| 65 | + ctx = OperationContext(user_agent_context="app=test/1.0;agent=claude-code") |
| 66 | + config = DataverseConfig(operation_context=ctx) |
| 67 | + self.assertEqual(config.operation_context.user_agent_context, "app=test/1.0;agent=claude-code") |
| 68 | + |
| 69 | + def test_default_constructor_is_none(self): |
| 70 | + config = DataverseConfig() |
| 71 | + self.assertIsNone(config.operation_context) |
| 72 | + |
| 73 | + |
| 74 | +class TestOperationContextClient(unittest.TestCase): |
| 75 | + """Tests for context kwarg on DataverseClient.""" |
| 76 | + |
| 77 | + def setUp(self): |
| 78 | + self.mock_credential = MagicMock(spec=TokenCredential) |
| 79 | + self.base_url = "https://example.crm.dynamics.com" |
| 80 | + |
| 81 | + def test_kwarg_sets_config(self): |
| 82 | + ctx = OperationContext(user_agent_context="app=test/1.0;skill=dv-data;agent=claude-code") |
| 83 | + client = DataverseClient( |
| 84 | + self.base_url, |
| 85 | + self.mock_credential, |
| 86 | + context=ctx, |
| 87 | + ) |
| 88 | + self.assertEqual( |
| 89 | + client._config.operation_context.user_agent_context, |
| 90 | + "app=test/1.0;skill=dv-data;agent=claude-code", |
| 91 | + ) |
| 92 | + |
| 93 | + def test_no_kwarg_leaves_config_default(self): |
| 94 | + client = DataverseClient(self.base_url, self.mock_credential) |
| 95 | + self.assertIsNone(client._config.operation_context) |
| 96 | + |
| 97 | + def test_config_and_context_raises(self): |
| 98 | + ctx = OperationContext(user_agent_context="app=test/1.0") |
| 99 | + config = DataverseConfig(operation_context=ctx) |
| 100 | + with self.assertRaises(ValueError): |
| 101 | + DataverseClient( |
| 102 | + self.base_url, |
| 103 | + self.mock_credential, |
| 104 | + config=config, |
| 105 | + context=OperationContext(user_agent_context="app=other/2.0"), |
| 106 | + ) |
| 107 | + |
| 108 | + def test_config_alone_works(self): |
| 109 | + ctx = OperationContext(user_agent_context="app=test/1.0;agent=copilot") |
| 110 | + config = DataverseConfig(operation_context=ctx) |
| 111 | + client = DataverseClient(self.base_url, self.mock_credential, config=config) |
| 112 | + self.assertEqual( |
| 113 | + client._config.operation_context.user_agent_context, |
| 114 | + "app=test/1.0;agent=copilot", |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +class TestOperationContextUserAgent(unittest.TestCase): |
| 119 | + """Tests for User-Agent header with operation_context.""" |
| 120 | + |
| 121 | + def setUp(self): |
| 122 | + self.dummy_auth = MagicMock() |
| 123 | + token_result = MagicMock() |
| 124 | + token_result.access_token = "test-token" |
| 125 | + self.dummy_auth._acquire_token.return_value = token_result |
| 126 | + self.base_url = "https://org.example.com" |
| 127 | + |
| 128 | + def test_default_user_agent_unchanged(self): |
| 129 | + odata = _ODataClient(self.dummy_auth, self.base_url) |
| 130 | + headers = odata._headers() |
| 131 | + self.assertEqual(headers["User-Agent"], _USER_AGENT) |
| 132 | + |
| 133 | + def test_operation_context_appended(self): |
| 134 | + ctx_str = "app=dataverse-skills/1.2.1;skill=dv-data;agent=claude-code" |
| 135 | + ctx = OperationContext(user_agent_context=ctx_str) |
| 136 | + config = DataverseConfig(operation_context=ctx) |
| 137 | + odata = _ODataClient(self.dummy_auth, self.base_url, config=config) |
| 138 | + headers = odata._headers() |
| 139 | + self.assertEqual(headers["User-Agent"], f"{_USER_AGENT} ({ctx_str})") |
| 140 | + |
| 141 | + def test_none_context_no_parentheses(self): |
| 142 | + config = DataverseConfig(operation_context=None) |
| 143 | + odata = _ODataClient(self.dummy_auth, self.base_url, config=config) |
| 144 | + headers = odata._headers() |
| 145 | + self.assertNotIn("(", headers["User-Agent"]) |
| 146 | + |
| 147 | + def test_empty_string_rejected_at_creation(self): |
| 148 | + with self.assertRaises(ValueError): |
| 149 | + OperationContext(user_agent_context="") |
0 commit comments