Debian Codegraph @

tests/test_cloudhooks.py

raw
1 """Test cloud cloudhooks."""
2
3 from unittest.mock import AsyncMock, patch
4
5 import pytest
6 from syrupy import SnapshotAssertion
7
8 from hass_nabucasa import Cloud
9 from hass_nabucasa.const import STATE_CONNECTED
10 from hass_nabucasa.events.types import (
11 CloudEventType,
12 CloudhookCreatedEvent,
13 CloudhookDeletedEvent,
14 )
15 from tests.common import extract_log_messages
16 from tests.utils.aiohttp import AiohttpClientMocker
17
18
19 async def test_enable(
20 cloud: Cloud,
21 aioclient_mock: AiohttpClientMocker,
22 caplog: pytest.LogCaptureFixture,
23 snapshot: SnapshotAssertion,
24 ):
25 """Test enabling cloudhooks."""
26 aioclient_mock.get(
27 f"https://{cloud.api_server}/instance/webhook",
28 json={
29 "cloudhook_id": "mock-cloud-id",
30 "url": "https://hooks.nabu.casa/ZXCZCXZ",
31 },
32 )
33
34 hook = {
35 "webhook_id": "mock-webhook-id",
36 "cloudhook_id": "mock-cloud-id",
37 "cloudhook_url": "https://hooks.nabu.casa/ZXCZCXZ",
38 "managed": False,
39 }
40
41 cloud.iot.state = STATE_CONNECTED
42 with patch(
43 "hass_nabucasa.iot.CloudIoT.async_send_message", new_callable=AsyncMock
44 ) as mock_send:
45 result = await cloud.cloudhooks.async_create("mock-webhook-id", False)
46
47 assert result == hook
48 assert cloud.client.cloudhooks == {"mock-webhook-id": hook}
49
50 assert len(mock_send.mock_calls) == 1
51 assert mock_send.mock_calls[0][1][0] == "webhook-register"
52 assert mock_send.mock_calls[0][1][1] == {"cloudhook_ids": ["mock-cloud-id"]}
53 assert extract_log_messages(caplog) == snapshot
54
55
56 async def test_delete(
57 cloud: Cloud,
58 caplog: pytest.LogCaptureFixture,
59 snapshot: SnapshotAssertion,
60 ):
61 """Test deleting a cloudhook."""
62 cloud.client._cloudhooks = {
63 "mock-webhook-id": {
64 "webhook_id": "mock-webhook-id",
65 "cloudhook_id": "mock-cloud-id",
66 "cloudhook_url": "https://hooks.nabu.casa/ZXCZCXZ",
67 },
68 }
69
70 cloud.iot.state = STATE_CONNECTED
71 with patch(
72 "hass_nabucasa.iot.CloudIoT.async_send_message", new_callable=AsyncMock
73 ) as mock_send:
74 await cloud.cloudhooks.async_delete("mock-webhook-id")
75
76 assert cloud.client.cloudhooks == {}
77
78 assert len(mock_send.mock_calls) == 1
79 assert mock_send.mock_calls[0][1][0] == "webhook-register"
80 assert mock_send.mock_calls[0][1][1] == {"cloudhook_ids": []}
81 assert extract_log_messages(caplog) == snapshot
82
83
84 async def test_create_publishes_cloudhook_created_event(
85 cloud: Cloud,
86 aioclient_mock: AiohttpClientMocker,
87 ):
88 """Test that async_create publishes a CloudhookCreatedEvent."""
89 aioclient_mock.get(
90 f"https://{cloud.api_server}/instance/webhook",
91 json={
92 "cloudhook_id": "mock-cloud-id",
93 "url": "https://hooks.nabu.casa/ZXCZCXZ",
94 },
95 )
96
97 subscriber = AsyncMock()
98 cloud.events.subscribe(
99 event_type=CloudEventType.CLOUDHOOK_CREATED, handler=subscriber
100 )
101
102 cloud.iot.state = STATE_CONNECTED
103 with patch("hass_nabucasa.iot.CloudIoT.async_send_message", new_callable=AsyncMock):
104 await cloud.cloudhooks.async_create("mock-webhook-id", False)
105
106 assert len(subscriber.call_args_list) == 1
107 event = subscriber.call_args_list[0][0][0]
108 assert isinstance(event, CloudhookCreatedEvent)
109 assert event.type == CloudEventType.CLOUDHOOK_CREATED
110 assert event.cloudhook == {
111 "webhook_id": "mock-webhook-id",
112 "cloudhook_id": "mock-cloud-id",
113 "cloudhook_url": "https://hooks.nabu.casa/ZXCZCXZ",
114 "managed": False,
115 }
116 assert event.timestamp is not None
117
118
119 async def test_delete_publishes_cloudhook_deleted_event(
120 cloud: Cloud,
121 ):
122 """Test that async_delete publishes a CloudhookDeletedEvent."""
123 cloud.client._cloudhooks = {
124 "mock-webhook-id": {
125 "webhook_id": "mock-webhook-id",
126 "cloudhook_id": "mock-cloud-id",
127 "cloudhook_url": "https://hooks.nabu.casa/ZXCZCXZ",
128 "managed": True,
129 },
130 }
131
132 subscriber = AsyncMock()
133 cloud.events.subscribe(
134 event_type=CloudEventType.CLOUDHOOK_DELETED, handler=subscriber
135 )
136
137 cloud.iot.state = STATE_CONNECTED
138 with patch("hass_nabucasa.iot.CloudIoT.async_send_message", new_callable=AsyncMock):
139 await cloud.cloudhooks.async_delete("mock-webhook-id")
140
141 assert len(subscriber.call_args_list) == 1
142 event = subscriber.call_args_list[0][0][0]
143 assert isinstance(event, CloudhookDeletedEvent)
144 assert event.type == CloudEventType.CLOUDHOOK_DELETED
145 assert event.cloudhook == {
146 "webhook_id": "mock-webhook-id",
147 "cloudhook_id": "mock-cloud-id",
148 "cloudhook_url": "https://hooks.nabu.casa/ZXCZCXZ",
149 "managed": True,
150 }
151 assert event.timestamp is not None
152
153
154 async def test_create_without_connected(
155 cloud: Cloud,
156 caplog: pytest.LogCaptureFixture,
157 snapshot: SnapshotAssertion,
158 ):
159 """Test we raise ValueError if not connected."""
160 assert cloud.iot.state != STATE_CONNECTED
161 with pytest.raises(ValueError, match="Cloud is not connected"):
162 await cloud.cloudhooks.async_create("mock-webhook-id", True)
163
164 assert cloud.client.cloudhooks == {}
165 assert extract_log_messages(caplog) == snapshot