|
| 1 | +from datetime import datetime |
| 2 | +from unittest.mock import AsyncMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from mcp_email_server.config import EmailServer, EmailSettings |
| 7 | +from mcp_email_server.emails.classic import ClassicEmailHandler, EmailClient |
| 8 | +from mcp_email_server.emails.models import EmailData, EmailPageResponse |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def email_settings(): |
| 13 | + return EmailSettings( |
| 14 | + account_name="test_account", |
| 15 | + full_name="Test User", |
| 16 | + email_address="test@example.com", |
| 17 | + incoming=EmailServer( |
| 18 | + user_name="test_user", |
| 19 | + password="test_password", |
| 20 | + host="imap.example.com", |
| 21 | + port=993, |
| 22 | + use_ssl=True, |
| 23 | + ), |
| 24 | + outgoing=EmailServer( |
| 25 | + user_name="test_user", |
| 26 | + password="test_password", |
| 27 | + host="smtp.example.com", |
| 28 | + port=465, |
| 29 | + use_ssl=True, |
| 30 | + ), |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def classic_handler(email_settings): |
| 36 | + return ClassicEmailHandler(email_settings) |
| 37 | + |
| 38 | + |
| 39 | +class TestClassicEmailHandler: |
| 40 | + def test_init(self, email_settings): |
| 41 | + """Test initialization of ClassicEmailHandler.""" |
| 42 | + handler = ClassicEmailHandler(email_settings) |
| 43 | + |
| 44 | + assert handler.email_settings == email_settings |
| 45 | + assert isinstance(handler.incoming_client, EmailClient) |
| 46 | + assert isinstance(handler.outgoing_client, EmailClient) |
| 47 | + |
| 48 | + # Check that clients are initialized correctly |
| 49 | + assert handler.incoming_client.email_server == email_settings.incoming |
| 50 | + assert handler.outgoing_client.email_server == email_settings.outgoing |
| 51 | + assert handler.outgoing_client.sender == f"{email_settings.full_name} <{email_settings.email_address}>" |
| 52 | + |
| 53 | + @pytest.mark.asyncio |
| 54 | + async def test_get_emails(self, classic_handler): |
| 55 | + """Test get_emails method.""" |
| 56 | + # Create test data |
| 57 | + now = datetime.now() |
| 58 | + email_data = { |
| 59 | + "subject": "Test Subject", |
| 60 | + "from": "sender@example.com", |
| 61 | + "body": "Test Body", |
| 62 | + "date": now, |
| 63 | + "attachments": [], |
| 64 | + } |
| 65 | + |
| 66 | + # Mock the get_emails_stream method to yield our test data |
| 67 | + mock_stream = AsyncMock() |
| 68 | + mock_stream.__aiter__.return_value = [email_data] |
| 69 | + |
| 70 | + # Mock the get_email_count method |
| 71 | + mock_count = AsyncMock(return_value=1) |
| 72 | + |
| 73 | + # Apply the mocks |
| 74 | + with patch.object(classic_handler.incoming_client, "get_emails_stream", return_value=mock_stream): |
| 75 | + with patch.object(classic_handler.incoming_client, "get_email_count", mock_count): |
| 76 | + # Call the method |
| 77 | + result = await classic_handler.get_emails( |
| 78 | + page=1, |
| 79 | + page_size=10, |
| 80 | + before=now, |
| 81 | + since=None, |
| 82 | + subject="Test", |
| 83 | + body=None, |
| 84 | + text=None, |
| 85 | + from_address="sender@example.com", |
| 86 | + to_address=None, |
| 87 | + ) |
| 88 | + |
| 89 | + # Verify the result |
| 90 | + assert isinstance(result, EmailPageResponse) |
| 91 | + assert result.page == 1 |
| 92 | + assert result.page_size == 10 |
| 93 | + assert result.before == now |
| 94 | + assert result.since is None |
| 95 | + assert result.subject == "Test" |
| 96 | + assert result.body is None |
| 97 | + assert result.text is None |
| 98 | + assert len(result.emails) == 1 |
| 99 | + assert isinstance(result.emails[0], EmailData) |
| 100 | + assert result.emails[0].subject == "Test Subject" |
| 101 | + assert result.emails[0].sender == "sender@example.com" |
| 102 | + assert result.emails[0].body == "Test Body" |
| 103 | + assert result.emails[0].date == now |
| 104 | + assert result.emails[0].attachments == [] |
| 105 | + assert result.total == 1 |
| 106 | + |
| 107 | + # Verify the client methods were called correctly |
| 108 | + classic_handler.incoming_client.get_emails_stream.assert_called_once_with( |
| 109 | + 1, 10, now, None, "Test", None, None, "sender@example.com", None |
| 110 | + ) |
| 111 | + mock_count.assert_called_once_with(now, None, "Test", None, None, "sender@example.com", None) |
| 112 | + |
| 113 | + @pytest.mark.asyncio |
| 114 | + async def test_send_email(self, classic_handler): |
| 115 | + """Test send_email method.""" |
| 116 | + # Mock the outgoing_client.send_email method |
| 117 | + mock_send = AsyncMock() |
| 118 | + |
| 119 | + # Apply the mock |
| 120 | + with patch.object(classic_handler.outgoing_client, "send_email", mock_send): |
| 121 | + # Call the method |
| 122 | + await classic_handler.send_email( |
| 123 | + recipients=["recipient@example.com"], |
| 124 | + subject="Test Subject", |
| 125 | + body="Test Body", |
| 126 | + cc=["cc@example.com"], |
| 127 | + bcc=["bcc@example.com"], |
| 128 | + ) |
| 129 | + |
| 130 | + # Verify the client method was called correctly |
| 131 | + mock_send.assert_called_once_with( |
| 132 | + ["recipient@example.com"], |
| 133 | + "Test Subject", |
| 134 | + "Test Body", |
| 135 | + ["cc@example.com"], |
| 136 | + ["bcc@example.com"], |
| 137 | + ) |
0 commit comments