test update

This commit is contained in:
Dries Peeters
2025-10-24 18:16:15 +02:00
parent dae25edece
commit 7f918a378d
6 changed files with 51 additions and 36 deletions

View File

@@ -178,6 +178,12 @@ def admin_user(app):
return admin
@pytest.fixture
def auth_user(user):
"""Alias for user fixture (for backward compatibility with older tests)."""
return user
@pytest.fixture
def multiple_users(app):
"""Create multiple test users."""

View File

@@ -12,9 +12,9 @@ class TestKeyboardShortcutsRoutes:
"""Test keyboard shortcuts routes"""
@pytest.fixture(autouse=True)
def setup(self, client, auth_user):
def setup(self, authenticated_client, auth_user):
"""Setup for each test"""
self.client = client
self.client = authenticated_client
self.user = auth_user
def test_keyboard_shortcuts_settings_page(self):
@@ -54,9 +54,9 @@ class TestKeyboardShortcutsIntegration:
"""Integration tests for keyboard shortcuts"""
@pytest.fixture(autouse=True)
def setup(self, client, auth_user):
def setup(self, authenticated_client, auth_user):
"""Setup for each test"""
self.client = client
self.client = authenticated_client
self.user = auth_user
def test_keyboard_shortcuts_in_base_template(self):
@@ -160,7 +160,10 @@ def app():
'TESTING': True,
'SQLALCHEMY_DATABASE_URI': 'sqlite:///:memory:',
'WTF_CSRF_ENABLED': False,
'SECRET_KEY': 'test-secret-key'
'SECRET_KEY': 'test-secret-key-do-not-use-in-production',
'SERVER_NAME': 'localhost:5000',
'APPLICATION_ROOT': '/',
'PREFERRED_URL_SCHEME': 'http'
})
with app.app_context():
@@ -184,26 +187,29 @@ def runner(app):
@pytest.fixture
def auth_user(app):
"""Create and authenticate a test user"""
"""Create a test user for authentication"""
with app.app_context():
user = User(
username='testuser',
email='test@example.com',
is_active=True,
role='user'
)
user.set_password('password123')
user.is_active = True # Set after creation
db.session.add(user)
db.session.commit()
# Login the user
from flask_login import login_user
with app.test_request_context():
login_user(user)
db.session.refresh(user)
return user
@pytest.fixture
def authenticated_client(client, auth_user):
"""Create an authenticated test client"""
with client.session_transaction() as sess:
sess['_user_id'] = str(auth_user.id)
sess['_fresh'] = True
return client
@pytest.fixture
def admin_user(app):
"""Create and authenticate an admin user"""
@@ -211,10 +217,9 @@ def admin_user(app):
user = User(
username='admin',
email='admin@example.com',
is_active=True,
role='admin'
)
user.set_password('admin123')
user.is_active = True # Set after creation
db.session.add(user)
db.session.commit()
@@ -268,9 +273,9 @@ class TestKeyboardShortcutsPerformance:
"""Test keyboard shortcuts performance"""
@pytest.fixture(autouse=True)
def setup(self, client, auth_user):
def setup(self, authenticated_client, auth_user):
"""Setup for each test"""
self.client = client
self.client = authenticated_client
self.user = auth_user
def test_settings_page_loads_quickly(self):
@@ -304,9 +309,9 @@ class TestKeyboardShortcutsSecurity:
"""Test keyboard shortcuts security"""
@pytest.fixture(autouse=True)
def setup(self, client, auth_user):
def setup(self, authenticated_client, auth_user):
"""Setup for each test"""
self.client = client
self.client = authenticated_client
self.user = auth_user
def test_settings_requires_authentication(self):
@@ -334,9 +339,9 @@ class TestKeyboardShortcutsEdgeCases:
"""Test edge cases for keyboard shortcuts"""
@pytest.fixture(autouse=True)
def setup(self, client, auth_user):
def setup(self, authenticated_client, auth_user):
"""Setup for each test"""
self.client = client
self.client = authenticated_client
self.user = auth_user
def test_settings_page_with_no_shortcuts(self):

View File

@@ -244,7 +244,7 @@ class TestProjectArchivingRoutes:
)
assert response.status_code == 200
assert b'Only administrators can archive projects' in response.data
assert b'You do not have permission to archive projects' in response.data
class TestArchivedProjectValidation:

View File

@@ -297,7 +297,7 @@ class TestProjectArchiveProperties:
# Create a temporary user
temp_user = User(username='tempuser', email='temp@test.com')
temp_user.set_password('password')
temp_user.is_active = True # Set after creation
db.session.add(temp_user)
db.session.commit()
temp_user_id = temp_user.id

View File

@@ -32,12 +32,16 @@ def client(app):
def test_user(app):
"""Create a test user with default rounding preferences"""
with app.app_context():
user = User(username='testuser', role='user')
user.time_rounding_enabled = True
user.time_rounding_minutes = 15
user.time_rounding_method = 'nearest'
db.session.add(user)
db.session.commit()
# Check if user already exists (for PostgreSQL tests)
user = User.query.filter_by(username='roundinguser').first()
if not user:
user = User(username='roundinguser', role='user')
user.is_active = True # Set after creation
user.time_rounding_enabled = True
user.time_rounding_minutes = 15
user.time_rounding_method = 'nearest'
db.session.add(user)
db.session.commit()
# Return the user ID instead of the object
user_id = user.id
@@ -55,10 +59,9 @@ def test_project(app, test_user):
user = User.query.get(test_user.id)
project = Project(
name='Test Project',
client='Test Client',
status='active',
created_by_id=user.id
client='Test Client'
)
project.status = 'active' # Set after creation
db.session.add(project)
db.session.commit()
@@ -84,6 +87,7 @@ class TestUserRoundingPreferences:
"""Test default rounding values for new users"""
with app.app_context():
user = User(username='newuser', role='user')
user.is_active = True # Set after creation
db.session.add(user)
db.session.commit()

View File

@@ -33,6 +33,7 @@ def authenticated_user(app, client):
"""Create and authenticate a test user"""
with app.app_context():
user = User(username='smoketest', role='user', email='smoke@test.com')
user.is_active = True # Set after creation
user.time_rounding_enabled = True
user.time_rounding_minutes = 15
user.time_rounding_method = 'nearest'
@@ -40,10 +41,9 @@ def authenticated_user(app, client):
project = Project(
name='Smoke Test Project',
client='Smoke Test Client',
status='active',
created_by_id=1
client='Smoke Test Client'
)
project.status = 'active' # Set after creation
db.session.add(project)
db.session.commit()