Debian Codegraph @

tests/test_utils.py

raw symbols SCIP
1 """Test pydeCONZ utilities.
2
3 pytest --cov-report term-missing --cov=pydeconz.utils tests/test_utils.py
4 """
5
6 from __future__ import annotations
7
8 from unittest.mock import AsyncMock, Mock, patch
9
10 import aiohttp
11 import pytest
12
13 from pydeconz import errors, utils
14
15 API_KEY = "1234567890"
16 IP = "127.0.0.1"
17 PORT = "80"
18
19
20 async def test_delete_api_key() -> None:
21 """Test a successful call of delete_api_key."""
22 session = Mock()
23
24 with patch("pydeconz.utils.request", new=AsyncMock(return_value=True)):
25 await utils.delete_api_key(session, IP, PORT, API_KEY)
26
27
28 async def test_delete_all_keys() -> None:
29 """Test a successful call of delete_all_keys.
30
31 Delete all keys doesn't care what happens with delete_api_key.
32 """
33 session = Mock()
34
35 with patch(
36 "pydeconz.utils.request",
37 new=AsyncMock(return_value={"whitelist": {1: "123", 2: "456"}}),
38 ):
39 await utils.delete_all_keys(session, IP, PORT, API_KEY)
40
41
42 async def test_get_bridge_id() -> None:
43 """Test a successful call of get_bridgeid."""
44 session = Mock()
45
46 with patch(
47 "pydeconz.utils.request",
48 new=AsyncMock(return_value={"bridgeid": "12345"}),
49 ):
50 response = await utils.get_bridge_id(session, IP, PORT, API_KEY)
51
52 assert response == "12345"
53
54
55 async def test_discovery() -> None:
56 """Test a successful call to discovery."""
57 session = Mock()
58
59 with patch(
60 "pydeconz.utils.request",
61 new=AsyncMock(
62 return_value=[
63 {
64 "id": "123456FFFFABCDEF",
65 "internalipaddress": "host1",
66 "internalport": "port1",
67 "macaddress": "a:b:c",
68 "name": "gateway",
69 },
70 {
71 "id": "234567BCDEFG",
72 "internalipaddress": "host2",
73 "internalport": "port2",
74 },
75 ]
76 ),
77 ):
78 response = await utils.discovery(session)
79
80 assert response == [
81 {
82 "id": "123456ABCDEF",
83 "host": "host1",
84 "port": "port1",
85 "mac": "a:b:c",
86 "name": "gateway",
87 },
88 {
89 "id": "234567BCDEFG",
90 "host": "host2",
91 "port": "port2",
92 "mac": "",
93 "name": "",
94 },
95 ]
96
97
98 async def test_discovery_response_empty() -> None:
99 """Test an empty discovery returns an empty list."""
100 session = Mock()
101
102 with patch("pydeconz.utils.request", new=AsyncMock(return_value={})):
103 response = await utils.discovery(session)
104
105 assert not response
106
107
108 async def test_request() -> None:
109 """Test a successful call of request."""
110 response = Mock()
111 response.content_type = "application/json"
112 response.json = AsyncMock(return_value={"json": "response"})
113 session = AsyncMock(return_value=response)
114
115 result = await utils.request(session, "url")
116
117 assert result == {"json": "response"}
118
119
120 async def test_request_fails_client_error() -> None:
121 """Test a successful call of request."""
122 session = AsyncMock(side_effect=aiohttp.ClientError)
123
124 with pytest.raises(errors.RequestError) as e_info:
125 await utils.request(session, "url")
126
127 assert str(e_info.value) == "Error requesting data from url: "
128
129
130 async def test_request_fails_invalid_content() -> None:
131 """Test a successful call of request."""
132 response = Mock()
133 response.content_type = "application/binary"
134 session = AsyncMock(return_value=response)
135
136 with pytest.raises(errors.ResponseError) as e_info:
137 await utils.request(session, "url")
138
139 assert str(e_info.value) == "Invalid content type: application/binary"
140
141
142 async def test_request_fails_raise_error() -> None:
143 """Test a successful call of request."""
144 response = Mock()
145 response.content_type = "application/json"
146 response.json = AsyncMock(
147 return_value=[
148 {"error": {"type": 1, "address": "address", "description": "description"}}
149 ]
150 )
151 session = AsyncMock(return_value=response)
152
153 with pytest.raises(errors.Unauthorized) as e_info:
154 await utils.request(session, "url")
155
156 assert str(e_info.value) == "1 address description"