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
|
import json
import unittest
import zlib
from http import client as httplib
from unittest import mock
from uvcclient import nvr
class TestClientLowLevel(unittest.TestCase):
def setUp(self):
super().setUp()
self._patches = []
try:
import httplib # noqa: F401
http_mock = mock.patch("httplib.HTTPConnection")
except ImportError:
http_mock = mock.patch("http.client.HTTPConnection")
http_mock.start()
self._patches.append(http_mock)
bootstrap_mock = mock.patch.object(
nvr.UVCRemote, "_get_bootstrap", side_effect=self._bootstrap
)
bootstrap_mock.start()
self._patches.append(bootstrap_mock)
def _bootstrap(self):
return {"systemInfo": {"version": "3.1.3"}}
def cleanUp(self):
for i in self._patches:
i.stop()
def test_uvc_request_get(self):
client = nvr.UVCRemote("foo", 7080, "key")
conn = httplib.HTTPConnection.return_value
resp = conn.getresponse.return_value
resp.status = 200
resp.read.return_value = json.dumps({}).encode()
client._uvc_request("/bar")
headers = {
"Content-Type": "application/json",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate, sdch",
}
conn.request.assert_called_once_with("GET", "/bar?apiKey=key", None, headers)
def test_uvc_request_put(self):
client = nvr.UVCRemote("foo", 7080, "key")
conn = httplib.HTTPConnection.return_value
resp = conn.getresponse.return_value
resp.status = 200
resp.read.return_value = json.dumps({}).encode()
result = client._uvc_request("/bar?foo=bar", method="PUT", data={"foo": "bar"})
self.assertEqual({}, result)
headers = {
"Content-Type": "application/json",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate, sdch",
}
conn.request.assert_called_once_with(
"PUT", "/bar?foo=bar&apiKey=key", '{"foo": "bar"}', headers
)
def test_uvc_request_failed(self):
client = nvr.UVCRemote("foo", 7080, "key")
conn = httplib.HTTPConnection.return_value
resp = conn.getresponse.return_value
resp.status = 404
self.assertRaises(
nvr.NvrError, client._uvc_request, "/bar", method="PUT", data={"foo": "bar"}
)
def test_uvc_request_failed_noauth(self):
client = nvr.UVCRemote("foo", 7080, "key")
conn = httplib.HTTPConnection.return_value
resp = conn.getresponse.return_value
resp.status = 401
self.assertRaises(
nvr.NotAuthorized,
client._uvc_request,
"/bar",
method="PUT",
data={"foo": "bar"},
)
def test_uvc_request_deflated(self):
client = nvr.UVCRemote("foo", 7080, "key")
conn = httplib.HTTPConnection.return_value
resp = conn.getresponse.return_value
resp.status = 200
resp.read.return_value = zlib.compress(json.dumps({}).encode())
resp.getheaders.return_value = [("Content-Encoding", "gzip")]
client._uvc_request("/bar")
headers = {
"Content-Type": "application/json",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate, sdch",
}
conn.request.assert_called_once_with("GET", "/bar?apiKey=key", None, headers)
class TestClient32(unittest.TestCase):
@mock.patch.object(nvr.UVCRemote, "_get_bootstrap")
def test_bootstrap_server_version(self, mock_bootstrap):
mock_bootstrap.return_value = {"systemInfo": {"version": "3.4.5"}}
client = nvr.UVCRemote("foo", 7080, "key")
self.assertEqual((3, 4, 5), client.server_version)
@mock.patch.object(nvr.UVCRemote, "_get_bootstrap")
def test_bootstrap_server_version_beta(self, mock_bootstrap):
mock_bootstrap.return_value = {"systemInfo": {"version": "3.4.beta5"}}
client = nvr.UVCRemote("foo", 7080, "key")
self.assertEqual((3, 4, 0), client.server_version)
@mock.patch.object(nvr.UVCRemote, "_get_bootstrap")
@mock.patch.object(nvr.UVCRemote, "index")
def test_310_returns_uuid(self, mock_index, mock_bootstrap):
mock_index.return_value = [
{
"name": mock.sentinel.name,
"uuid": mock.sentinel.uuid,
"id": mock.sentinel.id,
}
]
mock_bootstrap.return_value = {"systemInfo": {"version": "3.1.0"}}
client = nvr.UVCRemote("foo", 7080, "key")
self.assertEqual(mock.sentinel.uuid, client.name_to_uuid(mock.sentinel.name))
@mock.patch.object(nvr.UVCRemote, "_get_bootstrap")
@mock.patch.object(nvr.UVCRemote, "index")
def test_320_returns_uuid(self, mock_index, mock_bootstrap):
mock_index.return_value = [
{
"name": mock.sentinel.name,
"uuid": mock.sentinel.uuid,
"id": mock.sentinel.id,
}
]
mock_bootstrap.return_value = {"systemInfo": {"version": "3.2.0"}}
client = nvr.UVCRemote("foo", 7080, "key")
self.assertEqual(mock.sentinel.id, client.name_to_uuid(mock.sentinel.name))
class TestClient(unittest.TestCase):
def setUp(self):
super().setUp()
self._patches = []
try:
import httplib # noqa: F401
http_mock = mock.patch("httplib.HTTPConnection")
except ImportError:
http_mock = mock.patch("http.client.HTTPConnection")
http_mock.start()
self._patches.append(http_mock)
bootstrap_mock = mock.patch.object(
nvr.UVCRemote, "_get_bootstrap", side_effect=self._bootstrap
)
bootstrap_mock.start()
self._patches.append(bootstrap_mock)
def _bootstrap(self):
return {"systemInfo": {"version": "3.1.3"}}
def cleanUp(self):
for i in self._patches:
i.stop()
def test_set_recordmode(self):
fake_resp1 = {
"data": [
{
"recordingSettings": {
"fullTimeRecordEnabled": False,
"motionRecordEnabled": False,
}
}
]
}
fake_resp2 = {
"data": [
{
"recordingSettings": {
"fullTimeRecordEnabled": True,
"motionRecordEnabled": False,
"channel": 1,
}
}
]
}
def fake_req(path, method="GET", data=None):
if method == "GET":
return fake_resp1
elif method == "PUT":
self.assertEqual(json.dumps(fake_resp2["data"][0]), data)
return fake_resp2
client = nvr.UVCRemote("foo", 7080, "key")
with mock.patch.object(client, "_uvc_request") as mock_r:
mock_r.side_effect = fake_req
client.set_recordmode("uuid", "full", chan="medium")
self.assertTrue(mock_r.called)
fake_resp2["data"][0]["recordingSettings"] = {
"fullTimeRecordEnabled": False,
"motionRecordEnabled": True,
"channel": 0,
}
with mock.patch.object(client, "_uvc_request") as mock_r:
mock_r.side_effect = fake_req
client.set_recordmode("uuid", "motion", chan="high")
self.assertTrue(mock_r.called)
def test_get_picture_settings(self):
fake_resp = {"data": [{"ispSettings": {"settingA": 1, "settingB": "foo"}}]}
client = nvr.UVCRemote("foo", 7080, "key")
with mock.patch.object(client, "_uvc_request") as mock_r:
mock_r.return_value = fake_resp
self.assertEqual(
{"settingA": 1, "settingB": "foo"}, client.get_picture_settings("uuid")
)
def test_set_picture_settings(self):
fake_resp = {"data": [{"ispSettings": {"settingA": 1, "settingB": "foo"}}]}
client = nvr.UVCRemote("foo", 7080, "key")
newvals = {"settingA": 2, "settingB": "foo"}
with mock.patch.object(client, "_uvc_request") as mock_r:
mock_r.return_value = fake_resp
resp = client.set_picture_settings("uuid", newvals)
mock_r.assert_any_call(
"/api/2.0/camera/uuid", "PUT", json.dumps({"ispSettings": newvals})
)
self.assertEqual(fake_resp["data"][0]["ispSettings"], resp)
def test_set_picture_settings_coerces(self):
fake_resp = {
"data": [
{
"ispSettings": {
"settingA": 1,
"settingB": "foo",
"settingC": False,
}
}
]
}
client = nvr.UVCRemote("foo", 7080, "key")
newvals = {"settingA": "2", "settingB": False, "settingC": "foo"}
newvals_expected = {"settingA": 2, "settingB": "False", "settingC": True}
with mock.patch.object(client, "_uvc_request") as mock_r:
mock_r.return_value = fake_resp
resp = client.set_picture_settings("uuid", newvals)
mock_r.assert_any_call(
"/api/2.0/camera/uuid",
"PUT",
json.dumps({"ispSettings": newvals_expected}),
)
self.assertEqual(fake_resp["data"][0]["ispSettings"], resp)
def test_get_zones(self):
fake_resp = {"data": [{"zones": ["fake-zone1", "fake-zone2"]}]}
client = nvr.UVCRemote("foo", 7080, "key")
with mock.patch.object(client, "_uvc_request") as mock_r:
mock_r.return_value = fake_resp
resp = client.list_zones("uuid")
mock_r.assert_any_call("/api/2.0/camera/uuid")
self.assertEqual(fake_resp["data"][0]["zones"], resp)
def test_prune_zones(self):
fake_resp = {"data": [{"zones": ["fake-zone1", "fake-zone2"]}]}
client = nvr.UVCRemote("foo", 7080, "key")
with mock.patch.object(client, "_uvc_request") as mock_r:
mock_r.return_value = fake_resp
client.prune_zones("uuid")
mock_r.assert_any_call(
"/api/2.0/camera/uuid", "PUT", json.dumps({"zones": ["fake-zone1"]})
)
def test_get_snapshot(self):
client = nvr.UVCRemote("foo", 7080, "key")
with mock.patch.object(client, "_safe_request") as mock_r:
mock_r.return_value.status = 200
mock_r.return_value.read.return_value = "image"
resp = client.get_snapshot("foo")
mock_r.assert_called_once_with(
"GET", "/api/2.0/snapshot/camera/foo?force=true&apiKey=key"
)
self.assertEqual("image", resp)
def test_get_snapshot_error(self):
client = nvr.UVCRemote("foo", 7080, "key")
with mock.patch.object(client, "_safe_request") as mock_r:
mock_r.return_value.status = 401
self.assertRaises(nvr.NvrError, client.get_snapshot, "foo")
|