56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
import pytest
|
|
from app import create_app, db
|
|
from app.models import User, Agent, QuickQuestion
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
app = create_app()
|
|
app.config['TESTING'] = True
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
|
|
|
|
with app.test_client() as client:
|
|
with app.app_context():
|
|
db.create_all()
|
|
yield client
|
|
db.session.remove()
|
|
db.drop_all()
|
|
|
|
def test_quick_question_crud(client):
|
|
# Setup
|
|
user = User(username='testuser', email='test@example.com', role='user')
|
|
user.set_password('password')
|
|
agent = Agent(name='Test Agent', dify_app_id='app-123', dify_api_key='key-123')
|
|
db.session.add(user)
|
|
db.session.add(agent)
|
|
db.session.commit()
|
|
|
|
# Login
|
|
login_res = client.post('/api/user/login', json={'username': 'testuser', 'password': 'password'})
|
|
assert login_res.status_code == 200
|
|
user_data = login_res.get_json()
|
|
headers = {'X-User-Id': str(user_data['id']), 'X-Username': user_data['username'], 'X-Role': user_data['role']}
|
|
|
|
# Create
|
|
res = client.post('/api/user/quick-questions', json={
|
|
'title': 'Test Q',
|
|
'question': 'How are you?',
|
|
'agent_id': agent.id
|
|
}, headers=headers)
|
|
assert res.status_code == 201
|
|
qq_id = res.get_json()['id']
|
|
|
|
# Read (via Home)
|
|
res = client.get('/api/user/home', headers=headers)
|
|
assert res.status_code == 200
|
|
data = res.get_json()
|
|
assert len(data['quick_questions']) == 1
|
|
assert data['quick_questions'][0]['title'] == 'Test Q'
|
|
|
|
# Delete
|
|
res = client.delete(f'/api/user/quick-questions/{qq_id}', headers=headers)
|
|
assert res.status_code == 200
|
|
|
|
# Verify Delete
|
|
res = client.get('/api/user/home', headers=headers)
|
|
assert len(res.get_json()['quick_questions']) == 0
|