Debian Codegraph @

tests/test_description_cache.py

raw
1 """Unit tests for description_cache."""
2
3 import asyncio
4 from unittest.mock import patch
5
6 import aiohttp
7 import defusedxml.ElementTree as DET
8 import pytest
9
10 from async_upnp_client.const import HttpResponse
11 from async_upnp_client.description_cache import DescriptionCache
12
13 from .conftest import UpnpTestRequester
14
15
16 @pytest.mark.asyncio
17 async def test_fetch_parse_success() -> None:
18 """Test properly fetching and parsing a description."""
19 xml = """<root xmlns="urn:schemas-upnp-org:device-1-0">
20 <device>
21 <deviceType>urn:schemas-upnp-org:device:TestDevice:1</deviceType>
22 <UDN>uuid:test_udn</UDN>
23 </device>
24 </root>"""
25 requester = UpnpTestRequester({("GET", "http://192.168.1.1/desc.xml"): HttpResponse(200, {}, xml)})
26 description_cache = DescriptionCache(requester)
27 descr_xml = await description_cache.async_get_description_xml("http://192.168.1.1/desc.xml")
28 assert descr_xml == xml
29
30 descr_dict = await description_cache.async_get_description_dict("http://192.168.1.1/desc.xml")
31 assert descr_dict == {
32 "deviceType": "urn:schemas-upnp-org:device:TestDevice:1",
33 "UDN": "uuid:test_udn",
34 }
35
36
37 @pytest.mark.asyncio
38 async def test_fetch_parse_success_invalid_chars() -> None:
39 """Test fail parsing a description with invalid characters."""
40 xml = """<root xmlns="urn:schemas-upnp-org:device-1-0">
41 <device>
42 <deviceType>urn:schemas-upnp-org:device:TestDevice:1</deviceType>
43 <UDN>uuid:test_udn</UDN>
44 <serialNumber>\xff\xff\xff\xff</serialNumber>
45 </device>
46 </root>"""
47 requester = UpnpTestRequester({("GET", "http://192.168.1.1/desc.xml"): HttpResponse(200, {}, xml)})
48 description_cache = DescriptionCache(requester)
49 descr_xml = await description_cache.async_get_description_xml("http://192.168.1.1/desc.xml")
50 assert descr_xml == xml
51
52 descr_dict = await description_cache.async_get_description_dict("http://192.168.1.1/desc.xml")
53 assert descr_dict == {
54 "deviceType": "urn:schemas-upnp-org:device:TestDevice:1",
55 "UDN": "uuid:test_udn",
56 "serialNumber": "ÿÿÿÿ",
57 }
58
59
60 @pytest.mark.asyncio
61 @pytest.mark.parametrize("exc", [asyncio.TimeoutError, aiohttp.ClientError])
62 async def test_fetch_fail(exc: Exception) -> None:
63 """Test fail fetching a description."""
64 xml = ""
65 requester = UpnpTestRequester({("GET", "http://192.168.1.1/desc.xml"): HttpResponse(200, {}, xml)})
66 requester.exceptions.append(exc)
67 description_cache = DescriptionCache(requester)
68 descr_xml = await description_cache.async_get_description_xml("http://192.168.1.1/desc.xml")
69 assert descr_xml is None
70
71 descr_dict = await description_cache.async_get_description_dict("http://192.168.1.1/desc.xml")
72 assert descr_dict is None
73
74
75 @pytest.mark.asyncio
76 async def test_parsing_fail_invalid_xml() -> None:
77 """Test fail parsing a description with invalid XML."""
78 xml = """<root xmlns="urn:schemas-upnp-org:device-1-0">INVALIDXML"""
79 requester = UpnpTestRequester({("GET", "http://192.168.1.1/desc.xml"): HttpResponse(200, {}, xml)})
80 description_cache = DescriptionCache(requester)
81 descr_xml = await description_cache.async_get_description_xml("http://192.168.1.1/desc.xml")
82 assert descr_xml == xml
83
84 descr_dict = await description_cache.async_get_description_dict("http://192.168.1.1/desc.xml")
85 assert descr_dict is None
86
87
88 @pytest.mark.asyncio
89 async def test_parsing_fail_error() -> None:
90 """Test fail parsing a description with invalid XML."""
91 xml = ""
92 requester = UpnpTestRequester({("GET", "http://192.168.1.1/desc.xml"): HttpResponse(200, {}, xml)})
93 description_cache = DescriptionCache(requester)
94 descr_xml = await description_cache.async_get_description_xml("http://192.168.1.1/desc.xml")
95 assert descr_xml == xml
96
97 with patch(
98 "async_upnp_client.description_cache.DET.fromstring",
99 side_effect=DET.ParseError,
100 ):
101 descr_dict = await description_cache.async_get_description_dict("http://192.168.1.1/desc.xml")
102 assert descr_dict is None