Started adding more camera tests, reorganized package
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Init file for tests directory."""
|
||||
+83
-62
@@ -1,18 +1,26 @@
|
||||
import constants as const
|
||||
"""
|
||||
Mock responses that mimic actual responses from Blink servers.
|
||||
|
||||
This file should be updated any time the Blink server responses
|
||||
change so we can make sure blinkpy can still communicate.
|
||||
"""
|
||||
|
||||
import helpers.constants as const
|
||||
|
||||
"""Fake device attributes."""
|
||||
NETWORKS_RESPONSE = {}
|
||||
NETWORKS_RESPONSE['summary'] = {'onboarded': True, 'name': 'Nilfgaard'}
|
||||
NETWORKS_RESPONSE['networks'] = [{'network_key': None,
|
||||
'name': NETWORKS_RESPONSE['summary']['name'],
|
||||
'account_id': 1989,
|
||||
'id': 9898,
|
||||
'encryption_key': None,
|
||||
'armed': True,
|
||||
'ping_interval': 60,
|
||||
'video_destination': 'server',
|
||||
'arm_string': 'Armed',
|
||||
'feature_plan_id': None}]
|
||||
NETWORKS_RESPONSE['networks'] = [{
|
||||
'network_key': None,
|
||||
'name': NETWORKS_RESPONSE['summary']['name'],
|
||||
'account_id': 1989,
|
||||
'id': 9898,
|
||||
'encryption_key': None,
|
||||
'armed': True,
|
||||
'ping_interval': 60,
|
||||
'video_destination': 'server',
|
||||
'arm_string': 'Armed',
|
||||
'feature_plan_id': None
|
||||
}]
|
||||
|
||||
FIRST_CAMERA = {'device_type': 'camera',
|
||||
'notifications': 1,
|
||||
@@ -50,50 +58,51 @@ SYNC_MODULE = {'updated_at': '1970-01-01T01:00:00+00:00',
|
||||
'device_id': 7990,
|
||||
'status': 'online'}
|
||||
|
||||
FIRST_EVENT = {'camera_name': FIRST_CAMERA['name'],
|
||||
'updated_at': '2017-10-10T03:37:37+00:00',
|
||||
'sync_module_id': None,
|
||||
'camera': FIRST_CAMERA['device_id'],
|
||||
'type': 'motion',
|
||||
'duration': None,
|
||||
'status': None,
|
||||
'created_at': '2017-01-28T19:51:52+00:00',
|
||||
'camera_id': FIRST_CAMERA['device_id'],
|
||||
'id': 666777888,
|
||||
'siren_id': None,
|
||||
'account_id': NETWORKS_RESPONSE['networks'][0]['account_id'],
|
||||
'notified': True,
|
||||
'siren': None,
|
||||
'syncmodule': None,
|
||||
'video_url': FIRST_CAMERA['thumbnail'] + '.mp4',
|
||||
'command_id': None,
|
||||
'network_id': None,
|
||||
'account': NETWORKS_RESPONSE['networks'][0]['account_id'],
|
||||
FIRST_EVENT = {'camera_name': FIRST_CAMERA['name'],
|
||||
'updated_at': '2017-10-10T03:37:37+00:00',
|
||||
'sync_module_id': None,
|
||||
'camera': FIRST_CAMERA['device_id'],
|
||||
'type': 'motion',
|
||||
'duration': None,
|
||||
'status': None,
|
||||
'created_at': '2017-01-28T19:51:52+00:00',
|
||||
'camera_id': FIRST_CAMERA['device_id'],
|
||||
'id': 666777888,
|
||||
'siren_id': None,
|
||||
'account_id': NETWORKS_RESPONSE['networks'][0]['account_id'],
|
||||
'notified': True,
|
||||
'siren': None,
|
||||
'syncmodule': None,
|
||||
'video_url': FIRST_CAMERA['thumbnail'] + '.mp4',
|
||||
'command_id': None,
|
||||
'network_id': None,
|
||||
'account': NETWORKS_RESPONSE['networks'][0]['account_id'],
|
||||
'video_id': 123000321}
|
||||
|
||||
SECOND_EVENT = {'updated_at': '2017-01-28T19:51:29+00:00',
|
||||
'sync_module_id': SYNC_MODULE['device_id'],
|
||||
'camera': None,
|
||||
'type': 'armed',
|
||||
'duration': None,
|
||||
'status': None,
|
||||
'created_at': '2017-01-28T19:51:29+00:00',
|
||||
'camera_id': None,
|
||||
'id': 333777555,
|
||||
'siren_id': None,
|
||||
'account_id': NETWORKS_RESPONSE['networks'][0]['account_id'],
|
||||
'notified': False,
|
||||
'siren': None,
|
||||
'syncmodule': SYNC_MODULE['device_id'],
|
||||
'command_id': None,
|
||||
'network_id': None,
|
||||
'account': NETWORKS_RESPONSE['networks'][0]['account_id']}
|
||||
|
||||
SECOND_EVENT = {'updated_at': '2017-01-28T19:51:29+00:00',
|
||||
'sync_module_id': SYNC_MODULE['device_id'],
|
||||
'camera': None,
|
||||
'type': 'armed',
|
||||
'duration': None,
|
||||
'status': None,
|
||||
'created_at': '2017-01-28T19:51:29+00:00',
|
||||
'camera_id': None,
|
||||
'id': 333777555,
|
||||
'siren_id': None,
|
||||
'account_id': NETWORKS_RESPONSE['networks'][0]['account_id'],
|
||||
'notified': False,
|
||||
'siren': None,
|
||||
'syncmodule': SYNC_MODULE['device_id'],
|
||||
'command_id': None,
|
||||
'network_id': None,
|
||||
'account': NETWORKS_RESPONSE['networks'][0]['account_id']}
|
||||
|
||||
"""Fake response content."""
|
||||
LOGIN_RESPONSE = {}
|
||||
LOGIN_RESPONSE['region'] = {'ciri': 'Cintra'}
|
||||
LOGIN_RESPONSE['networks'] = {NETWORKS_RESPONSE['networks'][0]['id'] : NETWORKS_RESPONSE['summary']}
|
||||
LOGIN_RESPONSE['networks'] = {
|
||||
NETWORKS_RESPONSE['networks'][0]['id']: NETWORKS_RESPONSE['summary']
|
||||
}
|
||||
LOGIN_RESPONSE['authtoken'] = {'authtoken': 'foobar7117', 'message': 'auth'}
|
||||
|
||||
RESPONSE = {}
|
||||
@@ -111,42 +120,54 @@ RESPONSE['syncmodule'] = {'name': 'Vengerberg', 'status': 'online'}
|
||||
def mocked_requests_post(*args, **kwargs):
|
||||
"""Mock post request."""
|
||||
class MockPostResponse:
|
||||
def __init__(self,json_data,status_code):
|
||||
"""Class for mock post response."""
|
||||
|
||||
def __init__(self, json_data, status_code):
|
||||
"""Initialze mock post response."""
|
||||
self.json_data = json_data
|
||||
self.status_code = status_code
|
||||
|
||||
def json(self):
|
||||
"""Return json data from post request."""
|
||||
return self.json_data
|
||||
|
||||
|
||||
url_tail = args[0].split("/")[-1]
|
||||
if args[0] == const.LOGIN_URL:
|
||||
return MockPostResponse(LOGIN_RESPONSE, 200)
|
||||
elif url_tail == 'arm' or url_tail == 'disarm':
|
||||
# pylint: disable=global-variable-not-assigned
|
||||
global NETWORKS_RESPONSE
|
||||
# pylint: disable=global-variable-not-assigned
|
||||
global RESPONSE
|
||||
NETWORKS_RESPONSE['networks'][0]['armed'] = url_tail == 'arm'
|
||||
RESPONSE['network']['armed'] = NETWORKS_RESPONSE['networks'][0]['armed']
|
||||
RESPONSE['network']['armed'] = url_tail == 'arm'
|
||||
return MockPostResponse({}, 200)
|
||||
|
||||
return MockPostResponse({'message':'ERROR','code':404}, 404)
|
||||
|
||||
return MockPostResponse({'message': 'ERROR', 'code': 404}, 404)
|
||||
|
||||
|
||||
def mocked_requests_get(*args, **kwargs):
|
||||
"""Mock get request."""
|
||||
class MockGetResponse:
|
||||
def __init__(self,json_data,status_code):
|
||||
"""Class for mock get response."""
|
||||
|
||||
def __init__(self, json_data, status_code):
|
||||
"""Initialze mock get response."""
|
||||
self.json_data = json_data
|
||||
self.status_code = status_code
|
||||
def json(self):
|
||||
return self.json_data
|
||||
|
||||
def json(self):
|
||||
"""Return json data from post request."""
|
||||
return self.json_data
|
||||
# pylint: disable=unused-variable
|
||||
(region_id, region), = LOGIN_RESPONSE['region'].items()
|
||||
set_region_id = args[0].split('/')[2].split('.')[0]
|
||||
NETURL = 'https://' + set_region_id + '.' + const.BLINK_URL + '/networks'
|
||||
if args[0] == NETURL:
|
||||
neturl = 'https://' + set_region_id + '.' + const.BLINK_URL + '/networks'
|
||||
if args[0] == neturl:
|
||||
return MockGetResponse(NETWORKS_RESPONSE, 200)
|
||||
elif set_region_id != region_id:
|
||||
raise ConnectionError('Received url ' + args[0])
|
||||
else:
|
||||
return MockGetResponse(RESPONSE, 200)
|
||||
|
||||
return MockGetResponse({'message':'ERROR','code':404}, 404)
|
||||
|
||||
return MockGetResponse({'message': 'ERROR', 'code': 404}, 404)
|
||||
|
||||
@@ -1 +1,80 @@
|
||||
# TODO
|
||||
"""
|
||||
Tests the camera initialization and attributes of
|
||||
individual BlinkCamera instantiations.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import blinkpy
|
||||
import tests.mock_responses as mresp
|
||||
|
||||
USERNAME = 'foobar'
|
||||
PASSWORD = 'deadbeef'
|
||||
|
||||
|
||||
class TestBlinkCameraSetup(unittest.TestCase):
|
||||
"""Test the Blink class in blinkpy."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up Blink module."""
|
||||
self.blink = blinkpy.Blink(username=USERNAME,
|
||||
password=PASSWORD)
|
||||
|
||||
def tearDown(self):
|
||||
"""Clean up after test."""
|
||||
self.blink = None
|
||||
|
||||
@mock.patch('blinkpy.blinkpy.requests.post',
|
||||
side_effect=mresp.mocked_requests_post)
|
||||
@mock.patch('blinkpy.blinkpy.requests.get',
|
||||
side_effect=mresp.mocked_requests_get)
|
||||
def test_camera_values_from_setup(self, mock_get, mock_post):
|
||||
"""Tests all property values after camera setup."""
|
||||
self.blink.setup_system()
|
||||
|
||||
# Get expected test values
|
||||
test_network_id = str(mresp.NETWORKS_RESPONSE['networks'][0]['id'])
|
||||
# pylint: disable=unused-variable
|
||||
(region_id, region), = mresp.LOGIN_RESPONSE['region'].items()
|
||||
# pylint: disable=protected-access
|
||||
expected_header = self.blink._auth_header
|
||||
test_urls = blinkpy.BlinkURLHandler(region_id)
|
||||
|
||||
test_cameras = dict()
|
||||
for element in mresp.RESPONSE['devices']:
|
||||
if ('device_type' in element and
|
||||
element['device_type'] == 'camera'):
|
||||
test_cameras[element['name']] = {
|
||||
'device_id': str(element['device_id']),
|
||||
'armed': element['armed'],
|
||||
'thumbnail': (test_urls.base_url +
|
||||
element['thumbnail'] + '.jpg'),
|
||||
'temperature': element['temp'],
|
||||
'battery': element['battery'],
|
||||
'notifications': element['notifications']
|
||||
}
|
||||
test_net_id_url = test_urls.network_url + test_network_id
|
||||
for name in self.blink.cameras:
|
||||
camera = self.blink.cameras[name]
|
||||
self.assertEqual(name, camera.name)
|
||||
if name in test_cameras:
|
||||
self.assertEqual(camera.id,
|
||||
test_cameras[name]['device_id'])
|
||||
self.assertEqual(camera.armed,
|
||||
test_cameras[name]['armed'])
|
||||
self.assertEqual(camera.thumbnail,
|
||||
test_cameras[name]['thumbnail'])
|
||||
self.assertEqual(camera.temperature,
|
||||
test_cameras[name]['temperature'])
|
||||
self.assertEqual(camera.battery,
|
||||
test_cameras[name]['battery'])
|
||||
self.assertEqual(camera.notifications,
|
||||
test_cameras[name]['notifications'])
|
||||
else:
|
||||
self.fail("Camera wasn't initialized: " + name)
|
||||
|
||||
expected_arm_link = test_net_id_url + '/camera/' + camera.id + '/'
|
||||
expected_image_link = expected_arm_link + 'thumbnail'
|
||||
self.assertEqual(camera.image_link, expected_image_link)
|
||||
self.assertEqual(camera.arm_link, expected_arm_link)
|
||||
self.assertEqual(camera.header, expected_header)
|
||||
|
||||
+45
-24
@@ -1,15 +1,22 @@
|
||||
import requests
|
||||
"""
|
||||
Tests the system initialization and attributes of
|
||||
the main Blink system. Tests if we properly catch
|
||||
any communication related errors at startup.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import blinkpy
|
||||
import tests.mock_responses as mresp
|
||||
import constants as const
|
||||
import helpers.constants as const
|
||||
|
||||
USERNAME = 'foobar'
|
||||
PASSWORD = 'deadbeef'
|
||||
|
||||
|
||||
class TestBlinkSetup(unittest.TestCase):
|
||||
"""Test the Blink class in blinkpy."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up Blink module."""
|
||||
self.blink_no_cred = blinkpy.Blink()
|
||||
@@ -23,7 +30,9 @@ class TestBlinkSetup(unittest.TestCase):
|
||||
|
||||
def test_initialization(self):
|
||||
"""Verify we can initialize blink."""
|
||||
# pylint: disable=protected-access
|
||||
self.assertEqual(self.blink._username, USERNAME)
|
||||
# pylint: disable=protected-access
|
||||
self.assertEqual(self.blink._password, PASSWORD)
|
||||
|
||||
def test_no_credentials(self):
|
||||
@@ -35,62 +44,72 @@ class TestBlinkSetup(unittest.TestCase):
|
||||
|
||||
def test_no_auth_header(self):
|
||||
"""Check that we throw an excpetion when no auth header given."""
|
||||
# pylint: disable=unused-variable
|
||||
(region_id, region), = mresp.LOGIN_RESPONSE['region'].items()
|
||||
self.blink.urls = blinkpy.BlinkURLHandler(region_id)
|
||||
with self.assertRaises(blinkpy.BlinkException):
|
||||
self.blink.get_ids()
|
||||
|
||||
@mock.patch('blinkpy.getpass.getpass')
|
||||
|
||||
@mock.patch('blinkpy.blinkpy.getpass.getpass')
|
||||
def test_manual_login(self, getpwd):
|
||||
"""Check that we can manually use the login() function."""
|
||||
getpwd.return_value = PASSWORD
|
||||
with mock.patch('builtins.input', return_value=USERNAME):
|
||||
self.blink_no_cred.login()
|
||||
# pylint: disable=protected-access
|
||||
self.assertEqual(self.blink_no_cred._username, USERNAME)
|
||||
# pylint: disable=protected-access
|
||||
self.assertEqual(self.blink_no_cred._password, PASSWORD)
|
||||
|
||||
@mock.patch('blinkpy.requests.post', side_effect=mresp.mocked_requests_post)
|
||||
@mock.patch('blinkpy.requests.get', side_effect=mresp.mocked_requests_get)
|
||||
@mock.patch('blinkpy.blinkpy.requests.post',
|
||||
side_effect=mresp.mocked_requests_post)
|
||||
@mock.patch('blinkpy.blinkpy.requests.get',
|
||||
side_effect=mresp.mocked_requests_get)
|
||||
def test_full_setup(self, mock_get, mock_post):
|
||||
"""Check that we can set Blink up properly."""
|
||||
self.blink.setup_system()
|
||||
|
||||
|
||||
# Get all test values
|
||||
authtoken = mresp.LOGIN_RESPONSE['authtoken']['authtoken']
|
||||
(region_id, region), = mresp.LOGIN_RESPONSE['region'].items()
|
||||
host = region_id + '.' + const.BLINK_URL
|
||||
network_id = mresp.NETWORKS_RESPONSE['networks'][0]['id']
|
||||
account_id = mresp.NETWORKS_RESPONSE['networks'][0]['account_id']
|
||||
TestURLs = blinkpy.BlinkURLHandler(region_id)
|
||||
TestCameras = list()
|
||||
TestCameraID = dict()
|
||||
test_urls = blinkpy.BlinkURLHandler(region_id)
|
||||
test_cameras = list()
|
||||
test_camera_id = dict()
|
||||
for element in mresp.RESPONSE['devices']:
|
||||
if 'device_type' in element and element['device_type'] == 'camera':
|
||||
TestCameras.append(element['name'])
|
||||
TestCameraID[str(element['device_id'])] = element['name']
|
||||
if ('device_type' in element and
|
||||
element['device_type'] == 'camera'):
|
||||
test_cameras.append(element['name'])
|
||||
test_camera_id[str(element['device_id'])] = element['name']
|
||||
|
||||
# Check that all links have been set properly
|
||||
self.assertEqual(self.blink.region_id, region_id)
|
||||
self.assertEqual(self.blink.urls.base_url, TestURLs.base_url)
|
||||
self.assertEqual(self.blink.urls.home_url, TestURLs.home_url)
|
||||
self.assertEqual(self.blink.urls.event_url, TestURLs.event_url)
|
||||
self.assertEqual(self.blink.urls.network_url, TestURLs.network_url)
|
||||
self.assertEqual(self.blink.urls.networks_url, TestURLs.networks_url)
|
||||
self.assertEqual(self.blink.urls.base_url, test_urls.base_url)
|
||||
self.assertEqual(self.blink.urls.home_url, test_urls.home_url)
|
||||
self.assertEqual(self.blink.urls.event_url, test_urls.event_url)
|
||||
self.assertEqual(self.blink.urls.network_url, test_urls.network_url)
|
||||
self.assertEqual(self.blink.urls.networks_url, test_urls.networks_url)
|
||||
|
||||
# Check that all properties have been set after startup
|
||||
# pylint: disable=protected-access
|
||||
self.assertEqual(self.blink._token, authtoken)
|
||||
# pylint: disable=protected-access
|
||||
self.assertEqual(self.blink._host, host)
|
||||
self.assertEqual(self.blink.network_id, str(network_id))
|
||||
self.assertEqual(self.blink.account_id, str(account_id))
|
||||
self.assertEqual(self.blink.region, region)
|
||||
|
||||
# Verify we have initialized all the cameras
|
||||
self.assertEqual(self.blink.id_table, TestCameraID)
|
||||
for camera in TestCameras:
|
||||
self.assertEqual(self.blink.id_table, test_camera_id)
|
||||
for camera in test_cameras:
|
||||
self.assertTrue(camera in self.blink.cameras)
|
||||
|
||||
@mock.patch('blinkpy.requests.post', side_effect=mresp.mocked_requests_post)
|
||||
@mock.patch('blinkpy.requests.get', side_effect=mresp.mocked_requests_get)
|
||||
@mock.patch('blinkpy.blinkpy.requests.post',
|
||||
side_effect=mresp.mocked_requests_post)
|
||||
@mock.patch('blinkpy.blinkpy.requests.get',
|
||||
side_effect=mresp.mocked_requests_get)
|
||||
def test_arm_disarm_system(self, mock_get, mock_post):
|
||||
"""Check that we can arm/disarm the system"""
|
||||
self.blink.setup_system()
|
||||
@@ -99,8 +118,10 @@ class TestBlinkSetup(unittest.TestCase):
|
||||
self.blink.arm = True
|
||||
self.assertIs(self.blink.arm, True)
|
||||
|
||||
@mock.patch('blinkpy.requests.post', side_effect=mresp.mocked_requests_post)
|
||||
@mock.patch('blinkpy.requests.get', side_effect=mresp.mocked_requests_get)
|
||||
@mock.patch('blinkpy.blinkpy.requests.post',
|
||||
side_effect=mresp.mocked_requests_post)
|
||||
@mock.patch('blinkpy.blinkpy.requests.get',
|
||||
side_effect=mresp.mocked_requests_get)
|
||||
def test_check_online_status(self, mock_get, mock_post):
|
||||
"""Check that we can get our online status."""
|
||||
self.blink.setup_system()
|
||||
|
||||
Reference in New Issue
Block a user