File: test_nvr.py

package info (click to toggle)
python-uiprotect 6.1.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,848 kB
  • sloc: python: 16,451; makefile: 21
file content (262 lines) | stat: -rw-r--r-- 7,692 bytes parent folder | download
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
# mypy: disable-error-code="attr-defined, dict-item, assignment, union-attr, arg-type, list-item"

from __future__ import annotations

from datetime import timedelta
from ipaddress import IPv4Address, IPv6Address

import pytest
from pydantic.v1 import ValidationError

from uiprotect.data import (
    NVR,
    AnalyticsOption,
    DoorbellMessage,
    DoorbellMessageType,
)
from uiprotect.data.nvr import NVRSmartDetection
from uiprotect.exceptions import BadRequest
from uiprotect.utils import to_ms


@pytest.mark.parametrize("status", [True, False])
@pytest.mark.asyncio()
async def test_nvr_set_insights(nvr_obj: NVR, status: bool):
    nvr_obj.api.api_request.reset_mock()

    nvr_obj.is_insights_enabled = not status

    await nvr_obj.set_insights(status)

    nvr_obj.api.api_request.assert_called_with(
        "nvr",
        method="patch",
        json={"isInsightsEnabled": status},
    )


@pytest.mark.asyncio()
async def test_nvr_set_anonymous_analytics(nvr_obj: NVR):
    nvr_obj.api.api_request.reset_mock()

    nvr_obj.analytics_data = AnalyticsOption.ANONYMOUS

    await nvr_obj.set_anonymous_analytics(False)

    nvr_obj.api.api_request.assert_called_with(
        "nvr",
        method="patch",
        json={"analyticsData": "none"},
    )


@pytest.mark.asyncio()
async def test_nvr_set_default_reset_timeout(nvr_obj: NVR):
    nvr_obj.api.api_request.reset_mock()

    duration = timedelta(seconds=10)
    await nvr_obj.set_default_reset_timeout(duration)

    nvr_obj.api.api_request.assert_called_with(
        "nvr",
        method="patch",
        json={"doorbellSettings": {"defaultMessageResetTimeoutMs": to_ms(duration)}},
    )


@pytest.mark.parametrize("message", ["Test", "fqthpqBgVMKXp9jXX2VeuGeXYfx2mMjB"])
@pytest.mark.asyncio()
async def test_nvr_set_default_doorbell_message(nvr_obj: NVR, message: str):
    nvr_obj.api.api_request.reset_mock()

    if len(message) > 30:
        with pytest.raises(ValidationError):
            await nvr_obj.set_default_doorbell_message(message)

        assert not nvr_obj.api.api_request.called
    else:
        await nvr_obj.set_default_doorbell_message(message)

        nvr_obj.api.api_request.assert_called_with(
            "nvr",
            method="patch",
            json={"doorbellSettings": {"defaultMessageText": message}},
        )


@pytest.mark.parametrize(
    "message",
    ["Welcome", "Test", "fqthpqBgVMKXp9jXX2VeuGeXYfx2mMjB"],
)
@pytest.mark.asyncio()
async def test_nvr_add_custom_doorbell_message(nvr_obj: NVR, message: str):
    nvr_obj.api.api_request.reset_mock()

    nvr_obj.doorbell_settings.custom_messages = ["Welcome"]

    if message != "Test":
        with pytest.raises(BadRequest):
            await nvr_obj.add_custom_doorbell_message(message)

        assert not nvr_obj.api.api_request.called
    else:
        await nvr_obj.add_custom_doorbell_message(message)

        nvr_obj.api.api_request.assert_called_with(
            "nvr",
            method="patch",
            json={"doorbellSettings": {"customMessages": ["Welcome", "Test"]}},
        )

        assert nvr_obj.doorbell_settings.all_messages == [
            DoorbellMessage(
                type=DoorbellMessageType.LEAVE_PACKAGE_AT_DOOR,
                text=DoorbellMessageType.LEAVE_PACKAGE_AT_DOOR.value.replace("_", " "),
            ),
            DoorbellMessage(
                type=DoorbellMessageType.DO_NOT_DISTURB,
                text=DoorbellMessageType.DO_NOT_DISTURB.value.replace("_", " "),
            ),
            DoorbellMessage(
                type=DoorbellMessageType.CUSTOM_MESSAGE,
                text="Welcome",
            ),
            DoorbellMessage(
                type=DoorbellMessageType.CUSTOM_MESSAGE,
                text="Test",
            ),
        ]


@pytest.mark.parametrize("message", ["Welcome", "Test"])
@pytest.mark.asyncio()
async def test_nvr_remove_custom_doorbell_message(nvr_obj: NVR, message: str):
    nvr_obj.api.api_request.reset_mock()

    nvr_obj.doorbell_settings.custom_messages = ["Welcome"]

    if message == "Test":
        with pytest.raises(BadRequest):
            await nvr_obj.remove_custom_doorbell_message(message)

        assert not nvr_obj.api.api_request.called
    else:
        await nvr_obj.remove_custom_doorbell_message(message)

        nvr_obj.api.api_request.assert_called_with(
            "nvr",
            method="patch",
            json={"doorbellSettings": {"customMessages": []}},
        )

        assert nvr_obj.doorbell_settings.all_messages == [
            DoorbellMessage(
                type=DoorbellMessageType.LEAVE_PACKAGE_AT_DOOR,
                text=DoorbellMessageType.LEAVE_PACKAGE_AT_DOOR.value.replace("_", " "),
            ),
            DoorbellMessage(
                type=DoorbellMessageType.DO_NOT_DISTURB,
                text=DoorbellMessageType.DO_NOT_DISTURB.value.replace("_", " "),
            ),
        ]


@pytest.mark.parametrize(
    ("ip", "expected"),
    [
        ("192.168.1.1", IPv4Address("192.168.1.1")),
        ("fe80::1ff:fe23:4567:890a", IPv6Address("fe80::1ff:fe23:4567:890a")),
    ],
)
@pytest.mark.asyncio()
async def test_nvr_wan_ip(nvr_obj: NVR, ip: str, expected: IPv4Address | IPv6Address):
    nvr_dict = nvr_obj.unifi_dict()
    nvr_dict["wanIp"] = ip

    nvr = NVR.from_unifi_dict(**nvr_dict)
    assert nvr.wan_ip == expected
    assert nvr.unifi_dict()["wanIp"] == ip


@pytest.mark.asyncio()
async def test_nvr_set_smart_detections(nvr_obj: NVR):
    nvr_obj.smart_detection = NVRSmartDetection(
        enable=False,
        face_recognition=False,
        license_plate_recognition=False,
    )
    nvr_obj.api.api_request.reset_mock()

    await nvr_obj.set_smart_detections(True)

    nvr_obj.api.api_request.assert_called_with(
        "nvr",
        method="patch",
        json={"smartDetection": {"enable": True}},
    )


@pytest.mark.asyncio()
async def test_nvr_set_face_recognition(nvr_obj: NVR):
    nvr_obj.smart_detection = NVRSmartDetection(
        enable=True,
        face_recognition=False,
        license_plate_recognition=False,
    )
    nvr_obj.api.api_request.reset_mock()

    await nvr_obj.set_face_recognition(True)

    nvr_obj.api.api_request.assert_called_with(
        "nvr",
        method="patch",
        json={"smartDetection": {"faceRecognition": True}},
    )


@pytest.mark.asyncio()
async def test_nvr_set_face_recognition_no_smart(nvr_obj: NVR):
    nvr_obj.smart_detection = NVRSmartDetection(
        enable=False,
        face_recognition=False,
        license_plate_recognition=False,
    )
    nvr_obj.api.api_request.reset_mock()

    with pytest.raises(BadRequest):
        await nvr_obj.set_face_recognition(True)

    assert not nvr_obj.api.api_request.called


@pytest.mark.asyncio()
async def test_nvr_set_license_plate_recognition(nvr_obj: NVR):
    nvr_obj.smart_detection = NVRSmartDetection(
        enable=True,
        face_recognition=False,
        license_plate_recognition=False,
    )
    nvr_obj.api.api_request.reset_mock()

    await nvr_obj.set_license_plate_recognition(True)

    nvr_obj.api.api_request.assert_called_with(
        "nvr",
        method="patch",
        json={"smartDetection": {"licensePlateRecognition": True}},
    )


@pytest.mark.asyncio()
async def test_nvr_set_license_plate_recognition_no_smart(nvr_obj: NVR):
    nvr_obj.smart_detection = NVRSmartDetection(
        enable=False,
        face_recognition=False,
        license_plate_recognition=False,
    )
    nvr_obj.api.api_request.reset_mock()

    with pytest.raises(BadRequest):
        await nvr_obj.set_license_plate_recognition(True)

    assert not nvr_obj.api.api_request.called