import json, urllib.request, urllib.error BASE = 'http://localhost:8980/js/a' def call(m, p, b=None, t=None): d = json.dumps(b).encode() if b is not None else None r = urllib.request.Request(BASE + p, data=d, method=m) r.add_header('Content-Type', 'application/json') if t: r.add_header('x-token', t) try: with urllib.request.urlopen(r, timeout=30) as x: return json.loads(x.read().decode()) except urllib.error.HTTPError as e: return {'http': e.code, 'body': e.read().decode()[:300]} def login(u, p): return call('POST', '/auth/login', {'username': u, 'password': p, 'force': True})['data']['token'] T = { 'admin': login('admin', 'admin123'), 'A': login('vsuserA', 'Vs1118pass'), 'B': login('vsuserB', 'Vs1118pass'), 'C': login('vsuserC', 'Vs1118pass'), } D1 = '2123022706158374913' D2 = '2123022706573611010' ids = { 'expert_pub': '2123022764635361281', 'expert_dept': '2123022766514409473', 'expert_self': '2123022768359903234', 'skill_pub': '2123022765163843585', 'skill_dept': '2123022767038697473', 'skill_self': '2123022768850636802', 'pack_pub': '2123022765524553730', 'pack_dept': '2123022767378436098', 'pack_self': '2123022769240707074', 'kb_pub': '2123022766036258817', 'kb_dept': '2123022767873363969', 'kb_self': '2123022769697886209', } results = [] def check(name, cond, detail=''): results.append({'case': name, 'pass': bool(cond), 'detail': detail}) print(('PASS' if cond else 'FAIL'), name, str(detail)[:300]) def list_rows(path, t): r = call('GET', path, t=t) return r, (r.get('data') or {}).get('rows') or [] # T1/T2 scope options r = call('GET', '/ai/agent/scope-options', t=T['admin']) check('T1 admin scope-options superAdmin=true + depts', r.get('code') == 200 and r['data']['superAdmin'] is True and len(r['data']['depts']) > 0, json.dumps(r, ensure_ascii=False)[:200]) r = call('GET', '/ai/agent/scope-options', t=T['A']) check('T2 normal scope-options superAdmin=false + depts', r.get('code') == 200 and r['data']['superAdmin'] is False and len(r['data']['depts']) > 0, json.dumps(r, ensure_ascii=False)[:200]) # T3 list visibility counts for kind, path in [('expert', '/ai/agent/list?pageSize=100'), ('skill', '/ai/skill/list?pageSize=100'), ('pack', '/ai/pack/list?pageSize=100'), ('kb', '/ai/kb/list?pageSize=100')]: for u, exp in [('A', 3), ('B', 2), ('C', 1), ('admin', 3)]: r, rows = list_rows(path, T[u]) names = [x.get('agentName') or x.get('name') or x.get('packName') or x.get('kbName') for x in rows] check(f'T3 {kind} list {u} total={exp}', r.get('code') == 200 and len(rows) == exp, 'names=' + ','.join(names)) # T4 canManage flags _, rows = list_rows('/ai/agent/list?pageSize=100', T['B']) by = {x['agentCode']: x for x in rows} check('T4 B cannot manage A dept expert', by['vs_expert_dept']['canManage'] is False, str(by['vs_expert_dept'].get('canManage'))) check('T4 B cannot manage admin public expert', by['vs_expert_pub']['canManage'] is False, str(by['vs_expert_pub'].get('canManage'))) _, rows = list_rows('/ai/agent/list?pageSize=100', T['A']) byA = {x['agentCode']: x for x in rows} check('T4 A can manage own dept/self expert', byA['vs_expert_dept']['canManage'] is True and byA['vs_expert_self']['canManage'] is True, '') check('T4 A cannot manage admin public expert', byA['vs_expert_pub']['canManage'] is False, '') # T5 detail visibility for key, path in [('expert_self', '/ai/agent/'), ('skill_self', '/ai/skill/'), ('pack_self', '/ai/pack/'), ('kb_self', '/ai/kb/')]: r = call('GET', path + ids[key], t=T['B']) check(f'T5 B cannot view A self {key}', r.get('code') == 500, json.dumps(r, ensure_ascii=False)[:160]) for key, path in [('expert_dept', '/ai/agent/'), ('skill_dept', '/ai/skill/'), ('pack_dept', '/ai/pack/'), ('kb_dept', '/ai/kb/')]: r = call('GET', path + ids[key], t=T['B']) check(f'T5 B can view dept {key}', r.get('code') == 200 and r['data'] is not None, str(r.get('code'))) for key, path in [('expert_dept', '/ai/agent/'), ('skill_dept', '/ai/skill/'), ('pack_dept', '/ai/pack/'), ('kb_dept', '/ai/kb/')]: r = call('GET', path + ids[key], t=T['C']) check(f'T5 C(D2) cannot view D1 dept {key}', r.get('code') == 500, json.dumps(r, ensure_ascii=False)[:160]) for key, path in [('expert_pub', '/ai/agent/'), ('skill_pub', '/ai/skill/'), ('pack_pub', '/ai/pack/'), ('kb_pub', '/ai/kb/')]: r = call('GET', path + ids[key], t=T['C']) check(f'T5 C can view public {key}', r.get('code') == 200, str(r.get('code'))) # T6 update authorization expert_body = lambda scope, oid=None: { 'id': ids['expert_dept'], 'agentName': 'VS部门专家', 'agentCode': 'vs_expert_dept', 'modelId': 1761400000000020400, 'visibleScope': scope, 'ownerDeptId': oid} r = call('PUT', '/ai/agent', expert_body('1', D1), T['B']) check('T6 B update A dept expert rejected', r.get('code') == 500 and '仅创建人或超级管理员' in (r.get('message') or ''), json.dumps(r, ensure_ascii=False)[:200]) r = call('PUT', '/ai/agent', expert_body('1', D1), T['C']) check('T6 C update A dept expert rejected', r.get('code') == 500, json.dumps(r, ensure_ascii=False)[:200]) r = call('PUT', '/ai/agent', expert_body('1', D1), T['admin']) check('T6 admin update A dept expert allowed', r.get('code') == 200, json.dumps(r, ensure_ascii=False)[:200]) r = call('PUT', '/ai/agent', expert_body('2', None), T['A']) check('T6 A update own expert allowed', r.get('code') == 200, json.dumps(r, ensure_ascii=False)[:200]) # T7 normal user cannot set 0 on own record; invalid rejected; blank inherits r = call('PUT', '/ai/agent', {'id': ids['expert_self'], 'agentName': 'VS自建专家', 'agentCode': 'vs_expert_self', 'modelId': 1761400000000020400, 'visibleScope': '0'}, T['A']) check('T7 A set own expert scope=0 rejected', r.get('code') == 500 and '普通用户不能设置全员/公开可见' in (r.get('message') or ''), json.dumps(r, ensure_ascii=False)[:200]) r = call('PUT', '/ai/agent', {'id': ids['expert_self'], 'agentName': 'VS自建专家', 'agentCode': 'vs_expert_self', 'modelId': 1761400000000020400, 'visibleScope': '9'}, T['A']) check('T7 A set invalid scope=9 rejected', r.get('code') != 200, json.dumps(r, ensure_ascii=False)[:200]) r = call('PUT', '/ai/agent', {'id': ids['expert_self'], 'agentName': 'VS自建专家', 'agentCode': 'vs_expert_self', 'modelId': 1761400000000020400}, T['A']) check('T7 A blank scope inherits 2', r.get('code') == 200, json.dumps(r, ensure_ascii=False)[:200]) r = call('GET', '/ai/agent/' + ids['expert_self'], t=T['A']) check('T7 self expert scope still 2', r.get('code') == 200 and r['data']['visibleScope'] == '2', str((r.get('data') or {}).get('visibleScope'))) # T8 workbench options filtered for path, label in [('/ai/agent/experts', 'visible experts'), ('/ai/agent/experts/candidates', 'candidates'), ('/ai/skill/visible-options', 'skill options'), ('/ai/pack/options', 'pack options'), ('/ai/kb/visible-options', 'kb options')]: for u, exp in [('A', 3), ('B', 2), ('C', 1), ('admin', 3)]: r = call('GET', path, t=T[u]) n = len(r.get('data') or []) check(f'T8 {label} {u} count={exp}', r.get('code') == 200 and n == exp, f'n={n}') r = call('GET', '/ai/kb/options', t=T['C']) check('T8 kb full options 3 for C', r.get('code') == 200 and len(r.get('data') or []) == 3, f"n={len(r.get('data') or [])}") # T9 doc list filtering r = call('GET', f'/ai/kb/doc/list?kbId={ids["kb_dept"]}&pageSize=20', t=T['B']) check('T9 B doc list dept kb allowed(empty ok)', r.get('code') == 200 and len((r.get('data') or {}).get('rows') or []) == 0, json.dumps(r, ensure_ascii=False)[:200]) r = call('GET', f'/ai/kb/doc/list?kbId={ids["kb_dept"]}&pageSize=20', t=T['C']) check('T9 C doc list dept kb returns empty', r.get('code') == 200 and len((r.get('data') or {}).get('rows') or []) == 0, json.dumps(r, ensure_ascii=False)[:200]) r = call('GET', f'/ai/kb/doc/list?kbId={ids["kb_self"]}&pageSize=20', t=T['B']) check('T9 B doc list A self kb empty', r.get('code') == 200 and len((r.get('data') or {}).get('rows') or []) == 0, json.dumps(r, ensure_ascii=False)[:200]) # T10 entry behavior r = call('GET', '/ai/agent/entry', t=T['admin']) entry = r.get('data') or {} check('T10 entry readable', r.get('code') == 200 and entry.get('id'), str(entry.get('id'))) def entry_body(scope, oid): return {'id': entry['id'], 'agentName': entry['agentName'], 'agentCode': entry['agentCode'], 'modelId': entry['modelId'], 'visibleScope': scope, 'ownerDeptId': oid} r = call('PUT', '/ai/agent/entry', entry_body('1', D1), T['admin']) check('T10 admin save entry scope=1', r.get('code') == 200, json.dumps(r, ensure_ascii=False)[:200]) r = call('GET', '/ai/agent/entry', t=T['B']) e2 = r.get('data') or {} check('T10 entry scope persisted=1', e2.get('visibleScope') == '1' and str(e2.get('ownerDeptId')) == D1, f"{e2.get('visibleScope')}/{e2.get('ownerDeptId')}") r = call('GET', '/ai/agent/options', t=T['B']) names = [x.get('agentCode') for x in (r.get('data') or [])] check('T10 entry still offered to B despite dept scope', 'entry_agent' in names, str(names)) r = call('PUT', '/ai/agent/entry', entry_body('2', None), T['A']) check('T10 normal user A can save entry (no creator check)', r.get('code') == 200, json.dumps(r, ensure_ascii=False)[:200]) r = call('PUT', '/ai/agent/entry', entry_body('0', None), T['admin']) check('T10 admin restore entry scope=0', r.get('code') == 200, json.dumps(r, ensure_ascii=False)[:200]) # T11 admin dept record visible only to target dept r = call('POST', '/ai/agent', {'agentName': 'VS跨部门专家', 'agentCode': 'vs_expert_d2', 'description': 'admin d2', 'modelId': 1761400000000020400, 'status': '0', 'visibleScope': '1', 'ownerDeptId': D2, 'autoRoute': '1', 'memoryEnabled': '0', 'expertWriteEnabled': '1'}, T['admin']) check('T11 create admin record ownerDept=D2', r.get('code') == 200, json.dumps(r, ensure_ascii=False)[:200]) _, rows = list_rows('/ai/agent/list?pageSize=100', T['A']) nA = len(rows) _, rows = list_rows('/ai/agent/list?pageSize=100', T['C']) codesC = [x['agentCode'] for x in rows] check('T11 D2 record hidden from A(D1)', nA == 3, f'nA={nA}') check('T11 D2 record visible to C(D2)', 'vs_expert_d2' in codesC, str(codesC)) # T12 default scope for normal user when omitted r = call('POST', '/ai/skill', {'name': 'vs_skill_default', 'description': 'default scope test', 'status': '0'}, T['A']) check('T12 create skill without scope ok', r.get('code') == 200, json.dumps(r, ensure_ascii=False)[:200]) _, rows = list_rows('/ai/skill/list?pageSize=100', T['A']) row = [x for x in rows if x['name'] == 'vs_skill_default'] check('T12 default scope=2 for normal user', bool(row) and row[0]['visibleScope'] == '2', str(row[0]['visibleScope'] if row else None)) if row: ids['skill_default'] = row[0]['id'] # T13 delete authorization r = call('POST', '/ai/skill', {'name': 'vs_skill_del', 'description': 'delete test', 'status': '0', 'visibleScope': '1', 'ownerDeptId': D1}, T['A']) check('T13 create temp skill', r.get('code') == 200, '') _, rows = list_rows('/ai/skill/list?pageSize=100', T['A']) row = [x for x in rows if x['name'] == 'vs_skill_del'] delid = row[0]['id'] if row else None r = call('DELETE', '/ai/skill/' + str(delid), t=T['B']) check('T13 B delete A skill rejected', r.get('code') == 500, json.dumps(r, ensure_ascii=False)[:200]) r = call('DELETE', '/ai/skill/' + str(delid), t=T['C']) check('T13 C delete A skill rejected', r.get('code') == 500, json.dumps(r, ensure_ascii=False)[:200]) r = call('DELETE', '/ai/skill/' + str(delid), t=T['A']) check('T13 A delete own skill allowed', r.get('code') == 200, json.dumps(r, ensure_ascii=False)[:200]) if ids.get('skill_default'): r = call('DELETE', '/ai/skill/' + str(ids['skill_default']), t=T['A']) check('T13 cleanup default skill', r.get('code') == 200, '') _, rows = list_rows('/ai/agent/list?pageSize=100', T['admin']) row = [x for x in rows if x['agentCode'] == 'vs_expert_d2'] if row: r = call('DELETE', '/ai/agent/' + row[0]['id'], t=T['admin']) check('T13 cleanup d2 expert', r.get('code') == 200, '') json.dump(results, open('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']))