1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
"""Tests camera and system functions."""
from unittest import mock, IsolatedAsyncioTestCase
import time
import random
from io import BufferedIOBase
import aiofiles
from blinkpy import blinkpy
from blinkpy.sync_module import BlinkSyncModule
from blinkpy.camera import BlinkCamera
from blinkpy.helpers.util import get_time, BlinkURLHandler
class MockSyncModule(BlinkSyncModule):
"""Mock blink sync module object."""
async def get_network_info(self):
"""Mock network info method."""
return True
class MockCamera(BlinkCamera):
"""Mock blink camera object."""
def __init__(self, sync):
"""Initialize mock camera."""
super().__init__(sync)
self.camera_id = random.randint(1, 100000)
async def update(self, config, force_cache=False, **kwargs):
"""Mock camera update method."""
class TestBlinkFunctions(IsolatedAsyncioTestCase):
"""Test Blink and BlinkCamera functions in blinkpy."""
def setUp(self):
"""Set up Blink module."""
self.blink = blinkpy.Blink(session=mock.AsyncMock())
self.blink.urls = BlinkURLHandler("test")
def tearDown(self):
"""Clean up after test."""
self.blink = None
def test_merge_cameras(self):
"""Test merge camera functionality."""
first_dict = {"foo": "bar", "test": 123}
next_dict = {"foobar": 456, "bar": "foo"}
self.blink.sync["foo"] = BlinkSyncModule(self.blink, "foo", 1, [])
self.blink.sync["bar"] = BlinkSyncModule(self.blink, "bar", 2, [])
self.blink.sync["foo"].cameras = first_dict
self.blink.sync["bar"].cameras = next_dict
result = self.blink.merge_cameras()
expected = {"foo": "bar", "test": 123, "foobar": 456, "bar": "foo"}
self.assertEqual(expected, result)
@mock.patch("blinkpy.blinkpy.api.request_videos")
async def test_download_video_exit(self, mock_req):
"""Test we exit method when provided bad response."""
blink = blinkpy.Blink(session=mock.AsyncMock())
blink.last_refresh = 0
mock_req.return_value = {}
formatted_date = get_time(blink.last_refresh)
expected_log = [
f"INFO:blinkpy.blinkpy:Retrieving videos since {formatted_date}",
"DEBUG:blinkpy.blinkpy:Processing page 1",
"INFO:blinkpy.blinkpy:No videos found on page 1. Exiting.",
]
with self.assertLogs(level="DEBUG") as dl_log:
await blink.download_videos("/tmp")
self.assertListEqual(dl_log.output, expected_log)
@mock.patch("blinkpy.blinkpy.api.request_videos")
async def test_parse_downloaded_items(self, mock_req):
"""Test ability to parse downloaded items list."""
blink = blinkpy.Blink(session=mock.AsyncMock())
generic_entry = {
"created_at": "1970",
"device_name": "foo",
"deleted": True,
"media": "/bar.mp4",
}
result = [generic_entry]
mock_req.return_value = {"media": result}
blink.last_refresh = 0
formatted_date = get_time(blink.last_refresh)
expected_log = [
f"INFO:blinkpy.blinkpy:Retrieving videos since {formatted_date}",
"DEBUG:blinkpy.blinkpy:Processing page 1",
"DEBUG:blinkpy.blinkpy:foo: /bar.mp4 is marked as deleted.",
]
with self.assertLogs(level="DEBUG") as dl_log:
await blink.download_videos("/tmp", stop=2, delay=0)
self.assertListEqual(dl_log.output, expected_log)
@mock.patch("blinkpy.blinkpy.api.request_videos")
async def test_parse_downloaded_throttle(self, mock_req):
"""Test ability to parse downloaded items list."""
generic_entry = {
"created_at": "1970",
"device_name": "foo",
"deleted": False,
"media": "/bar.mp4",
}
result = [generic_entry]
mock_req.return_value = {"media": result}
self.blink.last_refresh = 0
start = time.time()
await self.blink.download_videos("/tmp", stop=2, delay=0, debug=True)
now = time.time()
delta = now - start
self.assertTrue(delta < 0.1)
start = time.time()
await self.blink.download_videos("/tmp", stop=2, delay=0.1, debug=True)
now = time.time()
delta = now - start
self.assertTrue(delta >= 0.1)
@mock.patch("blinkpy.blinkpy.api.request_videos")
async def test_get_videos_metadata(self, mock_req):
"""Test ability to fetch videos metadata."""
generic_entry = {
"created_at": "1970",
"device_name": "foo",
"deleted": True,
"media": "/bar.mp4",
}
result = [generic_entry]
mock_req.return_value = {"media": result}
self.blink.last_refresh = 0
results = await self.blink.get_videos_metadata(stop=2)
self.assertListEqual(results, result)
results = await self.blink.get_videos_metadata(
since="2018/07/28 12:33:00", stop=2
)
self.assertListEqual(results, result)
mock_req.return_value = {"media": None}
results = await self.blink.get_videos_metadata(stop=2)
self.assertListEqual(results, [])
@mock.patch("blinkpy.blinkpy.api.http_get")
async def test_do_http_get(self, mock_req):
"""Test ability to do_http_get."""
blink = blinkpy.Blink(session=mock.AsyncMock())
blink.urls = BlinkURLHandler("test")
response = await blink.do_http_get("/path/to/request")
self.assertTrue(response is not None)
@mock.patch("blinkpy.blinkpy.api.request_videos")
async def test_download_videos_deleted(self, mock_req):
"""Test ability to download videos."""
generic_entry = {
"created_at": "1970",
"device_name": "foo",
"deleted": True,
"media": "/bar.mp4",
}
result = [generic_entry]
mock_req.return_value = {"media": result}
self.blink.last_refresh = 0
formatted_date = get_time(self.blink.last_refresh)
expected_log = [
f"INFO:blinkpy.blinkpy:Retrieving videos since {formatted_date}",
"DEBUG:blinkpy.blinkpy:Processing page 1",
"DEBUG:blinkpy.blinkpy:foo: /bar.mp4 is marked as deleted.",
]
with self.assertLogs(level="DEBUG") as dl_log:
await self.blink.download_videos("/tmp", camera="foo", stop=2, delay=0)
self.assertListEqual(dl_log.output, expected_log)
@mock.patch("blinkpy.blinkpy.api.request_videos")
@mock.patch("aiofiles.ospath.isfile")
async def test_download_videos_file(self, mock_isfile, mock_req):
"""Test ability to download videos to a file."""
generic_entry = {
"created_at": "1970",
"device_name": "foo",
"deleted": False,
"media": "/bar.mp4",
}
result = [generic_entry]
mock_req.return_value = {"media": result}
mock_isfile.return_value = False
self.blink.last_refresh = 0
aiofiles.threadpool.wrap.register(mock.MagicMock)(
lambda *args, **kwargs: aiofiles.threadpool.AsyncBufferedIOBase(
*args, **kwargs
)
)
mock_file = mock.MagicMock(spec=BufferedIOBase)
with mock.patch("aiofiles.threadpool.sync_open", return_value=mock_file):
await self.blink.download_videos("/tmp", camera="foo", stop=2, delay=0)
assert mock_file.write.call_count == 1
@mock.patch("blinkpy.blinkpy.api.request_videos")
@mock.patch("aiofiles.ospath.isfile")
async def test_download_videos_file_exists(self, mock_isfile, mock_req):
"""Test ability to download videos with file exists."""
generic_entry = {
"created_at": "1970",
"device_name": "foo",
"deleted": False,
"media": "/bar.mp4",
}
result = [generic_entry]
mock_req.return_value = {"media": result}
mock_isfile.return_value = True
self.blink.last_refresh = 0
formatted_date = get_time(self.blink.last_refresh)
expected_log = [
f"INFO:blinkpy.blinkpy:Retrieving videos since {formatted_date}",
"DEBUG:blinkpy.blinkpy:Processing page 1",
"INFO:blinkpy.blinkpy:/tmp/foo-1970.mp4 already exists, skipping...",
]
with self.assertLogs(level="DEBUG") as dl_log:
await self.blink.download_videos("/tmp", camera="foo", stop=2, delay=0)
assert expected_log[0] in dl_log.output
assert expected_log[1] in dl_log.output
assert expected_log[2] in dl_log.output
@mock.patch("blinkpy.blinkpy.api.request_videos")
async def test_parse_camera_not_in_list(self, mock_req):
"""Test ability to parse downloaded items list."""
generic_entry = {
"created_at": "1970",
"device_name": "foo",
"deleted": True,
"media": "/bar.mp4",
}
result = [generic_entry]
mock_req.return_value = {"media": result}
self.blink.last_refresh = 0
formatted_date = get_time(self.blink.last_refresh)
expected_log = [
f"INFO:blinkpy.blinkpy:Retrieving videos since {formatted_date}",
"DEBUG:blinkpy.blinkpy:Processing page 1",
"DEBUG:blinkpy.blinkpy:Skipping videos for foo.",
]
with self.assertLogs(level="DEBUG") as dl_log:
await self.blink.download_videos("/tmp", camera="bar", stop=2, delay=0)
self.assertListEqual(dl_log.output, expected_log)
@mock.patch("blinkpy.blinkpy.api.request_videos")
async def test_parse_malformed_entry(self, mock_req):
"""Test ability to parse downloaded items in malformed list."""
self.blink.last_refresh = 0
formatted_date = get_time(self.blink.last_refresh)
generic_entry = {
"created_at": "1970",
}
result = [generic_entry]
mock_req.return_value = {"media": result}
expected_log = [
f"INFO:blinkpy.blinkpy:Retrieving videos since {formatted_date}",
"DEBUG:blinkpy.blinkpy:Processing page 1",
"INFO:blinkpy.blinkpy:Missing clip information, skipping...",
]
with self.assertLogs(level="DEBUG") as dl_log:
await self.blink.download_videos("/tmp", camera="bar", stop=2, delay=0)
self.assertListEqual(dl_log.output, expected_log)
@mock.patch("blinkpy.blinkpy.api.request_network_update")
@mock.patch("blinkpy.auth.Auth.query")
async def test_refresh(self, mock_req, mock_update):
"""Test ability to refresh system."""
mock_update.return_value = {"network": {"sync_module_error": False}}
mock_req.return_value.json = mock.AsyncMock(return_value={})
self.blink.last_refresh = 0
self.blink.available = True
self.blink.auth.account_id = 1234
self.blink.sync["foo"] = MockSyncModule(self.blink, "foo", 1, [])
self.blink.cameras = {"bar": MockCamera(self.blink.sync)}
self.blink.sync["foo"].cameras = self.blink.cameras
self.assertTrue(await self.blink.refresh())
@mock.patch("blinkpy.blinkpy.api.request_notification_flags")
async def test_get_status(self, mock_req):
"""Test get of notification flags."""
mock_req.return_value = {"notifications": {"foo": True}}
self.assertDictEqual(await self.blink.get_status(), {"foo": True})
@mock.patch("blinkpy.blinkpy.api.request_notification_flags")
async def test_get_status_malformed(self, mock_req):
"""Test get of notification flags with malformed response."""
mock_req.return_value = {"nobueno": {"foo": False}}
self.assertDictEqual(await self.blink.get_status(), {"nobueno": {"foo": False}})
@mock.patch("blinkpy.blinkpy.api.request_set_notification_flag")
async def test_set_status(self, mock_req):
"""Test set of notification flags."""
mock_req.return_value = True
self.assertTrue(await self.blink.set_status())
|