113 lines
3.6 KiB
Python
113 lines
3.6 KiB
Python
import pytest
|
|
from app import create_app, db
|
|
from app.models import Agent, User
|
|
import json
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
# Use 'testing' config to ensure SQLite in-memory DB is used
|
|
app = create_app('testing')
|
|
|
|
with app.app_context():
|
|
db.create_all()
|
|
# Create admin user for auth
|
|
# Ensure all required fields are provided
|
|
admin = User(
|
|
username='admin',
|
|
email='admin@example.com',
|
|
role='admin'
|
|
)
|
|
admin.set_password('admin')
|
|
db.session.add(admin)
|
|
db.session.commit()
|
|
|
|
yield app.test_client()
|
|
|
|
db.session.remove()
|
|
db.drop_all()
|
|
|
|
def get_headers():
|
|
return {
|
|
'X-Username': 'admin',
|
|
'X-Role': 'admin',
|
|
'Content-Type': 'application/json'
|
|
}
|
|
|
|
def test_create_iframe_agent(client):
|
|
"""
|
|
Test creating an Iframe agent with user provided data.
|
|
"""
|
|
data = {
|
|
"name": "Test Iframe Agent",
|
|
"type": "iframe",
|
|
"description": "Auto-test Iframe Agent",
|
|
"iframe_url": "http://192.168.100.230:8882/chatbot/imrmbYyasJUHjXQg"
|
|
}
|
|
|
|
# Frontend logic: extra_config = { iframe_url: ... }
|
|
payload = {
|
|
"name": data["name"],
|
|
"type": data["type"],
|
|
"description": data["description"],
|
|
"dify_app_id": "", # Frontend sends empty string
|
|
"dify_api_key": "", # Frontend sends empty string
|
|
"extra_config": {
|
|
"iframe_url": data["iframe_url"]
|
|
}
|
|
}
|
|
|
|
response = client.post('/api/admin/agents', json=payload, headers=get_headers())
|
|
|
|
assert response.status_code == 201, f"Failed to create Iframe agent: {response.text}"
|
|
resp_data = response.get_json()
|
|
assert 'id' in resp_data
|
|
|
|
# Verify DB
|
|
# Need to access DB within app context or use client application context if possible
|
|
# But db.session is bound to the app context.
|
|
# We can use client.application.app_context() if needed, but db.session should work if we are in the same thread/process and using shared memory (for sqlite).
|
|
# Since we yielded app.test_client(), the app context in fixture is gone.
|
|
# But we can access the app from client.application
|
|
|
|
with client.application.app_context():
|
|
agent = db.session.get(Agent, resp_data['id'])
|
|
assert agent is not None
|
|
assert agent.type == 'iframe'
|
|
# Ensure empty string was converted to None
|
|
assert agent.dify_app_id is None
|
|
assert agent.extra_config['iframe_url'] == data['iframe_url']
|
|
|
|
def test_create_api_agent(client):
|
|
"""
|
|
Test creating an API agent with user provided data.
|
|
"""
|
|
data = {
|
|
"name": "Test API Agent",
|
|
"type": "api",
|
|
"api_base_url": "http://127.0.0.1:5001/v1",
|
|
"dify_api_key": "app-xdiejQibWQy8ZQWXcdxJjS9I"
|
|
}
|
|
|
|
# We need to provide a dify_app_id because it is required for API agents
|
|
payload = {
|
|
"name": data["name"],
|
|
"type": data["type"],
|
|
"dify_app_id": "test-api-agent-id", # Dummy ID
|
|
"dify_api_key": data["dify_api_key"],
|
|
"api_base_url": data["api_base_url"]
|
|
}
|
|
|
|
response = client.post('/api/admin/agents', json=payload, headers=get_headers())
|
|
|
|
assert response.status_code == 201, f"Failed to create API agent: {response.text}"
|
|
resp_data = response.get_json()
|
|
assert 'id' in resp_data
|
|
|
|
# Verify DB
|
|
with client.application.app_context():
|
|
agent = db.session.get(Agent, resp_data['id'])
|
|
assert agent is not None
|
|
assert agent.type == 'api'
|
|
assert agent.dify_api_key == data['dify_api_key']
|
|
assert agent.api_base_url == data['api_base_url']
|