changes
This commit is contained in:
175
tests/test_email_destination.py
Normal file
175
tests/test_email_destination.py
Normal file
@@ -0,0 +1,175 @@
|
||||
import pytest
|
||||
from unittest.mock import Mock, patch
|
||||
from app.email_processor import EmailProcessor
|
||||
|
||||
def test_get_email_destinations_single_email():
|
||||
"""Test getting destination for a single email."""
|
||||
# Mock user and processor
|
||||
mock_user = Mock()
|
||||
processor = EmailProcessor(mock_user)
|
||||
|
||||
# Test data
|
||||
emails = [
|
||||
{
|
||||
'uid': '1',
|
||||
'headers': {
|
||||
'subject': 'Meeting about Q4 goals',
|
||||
'from': 'boss@company.com',
|
||||
'to': 'user@company.com',
|
||||
'date': '2025-01-15'
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
rules = [
|
||||
{
|
||||
'name': 'Work',
|
||||
'rule_text': 'All work-related emails should go to Work folder',
|
||||
'priority': 2
|
||||
},
|
||||
{
|
||||
'name': 'Important',
|
||||
'rule_text': 'Emails from boss@company.com should go to Important folder',
|
||||
'priority': 1
|
||||
}
|
||||
]
|
||||
|
||||
# Mock the API response
|
||||
with patch('requests.post') as mock_post:
|
||||
mock_post.return_value.status_code = 200
|
||||
mock_post.return_value.text = 'Important'
|
||||
|
||||
result = processor.get_email_destinations(emails, rules)
|
||||
|
||||
assert result == {'1': 'Important'}
|
||||
mock_post.assert_called_once()
|
||||
|
||||
|
||||
def test_get_email_destinations_multiple_emails():
|
||||
"""Test getting destinations for multiple emails."""
|
||||
mock_user = Mock()
|
||||
processor = EmailProcessor(mock_user)
|
||||
|
||||
emails = [
|
||||
{
|
||||
'uid': '1',
|
||||
'headers': {
|
||||
'subject': 'Meeting about Q4 goals',
|
||||
'from': 'boss@company.com',
|
||||
'to': 'user@company.com',
|
||||
'date': '2025-01-15'
|
||||
}
|
||||
},
|
||||
{
|
||||
'uid': '2',
|
||||
'headers': {
|
||||
'subject': 'Dinner plans',
|
||||
'from': 'friend@company.com',
|
||||
'to': 'user@company.com',
|
||||
'date': '2025-01-14'
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
rules = [
|
||||
{
|
||||
'name': 'Work',
|
||||
'rule_text': 'All work-related emails should go to Work folder',
|
||||
'priority': 3
|
||||
},
|
||||
{
|
||||
'name': 'Important',
|
||||
'rule_text': 'Emails from boss@company.com should go to Important folder',
|
||||
'priority': 2
|
||||
},
|
||||
{
|
||||
'name': 'Personal',
|
||||
'rule_text': 'All personal emails should go to Personal folder',
|
||||
'priority': 1
|
||||
}
|
||||
]
|
||||
|
||||
with patch('requests.post') as mock_post:
|
||||
# Return different responses for different calls
|
||||
mock_post.side_effect = [
|
||||
type('', (), {'status_code': 200, 'text': 'Important'})(),
|
||||
type('', (), {'status_code': 200, 'text': 'Personal'})()
|
||||
]
|
||||
|
||||
result = processor.get_email_destinations(emails, rules)
|
||||
|
||||
assert result == {
|
||||
'1': 'Important',
|
||||
'2': 'Personal'
|
||||
}
|
||||
assert mock_post.call_count == 2
|
||||
|
||||
|
||||
def test_get_email_destinations_api_failure():
|
||||
"""Test behavior when API call fails."""
|
||||
mock_user = Mock()
|
||||
processor = EmailProcessor(mock_user)
|
||||
|
||||
emails = [
|
||||
{
|
||||
'uid': '1',
|
||||
'headers': {
|
||||
'subject': 'Meeting about Q4 goals',
|
||||
'from': 'boss@company.com',
|
||||
'to': 'user@company.com',
|
||||
'date': '2025-01-15'
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
rules = [
|
||||
{
|
||||
'name': 'Work',
|
||||
'rule_text': 'All work-related emails should go to Work folder',
|
||||
'priority': 1
|
||||
}
|
||||
]
|
||||
|
||||
with patch('requests.post') as mock_post:
|
||||
mock_post.return_value.status_code = 500
|
||||
|
||||
result = processor.get_email_destinations(emails, rules)
|
||||
|
||||
# Should return empty dict on failure
|
||||
assert result == {}
|
||||
mock_post.assert_called_once()
|
||||
|
||||
|
||||
def test_get_email_destinations_no_matching_rules():
|
||||
"""Test when no rules match and email should stay in INBOX."""
|
||||
mock_user = Mock()
|
||||
processor = EmailProcessor(mock_user)
|
||||
|
||||
emails = [
|
||||
{
|
||||
'uid': '1',
|
||||
'headers': {
|
||||
'subject': 'Newsletter',
|
||||
'from': 'newsletter@company.com',
|
||||
'to': 'user@company.com',
|
||||
'date': '2025-01-15'
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
rules = [
|
||||
{
|
||||
'name': 'Work',
|
||||
'rule_text': 'All work-related emails should go to Work folder',
|
||||
'priority': 1
|
||||
}
|
||||
]
|
||||
|
||||
with patch('requests.post') as mock_post:
|
||||
mock_post.return_value.status_code = 200
|
||||
mock_post.return_value.text = 'INBOX'
|
||||
|
||||
result = processor.get_email_destinations(emails, rules)
|
||||
|
||||
assert result == {'1': 'INBOX'}
|
||||
mock_post.assert_called_once()
|
||||
Reference in New Issue
Block a user