Files
dpl/scripts/fuc-xb-acceptance/test_cashier.py
T

43 lines
2.4 KiB
Python

"""Regression for the observed processor's HTML negotiation and unpaid boundary."""
import importlib.util,json,pathlib,tempfile,unittest,urllib.parse
spec=importlib.util.spec_from_file_location('cashier',pathlib.Path(__file__).with_name('cashier.py'))
cashier=importlib.util.module_from_spec(spec);spec.loader.exec_module(cashier)
class Response:
def __init__(self,status,body):self.status=status;self.body=body;self.headers={}
def __enter__(self):return self
def __exit__(self,*args):return False
def read(self,limit):return self.body[:limit]
class Processor:
def __init__(self,expired=False):self.requests=[];self.expired=expired
def open(self,req,timeout):
self.requests.append(req)
if req.get_header('Accept')!='text/html':return Response(404,b'<title>404 Not Found</title>')
if self.expired:return Response(200,'<title>支付错误</title>订单已过期'.encode())
fields=urllib.parse.parse_qs(req.data.decode())
return Response(200,('<title>'+fields['type'][0]+' cashier</title>').encode())
class Cashier(unittest.TestCase):
def fixture(self,root):
for kind in ['alipay','wxpay']:
fields={'pid':'synthetic','type':kind,'out_trade_no':'QA-only','money':'1','sign':'synthetic','sign_type':'MD5'}
html='<form action="https://api.shangri-la.vc/submit.php" method="post">'+''.join('<input name="'+k+'" value="'+v+'">' for k,v in fields.items())+'</form>'
(root/('cashier-'+kind+'.html')).write_text(html)
def test_html_navigation_reaches_both_cashiers_without_payment_or_new_order(self):
with tempfile.TemporaryDirectory()as d:
root=pathlib.Path(d);self.fixture(root);processor=Processor();result=cashier.run(root,processor)
self.assertEqual([c['http_status']for c in result['cases']],[200,200])
self.assertEqual(len(processor.requests),2)
self.assertTrue(all(req.get_method()=='POST'and req.full_url=='https://api.shangri-la.vc/submit.php'for req in processor.requests))
self.assertEqual(result['real_money_paid'],0)
self.assertTrue(result['actual_paid_callback_not_tested'])
self.assertEqual(json.loads((root/'cashier-final-result.json').read_text()),result)
def test_expired_200_is_reported_as_business_error(self):
with tempfile.TemporaryDirectory()as d:
root=pathlib.Path(d);self.fixture(root);result=cashier.run(root,Processor(expired=True))
self.assertTrue(all('订单已过期'in c['error_indicators']for c in result['cases']))
if __name__=='__main__':unittest.main()