""" tests/services/admin/test_permission_service.py 用途:permission_service 批量授权与权限申请审批单元测试。 """ import pytest from app import db from app.core.errors import ForbiddenError, NotFoundError, ValidationError from app.models import Agent, PermissionRequest, User from app.services.admin import permission_service def test_update_user_permissions(app): with app.app_context(): agent = Agent(name='perm-agent', type='api', dify_app_id='app-p', dify_api_key='key-p') user = User(username='perm-user', email='perm@example.com', role='user') db.session.add_all([agent, user]) db.session.commit() permission_service.update_user_permissions(user.id, {'agent_ids': [agent.id]}) db.session.refresh(user) assert len(user.agents) == 1 assert user.agents[0].id == agent.id def test_batch_update_permissions_add(app, monkeypatch): with app.app_context(): agent = Agent(name='batch-agent', type='api', dify_app_id='app-b', dify_api_key='key-b') user = User(username='batch-user', email='batch@example.com', role='user') db.session.add_all([agent, user]) db.session.commit() monkeypatch.setattr( 'app.services.admin.permission_service.send_wecom_event_message', lambda **kwargs: None, ) result = permission_service.batch_update_permissions( {'user_ids': [user.id], 'agent_ids': [agent.id], 'action': 'add'} ) assert result['updated_count'] == 1 db.session.refresh(user) assert agent in user.agents def test_batch_update_permissions_no_users(app): with app.app_context(): with pytest.raises(ValidationError): permission_service.batch_update_permissions({'user_ids': [], 'agent_ids': [1]}) @pytest.mark.parametrize('operation', ['single', 'batch']) def test_internal_executor_cannot_be_assigned_to_user(app, operation): """授权服务必须在所有写入口拒绝 internal 执行器。""" with app.app_context(): executor = Agent( name='internal-executor', type='api', dify_app_id='wf-internal', dify_api_key='key', dify_app_type='workflow', runtime_provider='dify', exposure_mode='internal', ) user = User(username=f'internal-{operation}', email=f'internal-{operation}@example.com', role='user') db.session.add_all([executor, user]) db.session.commit() with pytest.raises(ValidationError) as caught: if operation == 'single': permission_service.update_user_permissions(user.id, {'agent_ids': [executor.id]}) else: permission_service.batch_update_permissions({ 'user_ids': [user.id], 'agent_ids': [executor.id], 'action': 'add', }) assert caught.value.code == 'AGENT.INTERNAL_EXECUTOR_USER_ACCESS_FORBIDDEN' db.session.refresh(user) assert executor not in user.agents def test_permission_request_approve(app, monkeypatch): with app.app_context(): agent = Agent(name='req-agent', type='api', dify_app_id='app-r', dify_api_key='key-r') user = User(username='req-user', email='req@example.com', role='user') db.session.add_all([agent, user]) db.session.flush() req = PermissionRequest(user_id=user.id, agent_id=agent.id, reason='need access', status='pending') db.session.add(req) db.session.commit() monkeypatch.setattr( 'app.services.admin.permission_service.send_wecom_event_message', lambda **kwargs: None, ) permission_service.approve_permission_request(req.id) db.session.refresh(req) db.session.refresh(user) assert req.status == 'approved' assert req.reviewed_at is not None assert agent in user.agents def test_permission_request_not_found(app): with app.app_context(): with pytest.raises(NotFoundError): permission_service.approve_permission_request(99999) def test_internal_executor_permission_request_cannot_be_approved(app): """历史或伪造的内部执行器权限申请不得转化为用户授权。""" with app.app_context(): executor = Agent( name='request-executor', type='api', dify_app_id='wf-request', dify_api_key='key', dify_app_type='workflow', runtime_provider='dify', exposure_mode='internal', ) user = User(username='request-internal', email='request-internal@example.com', role='user') db.session.add_all([executor, user]) db.session.flush() req = PermissionRequest(user_id=user.id, agent_id=executor.id, reason='invalid', status='pending') db.session.add(req) db.session.commit() with pytest.raises(ForbiddenError) as caught: permission_service.approve_permission_request(req.id) assert getattr(caught.value, 'code', None) == 'AGENT.INTERNAL_EXECUTOR_USER_ACCESS_FORBIDDEN' db.session.refresh(user) assert executor not in user.agents