# -*- coding: utf-8 -*- #!/usr/bin/env python """ Views: Viewing of data, changing of data, and custom class-based view methods. Models: Creating/updating/deleting of models, model methods, model manager methods. Forms: Form methods, clean() methods, and custom fields Validators: Really dig in and write multiple test methods against each custom validator you write. Pretend you are a malignant intruder attempting to damage the data in the site. Signals: Since they act at a distance, signals can cause grief especially if you lack tests on them. Filters: Since filters are essentially just functions accepting one or two arguments, writing tests for them should be easy. Miscellany: Context processors, middleware, email, and anything else not covered in this list. Failure What happens when any of the above fail? See """ import mock import base64 from apps.web.dealer.utils import get_devices_by_dealer from apilib.utils_json import JsonResponse from common import url_fn def gen_auth_header(username, password, sign=None, test=False): value = 'Basic ' + base64.b64encode('{username}:{password}'.format(username=username, password=password)) if not test: headers = {'Authorization': value} if sign is not None: headers['Sign'] = sign else: headers = {'HTTP_AUTHORIZATION': value} if sign is not None: headers['HTTP_SIGN'] = sign return headers def gen_auth_test_header(username, password, sign=None): return gen_auth_header(username=username, password=password, sign=sign, test=True) def test_apiStartDevice(client, device, dealer, agent, mocker): from apps.web.api.v1.views import apiStartDevice from apps.web.core.services import MessageSender url = url_fn(apiStartDevice) headers = gen_auth_test_header(dealer.username, dealer._raw_password) payload = { 'sign': agent.agentSign, 'channel': '', 'coins': 1, 'deviceCode': device.logicalCode } mocker.patch.object(MessageSender, 'net_pay', return_value={'rst': 1}) response = client.post_json(url, payload, **headers) assert response.json()['payload']['status'] == 1 mocker.patch.object(MessageSender, 'net_pay', return_value={'rst': 0}) response = client.post_json(url, payload, **headers) assert response.json()['payload']['status'] == 0 def test_sijiang_api_start_device(client, device, dealer, agent, mocker): from apps.web.api.v1.views import apiStartDevice from apps.web.core.helpers import ActionDeviceBuilder from apps.web.core.adapter.sijiang import ChargingSiJiangBox from apps.web.core.networking import MessageSender url = url_fn(apiStartDevice) mocker.patch.object(ActionDeviceBuilder, 'create_action_device', return_value=ChargingSiJiangBox(device)) headers = gen_auth_test_header(dealer.username, dealer._raw_password) payload = { 'sign': agent.agentSign, 'channel': '', 'package': '1', 'deviceCode': device.logicalCode, 'attachParas': {'chargeIndex': '1'} } mocker.patch.object(MessageSender, 'send', return_value={'rst': -1}) response = client.post_json(url, payload, **headers) assert response.json()['payload']['status'] == -2 devInfo = {'rst': 0, 'data': 'EE060501005702570257'} mocker.patch.object(MessageSender, 'send', return_value=devInfo) response = client.post_json(url, payload, **headers) assert response.json()['payload']['status'] == 0 def test_deviceOnlineStatus(client, device, dealer, agent, mocker): from apps.web.api.v1.views import deviceOnlineStatus from apps.web.core.services import MessageSender url = url_fn(deviceOnlineStatus) headers = gen_auth_test_header(dealer.username, dealer._raw_password) payload = { 'sign': agent.agentSign, 'channel': '', 'deviceCode': device.logicalCode } mocker.patch.object(MessageSender, 'send', return_value={'rst': 0, 'signal': 20}) response = client.post_json(url, payload, **headers) assert response.json()['result'] == 1 assert response.json()['payload']['online'] == 1 def test_apiGetDevicePackageList(client, device, dealer, agent): from apps.web.api.v1.views import apiGetDevicePackageList url = url_fn(apiGetDevicePackageList) headers = gen_auth_test_header(dealer.username, dealer._raw_password, sign=agent.agentSign) payload = {'deviceCode': device.logicalCode} assert client.post_json(url, payload, **headers).json()['payload'] == device.washConfig def patched_get_devices_by_dealer(dealer): response = get_devices_by_dealer(dealer) return JsonResponse({}) def test_getDeviceListByDealer(client, dealer, agent, mocker): from apps.web.api.v1.views import getDeviceListByDealer from apps.web.dealer.utils import get_devices_by_dealer url = url_fn(getDeviceListByDealer) headers = gen_auth_test_header(dealer.username, dealer._raw_password, sign=agent.agentSign) response = client.get(url, **headers) devices = response.json()['payload']['devices'] assert isinstance(devices, list) response = client.get(url, **headers) assert 1 def test_apiGetDevStatus(client, device, dealer, agent): from apps.web.api.v1.views import apiGetDevStatus url = url_fn(apiGetDevStatus) headers = gen_auth_test_header(dealer.username, dealer._raw_password, sign=agent.agentSign) payload = {'deviceCode': device['logicalCode']} response = client.post_json(url, payload, **headers) assert response.json()['result'] == 0 def test_sijiang_apiGetDevStatus(client, device, dealer, agent, mocker): from apps.web.api.v1.views import apiGetDevStatus url = url_fn(apiGetDevStatus) headers = gen_auth_test_header(dealer.username, dealer._raw_password, sign=agent.agentSign) payload = {'deviceCode': device['logicalCode']} from apps.web.constant import Const from apps.web.core.helpers import ActionDeviceBuilder from apps.web.core.adapter.sijiang import ChargingSiJiangBox portStatus = {'0': {'status': Const.DEV_WORK_STATUS_IDLE}} mocker.patch.object(ActionDeviceBuilder, 'create_action_device', return_value=ChargingSiJiangBox(device)) mocker.patch.object(ChargingSiJiangBox, 'get_port_status_from_dev', return_value=portStatus) response = client.post_json(url, payload, **headers) assert response.json()['result'] == 1 assert response.json()['payload']['portStatus'] == portStatus