前后端更新
This commit is contained in:
1 parent
a0c8721fbb
commit
24da604727
33 files changed
+1951
-580
No files matched your search
@@ -0,0 +1,99 @@
|
||||
import pytest
|
||||
from app import create_app, db
|
||||
from app.models import Agent, 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
|
||||
user = User(username='testuser', role='user', email='test@example.com')
|
||||
user.set_password('password')
|
||||
db.session.add(user)
|
||||
|
||||
# Create agent with trailing slash in URL (to test strip logic)
|
||||
agent = Agent(
|
||||
name='Test Agent',
|
||||
dify_api_key='test-key',
|
||||
api_base_url='http://dify.example.com/v1/chat-messages', # Intentionally wrong to simulate user error
|
||||
type='api'
|
||||
)
|
||||
db.session.add(agent)
|
||||
|
||||
# Grant permission
|
||||
user.agents.append(agent)
|
||||
|
||||
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'],
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
def test_chat_url_logic(client):
|
||||
"""
|
||||
Test that the backend correctly constructs the Dify URL
|
||||
Note: We can't easily mock the external request in this integration test without 'requests_mock',
|
||||
but we can verify the logic in a unit test style or assume the service handles it.
|
||||
Here we verify the permission check passes.
|
||||
"""
|
||||
headers = get_headers(client)
|
||||
|
||||
with client.application.app_context():
|
||||
agent_id = Agent.query.first().id
|
||||
|
||||
# Test permission (should be allowed)
|
||||
# We send a request. It will fail at the requests.post stage (connection error),
|
||||
# but that means it passed the permission check.
|
||||
# If permission denied, it would return 403 immediately.
|
||||
|
||||
try:
|
||||
res = client.post('/api/chat/completions', headers=headers, json={
|
||||
'agent_id': agent_id,
|
||||
'query': 'Hello'
|
||||
})
|
||||
except:
|
||||
# Expected connection error to fake URL
|
||||
pass
|
||||
|
||||
# To truly verify permission, we can check the decorator logic via a mocked endpoint or just rely on the status code not being 403
|
||||
# Since requests.post raises exception, we can't check status code easily here without mocking.
|
||||
|
||||
# Let's test the permission check explicitly via another endpoint that uses the same decorator
|
||||
res = client.get(f'/api/chat/sessions?agent_id={agent_id}', headers=headers, json={'agent_id': agent_id})
|
||||
# GET request with json body is weird but allowed in Flask test client,
|
||||
# but the decorator reads `request.get_json()`.
|
||||
# `get_chat_sessions` uses `g.current_agent` which is set by the decorator.
|
||||
|
||||
# Actually, `require_agent_permission` expects agent_id in JSON body.
|
||||
# Standard GET requests don't have body.
|
||||
# But let's try sending body with GET.
|
||||
res = client.get('/api/chat/sessions', headers=headers, json={'agent_id': agent_id})
|
||||
|
||||
assert res.status_code == 200 # Should succeed (empty list)
|
||||
|
||||
# Now remove permission
|
||||
with client.application.app_context():
|
||||
user = User.query.filter_by(username='testuser').first()
|
||||
agent = Agent.query.first()
|
||||
user.agents.remove(agent)
|
||||
db.session.commit()
|
||||
|
||||
res = client.get('/api/chat/sessions', headers=headers, json={'agent_id': agent_id})
|
||||
assert res.status_code == 403
|
||||
assert res.get_json()['error'] == 'Permission denied for this agent'
|
||||
Reference in new issue
Block a user