|
| 1 | +import pytest |
| 2 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 3 | +from azure.ai.evaluation.red_team._red_team import RedTeam, RiskCategory, SupportedLanguages |
| 4 | +from azure.core.credentials import TokenCredential |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def mock_azure_ai_project(): |
| 9 | + return { |
| 10 | + "subscription_id": "test-subscription", |
| 11 | + "resource_group_name": "test-resource-group", |
| 12 | + "project_name": "test-project", |
| 13 | + } |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def mock_credential(): |
| 18 | + return MagicMock(spec=TokenCredential) |
| 19 | + |
| 20 | + |
| 21 | +class TestRedTeamLanguageSupport: |
| 22 | + """Test language support functionality in RedTeam class.""" |
| 23 | + |
| 24 | + def test_red_team_init_default_language(self, mock_azure_ai_project, mock_credential): |
| 25 | + """Test that RedTeam initializes with default English language.""" |
| 26 | + with patch("azure.ai.evaluation.red_team._red_team.GeneratedRAIClient"), patch( |
| 27 | + "azure.ai.evaluation.red_team._red_team.setup_logger" |
| 28 | + ) as mock_setup_logger, patch("azure.ai.evaluation.red_team._red_team.initialize_pyrit"), patch( |
| 29 | + "azure.ai.evaluation.red_team._red_team._AttackObjectiveGenerator" |
| 30 | + ): |
| 31 | + |
| 32 | + mock_logger = MagicMock() |
| 33 | + mock_setup_logger.return_value = mock_logger |
| 34 | + |
| 35 | + agent = RedTeam( |
| 36 | + azure_ai_project=mock_azure_ai_project, |
| 37 | + credential=mock_credential, |
| 38 | + risk_categories=[RiskCategory.Violence], |
| 39 | + num_objectives=5, |
| 40 | + ) |
| 41 | + |
| 42 | + # Verify default language is English |
| 43 | + assert agent.language == SupportedLanguages.English |
| 44 | + |
| 45 | + def test_red_team_init_custom_language(self, mock_azure_ai_project, mock_credential): |
| 46 | + """Test that RedTeam initializes with custom language.""" |
| 47 | + with patch("azure.ai.evaluation.red_team._red_team.GeneratedRAIClient"), patch( |
| 48 | + "azure.ai.evaluation.red_team._red_team.setup_logger" |
| 49 | + ) as mock_setup_logger, patch("azure.ai.evaluation.red_team._red_team.initialize_pyrit"), patch( |
| 50 | + "azure.ai.evaluation.red_team._red_team._AttackObjectiveGenerator" |
| 51 | + ): |
| 52 | + |
| 53 | + mock_logger = MagicMock() |
| 54 | + mock_setup_logger.return_value = mock_logger |
| 55 | + |
| 56 | + # Test with Spanish language |
| 57 | + agent = RedTeam( |
| 58 | + azure_ai_project=mock_azure_ai_project, |
| 59 | + credential=mock_credential, |
| 60 | + risk_categories=[RiskCategory.Violence], |
| 61 | + num_objectives=5, |
| 62 | + language=SupportedLanguages.Spanish, |
| 63 | + ) |
| 64 | + |
| 65 | + assert agent.language == SupportedLanguages.Spanish |
| 66 | + |
| 67 | + @pytest.mark.parametrize( |
| 68 | + "language", |
| 69 | + [ |
| 70 | + SupportedLanguages.English, |
| 71 | + SupportedLanguages.Spanish, |
| 72 | + SupportedLanguages.French, |
| 73 | + SupportedLanguages.German, |
| 74 | + SupportedLanguages.Italian, |
| 75 | + SupportedLanguages.Portuguese, |
| 76 | + SupportedLanguages.Japanese, |
| 77 | + SupportedLanguages.Korean, |
| 78 | + SupportedLanguages.SimplifiedChinese, |
| 79 | + ], |
| 80 | + ) |
| 81 | + def test_red_team_init_all_supported_languages(self, mock_azure_ai_project, mock_credential, language): |
| 82 | + """Test that RedTeam initializes correctly with all supported languages.""" |
| 83 | + with patch("azure.ai.evaluation.red_team._red_team.GeneratedRAIClient"), patch( |
| 84 | + "azure.ai.evaluation.red_team._red_team.setup_logger" |
| 85 | + ) as mock_setup_logger, patch("azure.ai.evaluation.red_team._red_team.initialize_pyrit"), patch( |
| 86 | + "azure.ai.evaluation.red_team._red_team._AttackObjectiveGenerator" |
| 87 | + ): |
| 88 | + |
| 89 | + mock_logger = MagicMock() |
| 90 | + mock_setup_logger.return_value = mock_logger |
| 91 | + |
| 92 | + agent = RedTeam( |
| 93 | + azure_ai_project=mock_azure_ai_project, |
| 94 | + credential=mock_credential, |
| 95 | + risk_categories=[RiskCategory.Violence], |
| 96 | + num_objectives=5, |
| 97 | + language=language, |
| 98 | + ) |
| 99 | + |
| 100 | + assert agent.language == language |
| 101 | + |
| 102 | + @pytest.mark.asyncio |
| 103 | + async def test_get_attack_objectives_passes_language(self, mock_azure_ai_project, mock_credential): |
| 104 | + """Test that _get_attack_objectives passes language parameter to generated RAI client.""" |
| 105 | + with patch("azure.ai.evaluation.red_team._red_team.GeneratedRAIClient") as mock_rai_client_class, patch( |
| 106 | + "azure.ai.evaluation.red_team._red_team.setup_logger" |
| 107 | + ) as mock_setup_logger, patch("azure.ai.evaluation.red_team._red_team.initialize_pyrit"), patch( |
| 108 | + "azure.ai.evaluation.red_team._red_team._AttackObjectiveGenerator" |
| 109 | + ) as mock_attack_obj_generator_class: |
| 110 | + |
| 111 | + mock_logger = MagicMock() |
| 112 | + mock_setup_logger.return_value = mock_logger |
| 113 | + |
| 114 | + # Set up mock RAI client instance |
| 115 | + mock_rai_client = MagicMock() |
| 116 | + mock_rai_client.get_attack_objectives = AsyncMock( |
| 117 | + return_value=[ |
| 118 | + { |
| 119 | + "id": "test-id", |
| 120 | + "messages": [{"role": "user", "content": "test prompt"}], |
| 121 | + "metadata": {"target_harms": [{"risk-type": "violence"}]}, |
| 122 | + } |
| 123 | + ] |
| 124 | + ) |
| 125 | + mock_rai_client_class.return_value = mock_rai_client |
| 126 | + |
| 127 | + # Set up mock attack objective generator instance |
| 128 | + mock_attack_obj_generator = MagicMock() |
| 129 | + mock_attack_obj_generator.num_objectives = 5 |
| 130 | + mock_attack_obj_generator.custom_attack_seed_prompts = None |
| 131 | + mock_attack_obj_generator.validated_prompts = False |
| 132 | + mock_attack_obj_generator_class.return_value = mock_attack_obj_generator |
| 133 | + |
| 134 | + # Create RedTeam instance with Spanish language |
| 135 | + agent = RedTeam( |
| 136 | + azure_ai_project=mock_azure_ai_project, |
| 137 | + credential=mock_credential, |
| 138 | + risk_categories=[RiskCategory.Violence], |
| 139 | + num_objectives=5, |
| 140 | + language=SupportedLanguages.Spanish, |
| 141 | + ) |
| 142 | + |
| 143 | + agent.generated_rai_client = mock_rai_client |
| 144 | + agent.scan_session_id = "test-session" |
| 145 | + |
| 146 | + # Call _get_attack_objectives |
| 147 | + await agent._get_attack_objectives( |
| 148 | + risk_category=RiskCategory.Violence, |
| 149 | + application_scenario="test scenario", |
| 150 | + strategy="baseline", |
| 151 | + ) |
| 152 | + |
| 153 | + # Verify that get_attack_objectives was called with Spanish language |
| 154 | + mock_rai_client.get_attack_objectives.assert_called_once() |
| 155 | + call_args = mock_rai_client.get_attack_objectives.call_args |
| 156 | + assert call_args.kwargs["language"] == SupportedLanguages.Spanish.value |
| 157 | + |
| 158 | + @pytest.mark.asyncio |
| 159 | + async def test_get_attack_objectives_tense_strategy_passes_language(self, mock_azure_ai_project, mock_credential): |
| 160 | + """Test that _get_attack_objectives passes language parameter for tense strategy.""" |
| 161 | + with patch("azure.ai.evaluation.red_team._red_team.GeneratedRAIClient") as mock_rai_client_class, patch( |
| 162 | + "azure.ai.evaluation.red_team._red_team.setup_logger" |
| 163 | + ) as mock_setup_logger, patch("azure.ai.evaluation.red_team._red_team.initialize_pyrit"), patch( |
| 164 | + "azure.ai.evaluation.red_team._red_team._AttackObjectiveGenerator" |
| 165 | + ) as mock_attack_obj_generator_class: |
| 166 | + |
| 167 | + mock_logger = MagicMock() |
| 168 | + mock_setup_logger.return_value = mock_logger |
| 169 | + |
| 170 | + # Set up mock RAI client instance |
| 171 | + mock_rai_client = MagicMock() |
| 172 | + mock_rai_client.get_attack_objectives = AsyncMock( |
| 173 | + return_value=[ |
| 174 | + { |
| 175 | + "id": "test-id", |
| 176 | + "messages": [{"role": "user", "content": "test prompt"}], |
| 177 | + "metadata": {"target_harms": [{"risk-type": "violence"}]}, |
| 178 | + } |
| 179 | + ] |
| 180 | + ) |
| 181 | + mock_rai_client_class.return_value = mock_rai_client |
| 182 | + |
| 183 | + # Set up mock attack objective generator instance |
| 184 | + mock_attack_obj_generator = MagicMock() |
| 185 | + mock_attack_obj_generator.num_objectives = 5 |
| 186 | + mock_attack_obj_generator.custom_attack_seed_prompts = None |
| 187 | + mock_attack_obj_generator.validated_prompts = False |
| 188 | + mock_attack_obj_generator_class.return_value = mock_attack_obj_generator |
| 189 | + |
| 190 | + # Create RedTeam instance with French language |
| 191 | + agent = RedTeam( |
| 192 | + azure_ai_project=mock_azure_ai_project, |
| 193 | + credential=mock_credential, |
| 194 | + risk_categories=[RiskCategory.Violence], |
| 195 | + num_objectives=5, |
| 196 | + language=SupportedLanguages.French, |
| 197 | + ) |
| 198 | + |
| 199 | + agent.generated_rai_client = mock_rai_client |
| 200 | + agent.scan_session_id = "test-session" |
| 201 | + |
| 202 | + # Call _get_attack_objectives with tense strategy |
| 203 | + await agent._get_attack_objectives( |
| 204 | + risk_category=RiskCategory.Violence, |
| 205 | + application_scenario="test scenario", |
| 206 | + strategy="tense", |
| 207 | + ) |
| 208 | + |
| 209 | + # Verify that get_attack_objectives was called with French language and tense strategy |
| 210 | + mock_rai_client.get_attack_objectives.assert_called_once() |
| 211 | + call_args = mock_rai_client.get_attack_objectives.call_args |
| 212 | + assert call_args.kwargs["language"] == SupportedLanguages.French.value # French language code |
| 213 | + assert call_args.kwargs["strategy"] == "tense" |
0 commit comments