Spaces:
Sleeping
Sleeping
File size: 4,052 Bytes
caf8059 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import unittest
from pathlib import Path
from types import SimpleNamespace
from fastapi.testclient import TestClient
from src.adapters.persistence.local_identity import LocalIdentity
from src.apps.workbench.erp_flow import ERPFlow
from src.apps.workbench.web import create_web
from src.tests.runtime.fixtures import workspace
class BusinessWebTests(unittest.TestCase):
def setUp(self):
folder=Path(self.enterContext(workspace()))
self.identity=LocalIdentity(folder/'identity.sqlite')
self.identity.seed_projects();self.identity.seed_demo_users()
service=ERPFlow(self.identity,folder/'knowledge.sqlite')
self.client=self.enterContext(TestClient(create_web(SimpleNamespace(identity=self.identity,business=service,erp=service,shutdown=lambda:None))))
self.client.headers['Origin']='http://127.0.0.1:8789'
def login(self,username='programmer'):
response=self.client.post('/api/session/login',json={'username':username,'password':'VibeCoding@2026'})
self.assertEqual(response.status_code,200)
session=self.client.get('/api/session').json()
self.client.headers['X-CSRF-Token']=session['csrf']
return self.identity.actor(session['actor']['user_id'])
def test_business_endpoints_require_real_session_and_csrf(self):
self.assertEqual(self.client.get('/api/business/scenarios').status_code,401)
self.login()
response=self.client.post('/api/business/ask',json={'scenario':'erp','question':'查询最新完成订单'},headers={'X-CSRF-Token':'bad'})
self.assertEqual(response.status_code,403)
response=self.client.post('/api/business/actions',json={'scenario':'wms','action':'receive','entity_id':'SKU-DEMO-001','parameters':{'quantity':5},'expected_version':True,'request_key':'test-key-123'})
self.assertEqual(response.status_code,422)
def test_all_scenarios_expose_actual_stdio_health(self):
self.login();response=self.client.get('/api/business/scenarios')
self.assertEqual(response.status_code,200)
self.assertEqual({s['scenario'] for s in response.json()},{'erp','wms','equipment'})
for s in response.json():
self.assertEqual(s['health']['transport'],'stdio')
self.assertTrue(s['health']['process_exited']);self.assertTrue(s['tools'])
def test_unknown_action_parameters_cannot_change_record_owner(self):
self.login()
original=self.client.post('/api/business/ask',json={'scenario':'wms','question':'查询SKU-DEMO-001库存'}).json()['records'][0]
response=self.client.post('/api/business/actions',json={'scenario':'wms','action':'receive','entity_id':'SKU-DEMO-001','parameters':{'quantity':5,'owner':'manager'},'expected_version':original['version'],'request_key':'test-key-123'})
self.assertEqual(response.status_code,422)
self.assertEqual(response.json()['code'],'ACTION_INVALID')
current=self.client.post('/api/business/ask',json={'scenario':'wms','question':'查询SKU-DEMO-001库存'}).json()['records'][0]
self.assertEqual(original['content_digest'],current['content_digest'])
def test_membership_revocation_applies_to_ask_actions_and_reset(self):
actor=self.login()
token,_=self.identity.login('manager','VibeCoding@2026');manager=self.identity.session(token)[0]
self.identity.set_member(manager,'wms-demo',actor.user_id,False)
self.assertEqual(self.client.post('/api/business/ask',json={'scenario':'wms','question':'查询库存'}).status_code,404)
self.assertEqual(self.client.post('/api/business/reset',json={'scenario':'wms','request_key':'test-reset-key'}).status_code,404)
self.assertEqual(self.client.post('/api/business/actions',json={'scenario':'wms','action':'receive','entity_id':'SKU-DEMO-001','parameters':{'quantity':5},'expected_version':1,'request_key':'test-key-123'}).status_code,404)
self.assertEqual({s['scenario'] for s in self.client.get('/api/business/scenarios').json()},{'erp','equipment'})
|