legacy_test.py 2.3 KB

12345678910111213141516171819202122232425262728293031
  1. import json,urllib.request,urllib.error,psycopg2
  2. BASE='http://localhost:8980/js/a'
  3. def call(m,p,b=None,t=None):
  4. d=json.dumps(b).encode() if b is not None else None
  5. r=urllib.request.Request(BASE+p,data=d,method=m); r.add_header('Content-Type','application/json')
  6. if t:r.add_header('x-token',t)
  7. try:
  8. with urllib.request.urlopen(r,timeout=30) as x:return json.loads(x.read().decode())
  9. except urllib.error.HTTPError as e:return {'http':e.code,'body':e.read().decode()[:300]}
  10. def login(u,p):return call('POST','/auth/login',{'username':u,'password':p,'force':True})['data']['token']
  11. A=login('vsuserA','Vs1118pass'); admin=login('admin','admin123')
  12. conn=psycopg2.connect(host='192.168.0.106',port=5432,dbname='zsjz-ai',user='postgres',password='postgres')
  13. cur=conn.cursor()
  14. cur.execute("update ai_expert_agent set visible_scope='0' where id=2123022768359903234")
  15. conn.commit()
  16. print('simulate legacy scope=0 on A own expert')
  17. r=call('GET','/ai/agent/2123022768359903234',t=A); print('A get scope',r['data']['visibleScope'],'canManage',r['data'].get('canManage'))
  18. # A preserves 0 on update
  19. r=call('PUT','/ai/agent',{'id':'2123022768359903234','agentName':'VS自建专家','agentCode':'vs_expert_self','modelId':1761400000000020400,'visibleScope':'0'},A)
  20. print('A update legacy 0 preserving ->',r)
  21. cur.execute("select visible_scope from ai_expert_agent where id=2123022768359903234"); print('db scope',cur.fetchone())
  22. # A changes to 1 then tries back to 0 -> should fail
  23. r=call('PUT','/ai/agent',{'id':'2123022768359903234','agentName':'VS自建专家','agentCode':'vs_expert_self','modelId':1761400000000020400,'visibleScope':'1','ownerDeptId':'2123022706158374913'},A)
  24. print('A change to 1 ->',r.get('code'),r.get('message'))
  25. r=call('PUT','/ai/agent',{'id':'2123022768359903234','agentName':'VS自建专家','agentCode':'vs_expert_self','modelId':1761400000000020400,'visibleScope':'0'},A)
  26. print('A set back 0 (existing 1) ->',r.get('code'),r.get('message'))
  27. # restore to 2
  28. r=call('PUT','/ai/agent',{'id':'2123022768359903234','agentName':'VS自建专家','agentCode':'vs_expert_self','modelId':1761400000000020400,'visibleScope':'2'},A)
  29. print('A restore 2 ->',r.get('code'))
  30. cur.execute("select visible_scope,owner_dept_id from ai_expert_agent where id=2123022768359903234"); print('db final',cur.fetchone())
  31. conn.close()