Debian Codegraph @

tests/test_event_handler.py

raw
1 """Unit tests for event handler module."""
2
3 from datetime import timedelta
4 from typing import Generator, Sequence
5 from unittest.mock import Mock, patch
6
7 import pytest
8
9 from async_upnp_client.client import UpnpService, UpnpStateVariable
10 from async_upnp_client.client_factory import UpnpFactory
11 from async_upnp_client.const import HttpRequest
12 from async_upnp_client.event_handler import UpnpEventHandlerRegister
13
14 from .conftest import RESPONSE_MAP, UpnpTestNotifyServer, UpnpTestRequester
15
16
17 @pytest.fixture
18 def patched_local_ip() -> Generator:
19 """Patch get_local_ip to `'192.168.1.2"`."""
20 with patch("async_upnp_client.event_handler.get_local_ip") as mock:
21 yield mock
22
23
24 @pytest.mark.asyncio
25 async def test_subscribe() -> None:
26 """Test subscribing to a UpnpService."""
27 requester = UpnpTestRequester(RESPONSE_MAP)
28 factory = UpnpFactory(requester)
29 device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
30 notify_server = UpnpTestNotifyServer(
31 requester=requester,
32 source=("192.168.1.2", 8090),
33 )
34 event_handler = notify_server.event_handler
35
36 service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
37 sid, timeout = await event_handler.async_subscribe(service)
38 assert event_handler.service_for_sid("uuid:dummy") == service
39 assert sid == "uuid:dummy"
40 assert timeout == timedelta(seconds=300)
41 assert event_handler.callback_url == "http://192.168.1.2:8090/notify"
42
43
44 @pytest.mark.asyncio
45 async def test_subscribe_renew() -> None:
46 """Test renewing an existing subscription to a UpnpService."""
47 requester = UpnpTestRequester(RESPONSE_MAP)
48 factory = UpnpFactory(requester)
49 device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
50 notify_server = UpnpTestNotifyServer(
51 requester=requester,
52 source=("192.168.1.2", 8090),
53 )
54 event_handler = notify_server.event_handler
55
56 service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
57 sid, timeout = await event_handler.async_subscribe(service)
58 assert sid == "uuid:dummy"
59 assert event_handler.service_for_sid("uuid:dummy") == service
60 assert timeout == timedelta(seconds=300)
61
62 sid, timeout = await event_handler.async_resubscribe(service)
63 assert event_handler.service_for_sid("uuid:dummy") == service
64 assert sid == "uuid:dummy"
65 assert timeout == timedelta(seconds=300)
66
67
68 @pytest.mark.asyncio
69 async def test_unsubscribe() -> None:
70 """Test unsubscribing from a UpnpService."""
71 requester = UpnpTestRequester(RESPONSE_MAP)
72 factory = UpnpFactory(requester)
73 device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
74 notify_server = UpnpTestNotifyServer(
75 requester=requester,
76 source=("192.168.1.2", 8090),
77 )
78 event_handler = notify_server.event_handler
79
80 service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
81 sid, timeout = await event_handler.async_subscribe(service)
82 assert event_handler.service_for_sid("uuid:dummy") == service
83 assert sid == "uuid:dummy"
84 assert timeout == timedelta(seconds=300)
85
86 old_sid = await event_handler.async_unsubscribe(service)
87 assert event_handler.service_for_sid("uuid:dummy") is None
88 assert old_sid == "uuid:dummy"
89
90
91 @pytest.mark.asyncio
92 async def test_on_notify_upnp_event() -> None:
93 """Test handling of a UPnP event."""
94 changed_vars: Sequence[UpnpStateVariable] = []
95
96 def on_event(_self: UpnpService, changed_state_variables: Sequence[UpnpStateVariable]) -> None:
97 nonlocal changed_vars
98 changed_vars = changed_state_variables
99
100 requester = UpnpTestRequester(RESPONSE_MAP)
101 notify_server = UpnpTestNotifyServer(
102 requester=requester,
103 source=("192.168.1.2", 8090),
104 )
105 event_handler = notify_server.event_handler
106 factory = UpnpFactory(requester)
107 device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
108 service = device.service("urn:schemas-upnp-org:service:RenderingControl:1")
109 service.on_event = on_event
110 await event_handler.async_subscribe(service)
111
112 headers = {
113 "NT": "upnp:event",
114 "NTS": "upnp:propchange",
115 "SID": "uuid:dummy",
116 }
117 body = """
118 <e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
119 <e:property>
120 <Volume>60</Volume>
121 </e:property>
122 </e:propertyset>
123 """
124
125 http_request = HttpRequest("NOTIFY", "http://dlna_dmr:1234/upnp/event/RenderingControl1", headers, body)
126 result = await event_handler.handle_notify(http_request)
127 assert result == 200
128
129 assert len(changed_vars) == 1
130
131 state_var = service.state_variable("Volume")
132 assert state_var.value == 60
133
134
135 @pytest.mark.asyncio
136 async def test_register_device(patched_local_ip: Mock) -> None:
137 """Test registering a device with a UpnpEventHandlerRegister."""
138 # pylint: disable=redefined-outer-name
139 requester = UpnpTestRequester(RESPONSE_MAP)
140 register = UpnpEventHandlerRegister(requester, UpnpTestNotifyServer)
141 patched_local_ip.return_value = "192.168.1.2"
142
143 factory = UpnpFactory(requester)
144 device = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
145
146 event_handler = await register.async_add_device(device)
147 assert event_handler is not None
148 assert event_handler.callback_url == "http://192.168.1.2:0/notify"
149 assert register.has_event_handler_for_device(device)
150
151
152 @pytest.mark.asyncio
153 async def test_register_device_different_source_address(patched_local_ip: Mock) -> None:
154 """Test registering two devices with different source IPs with a UpnpEventHandlerRegister."""
155 # pylint: disable=redefined-outer-name
156 requester = UpnpTestRequester(RESPONSE_MAP)
157 register = UpnpEventHandlerRegister(requester, UpnpTestNotifyServer)
158 factory = UpnpFactory(requester)
159
160 patched_local_ip.return_value = "192.168.1.2"
161 device_1 = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
162 event_handler_1 = await register.async_add_device(device_1)
163 assert event_handler_1 is not None
164 assert event_handler_1.callback_url == "http://192.168.1.2:0/notify"
165
166 patched_local_ip.return_value = "192.168.2.2"
167 device_2 = await factory.async_create_device("http://igd:1234/device.xml")
168 event_handler_2 = await register.async_add_device(device_2)
169 assert event_handler_2 is not None
170 assert event_handler_2.callback_url == "http://192.168.2.2:0/notify"
171
172
173 @pytest.mark.asyncio
174 async def test_remove_device(patched_local_ip: Mock) -> None:
175 """Test removing a device from a UpnpEventHandlerRegister."""
176 # pylint: disable=redefined-outer-name
177 requester = UpnpTestRequester(RESPONSE_MAP)
178 register = UpnpEventHandlerRegister(requester, UpnpTestNotifyServer)
179 factory = UpnpFactory(requester)
180
181 patched_local_ip.return_value = "192.168.1.2"
182 device_1 = await factory.async_create_device("http://dlna_dmr:1234/device.xml")
183 device_2 = await factory.async_create_device("http://igd:1234/device.xml")
184
185 event_handler_1 = await register.async_add_device(device_1)
186 event_handler_2 = await register.async_add_device(device_2)
187 assert event_handler_1 is event_handler_2
188
189 removed_event_handler_1 = await register.async_remove_device(device_1)
190 assert removed_event_handler_1 is None # UpnpEventHandler is still being used
191 removed_event_handler_2 = await register.async_remove_device(device_2)
192 assert removed_event_handler_2 is event_handler_1