| 12345678910111213141516171819202122232425262728293031 |
- import json,urllib.request,urllib.error,psycopg2
- 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']
- A=login('vsuserA','Vs1118pass'); admin=login('admin','admin123')
- conn=psycopg2.connect(host='192.168.0.106',port=5432,dbname='zsjz-ai',user='postgres',password='postgres')
- cur=conn.cursor()
- cur.execute("update ai_expert_agent set visible_scope='0' where id=2123022768359903234")
- conn.commit()
- print('simulate legacy scope=0 on A own expert')
- r=call('GET','/ai/agent/2123022768359903234',t=A); print('A get scope',r['data']['visibleScope'],'canManage',r['data'].get('canManage'))
- # A preserves 0 on update
- r=call('PUT','/ai/agent',{'id':'2123022768359903234','agentName':'VS自建专家','agentCode':'vs_expert_self','modelId':1761400000000020400,'visibleScope':'0'},A)
- print('A update legacy 0 preserving ->',r)
- cur.execute("select visible_scope from ai_expert_agent where id=2123022768359903234"); print('db scope',cur.fetchone())
- # A changes to 1 then tries back to 0 -> should fail
- r=call('PUT','/ai/agent',{'id':'2123022768359903234','agentName':'VS自建专家','agentCode':'vs_expert_self','modelId':1761400000000020400,'visibleScope':'1','ownerDeptId':'2123022706158374913'},A)
- print('A change to 1 ->',r.get('code'),r.get('message'))
- r=call('PUT','/ai/agent',{'id':'2123022768359903234','agentName':'VS自建专家','agentCode':'vs_expert_self','modelId':1761400000000020400,'visibleScope':'0'},A)
- print('A set back 0 (existing 1) ->',r.get('code'),r.get('message'))
- # restore to 2
- r=call('PUT','/ai/agent',{'id':'2123022768359903234','agentName':'VS自建专家','agentCode':'vs_expert_self','modelId':1761400000000020400,'visibleScope':'2'},A)
- print('A restore 2 ->',r.get('code'))
- cur.execute("select visible_scope,owner_dept_id from ai_expert_agent where id=2123022768359903234"); print('db final',cur.fetchone())
- conn.close()
|