test_api.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. """
  4. Views: Viewing of data, changing of data, and custom class-based view methods.
  5. Models: Creating/updating/deleting of models, model methods, model manager methods.
  6. Forms: Form methods, clean() methods, and custom fields
  7. Validators: Really dig in and write multiple test methods against each custom validator you write.
  8. Pretend you are a malignant intruder attempting to damage the data in the site.
  9. Signals: Since they act at a distance, signals can cause grief especially if you lack tests on them.
  10. Filters: Since filters are essentially just functions accepting one or two arguments, writing tests for
  11. them should be easy.
  12. Miscellany: Context processors, middleware, email, and anything else not covered in this list.
  13. Failure What happens when any of the above fail? See
  14. """
  15. import mock
  16. import base64
  17. from apps.web.dealer.utils import get_devices_by_dealer
  18. from apilib.utils_json import JsonResponse
  19. from common import url_fn
  20. def gen_auth_header(username, password, sign=None, test=False):
  21. value = 'Basic ' + base64.b64encode('{username}:{password}'.format(username=username, password=password))
  22. if not test:
  23. headers = {'Authorization': value}
  24. if sign is not None:
  25. headers['Sign'] = sign
  26. else:
  27. headers = {'HTTP_AUTHORIZATION': value}
  28. if sign is not None:
  29. headers['HTTP_SIGN'] = sign
  30. return headers
  31. def gen_auth_test_header(username, password, sign=None):
  32. return gen_auth_header(username=username, password=password, sign=sign, test=True)
  33. def test_apiStartDevice(client, device, dealer, agent, mocker):
  34. from apps.web.api.v1.views import apiStartDevice
  35. from apps.web.core.services import MessageSender
  36. url = url_fn(apiStartDevice)
  37. headers = gen_auth_test_header(dealer.username, dealer._raw_password)
  38. payload = {
  39. 'sign': agent.agentSign,
  40. 'channel': '',
  41. 'coins': 1,
  42. 'deviceCode': device.logicalCode
  43. }
  44. mocker.patch.object(MessageSender, 'net_pay', return_value={'rst': 1})
  45. response = client.post_json(url, payload, **headers)
  46. assert response.json()['payload']['status'] == 1
  47. mocker.patch.object(MessageSender, 'net_pay', return_value={'rst': 0})
  48. response = client.post_json(url, payload, **headers)
  49. assert response.json()['payload']['status'] == 0
  50. def test_sijiang_api_start_device(client, device, dealer, agent, mocker):
  51. from apps.web.api.v1.views import apiStartDevice
  52. from apps.web.core.helpers import ActionDeviceBuilder
  53. from apps.web.core.adapter.sijiang import ChargingSiJiangBox
  54. from apps.web.core.networking import MessageSender
  55. url = url_fn(apiStartDevice)
  56. mocker.patch.object(ActionDeviceBuilder, 'create_action_device', return_value=ChargingSiJiangBox(device))
  57. headers = gen_auth_test_header(dealer.username, dealer._raw_password)
  58. payload = {
  59. 'sign': agent.agentSign,
  60. 'channel': '',
  61. 'package': '1',
  62. 'deviceCode': device.logicalCode,
  63. 'attachParas': {'chargeIndex': '1'}
  64. }
  65. mocker.patch.object(MessageSender, 'send', return_value={'rst': -1})
  66. response = client.post_json(url, payload, **headers)
  67. assert response.json()['payload']['status'] == -2
  68. devInfo = {'rst': 0, 'data': 'EE060501005702570257'}
  69. mocker.patch.object(MessageSender, 'send', return_value=devInfo)
  70. response = client.post_json(url, payload, **headers)
  71. assert response.json()['payload']['status'] == 0
  72. def test_deviceOnlineStatus(client, device, dealer, agent, mocker):
  73. from apps.web.api.v1.views import deviceOnlineStatus
  74. from apps.web.core.services import MessageSender
  75. url = url_fn(deviceOnlineStatus)
  76. headers = gen_auth_test_header(dealer.username, dealer._raw_password)
  77. payload = {
  78. 'sign': agent.agentSign,
  79. 'channel': '',
  80. 'deviceCode': device.logicalCode
  81. }
  82. mocker.patch.object(MessageSender, 'send', return_value={'rst': 0, 'signal': 20})
  83. response = client.post_json(url, payload, **headers)
  84. assert response.json()['result'] == 1
  85. assert response.json()['payload']['online'] == 1
  86. def test_apiGetDevicePackageList(client, device, dealer, agent):
  87. from apps.web.api.v1.views import apiGetDevicePackageList
  88. url = url_fn(apiGetDevicePackageList)
  89. headers = gen_auth_test_header(dealer.username, dealer._raw_password, sign=agent.agentSign)
  90. payload = {'deviceCode': device.logicalCode}
  91. assert client.post_json(url, payload, **headers).json()['payload'] == device.washConfig
  92. def patched_get_devices_by_dealer(dealer):
  93. response = get_devices_by_dealer(dealer)
  94. return JsonResponse({})
  95. def test_getDeviceListByDealer(client, dealer, agent, mocker):
  96. from apps.web.api.v1.views import getDeviceListByDealer
  97. from apps.web.dealer.utils import get_devices_by_dealer
  98. url = url_fn(getDeviceListByDealer)
  99. headers = gen_auth_test_header(dealer.username, dealer._raw_password, sign=agent.agentSign)
  100. response = client.get(url, **headers)
  101. devices = response.json()['payload']['devices']
  102. assert isinstance(devices, list)
  103. response = client.get(url, **headers)
  104. assert 1
  105. def test_apiGetDevStatus(client, device, dealer, agent):
  106. from apps.web.api.v1.views import apiGetDevStatus
  107. url = url_fn(apiGetDevStatus)
  108. headers = gen_auth_test_header(dealer.username, dealer._raw_password, sign=agent.agentSign)
  109. payload = {'deviceCode': device['logicalCode']}
  110. response = client.post_json(url, payload, **headers)
  111. assert response.json()['result'] == 0
  112. def test_sijiang_apiGetDevStatus(client, device, dealer, agent, mocker):
  113. from apps.web.api.v1.views import apiGetDevStatus
  114. url = url_fn(apiGetDevStatus)
  115. headers = gen_auth_test_header(dealer.username, dealer._raw_password, sign=agent.agentSign)
  116. payload = {'deviceCode': device['logicalCode']}
  117. from apps.web.constant import Const
  118. from apps.web.core.helpers import ActionDeviceBuilder
  119. from apps.web.core.adapter.sijiang import ChargingSiJiangBox
  120. portStatus = {'0': {'status': Const.DEV_WORK_STATUS_IDLE}}
  121. mocker.patch.object(ActionDeviceBuilder, 'create_action_device', return_value=ChargingSiJiangBox(device))
  122. mocker.patch.object(ChargingSiJiangBox, 'get_port_status_from_dev', return_value=portStatus)
  123. response = client.post_json(url, payload, **headers)
  124. assert response.json()['result'] == 1
  125. assert response.json()['payload']['portStatus'] == portStatus