import json import urllib.error import urllib.request BASE = 'http://localhost:8980/js/a' D1 = '2123022706158374913' results = [] def call(method, path, body=None, token=None): data = json.dumps(body).encode() if body is not None else None req = urllib.request.Request(BASE + path, data=data, method=method) req.add_header('Content-Type', 'application/json') if token: req.add_header('x-token', token) try: with urllib.request.urlopen(req, timeout=30) as resp: return json.loads(resp.read().decode()) except urllib.error.HTTPError as exc: return {'http': exc.code, 'body': exc.read().decode()[:500]} def login(user, pwd): resp = call('POST', '/auth/login', {'username': user, 'password': pwd, 'force': True}) if resp.get('code') != 200: raise RuntimeError(f'login {user} failed: {resp}') return resp['data']['token'] def check(name, cond, detail=''): results.append({'case': name, 'pass': bool(cond), 'detail': str(detail)[:500]}) print(('PASS' if cond else 'FAIL'), name, str(detail)[:300]) def rows(path, token): resp = call('GET', path, token=token) return resp, (resp.get('data') or {}).get('rows') or [] T = { 'admin': login('admin', 'admin123'), 'A': login('vsuserA', 'Vs1118pass'), 'B': login('vsuserB', 'Vs1118pass'), 'C': login('vsuserC', 'Vs1118pass'), 'D': login('vsuserD', 'Vs1118pass'), } # R1 D account permission set info = call('GET', '/auth/info', token=T['D']) perms = set((info.get('data') or {}).get('permissions') or []) check('R1 D permission set = expert only', {'ai:expert:query', 'ai:expert:edit', 'ai:expert:remove'}.issubset(perms) and 'ai:agent:query' not in perms, sorted(perms)) # R2 D can list experts (previously 403) resp, list_d = rows('/ai/agent/list?pageSize=100', T['D']) codes = sorted(x['agentCode'] for x in list_d) check('R2 D expert list 200 and sees public+dept (2)', resp.get('code') == 200 and len(list_d) == 2, codes) # R3 option endpoints usable with expert permission only opt_paths = [ ('model', '/ai/model/options?modelType=chat'), ('tool', '/ai/agent/tool/options'), ('kb', '/ai/kb/visible-options'), ('skill', '/ai/skill/visible-options'), ] for name, path in opt_paths: r = call('GET', path, token=T['D']) check(f'R3 D option {name} 200', r.get('code') == 200 and len(r.get('data') or []) >= 0, f"code={r.get('code')} n={len(r.get('data') or [])}") # R4 D create/update/delete own expert create_body = { 'agentName': 'VS复测仅专家角色专家', 'agentCode': 'vs_retest_d_expert', 'description': 'expert-only retest', 'modelId': 1761400000000020400, 'status': '0', 'visibleScope': '2', 'autoRoute': '1', 'memoryEnabled': '0', 'expertWriteEnabled': '1', } r = call('POST', '/ai/agent', create_body, T['D']) check('R4a D create own expert 200 (previously 403)', r.get('code') == 200, r) resp, list_d = rows('/ai/agent/list?pageSize=100', T['D']) own = [x for x in list_d if x['agentCode'] == 'vs_retest_d_expert'] check('R4b D list contains own record and total=3', len(list_d) == 3 and len(own) == 1, [x['agentCode'] for x in list_d]) own_id = own[0]['id'] if own else None if own_id: r = call('PUT', '/ai/agent', { 'id': own_id, 'agentName': 'VS复测仅专家角色专家改', 'agentCode': 'vs_retest_d_expert', 'modelId': 1761400000000020400, 'visibleScope': '2', }, T['D']) check('R4c D update own expert 200 (previously 403)', r.get('code') == 200, r) resp, list_d = rows('/ai/agent/list?pageSize=100', T['D']) own = [x for x in list_d if x['agentCode'] == 'vs_retest_d_expert'] check('R4d D update persisted', bool(own) and own[0]['agentName'] == 'VS复测仅专家角色专家改', own[0]['agentName'] if own else None) r = call('DELETE', '/ai/agent/' + own_id, token=T['D']) check('R4e D delete own expert 200 (previously 403)', r.get('code') == 200, r) resp, list_d = rows('/ai/agent/list?pageSize=100', T['D']) check('R4f D list back to 2 after delete', len(list_d) == 2, [x['agentCode'] for x in list_d]) # R5 entry remains ai:agent guarded (separation) r = call('GET', '/ai/agent/entry', token=T['D']) check('R5a D cannot read entry (403)', r.get('code') == 403, r) r = call('GET', '/ai/agent/entry', token=T['A']) check('R5b A can read entry with ai:agent:query', r.get('code') == 200, str(r.get('code'))) # R6 D cannot manage A dept expert; canManage error should now be reached r = call('DELETE', '/ai/agent/2123022766514409473', token=T['D']) check('R6 D delete A dept expert rejected by canManage', r.get('code') == 500 and '仅创建人或超级管理员' in (r.get('message') or ''), r) # R7 A delete own temp expert still works r = call('POST', '/ai/agent', { 'agentName': 'VS复测删除临时专家', 'agentCode': 'vs_retest_del_expert', 'description': 'delete retest', 'modelId': 1761400000000020400, 'status': '0', 'visibleScope': '2', 'autoRoute': '1', 'expertWriteEnabled': '1', }, T['A']) check('R7a A create temp expert 200', r.get('code') == 200, r) resp, list_a = rows('/ai/agent/list?pageSize=100', T['A']) temp = [x for x in list_a if x['agentCode'] == 'vs_retest_del_expert'] if temp: r = call('DELETE', '/ai/agent/' + temp[0]['id'], token=T['A']) check('R7b A delete own expert 200', r.get('code') == 200, r) # R8 D creates dept-scope record: B sees, C does not; D can delete it r = call('POST', '/ai/agent', { 'agentName': 'VS复测部门专家', 'agentCode': 'vs_retest_dept_expert', 'description': 'dept retest', 'modelId': 1761400000000020400, 'status': '0', 'visibleScope': '1', 'ownerDeptId': D1, 'autoRoute': '1', 'expertWriteEnabled': '1', }, T['D']) check('R8a D create dept-scope expert 200', r.get('code') == 200, r) resp, list_b = rows('/ai/agent/list?pageSize=100', T['B']) resp, list_c = rows('/ai/agent/list?pageSize=100', T['C']) check('R8b B(D1) sees D dept record', any(x['agentCode'] == 'vs_retest_dept_expert' for x in list_b), [x['agentCode'] for x in list_b]) check('R8c C(D2) cannot see D dept record', all(x['agentCode'] != 'vs_retest_dept_expert' for x in list_c), [x['agentCode'] for x in list_c]) resp, list_d = rows('/ai/agent/list?pageSize=100', T['D']) dept_rec = [x for x in list_d if x['agentCode'] == 'vs_retest_dept_expert'] if dept_rec: check('R8d D sees canManage=true for own dept record', dept_rec[0]['canManage'] is True, dept_rec[0]['canManage']) r = call('DELETE', '/ai/agent/' + dept_rec[0]['id'], token=T['D']) check('R8e D delete own dept record 200', r.get('code') == 200, r) # R9 D cannot create public expert r = call('POST', '/ai/agent', { 'agentName': 'VS复测越权公开', 'agentCode': 'vs_retest_pub_try', 'description': 'x', 'modelId': 1761400000000020400, 'status': '0', 'visibleScope': '0', 'autoRoute': '1', 'expertWriteEnabled': '1', }, T['D']) check('R9 D create scope=0 rejected', r.get('code') == 500 and '普通用户不能设置全员/公开可见' in (r.get('message') or ''), r) # R10 core list counts unchanged expected = {'A': 3, 'B': 2, 'C': 1, 'admin': 3} for user, exp in expected.items(): resp, list_rows = rows('/ai/agent/list?pageSize=100', T[user]) check(f'R10 expert list {user} count={exp}', resp.get('code') == 200 and len(list_rows) == exp, [x['agentCode'] for x in list_rows]) # R11 workbench expert candidates still filtered expected_candidates = {'A': 3, 'B': 2, 'C': 1, 'admin': 3} for user, exp in expected_candidates.items(): r = call('GET', '/ai/agent/experts/candidates', token=T[user]) check(f'R11 candidates {user} count={exp}', r.get('code') == 200 and len(r.get('data') or []) == exp, len(r.get('data') or [])) # cleanup any leftover retest records for user in ['A', 'D']: resp, list_rows = rows('/ai/agent/list?pageSize=100', T[user]) for rec in list_rows: if rec['agentCode'].startswith('vs_retest_'): call('DELETE', '/ai/agent/' + rec['id'], token=T[user]) json.dump(results, open('retest_api_results.json', 'w'), ensure_ascii=False, indent=2) print('TOTAL', len(results), 'PASS', sum(1 for x in results if x['pass']), 'FAIL', sum(1 for x in results if not x['pass']))