52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
import pytest
|
|
from app import create_app, db
|
|
from app.models import Agent, User
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
app = create_app('testing')
|
|
with app.app_context():
|
|
db.create_all()
|
|
|
|
# Create an active agent
|
|
agent = Agent(name="Test Agent", type="api", is_active=True)
|
|
db.session.add(agent)
|
|
|
|
# Create a regular user (no permissions)
|
|
user = User(username='test_user', role='user', email='test@example.com')
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
|
|
|
yield app.test_client()
|
|
|
|
db.session.remove()
|
|
db.drop_all()
|
|
|
|
def test_user_home_agents_visibility(client):
|
|
"""
|
|
Test that a regular user can see active agents in /api/user/home
|
|
even if they don't have explicit permission (visibility vs access).
|
|
"""
|
|
# Login as regular user
|
|
client.post('/api/user/login', json={'username': 'test_user', 'password': ''})
|
|
|
|
# Mock headers for auth (since login might not set session in test client depending on implementation)
|
|
# user.py uses _get_current_user which checks headers
|
|
headers = {
|
|
'X-Username': 'test_user',
|
|
'X-Role': 'user'
|
|
}
|
|
|
|
response = client.get('/api/user/home', headers=headers)
|
|
assert response.status_code == 200
|
|
|
|
data = response.get_json()
|
|
agents = data.get('agents', [])
|
|
|
|
# Current behavior (Bug): agents is empty for regular user
|
|
# Desired behavior: agents should contain the active agent
|
|
|
|
print(f"Agents found: {len(agents)}")
|
|
assert len(agents) > 0, "Agents list should not be empty for regular user"
|