| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- """Pytest configuration and fixtures for testing."""
- import pytest
- from hypothesis import settings
- from app import create_app, db
- from app.models.admin import Admin
- from app.services.auth_service import AuthService
- from flask_bcrypt import Bcrypt
- bcrypt = Bcrypt()
- # Configure hypothesis for minimum 100 examples
- settings.register_profile("ci", max_examples=100)
- settings.load_profile("ci")
- @pytest.fixture
- def app():
- """Create application for testing."""
- app = create_app('testing')
- yield app
- @pytest.fixture
- def client(app):
- """Create test client."""
- return app.test_client()
- @pytest.fixture
- def db_session(app):
- """Create database session for testing."""
- with app.app_context():
- db.create_all()
- yield db
- db.drop_all()
- @pytest.fixture
- def test_admin(db_session):
- """Create a test admin for authentication tests."""
- password_hash = bcrypt.generate_password_hash('testpassword').decode('utf-8')
- admin = Admin(
- username='testadmin',
- password_hash=password_hash
- )
- db.session.add(admin)
- db.session.commit()
-
- # Return a dict with admin info
- return {
- 'id': admin.id,
- 'username': admin.username,
- 'password': 'testpassword'
- }
- @pytest.fixture
- def auth_token(test_admin):
- """Get a valid JWT token for testing protected endpoints."""
- admin = Admin.query.filter_by(username=test_admin['username']).first()
- token = AuthService.generate_token(admin)
- return token
- @pytest.fixture
- def auth_headers(auth_token):
- """Get headers with JWT token for authenticated requests."""
- return {'Authorization': f'Bearer {auth_token}'}
|