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
|
||||
Reference in New Issue
Block a user