import pytest from app import create_app, db from app.models import User, Agent, UserFavoriteAgent, UserDefaultAgent, UserHomepageConfig @pytest.fixture def client(): app = create_app('testing') with app.test_client() as client: with app.app_context(): db.create_all() # Create user user = User(username='testuser', role='user', email='test@example.com') user.set_password('password') db.session.add(user) # Create agents a1 = Agent(name='Agent 1', dify_app_id='app-1', dify_api_key='key-1') a2 = Agent(name='Agent 2', dify_app_id='app-2', dify_api_key='key-2') a3 = Agent(name='Agent 3', dify_app_id='app-3', dify_api_key='key-3') db.session.add_all([a1, a2, a3]) # Grant permission user.agents.append(a1) user.agents.append(a2) user.agents.append(a3) db.session.commit() yield client db.session.remove() db.drop_all() def get_headers(client): res = client.post('/api/user/login', json={'username': 'testuser', 'password': 'password'}) data = res.get_json() return { 'X-User-Id': str(data['id']), 'X-Username': data['username'], 'X-Role': data['role'] } def get_agent_id(client, name): # Helper to get agent ID by name (since we can't query DB directly easily outside context without binding) # But we can use the API # Or just rely on order since we are in a fresh DB # Let's assume IDs are 1, 2, 3 for now if using SQLite. # To be safe, let's fetch from public-agents or user-home # But we need headers for user-home. pass def test_favorites(client): headers = get_headers(client) # Get agent ID res = client.get('/api/user/home', headers=headers) agents = res.get_json()['agents'] agent_id = agents[0]['id'] # Add favorite res = client.post(f'/api/user/favorites/{agent_id}', headers=headers) assert res.status_code == 200 # Check home res = client.get('/api/user/home', headers=headers) data = res.get_json() assert agent_id in data['favorite_agent_ids'] # Remove favorite res = client.delete(f'/api/user/favorites/{agent_id}', headers=headers) assert res.status_code == 200 # Check home res = client.get('/api/user/home', headers=headers) data = res.get_json() assert agent_id not in data['favorite_agent_ids'] def test_homepage_layout(client): headers = get_headers(client) layout = {'sections': [{'id': 'favorites'}, {'id': 'all_agents'}]} # Update layout res = client.put('/api/user/homepage-layout', json={'layout': layout}, headers=headers) assert res.status_code == 200 # Check home res = client.get('/api/user/home', headers=headers) data = res.get_json() assert data['layout']['sections'][0]['id'] == 'favorites' def test_embedded_agents(client): headers = get_headers(client) # Get agent IDs res = client.get('/api/user/home', headers=headers) agents = res.get_json()['agents'] id1 = agents[0]['id'] id2 = agents[1]['id'] id3 = agents[2]['id'] # Update embedded agents res = client.put('/api/user/embedded-agents', json={'agent_ids': [id1, id2]}, headers=headers) assert res.status_code == 200 # Check home res = client.get('/api/user/home', headers=headers) data = res.get_json() assert len(data['embedded_agents']) == 2 assert data['embedded_agents'][0]['id'] == id1 assert data['embedded_agents'][1]['id'] == id2 # Try more than 2 res = client.put('/api/user/embedded-agents', json={'agent_ids': [id1, id2, id3]}, headers=headers) assert res.status_code == 400