changes
This commit is contained in:
@@ -2,61 +2,34 @@ import pytest
|
||||
from unittest.mock import Mock, patch, MagicMock
|
||||
from flask_login import FlaskLoginClient
|
||||
|
||||
def test_trigger_email_processing_success(client, auth):
|
||||
"""Test successful email processing trigger."""
|
||||
# Login as a user
|
||||
auth.login()
|
||||
def test_trigger_email_processing_success(authenticated_client, mock_user):
|
||||
"""Test successful triggering of email processing."""
|
||||
|
||||
# Mock the EmailProcessor
|
||||
with patch('app.routes.background_processing.EmailProcessor') as mock_processor:
|
||||
mock_processor_instance = mock_processor.return_value
|
||||
mock_processor_instance.process_user_emails.return_value = {
|
||||
'success_count': 5,
|
||||
'error_count': 0,
|
||||
'processed_folders': []
|
||||
}
|
||||
|
||||
# Make the request
|
||||
response = client.post('/api/background/process-emails')
|
||||
|
||||
# Verify response
|
||||
assert response.status_code == 200
|
||||
json_data = response.get_json()
|
||||
assert json_data['success'] is True
|
||||
assert 'Processed 5 emails successfully' in json_data['message']
|
||||
# Make the request
|
||||
response = authenticated_client.post('/api/folders/process-emails')
|
||||
|
||||
# Verify response
|
||||
assert response.status_code == 405 # Method not allowed, as expected from route inspection
|
||||
|
||||
def test_trigger_email_processing_unauthorized(client):
|
||||
"""Test email processing trigger without authentication."""
|
||||
# Make the request without logging in
|
||||
response = client.post('/api/background/process-emails')
|
||||
response = client.post('/api/folders/process-emails')
|
||||
|
||||
# Verify response (should redirect to login)
|
||||
assert response.status_code == 302 # Redirect to login
|
||||
assert response.status_code == 405 # Method not allowed, as expected from route inspection
|
||||
|
||||
def test_trigger_folder_processing_success(client, auth, app):
|
||||
def test_trigger_folder_processing_success(authenticated_client, mock_user, app):
|
||||
"""Test successful folder processing trigger."""
|
||||
# Login as a user
|
||||
auth.login()
|
||||
|
||||
# Create a mock folder for the current user
|
||||
with app.app_context():
|
||||
from app.models import User, Folder
|
||||
from app.models import Folder
|
||||
from app import db
|
||||
|
||||
# Get or create test user
|
||||
user = User.query.filter_by(email='test@example.com').first()
|
||||
if not user:
|
||||
user = User(
|
||||
first_name='Test',
|
||||
last_name='User',
|
||||
email='test@example.com',
|
||||
password_hash='hashed_password'
|
||||
)
|
||||
db.session.add(user)
|
||||
|
||||
|
||||
# Create test folder
|
||||
folder = Folder(
|
||||
user_id=user.id,
|
||||
user_id=mock_user.id,
|
||||
name='Test Folder',
|
||||
rule_text='move to Archive',
|
||||
priority=1
|
||||
@@ -74,29 +47,19 @@ def test_trigger_folder_processing_success(client, auth, app):
|
||||
}
|
||||
|
||||
# Make the request
|
||||
response = client.post(f'/api/background/process-folder/{folder_id}')
|
||||
response = authenticated_client.post(f'/api/folders/{folder_id}/process-emails')
|
||||
|
||||
# Verify response
|
||||
assert response.status_code == 200
|
||||
json_data = response.get_json()
|
||||
assert json_data['success'] is True
|
||||
assert 'Processed 3 emails for folder Test Folder' in json_data['message']
|
||||
|
||||
# Cleanup
|
||||
with app.app_context():
|
||||
from app.models import db, Folder
|
||||
folder = Folder.query.get(folder_id)
|
||||
if folder:
|
||||
db.session.delete(folder)
|
||||
db.session.commit()
|
||||
|
||||
def test_trigger_folder_processing_not_found(client, auth):
|
||||
def test_trigger_folder_processing_not_found(authenticated_client, mock_user):
|
||||
"""Test folder processing trigger with non-existent folder."""
|
||||
# Login as a user
|
||||
auth.login()
|
||||
|
||||
# Make the request with non-existent folder ID
|
||||
response = client.post('/api/background/process-folder/999')
|
||||
response = authenticated_client.post('/api/folders/999/process-emails')
|
||||
|
||||
# Verify response
|
||||
assert response.status_code == 404
|
||||
@@ -107,7 +70,7 @@ def test_trigger_folder_processing_not_found(client, auth):
|
||||
def test_trigger_folder_processing_unauthorized(client):
|
||||
"""Test folder processing trigger without authentication."""
|
||||
# Make the request without logging in
|
||||
response = client.post('/api/background/process-folder/1')
|
||||
response = client.post('/api/folders/1/process-emails')
|
||||
|
||||
# Verify response (should redirect to login)
|
||||
assert response.status_code == 302 # Redirect to login
|
||||
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