BVB Source Codes

voltron Show api_message_tests.py Source code

Return Download voltron: download api_message_tests.py Source code - Download voltron Source code - Type:.py
  1. import time
  2.  
  3. from nose.tools import *
  4.  
  5. import voltron
  6. from voltron.core import *
  7. from voltron.api import *
  8. from voltron.plugin import *
  9.  
  10. log = logging.getLogger('tests')
  11.  
  12.  
  13. class APITestRequest(APIRequest):
  14.     _fields = {'target_id': False, 'address': False, 'count': True}
  15.     _types = {'target_id': int, 'address': int, 'count': int}
  16.  
  17.     target_id = 0
  18.     address = None
  19.     count = None
  20.  
  21.  
  22. class APITestResponse(APISuccessResponse):
  23.     _fields = {'disassembly': True}
  24.  
  25.  
  26. class APITestPlugin(APIPlugin):
  27.     request = 'test'
  28.     request_class = APITestRequest
  29.     response_class = APITestResponse
  30.  
  31.  
  32. def setup():
  33.     voltron.setup_env()
  34.  
  35.  
  36. def teardown():
  37.     time.sleep(2)
  38.  
  39.  
  40. def test_parent_message_validation_fail():
  41.     msg = APIMessage()
  42.     exception = False
  43.     try:
  44.         msg.validate()
  45.     except MissingFieldError:
  46.         exception = True
  47.     assert exception
  48.  
  49.  
  50. def test_parent_request_validation_fail():
  51.     msg = APIRequest()
  52.     exception = False
  53.     try:
  54.         msg.validate()
  55.     except MissingFieldError:
  56.         exception = True
  57.     assert exception
  58.  
  59.  
  60. def test_parent_request_type():
  61.     msg = APIRequest()
  62.     assert msg.type == 'request'
  63.  
  64.  
  65. def test_parent_request_request():
  66.     msg = APIRequest()
  67.     assert msg.request is None
  68.  
  69.  
  70. def test_parent_response_validation_fail():
  71.     msg = APIResponse()
  72.     exception = False
  73.     try:
  74.         msg.validate()
  75.     except MissingFieldError:
  76.         exception = True
  77.     assert exception
  78.  
  79.  
  80. def test_parent_response_type():
  81.     msg = APIResponse()
  82.     assert msg.type == 'response'
  83.  
  84.  
  85. def test_parent_response_status():
  86.     msg = APIResponse()
  87.     assert msg.status is None
  88.  
  89.  
  90. def test_success_response_validation_succeed():
  91.     msg = APISuccessResponse()
  92.     exception = False
  93.     try:
  94.         msg.validate()
  95.     except MissingFieldError:
  96.         exception = True
  97.     assert not exception
  98.  
  99.  
  100. def test_success_response_type():
  101.     msg = APISuccessResponse()
  102.     assert msg.type == 'response'
  103.  
  104.  
  105. def test_success_response_status():
  106.     msg = APISuccessResponse()
  107.     assert msg.status == 'success'
  108.  
  109.  
  110. def test_error_response_validation_fail():
  111.     msg = APIErrorResponse()
  112.     exception = False
  113.     try:
  114.         msg.validate()
  115.     except MissingFieldError:
  116.         exception = True
  117.     assert exception
  118.  
  119.  
  120. def test_error_response_type():
  121.     msg = APIErrorResponse()
  122.     assert msg.type == 'response'
  123.  
  124.  
  125. def test_error_response_status():
  126.     msg = APIErrorResponse()
  127.     assert msg.status == 'error'
  128.  
  129.  
  130. def test_invalid_request_error_response_validation_succeed():
  131.     msg = APIInvalidRequestErrorResponse()
  132.     exception = False
  133.     try:
  134.         msg.validate()
  135.     except MissingFieldError:
  136.         exception = True
  137.     assert not exception
  138.  
  139.  
  140. def test_invalid_request_error_response_type():
  141.     msg = APIInvalidRequestErrorResponse()
  142.     assert msg.type == 'response'
  143.  
  144.  
  145. def test_invalid_request_error_response_status():
  146.     msg = APIInvalidRequestErrorResponse()
  147.     assert msg.status == 'error'
  148.  
  149.  
  150. def test_test_request_validation_fail():
  151.     msg = APITestRequest()
  152.     exception = False
  153.     try:
  154.         msg.validate()
  155.     except MissingFieldError:
  156.         exception = True
  157.     assert exception
  158.  
  159.  
  160. def test_test_request_validation_fail_with_param():
  161.     msg = APITestRequest(target_id=0)
  162.     exception = False
  163.     try:
  164.         msg.validate()
  165.     except MissingFieldError:
  166.         exception = True
  167.     assert exception
  168.  
  169.  
  170. def test_test_request_validation_succeed_with_param():
  171.     msg = api_request('test', count=16)
  172.     exception = False
  173.     try:
  174.         msg.validate()
  175.     except MissingFieldError:
  176.         exception = True
  177.     assert not exception
  178.     assert msg.count == 16
  179.  
  180.  
  181. def test_test_request_validation_succeed_with_data():
  182.     msg = APITestRequest('{"data":{"count":16}}')
  183.     exception = False
  184.     try:
  185.         msg.validate()
  186.     except MissingFieldError as e:
  187.         exception = True
  188.     assert not exception
  189.     assert msg.count == 16
  190.  
  191.  
  192. def test_test_request_validation_succeed_by_assign():
  193.     msg = APITestRequest()
  194.     msg.count = 16
  195.     exception = False
  196.     try:
  197.         msg.validate()
  198.     except MissingFieldError as e:
  199.         exception = True
  200.     assert not exception
  201.     assert msg.count == 16
  202.  
  203.  
  204. def test_test_request_string():
  205.     msg = APITestRequest(count=16)
  206.     assert json.loads(str(msg)) == {"request": "test", "type": "request", "block": False, "timeout": 10,
  207.                                     "data": {"count": 16, "target_id": 0, "address": None}}
  208.  
  209.  
  210. def test_test_response_validation_fail():
  211.     msg = APITestResponse()
  212.     exception = False
  213.     try:
  214.         msg.validate()
  215.     except MissingFieldError as e:
  216.         exception = True
  217.     assert exception
  218.  
  219.  
  220. def test_test_response_validation_fail_with_param():
  221.     msg = APITestResponse(thing=1)
  222.     exception = False
  223.     try:
  224.         msg.validate()
  225.     except MissingFieldError as e:
  226.         exception = True
  227.     assert exception
  228.  
  229.  
  230. def test_test_response_validation_succeed_with_param():
  231.     msg = APITestResponse(disassembly="xxx")
  232.     exception = False
  233.     try:
  234.         msg.validate()
  235.     except MissingFieldError as e:
  236.         exception = True
  237.     assert not exception
  238.  
  239.  
  240. def test_test_response_validation_succeed_with_data():
  241.     msg = APITestResponse('{"data":{"disassembly":"xxx"}}')
  242.     exception = False
  243.     try:
  244.         msg.validate()
  245.     except MissingFieldError as e:
  246.         exception = True
  247.     assert not exception
  248.  
  249.  
  250. def test_test_response_validation_succeed_by_assign():
  251.     msg = APITestResponse()
  252.     msg.disassembly = "xxx"
  253.     exception = False
  254.     try:
  255.         msg.validate()
  256.     except MissingFieldError as e:
  257.         print(str(e))
  258.         exception = True
  259.     assert not exception
  260.  
  261.  
  262. def test_test_response_string():
  263.     msg = APITestResponse(disassembly='xxx')
  264.     assert json.loads(str(msg)) == {"status": "success", "type": "response", "data": {"disassembly": "xxx"}}
  265.  
  266.  
  267. class APIEncodeMsg(APIMessage):
  268.     _fields = {'enc': False}
  269.     _encode_fields = ['enc']
  270.  
  271.  
  272. def test_encode_fields():
  273.     msg = APIEncodeMsg()
  274.     msg.enc = six.b('').join([six.int2byte(x) for x in range(0x0, 0xff)])
  275.     assert msg.to_dict()['data']['enc'] == six.text_type('AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+')
  276.     assert msg.to_json() == '{"data": {"enc": "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+"}, "type": null}'
  277.  
  278.     msg2 = APIEncodeMsg(data=msg.to_json())
  279.     assert msg.to_dict() == msg2.to_dict()
  280.     assert msg.to_json() == msg2.to_json()
  281.     assert msg2.enc == msg.enc
  282.  
  283.     msg3 = APIEncodeMsg()
  284.     msg3.enc = six.u('xxxx')
  285.     assert msg3.to_dict() == {'data': {'enc': 'eHh4eA=='}, 'type': None}
  286.     msg3.enc = six.b('xxxx')
  287.     assert msg3.to_dict() == {'data': {'enc': 'eHh4eA=='}, 'type': None}
  288.  
  289.     msg4 = APIEncodeMsg()
  290.     msg4.from_dict(msg.to_dict())
  291.     assert msg4.to_dict() == msg.to_dict()
  292.  
downloadapi_message_tests.py Source code - Download voltron Source code
Related Source Codes/Software:
IGListKit - A data-driven UICollectionView framework for build... 2017-01-11
androidmvp - MVP Android Example 2017-01-11
svelte - The magical disappearing UI framework ... 2017-01-11
kcptun - A Simple UDP Tunnel Based On KCP 2017-01-11
poisontap - Exploits locked/password protected computers over ... 2017-01-10
ecma262 - Status, process, and documents for ECMA262 ... 2017-01-11
react-native-elements - React Native Elements UI Toolkit 2017-01-11
tensorflow-zh - Google new open source ai system TensorFlow Chines... 2017-01-11
forum - Blue light (the Lantern) the official BBS 2017-01-11
socketcluster - Highly scalable realtime framework ... 2017-01-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top