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