retest_api.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import json
  2. import urllib.error
  3. import urllib.request
  4. BASE = 'http://localhost:8980/js/a'
  5. D1 = '2123022706158374913'
  6. results = []
  7. def call(method, path, body=None, token=None):
  8. data = json.dumps(body).encode() if body is not None else None
  9. req = urllib.request.Request(BASE + path, data=data, method=method)
  10. req.add_header('Content-Type', 'application/json')
  11. if token:
  12. req.add_header('x-token', token)
  13. try:
  14. with urllib.request.urlopen(req, timeout=30) as resp:
  15. return json.loads(resp.read().decode())
  16. except urllib.error.HTTPError as exc:
  17. return {'http': exc.code, 'body': exc.read().decode()[:500]}
  18. def login(user, pwd):
  19. resp = call('POST', '/auth/login', {'username': user, 'password': pwd, 'force': True})
  20. if resp.get('code') != 200:
  21. raise RuntimeError(f'login {user} failed: {resp}')
  22. return resp['data']['token']
  23. def check(name, cond, detail=''):
  24. results.append({'case': name, 'pass': bool(cond), 'detail': str(detail)[:500]})
  25. print(('PASS' if cond else 'FAIL'), name, str(detail)[:300])
  26. def rows(path, token):
  27. resp = call('GET', path, token=token)
  28. return resp, (resp.get('data') or {}).get('rows') or []
  29. T = {
  30. 'admin': login('admin', 'admin123'),
  31. 'A': login('vsuserA', 'Vs1118pass'),
  32. 'B': login('vsuserB', 'Vs1118pass'),
  33. 'C': login('vsuserC', 'Vs1118pass'),
  34. 'D': login('vsuserD', 'Vs1118pass'),
  35. }
  36. # R1 D account permission set
  37. info = call('GET', '/auth/info', token=T['D'])
  38. perms = set((info.get('data') or {}).get('permissions') or [])
  39. check('R1 D permission set = expert only',
  40. {'ai:expert:query', 'ai:expert:edit', 'ai:expert:remove'}.issubset(perms) and 'ai:agent:query' not in perms,
  41. sorted(perms))
  42. # R2 D can list experts (previously 403)
  43. resp, list_d = rows('/ai/agent/list?pageSize=100', T['D'])
  44. codes = sorted(x['agentCode'] for x in list_d)
  45. check('R2 D expert list 200 and sees public+dept (2)', resp.get('code') == 200 and len(list_d) == 2, codes)
  46. # R3 option endpoints usable with expert permission only
  47. opt_paths = [
  48. ('model', '/ai/model/options?modelType=chat'),
  49. ('tool', '/ai/agent/tool/options'),
  50. ('kb', '/ai/kb/visible-options'),
  51. ('skill', '/ai/skill/visible-options'),
  52. ]
  53. for name, path in opt_paths:
  54. r = call('GET', path, token=T['D'])
  55. check(f'R3 D option {name} 200', r.get('code') == 200 and len(r.get('data') or []) >= 0,
  56. f"code={r.get('code')} n={len(r.get('data') or [])}")
  57. # R4 D create/update/delete own expert
  58. create_body = {
  59. 'agentName': 'VS复测仅专家角色专家', 'agentCode': 'vs_retest_d_expert',
  60. 'description': 'expert-only retest', 'modelId': 1761400000000020400, 'status': '0',
  61. 'visibleScope': '2', 'autoRoute': '1', 'memoryEnabled': '0', 'expertWriteEnabled': '1',
  62. }
  63. r = call('POST', '/ai/agent', create_body, T['D'])
  64. check('R4a D create own expert 200 (previously 403)', r.get('code') == 200, r)
  65. resp, list_d = rows('/ai/agent/list?pageSize=100', T['D'])
  66. own = [x for x in list_d if x['agentCode'] == 'vs_retest_d_expert']
  67. check('R4b D list contains own record and total=3', len(list_d) == 3 and len(own) == 1,
  68. [x['agentCode'] for x in list_d])
  69. own_id = own[0]['id'] if own else None
  70. if own_id:
  71. r = call('PUT', '/ai/agent', {
  72. 'id': own_id, 'agentName': 'VS复测仅专家角色专家改', 'agentCode': 'vs_retest_d_expert',
  73. 'modelId': 1761400000000020400, 'visibleScope': '2',
  74. }, T['D'])
  75. check('R4c D update own expert 200 (previously 403)', r.get('code') == 200, r)
  76. resp, list_d = rows('/ai/agent/list?pageSize=100', T['D'])
  77. own = [x for x in list_d if x['agentCode'] == 'vs_retest_d_expert']
  78. check('R4d D update persisted', bool(own) and own[0]['agentName'] == 'VS复测仅专家角色专家改',
  79. own[0]['agentName'] if own else None)
  80. r = call('DELETE', '/ai/agent/' + own_id, token=T['D'])
  81. check('R4e D delete own expert 200 (previously 403)', r.get('code') == 200, r)
  82. resp, list_d = rows('/ai/agent/list?pageSize=100', T['D'])
  83. check('R4f D list back to 2 after delete', len(list_d) == 2, [x['agentCode'] for x in list_d])
  84. # R5 entry remains ai:agent guarded (separation)
  85. r = call('GET', '/ai/agent/entry', token=T['D'])
  86. check('R5a D cannot read entry (403)', r.get('code') == 403, r)
  87. r = call('GET', '/ai/agent/entry', token=T['A'])
  88. check('R5b A can read entry with ai:agent:query', r.get('code') == 200, str(r.get('code')))
  89. # R6 D cannot manage A dept expert; canManage error should now be reached
  90. r = call('DELETE', '/ai/agent/2123022766514409473', token=T['D'])
  91. check('R6 D delete A dept expert rejected by canManage',
  92. r.get('code') == 500 and '仅创建人或超级管理员' in (r.get('message') or ''), r)
  93. # R7 A delete own temp expert still works
  94. r = call('POST', '/ai/agent', {
  95. 'agentName': 'VS复测删除临时专家', 'agentCode': 'vs_retest_del_expert',
  96. 'description': 'delete retest', 'modelId': 1761400000000020400, 'status': '0',
  97. 'visibleScope': '2', 'autoRoute': '1', 'expertWriteEnabled': '1',
  98. }, T['A'])
  99. check('R7a A create temp expert 200', r.get('code') == 200, r)
  100. resp, list_a = rows('/ai/agent/list?pageSize=100', T['A'])
  101. temp = [x for x in list_a if x['agentCode'] == 'vs_retest_del_expert']
  102. if temp:
  103. r = call('DELETE', '/ai/agent/' + temp[0]['id'], token=T['A'])
  104. check('R7b A delete own expert 200', r.get('code') == 200, r)
  105. # R8 D creates dept-scope record: B sees, C does not; D can delete it
  106. r = call('POST', '/ai/agent', {
  107. 'agentName': 'VS复测部门专家', 'agentCode': 'vs_retest_dept_expert',
  108. 'description': 'dept retest', 'modelId': 1761400000000020400, 'status': '0',
  109. 'visibleScope': '1', 'ownerDeptId': D1, 'autoRoute': '1', 'expertWriteEnabled': '1',
  110. }, T['D'])
  111. check('R8a D create dept-scope expert 200', r.get('code') == 200, r)
  112. resp, list_b = rows('/ai/agent/list?pageSize=100', T['B'])
  113. resp, list_c = rows('/ai/agent/list?pageSize=100', T['C'])
  114. check('R8b B(D1) sees D dept record', any(x['agentCode'] == 'vs_retest_dept_expert' for x in list_b),
  115. [x['agentCode'] for x in list_b])
  116. check('R8c C(D2) cannot see D dept record', all(x['agentCode'] != 'vs_retest_dept_expert' for x in list_c),
  117. [x['agentCode'] for x in list_c])
  118. resp, list_d = rows('/ai/agent/list?pageSize=100', T['D'])
  119. dept_rec = [x for x in list_d if x['agentCode'] == 'vs_retest_dept_expert']
  120. if dept_rec:
  121. check('R8d D sees canManage=true for own dept record', dept_rec[0]['canManage'] is True, dept_rec[0]['canManage'])
  122. r = call('DELETE', '/ai/agent/' + dept_rec[0]['id'], token=T['D'])
  123. check('R8e D delete own dept record 200', r.get('code') == 200, r)
  124. # R9 D cannot create public expert
  125. r = call('POST', '/ai/agent', {
  126. 'agentName': 'VS复测越权公开', 'agentCode': 'vs_retest_pub_try',
  127. 'description': 'x', 'modelId': 1761400000000020400, 'status': '0',
  128. 'visibleScope': '0', 'autoRoute': '1', 'expertWriteEnabled': '1',
  129. }, T['D'])
  130. check('R9 D create scope=0 rejected',
  131. r.get('code') == 500 and '普通用户不能设置全员/公开可见' in (r.get('message') or ''), r)
  132. # R10 core list counts unchanged
  133. expected = {'A': 3, 'B': 2, 'C': 1, 'admin': 3}
  134. for user, exp in expected.items():
  135. resp, list_rows = rows('/ai/agent/list?pageSize=100', T[user])
  136. check(f'R10 expert list {user} count={exp}', resp.get('code') == 200 and len(list_rows) == exp,
  137. [x['agentCode'] for x in list_rows])
  138. # R11 workbench expert candidates still filtered
  139. expected_candidates = {'A': 3, 'B': 2, 'C': 1, 'admin': 3}
  140. for user, exp in expected_candidates.items():
  141. r = call('GET', '/ai/agent/experts/candidates', token=T[user])
  142. check(f'R11 candidates {user} count={exp}', r.get('code') == 200 and len(r.get('data') or []) == exp,
  143. len(r.get('data') or []))
  144. # cleanup any leftover retest records
  145. for user in ['A', 'D']:
  146. resp, list_rows = rows('/ai/agent/list?pageSize=100', T[user])
  147. for rec in list_rows:
  148. if rec['agentCode'].startswith('vs_retest_'):
  149. call('DELETE', '/ai/agent/' + rec['id'], token=T[user])
  150. json.dump(results, open('retest_api_results.json', 'w'), ensure_ascii=False, indent=2)
  151. print('TOTAL', len(results), 'PASS', sum(1 for x in results if x['pass']),
  152. 'FAIL', sum(1 for x in results if not x['pass']))