def test_do_prepare_not_found(self): mock_send = mock.Mock( return_value=Response(self.status_not_found, self.resp_not_found)) mock_run_cmd = mock.Mock(side_effect=[ self.cipher, json.dumps({ "ErrCode": CODE_DECRYPT_FAILED, "ErrMessage": MSG_DECRYPT_FAILED }) ]) cert_store = Client(self.apikey, self.cert_path, self.sig_param, self.uri, True) with mock.patch('requests.Session.send', mock_send): poeid_filepart = ( "", "poe id", ) files = { "poe_id": poeid_filepart, } _, result = cert_store.do_prepare( requests.Request("POST", url=self.url, files=files).prepare(), ) self.assertEqual(MSG_DECRYPT_FAILED, result["ErrMessage"]) self.assertEqual(CODE_DECRYPT_FAILED, result["ErrCode"])
def test_do_prepare_empyt(self): mock_send = mock.Mock(return_value=Response(self.status_not_found, "")) mock_run_cmd = mock.Mock(side_effect=[ self.cipher, json.dumps({ "ErrCode": CODE_RESP_BODY_EMPTY, "ErrMsg": MSG_RESP_BODY_EMPTY }) ]) cert_store = Client(self.apikey, self.cert_path, self.uri) with mock.patch('cryption.crypto.run_cmd', mock_run_cmd): with mock.patch('requests.Session.send', mock_send): poeid_filepart = ( "", "poe id", ) files = { "poe_id": poeid_filepart, } _, result = cert_store.do_prepare( requests.Request("POST", url=self.url, files=files).prepare(), ) self.assertEqual(MSG_RESP_BODY_EMPTY, result["ErrMsg"]) self.assertEqual(CODE_RESP_BODY_EMPTY, result["ErrCode"])
def test_do_prepare_empyt(self): mock_send = mock.Mock(return_value=Response(self.status_not_found, "")) #mock_run_cmd = mock.Mock(side_effect=[self.cipher, json.dumps({"ErrCode": CODE_SERVER_RESP_INVALID, "ErrMessage": MSG_SERVER_RESP_INVALID})]) cert_store = Client( self.apikey, #self.cert_path, self.sig_param, self.uri, True, cert=('./tls/client.pem', './tls/client.key'), cacert='./tls/ca.crt') with mock.patch('requests.Session.send', mock_send): poeid_filepart = ( "", "poe id", ) files = { "poe_id": poeid_filepart, } _, result = cert_store.do_prepare( requests.Request("POST", url=self.url, files=files).prepare(), ) self.assertEqual(MSG_SERVER_RESP_INVALID, result["ErrMessage"]) self.assertEqual(CODE_SERVER_RESP_INVALID, result["ErrCode"])
def test_do_put(self): httpretty.register_uri(httpretty.PUT, self.uri, status=self.status_ok, body=json.dumps(self.resp)) cert_store = Client(self.apikey, self.cert_path, self.uri) cert_store.set_url() result = cert_store.do_put(self.header, self.request) content = json.loads(result.content) self.assertEqual(self.status_ok, result.status_code) self.assertEqual(0, content["ErrCode"])
def test_do_delete(self): httpretty.register_uri(httpretty.DELETE, self.uri, status=self.status_ok, body=json.dumps(self.resp)) cert_store = Client(self.apikey, self.cert_path, self.sig_param, self.uri, True) # cert_store.set_url() result = cert_store.do_delete(self.uri, self.header) content = json.loads(result.content) self.assertEqual(self.status_ok, result.status_code) self.assertEqual(0, content["ErrCode"])
def test_do_request_not_found(self): mock_do_post = mock.Mock( return_value=Response(self.status_not_found, self.resp_not_found)) cert_store = Client(self.apikey, self.cert_path, self.sig_param, self.uri, True) request_func = cert_store.do_post with mock.patch('requests.post', mock_do_post): _, result = cert_store.do_request( { "url": self.uri, "headers": self.header, "body": self.request, }, request_func) self.assertEqual(MSG_DECRYPT_FAILED, result["ErrMessage"]) self.assertEqual(CODE_DECRYPT_FAILED, result["ErrCode"])
def test_do_request_succ(self): mock_do_post = mock.Mock( return_value=Response(self.status_ok, json.dumps(self.resp))) cert_store = Client(self.apikey, self.cert_path, self.sig_param, self.uri, True) request_func = cert_store.do_post with mock.patch('requests.post', mock_do_post): _, result = cert_store.do_request( { "url": self.uri, "headers": self.header, "body": self.request, }, request_func) self.assertTrue(result)
def test_do_request_with_no_encrypt_succ(self): mock_do_post = mock.Mock( return_value=Response(self.status_ok, json.dumps(self.resp))) cert_store = Client(self.apikey, self.cert_path, self.uri, False) request_func = cert_store.do_post with mock.patch('requests.post', mock_do_post): _, result = cert_store.do_request( { "headers": self.header, "body": self.request, }, request_func, ) self.assertEqual(0, result["ErrCode"])
def test_do_request_empty(self): mock_do_post = mock.Mock( return_value=Response(self.status_not_found, "")) mock_run_cmd = mock.Mock(return_value=self.cipher) cert_store = Client(self.apikey, self.cert_path, self.uri) request_func = cert_store.do_post with mock.patch('cryption.crypto.run_cmd', mock_run_cmd): with mock.patch('requests.post', mock_do_post): _, result = cert_store.do_request( { "headers": self.header, "body": self.request, }, request_func) self.assertEqual(MSG_RESP_BODY_EMPTY, result["ErrMsg"]) self.assertEqual(CODE_RESP_BODY_EMPTY, result["ErrCode"])
def test_do_request_succ(self): mock_do_post = mock.Mock( return_value=Response(self.status_ok, json.dumps(self.resp))) mock_run_cmd = mock.Mock( side_effect=[self.cipher, json.dumps(self.resp)]) cert_store = Client(self.apikey, self.cert_path, self.uri) request_func = cert_store.do_post with mock.patch('cryption.crypto.run_cmd', mock_run_cmd): with mock.patch('requests.post', mock_do_post): _, result = cert_store.do_request( { "headers": self.header, "body": self.request, }, request_func) self.assertEqual(0, result["ErrCode"])
def test_do_request_empty(self): mock_do_post = mock.Mock( return_value=Response(self.status_not_found, "")) mock_run_cmd = mock.Mock(return_value=self.cipher) cert_store = Client(self.apikey, self.cert_path, self.sig_param, self.uri, True) request_func = cert_store.do_post with mock.patch('requests.post', mock_do_post): _, result = cert_store.do_request( { "url": self.uri, "headers": self.header, "body": self.request, }, request_func) self.assertEqual(MSG_SERVER_RESP_INVALID, result["ErrMessage"]) self.assertEqual(CODE_SERVER_RESP_INVALID, result["ErrCode"])
def test_do_prepare_succ(self): mock_send = mock.Mock( return_value=Response(self.status_ok, json.dumps(self.resp))) cert_store = Client(self.apikey, self.cert_path, self.sig_param, self.uri, True) with mock.patch('requests.Session.send', mock_send): poeid_filepart = ( "", "poe id", ) files = {"poe_id": poeid_filepart} _, result = cert_store.do_prepare( requests.Request("POST", url=self.url, files=files).prepare(), ) self.assertTrue(result["ErrCode"])
def test_do_post(self): httpretty.register_uri(httpretty.POST, self.uri, status=self.status_ok, body=json.dumps(self.resp)) cert_store = Client( self.apikey, #self.cert_path, self.sig_param, self.uri, True, cert=('./tls/client.pem', './tls/client.key'), cacert='./tls/ca.crt') # cert_store.set_url() result = cert_store.do_post(self.uri, self.header, self.request) content = json.loads(result.content) self.assertEqual(self.status_ok, result.status_code) self.assertEqual(0, content["ErrCode"])
def test_do_prepare_succ(self): mock_send = mock.Mock( return_value=Response(self.status_ok, json.dumps(self.resp))) mock_run_cmd = mock.Mock( side_effect=[self.cipher, json.dumps(self.resp)]) cert_store = Client(self.apikey, self.cert_path, self.uri) with mock.patch('cryption.crypto.run_cmd', mock_run_cmd): with mock.patch('requests.Session.send', mock_send): poeid_filepart = ( "", "poe id", ) files = {"poe_id": poeid_filepart} _, result = cert_store.do_prepare( requests.Request("POST", url=self.url, files=files).prepare(), ) self.assertEqual(0, result["ErrCode"])
def test_do_request_not_found(self): mock_do_post = mock.Mock( return_value=Response(self.status_not_found, self.resp_not_found)) cert_store = Client( self.apikey, #self.cert_path, self.sig_param, self.uri, True, cert=('./tls/client.pem', './tls/client.key'), cacert='./tls/ca.crt') request_func = cert_store.do_post with mock.patch('requests.post', mock_do_post): _, result = cert_store.do_request( { "url": self.uri, "headers": self.header, "body": self.request, }, request_func) self.assertEqual(MSG_SERVER_RESP_INVALID, result["ErrMessage"]) self.assertEqual(CODE_SERVER_RESP_INVALID, result["ErrCode"])
def test_do_request_not_found(self): mock_do_post = mock.Mock( return_value=Response(self.status_not_found, self.resp_not_found)) mock_run_cmd = mock.Mock(side_effect=[ self.cipher, json.dumps({ "ErrCode": CODE_DECRYPT_FAILED, "ErrMsg": MSG_DECRYPT_FAILED }) ]) cert_store = Client(self.apikey, self.cert_path, self.uri) request_func = cert_store.do_post with mock.patch('cryption.crypto.run_cmd', mock_run_cmd): with mock.patch('requests.post', mock_do_post): _, result = cert_store.do_request( { "headers": self.header, "body": self.request, }, request_func) self.assertEqual(MSG_DECRYPT_FAILED, result["ErrMsg"]) self.assertEqual(CODE_DECRYPT_FAILED, result["ErrCode"])
def test_do_request_with_no_encrypt_succ(self): mock_do_post = mock.Mock( return_value=Response(self.status_ok, json.dumps(self.resp))) cert_store = Client( self.apikey, #self.cert_path, self.sig_param, self.uri, True, cert=('./tls/client.pem', './tls/client.key'), cacert='./tls/ca.crt') request_func = cert_store.do_post with mock.patch('requests.post', mock_do_post): _, result = cert_store.do_request( { "url": self.uri, "headers": self.header, "body": self.request, }, request_func, ) self.assertEqual(0, result["ErrCode"])
def __init__(self): self.api_key = "IoZYarPTp1532411905" self.cert_path = "C:/Python27/Lib/site-packages/py_common-2.0.1-py2.7.egg/cryption/ecc/certs" self.ent_sign_param = { "creator": "did:axn:124d00f2-ea55-4724-8e58-31680d443628", "created": "", "nonce": "", "privateB64": "2RPpCLAl0CNiiXMLjUNSC1acqtkvU8+U9MtU2yvo4Vz52m8mW4+UrvqmFosxi/pu/AzpFf+CCQtutYCtKOZFoQ==" } self.ip_address = "http://139.198.15.132:9143" self.client = Client(self.api_key, self.cert_path, self.ent_sign_param, self.ip_address) self.wallet = WalletClient(self.client) self.header = { "Bc-Invoke-Mode": "sync" } self.message_center = "did:axn:af7380cb-2137-4c2d-a98e-62aba671f6df" self.permission_center = "did:axn:a20b40ff-ccff-4f7a-a839-acbde84b6a6e"
def __init__(self, ): #self.apikey = apikey #self.cert_path = cert_path #self.ip_addr = ip_addr with open('api_data.csv', 'rb', encoding='utf-8') as f: reader = csv.DictReader(f) js = dict(reader) self.apikey = js['apikey'] self.cert_path = js['cert_path'] self.ip_addr = js['ip_addr'] self.__creator = js['creator'] self.__created = js['created'] self.__nonce = js['nonce'] self.__privateB64 = js['privateB64'] self.ent_sign_param = { "creator": self.__creator, "created": self.__created, "nonce": self.__nonce, "privateB64": self.__privateB64 } self.client = Client(self.apikey, self.cert_path, self.ent_sign_param, self.ip_addr) self.walletclient = WalletClient(self.client) self.header = {"Bc-Invoke-Mode": "sync"}
# coding=utf-8 from api.wallet import WalletClient import json from rest.api.api import Client ent_sign_param = { "creator": "did:axn:17e8db57-a0fa-4028-b17f-802d01602194", "nonce": "0", "privateB64": "lgdYjMbh7BKCeGxmJrempaP+/cDGMy5StdjM4ZexB0/Ku8V1uI81OlhKdnsCJVqTrXe2vGWKtXR+ciuozCgqjQ==" } client = Client( "qYIwhZPTp1532489219", # eg: "z_pGym-Tp15288809805" # 企业用户申请的API-KEY "/usr/local/lib/python2.7/dist-packages/py_common-2.0.1-py2.7.egg/cryption/ecc/certs", # eg: /usr/local/lib/python2.7/site-packages/py_common-1.5.0-py2.7.egg/cryption/ecc/certs ent_sign_param, "http://139.198.15.132:9143") walletclient = WalletClient(client) # header {"Bc-Invoke-Mode": "sync"} for sync invoke mode. {} or header {"Bc-Invoke-Mode": "async"} for async invoke mode. # read wallet-sdk-py README "Using callback URL to receive blockchain transaction event" for more details header = {"Bc-Invoke-Mode": "sync"} body = { "access": "test06", # 普通用户用户名, 长度为4-36位 "secret": "Integrate1230", # 普通用户密码, 由8-16位英文大小写字母和数字组成,必须同时包含大小写字母和数字 "type": "Person", # 类型 "id": ""} # id可以为空,由系统自动生成 _,response = walletclient.register(header,body) print response
from api.wallet import WalletClient from rest.api.api import Client from cryption.crypto import sign class Response(object): def __init__(self, status_code, text): self.status_code = status_code self.text = text IP = "http://127.0.0.1:9143" APIKEY = "pWEzB4yMM1518346407" cert_path = "/your/cert/path" ent_sign_param = {} cert_store = Client( APIKEY, cert_path, ent_sign_param, IP ) secret_key_b64 = "0lxEFzMQhn68vY2F0f+nOwP7kl5zjahjPcfyMAJVmzn0HNQssIIYh+c2CgCKEHeUvxqCu6W/sJKqKt2DLJnKpw==" nonce = "nonce" did = "did:ara:8uQhQMGzWxR8vw5P3UWH1j" fromdid = "did:ara:9uQhQMGzWxR8vw5P3UWH1b" create_time = "1517818060" payload = { "from": "did:axn:8uQhQMGzWxR8vw5P3UWH1j", "to": "did:axn:21tDAKCERh95uGgKbJNHYp", "issuer": "did:axn:8uQhQMGzWxR8vw5P3UWH1j", "asset_id": "1f38a7a1-2c79-465e-a4c0-0038e25c7edg", "tokens": [ { "token_id": "0684f2fccc8c3209d34c0ab3d8f690b2c88c9aa87cad41259e981ba9556f42e7",
"accounts": [ did ], "coins": [ { "coin_id": coin_id, "amount": 5 } ] } } sig_cipher = "signed cipher" cert_store = Client( APIKEY, cert_path, url ) tc = Transaction(cert_store) client_cipher = "client cipher" server_cipher = "server cipher" class TransactionTest(unittest.TestCase): """Transaction test. """ def setUp(self): pass def test_transfer_asset_succ(self): """Test transfer asset successfully returned. """ mock_do_post = mock.Mock(return_value=Response(200, server_cipher))
from cryption.crypto import sign class Response(object): def __init__(self, status_code, text): self.status_code = status_code self.text = text IP = "http://127.0.0.1:9143" APIKEY = "pWEzB4yMM1518346407" cert_path = "/your/cert/path" ent_sign_param = {} cert_store = Client( APIKEY, #cert_path, ent_sign_param, IP, True, cert=('./tls/client.pem', './tls/client.key'), cacert='./tls/ca.crt' ) secret_key_b64 = "0lxEFzMQhn68vY2F0f+nOwP7kl5zjahjPcfyMAJVmzn0HNQssIIYh+c2CgCKEHeUvxqCu6W/sJKqKt2DLJnKpw==" nonce = "nonce" did = "did:axn:8uQhQMGzWxR8vw5P3UWH1j" fromdid = "did:ara:9uQhQMGzWxR8vw5P3UWH1b" create_time = "1517818060" payload = { "from": "did:axn:8uQhQMGzWxR8vw5P3UWH1j", "to": "did:axn:21tDAKCERh95uGgKbJNHYp", "issuer": "did:axn:8uQhQMGzWxR8vw5P3UWH1j", "asset_id": "1f38a7a1-2c79-465e-a4c0-0038e25c7edg", "tokens": [
from rest.api.api import Client from api.wallet import WalletClient apikey = "JTZdtpNTp1540767548" cert_path = "/usr/local/lib/python2.7/site-packages/py_common-2.0.1-py2.7.egg/cryption/ecc/certs" ip_addr = "http://139.198.15.132:9143" ent_sign_param = { "creator": "did:axn:bcaad1cb-e1f2-415b-b110-1fb4da5a08a2", "nonce": "ubiright", "privateB64": "SELx1xc9ad83Ixa10lXU96pFq7mn4OAFMuuU5uGXtQx8e3+TF6ulYl0xsbVnGQOShKaPqY7We4tjTZ4wOkn6dw==" } client = Client(apikey, cert_path, ent_sign_param, ip_addr) walletClient = WalletClient(client)