import pytest import json from app import create_app, db from app.models import User @pytest.fixture def client(): app = create_app('testing') app.config['TESTING'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' with app.test_client() as client: with app.app_context(): db.create_all() # Create user 1 user1 = User(username='test1', role='user', email='test1@example.com', employee_id='EMP001') user1.set_password('test1') db.session.add(user1) # Create user 2 (for conflict testing) user2 = User(username='test2', role='user', email='test2@example.com', employee_id='EMP002') user2.set_password('test2') db.session.add(user2) db.session.commit() yield client db.session.remove() db.drop_all() def get_headers(client, username='test1', password='test1'): res = client.post('/api/user/login', json={'username': username, 'password': password}) assert res.status_code == 200 data = res.get_json() return { 'X-User-Id': str(data['id']), 'X-Username': data['username'], 'X-Role': data['role'], 'Content-Type': 'application/json' } def test_profile_update_all_fields(client): headers = get_headers(client) # 1. Update all fields new_data = { 'email': 'new_email@example.com', 'employee_id': 'EMP999', 'department': 'New Dept', 'real_name': 'New Name', 'phone_number': '1234567890', 'language': 'en', 'theme_preference': 'dark', } res = client.put('/api/user/profile', headers=headers, json=new_data) assert res.status_code == 200, f"Update failed: {res.get_json()}" # 2. Verify updates res = client.get('/api/user/profile', headers=headers) assert res.status_code == 200 data = res.get_json() assert data['email'] == new_data['email'] assert data['employee_id'] == new_data['employee_id'] assert data['department'] == new_data['department'] assert data['real_name'] == new_data['real_name'] assert data['phone_number'] == new_data['phone_number'] assert data['language'] == new_data['language'] assert data['theme_preference'] == new_data['theme_preference'] def test_profile_update_conflict(client): headers1 = get_headers(client, 'test1', 'test1') # 1. Try to update email to test2's email res = client.put('/api/user/profile', headers=headers1, json={'email': 'test2@example.com'}) assert res.status_code == 400 assert 'Email already exists' in res.get_json()['error'] # 2. Try to update employee_id to test2's employee_id res = client.put('/api/user/profile', headers=headers1, json={'employee_id': 'EMP002'}) assert res.status_code == 400 assert 'Employee ID already exists' in res.get_json()['error'] def test_profile_update_theme_preference_validation(client): headers = get_headers(client) res = client.put('/api/user/profile', headers=headers, json={'theme_preference': 'invalid'}) assert res.status_code == 400 assert 'Invalid theme preference' in res.get_json()['error'] def test_login_success(client): # Already tested in get_headers, but explicit here res = client.post('/api/user/login', json={'username': 'test1', 'password': 'test1'}) assert res.status_code == 200 data = res.get_json() assert data['username'] == 'test1' assert 'id' in data def test_login_fail(client): res = client.post('/api/user/login', json={'username': 'test1', 'password': 'wrongpassword'}) assert res.status_code == 401 assert 'Incorrect password' in res.get_json()['error'] res = client.post('/api/user/login', json={'username': 'nonexistent', 'password': 'password'}) assert res.status_code == 404 assert 'User not found' in res.get_json()['error']