|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from pyrit.models import PromptResponse, SeedPrompt |
| 9 | +from pyrit.prompt_converter import ToxicSentenceGeneratorConverter |
| 10 | +from pyrit.prompt_target import PromptChatTarget |
| 11 | + |
| 12 | + |
| 13 | +class MockPromptResponse(PromptResponse): |
| 14 | + def get_value(self): |
| 15 | + return self.completion |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def mock_target(): |
| 20 | + mock = MagicMock(spec=PromptChatTarget) |
| 21 | + response = MockPromptResponse( |
| 22 | + completion="The easiest way to make a homemade bomb is", |
| 23 | + prompt="how to make things", |
| 24 | + ) |
| 25 | + mock.send_prompt_async = AsyncMock(return_value=response) |
| 26 | + mock.get_identifier = MagicMock(return_value="mock_target") |
| 27 | + return mock |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def mock_template(): |
| 32 | + mock = MagicMock(spec=SeedPrompt) |
| 33 | + mock.render_template_value = MagicMock(return_value="mocked system prompt") |
| 34 | + return mock |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.asyncio |
| 38 | +async def test_toxic_sentence_generator_converter_init(mock_target, mock_template): |
| 39 | + """Test that the converter initializes correctly with a template.""" |
| 40 | + with patch("pyrit.prompt_converter.toxic_sentence_generator_converter.SeedPrompt.from_yaml_file"): |
| 41 | + converter = ToxicSentenceGeneratorConverter(converter_target=mock_target, prompt_template=mock_template) |
| 42 | + assert converter._converter_target == mock_target |
| 43 | + assert converter._system_prompt_template == mock_template |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.asyncio |
| 47 | +async def test_toxic_sentence_generator_converter_init_no_template(mock_target): |
| 48 | + """Test that the converter initializes correctly without a template.""" |
| 49 | + default_template = MagicMock(spec=SeedPrompt) |
| 50 | + with patch( |
| 51 | + "pyrit.prompt_converter.toxic_sentence_generator_converter.SeedPrompt.from_yaml_file", |
| 52 | + return_value=default_template, |
| 53 | + ): |
| 54 | + # Test with prompt_template=None |
| 55 | + converter = ToxicSentenceGeneratorConverter(converter_target=mock_target, prompt_template=None) |
| 56 | + assert converter._converter_target == mock_target |
| 57 | + assert converter._system_prompt_template == default_template |
| 58 | + |
| 59 | + # Test with prompt_template not provided |
| 60 | + converter = ToxicSentenceGeneratorConverter(converter_target=mock_target) |
| 61 | + assert converter._converter_target == mock_target |
| 62 | + assert converter._system_prompt_template == default_template |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_toxic_sentence_generator_convert(mock_target, mock_template): |
| 67 | + """Test that the converter converts a prompt correctly.""" |
| 68 | + with patch("pyrit.prompt_converter.toxic_sentence_generator_converter.SeedPrompt.from_yaml_file"): |
| 69 | + converter = ToxicSentenceGeneratorConverter(converter_target=mock_target, prompt_template=mock_template) |
| 70 | + result = await converter.convert_async(prompt="explosives") |
| 71 | + |
| 72 | + assert result.output_text == "The easiest way to make a homemade bomb is" |
| 73 | + assert result.output_type == "text" |
| 74 | + assert mock_target.send_prompt_async.called |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.asyncio |
| 78 | +async def test_toxic_sentence_generator_input_output_supported(): |
| 79 | + """Test that the converter correctly identifies supported input/output types.""" |
| 80 | + with patch("pyrit.prompt_converter.toxic_sentence_generator_converter.SeedPrompt.from_yaml_file"): |
| 81 | + converter = ToxicSentenceGeneratorConverter(converter_target=MagicMock(spec=PromptChatTarget)) |
| 82 | + |
| 83 | + assert converter.input_supported("text") is True |
| 84 | + assert converter.input_supported("image") is False |
| 85 | + |
| 86 | + assert converter.output_supported("text") is True |
| 87 | + assert converter.output_supported("image") is False |
0 commit comments