85 lines
2.3 KiB
Python
85 lines
2.3 KiB
Python
import pytest
|
|
import sys
|
|
import os
|
|
import flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_login import login_user
|
|
|
|
# Add the project root directory to the Python path
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
|
|
from app import create_app, db
|
|
from app.models import User, Folder
|
|
from app.auth import auth
|
|
import uuid
|
|
|
|
@pytest.fixture(scope="function")
|
|
def app():
|
|
"""Create a fresh app with in-memory database for each test."""
|
|
app = create_app('testing')
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False # Recommended for Flask-SQLAlchemy
|
|
|
|
with app.app_context():
|
|
db.create_all() # Create tables for your models
|
|
db.session.begin()
|
|
yield app # Yield the app for tests to use
|
|
db.session.close()
|
|
db.drop_all() # Clean up after tests
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app):
|
|
"""A test client for the app."""
|
|
return app.test_client()
|
|
|
|
@pytest.fixture(scope="function")
|
|
def mock_user(app):
|
|
"""Create a mock user for testing."""
|
|
user = User(
|
|
first_name='Test',
|
|
last_name='User',
|
|
email='test@example.com',
|
|
password_hash='hashed_password' # Will be properly hashed in real tests
|
|
)
|
|
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
return user
|
|
|
|
@pytest.fixture(scope="function")
|
|
def authenticated_client(client, mock_user):
|
|
"""Create a test client with authenticated user."""
|
|
with client.session_transaction() as sess:
|
|
sess['_user_id'] = str(mock_user.id)
|
|
sess['_fresh'] = True
|
|
return client
|
|
|
|
@pytest.fixture(scope="function")
|
|
def mock_folder(app, mock_user):
|
|
"""Create a mock folder for testing."""
|
|
folder = Folder(
|
|
user_id=mock_user.id,
|
|
name='Test Folder',
|
|
rule_text='Test rule',
|
|
priority=1
|
|
)
|
|
db.session.add(folder)
|
|
db.session.commit()
|
|
return folder
|
|
|
|
@pytest.fixture(scope="function")
|
|
def mock_user_with_password(app):
|
|
"""Create a mock user with proper password hashing for testing."""
|
|
user = User(
|
|
first_name='Test',
|
|
last_name='User',
|
|
email='test@example.com'
|
|
)
|
|
user.set_password('testpassword')
|
|
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
return user
|
|
|