113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
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()
|
|
|
|
# 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']
|
|
|
|
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')
|
|
|
|
# Verify response (should redirect to login)
|
|
assert response.status_code == 302 # Redirect to login
|
|
|
|
def test_trigger_folder_processing_success(client, auth, 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 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,
|
|
name='Test Folder',
|
|
rule_text='move to Archive',
|
|
priority=1
|
|
)
|
|
db.session.add(folder)
|
|
db.session.commit()
|
|
folder_id = folder.id
|
|
|
|
# Mock the EmailProcessor
|
|
with patch('app.routes.background_processing.EmailProcessor') as mock_processor:
|
|
mock_processor_instance = mock_processor.return_value
|
|
mock_processor_instance.process_folder_emails.return_value = {
|
|
'processed_count': 3,
|
|
'error_count': 0
|
|
}
|
|
|
|
# Make the request
|
|
response = client.post(f'/api/background/process-folder/{folder_id}')
|
|
|
|
# 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):
|
|
"""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')
|
|
|
|
# Verify response
|
|
assert response.status_code == 404
|
|
json_data = response.get_json()
|
|
assert json_data['success'] is False
|
|
assert 'Folder not found or access denied' in json_data['error']
|
|
|
|
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')
|
|
|
|
# Verify response (should redirect to login)
|
|
assert response.status_code == 302 # Redirect to login |