conftest.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """Pytest configuration and fixtures for testing."""
  2. import pytest
  3. from hypothesis import settings
  4. from app import create_app, db
  5. from app.models.admin import Admin
  6. from app.services.auth_service import AuthService
  7. from flask_bcrypt import Bcrypt
  8. bcrypt = Bcrypt()
  9. # Configure hypothesis for minimum 100 examples
  10. settings.register_profile("ci", max_examples=100)
  11. settings.load_profile("ci")
  12. @pytest.fixture
  13. def app():
  14. """Create application for testing."""
  15. app = create_app('testing')
  16. yield app
  17. @pytest.fixture
  18. def client(app):
  19. """Create test client."""
  20. return app.test_client()
  21. @pytest.fixture
  22. def db_session(app):
  23. """Create database session for testing."""
  24. with app.app_context():
  25. db.create_all()
  26. yield db
  27. db.drop_all()
  28. @pytest.fixture
  29. def test_admin(db_session):
  30. """Create a test admin for authentication tests."""
  31. password_hash = bcrypt.generate_password_hash('testpassword').decode('utf-8')
  32. admin = Admin(
  33. username='testadmin',
  34. password_hash=password_hash
  35. )
  36. db.session.add(admin)
  37. db.session.commit()
  38. # Return a dict with admin info
  39. return {
  40. 'id': admin.id,
  41. 'username': admin.username,
  42. 'password': 'testpassword'
  43. }
  44. @pytest.fixture
  45. def auth_token(test_admin):
  46. """Get a valid JWT token for testing protected endpoints."""
  47. admin = Admin.query.filter_by(username=test_admin['username']).first()
  48. token = AuthService.generate_token(admin)
  49. return token
  50. @pytest.fixture
  51. def auth_headers(auth_token):
  52. """Get headers with JWT token for authenticated requests."""
  53. return {'Authorization': f'Bearer {auth_token}'}