2016-08-14 08:10:07 +00:00
|
|
|
"""Test HTML5 notify platform."""
|
|
|
|
import json
|
2019-12-09 13:19:48 +00:00
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
from aiohttp.hdrs import AUTHORIZATION
|
2016-08-14 08:10:07 +00:00
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
import homeassistant.components.html5.notify as html5
|
2020-04-08 21:20:03 +00:00
|
|
|
from homeassistant.const import HTTP_INTERNAL_SERVER_ERROR
|
2019-12-09 13:19:48 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
from homeassistant.setup import async_setup_component
|
2016-08-14 08:10:07 +00:00
|
|
|
|
2020-05-03 18:27:19 +00:00
|
|
|
from tests.async_mock import MagicMock, mock_open, patch
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_FILE = "file.conf"
|
2018-01-05 22:29:27 +00:00
|
|
|
|
2019-05-06 23:37:05 +00:00
|
|
|
VAPID_CONF = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"vapid_pub_key": "BJMA2gDZEkHaXRhf1fhY_"
|
|
|
|
+ "QbKbhVIHlSJXI0bFyo0eJXnUPOjdgycCAbj-2bMKMKNKs"
|
|
|
|
+ "_rM8JoSnyKGCXAY2dbONI",
|
|
|
|
"vapid_prv_key": "ZwPgwKpESGuGLMZYU39vKgrekrWzCijo-LsBM3CZ9-c",
|
|
|
|
"vapid_email": "someone@example.com",
|
2019-05-06 23:37:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-21 23:01:24 +00:00
|
|
|
SUBSCRIPTION_1 = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"browser": "chrome",
|
|
|
|
"subscription": {
|
|
|
|
"endpoint": "https://googleapis.com",
|
|
|
|
"keys": {"auth": "auth", "p256dh": "p256dh"},
|
2016-08-21 23:01:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
SUBSCRIPTION_2 = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"browser": "firefox",
|
|
|
|
"subscription": {
|
|
|
|
"endpoint": "https://example.com",
|
|
|
|
"keys": {"auth": "bla", "p256dh": "bla"},
|
2016-08-21 23:01:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
SUBSCRIPTION_3 = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"browser": "chrome",
|
|
|
|
"subscription": {
|
|
|
|
"endpoint": "https://example.com/not_exist",
|
|
|
|
"keys": {"auth": "bla", "p256dh": "bla"},
|
2016-08-21 23:01:24 +00:00
|
|
|
},
|
|
|
|
}
|
2017-06-05 05:46:18 +00:00
|
|
|
SUBSCRIPTION_4 = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"browser": "chrome",
|
|
|
|
"subscription": {
|
|
|
|
"endpoint": "https://googleapis.com",
|
|
|
|
"expirationTime": None,
|
|
|
|
"keys": {"auth": "auth", "p256dh": "p256dh"},
|
2017-06-05 05:46:18 +00:00
|
|
|
},
|
|
|
|
}
|
2016-08-21 23:01:24 +00:00
|
|
|
|
2019-05-06 23:37:05 +00:00
|
|
|
SUBSCRIPTION_5 = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"browser": "chrome",
|
|
|
|
"subscription": {
|
|
|
|
"endpoint": "https://fcm.googleapis.com/fcm/send/LONG-RANDOM-KEY",
|
|
|
|
"expirationTime": None,
|
|
|
|
"keys": {"auth": "auth", "p256dh": "p256dh"},
|
2019-05-06 23:37:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
REGISTER_URL = "/api/notify.html5"
|
|
|
|
PUBLISH_URL = "/api/notify.html5/callback"
|
2016-10-24 06:48:01 +00:00
|
|
|
|
2016-08-14 08:10:07 +00:00
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def mock_client(hass, hass_client, registrations=None):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Create a test client for HTML5 views."""
|
|
|
|
if registrations is None:
|
|
|
|
registrations = {}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.html5.notify._load_config", return_value=registrations
|
|
|
|
):
|
|
|
|
await async_setup_component(hass, "notify", {"notify": {"platform": "html5"}})
|
2019-04-09 06:16:55 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
return await hass_client()
|
2018-02-15 21:06:14 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class TestHtml5Notify:
|
2016-08-14 08:10:07 +00:00
|
|
|
"""Tests for HTML5 notify platform."""
|
|
|
|
|
|
|
|
def test_get_service_with_no_json(self):
|
|
|
|
"""Test empty json file."""
|
|
|
|
hass = MagicMock()
|
|
|
|
|
2016-10-04 08:01:41 +00:00
|
|
|
m = mock_open()
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.json.open", m, create=True):
|
2016-08-14 08:10:07 +00:00
|
|
|
service = html5.get_service(hass, {})
|
|
|
|
|
|
|
|
assert service is not None
|
|
|
|
|
2019-10-17 19:17:23 +00:00
|
|
|
@patch("homeassistant.components.html5.notify.WebPusher")
|
2019-01-15 23:31:57 +00:00
|
|
|
def test_dismissing_message(self, mock_wp):
|
|
|
|
"""Test dismissing message."""
|
|
|
|
hass = MagicMock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"device": SUBSCRIPTION_1}
|
2019-01-15 23:31:57 +00:00
|
|
|
|
|
|
|
m = mock_open(read_data=json.dumps(data))
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.json.open", m, create=True):
|
|
|
|
service = html5.get_service(hass, {"gcm_sender_id": "100"})
|
2019-01-15 23:31:57 +00:00
|
|
|
|
|
|
|
assert service is not None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
service.dismiss(target=["device", "non_existing"], data={"tag": "test"})
|
2019-01-15 23:31:57 +00:00
|
|
|
|
|
|
|
assert len(mock_wp.mock_calls) == 3
|
|
|
|
|
|
|
|
# WebPusher constructor
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_1["subscription"]
|
2019-01-15 23:31:57 +00:00
|
|
|
# Third mock_call checks the status_code of the response.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"
|
2019-01-15 23:31:57 +00:00
|
|
|
|
|
|
|
# Call to send
|
|
|
|
payload = json.loads(mock_wp.mock_calls[1][1][0])
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert payload["dismiss"] is True
|
|
|
|
assert payload["tag"] == "test"
|
2019-01-15 23:31:57 +00:00
|
|
|
|
2019-10-17 19:17:23 +00:00
|
|
|
@patch("homeassistant.components.html5.notify.WebPusher")
|
2016-08-14 08:10:07 +00:00
|
|
|
def test_sending_message(self, mock_wp):
|
|
|
|
"""Test sending message."""
|
|
|
|
hass = MagicMock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"device": SUBSCRIPTION_1}
|
2016-08-14 08:10:07 +00:00
|
|
|
|
2016-10-04 08:01:41 +00:00
|
|
|
m = mock_open(read_data=json.dumps(data))
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.json.open", m, create=True):
|
|
|
|
service = html5.get_service(hass, {"gcm_sender_id": "100"})
|
2016-08-14 08:10:07 +00:00
|
|
|
|
|
|
|
assert service is not None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
service.send_message(
|
|
|
|
"Hello", target=["device", "non_existing"], data={"icon": "beer.png"}
|
|
|
|
)
|
2016-08-14 08:10:07 +00:00
|
|
|
|
2017-06-03 03:56:16 +00:00
|
|
|
assert len(mock_wp.mock_calls) == 3
|
2016-08-14 08:10:07 +00:00
|
|
|
|
|
|
|
# WebPusher constructor
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_1["subscription"]
|
2017-06-03 03:56:16 +00:00
|
|
|
# Third mock_call checks the status_code of the response.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"
|
2016-08-14 08:10:07 +00:00
|
|
|
|
|
|
|
# Call to send
|
|
|
|
payload = json.loads(mock_wp.mock_calls[1][1][0])
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert payload["body"] == "Hello"
|
|
|
|
assert payload["icon"] == "beer.png"
|
2016-08-14 08:10:07 +00:00
|
|
|
|
2019-10-17 19:17:23 +00:00
|
|
|
@patch("homeassistant.components.html5.notify.WebPusher")
|
2018-03-09 01:50:17 +00:00
|
|
|
def test_gcm_key_include(self, mock_wp):
|
|
|
|
"""Test if the gcm_key is only included for GCM endpoints."""
|
|
|
|
hass = MagicMock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"chrome": SUBSCRIPTION_1, "firefox": SUBSCRIPTION_2}
|
2018-03-09 01:50:17 +00:00
|
|
|
|
|
|
|
m = mock_open(read_data=json.dumps(data))
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.json.open", m, create=True):
|
|
|
|
service = html5.get_service(
|
|
|
|
hass, {"gcm_sender_id": "100", "gcm_api_key": "Y6i0JdZ0mj9LOaSI"}
|
|
|
|
)
|
2018-03-09 01:50:17 +00:00
|
|
|
|
|
|
|
assert service is not None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
service.send_message("Hello", target=["chrome", "firefox"])
|
2018-03-09 01:50:17 +00:00
|
|
|
|
|
|
|
assert len(mock_wp.mock_calls) == 6
|
|
|
|
|
|
|
|
# WebPusher constructor
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_1["subscription"]
|
|
|
|
assert mock_wp.mock_calls[3][1][0] == SUBSCRIPTION_2["subscription"]
|
2018-03-09 01:50:17 +00:00
|
|
|
|
|
|
|
# Third mock_call checks the status_code of the response.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"
|
|
|
|
assert mock_wp.mock_calls[5][0] == "().send().status_code.__eq__"
|
2018-03-09 01:50:17 +00:00
|
|
|
|
|
|
|
# Get the keys passed to the WebPusher's send method
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[1][2]["gcm_key"] is not None
|
|
|
|
assert mock_wp.mock_calls[4][2]["gcm_key"] is None
|
2018-03-09 01:50:17 +00:00
|
|
|
|
2019-10-17 19:17:23 +00:00
|
|
|
@patch("homeassistant.components.html5.notify.WebPusher")
|
2019-05-06 23:37:05 +00:00
|
|
|
def test_fcm_key_include(self, mock_wp):
|
|
|
|
"""Test if the FCM header is included."""
|
|
|
|
hass = MagicMock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"chrome": SUBSCRIPTION_5}
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
m = mock_open(read_data=json.dumps(data))
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.json.open", m, create=True):
|
2019-05-06 23:37:05 +00:00
|
|
|
service = html5.get_service(hass, VAPID_CONF)
|
|
|
|
|
|
|
|
assert service is not None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
service.send_message("Hello", target=["chrome"])
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
assert len(mock_wp.mock_calls) == 3
|
|
|
|
# WebPusher constructor
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_5["subscription"]
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
# Third mock_call checks the status_code of the response.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
# Get the keys passed to the WebPusher's send method
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[1][2]["headers"]["Authorization"] is not None
|
2019-05-06 23:37:05 +00:00
|
|
|
|
2019-10-17 19:17:23 +00:00
|
|
|
@patch("homeassistant.components.html5.notify.WebPusher")
|
2019-05-06 23:37:05 +00:00
|
|
|
def test_fcm_send_with_unknown_priority(self, mock_wp):
|
|
|
|
"""Test if the gcm_key is only included for GCM endpoints."""
|
|
|
|
hass = MagicMock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"chrome": SUBSCRIPTION_5}
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
m = mock_open(read_data=json.dumps(data))
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.json.open", m, create=True):
|
2019-05-06 23:37:05 +00:00
|
|
|
service = html5.get_service(hass, VAPID_CONF)
|
|
|
|
|
|
|
|
assert service is not None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
service.send_message("Hello", target=["chrome"], priority="undefined")
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
assert len(mock_wp.mock_calls) == 3
|
|
|
|
# WebPusher constructor
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_5["subscription"]
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
# Third mock_call checks the status_code of the response.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
# Get the keys passed to the WebPusher's send method
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[1][2]["headers"]["priority"] == "normal"
|
2019-05-06 23:37:05 +00:00
|
|
|
|
2019-10-17 19:17:23 +00:00
|
|
|
@patch("homeassistant.components.html5.notify.WebPusher")
|
2019-05-06 23:37:05 +00:00
|
|
|
def test_fcm_no_targets(self, mock_wp):
|
|
|
|
"""Test if the gcm_key is only included for GCM endpoints."""
|
|
|
|
hass = MagicMock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"chrome": SUBSCRIPTION_5}
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
m = mock_open(read_data=json.dumps(data))
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.json.open", m, create=True):
|
2019-05-06 23:37:05 +00:00
|
|
|
service = html5.get_service(hass, VAPID_CONF)
|
|
|
|
|
|
|
|
assert service is not None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
service.send_message("Hello")
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
assert len(mock_wp.mock_calls) == 3
|
|
|
|
# WebPusher constructor
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_5["subscription"]
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
# Third mock_call checks the status_code of the response.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
# Get the keys passed to the WebPusher's send method
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[1][2]["headers"]["priority"] == "normal"
|
2019-05-06 23:37:05 +00:00
|
|
|
|
2019-10-17 19:17:23 +00:00
|
|
|
@patch("homeassistant.components.html5.notify.WebPusher")
|
2019-05-06 23:37:05 +00:00
|
|
|
def test_fcm_additional_data(self, mock_wp):
|
|
|
|
"""Test if the gcm_key is only included for GCM endpoints."""
|
|
|
|
hass = MagicMock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"chrome": SUBSCRIPTION_5}
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
m = mock_open(read_data=json.dumps(data))
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.json.open", m, create=True):
|
2019-05-06 23:37:05 +00:00
|
|
|
service = html5.get_service(hass, VAPID_CONF)
|
|
|
|
|
|
|
|
assert service is not None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
service.send_message("Hello", data={"mykey": "myvalue"})
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
assert len(mock_wp.mock_calls) == 3
|
|
|
|
# WebPusher constructor
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_5["subscription"]
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
# Third mock_call checks the status_code of the response.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
# Get the keys passed to the WebPusher's send method
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[1][2]["headers"]["priority"] == "normal"
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_vapid_withoutvapid():
|
|
|
|
"""Test creating empty vapid."""
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = html5.create_vapid_headers(
|
|
|
|
vapid_email=None, vapid_private_key=None, subscription_info=None
|
|
|
|
)
|
2019-05-06 23:37:05 +00:00
|
|
|
assert resp is None
|
|
|
|
|
2016-08-14 08:10:07 +00:00
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_registering_new_device_view(hass, hass_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test that the HTML view works."""
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await mock_client(hass, hass_client)
|
2016-08-14 08:10:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.html5.notify.save_json") as mock_save:
|
2018-03-05 02:35:07 +00:00
|
|
|
resp = await client.post(REGISTER_URL, data=json.dumps(SUBSCRIPTION_1))
|
2018-01-05 22:29:27 +00:00
|
|
|
|
2018-02-15 21:06:14 +00:00
|
|
|
assert resp.status == 200
|
|
|
|
assert len(mock_save.mock_calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_save.mock_calls[0][1][1] == {"unnamed device": SUBSCRIPTION_1}
|
2017-06-05 05:46:18 +00:00
|
|
|
|
|
|
|
|
2019-01-12 04:44:29 +00:00
|
|
|
async def test_registering_new_device_view_with_name(hass, hass_client):
|
|
|
|
"""Test that the HTML view works with name attribute."""
|
|
|
|
client = await mock_client(hass, hass_client)
|
|
|
|
|
|
|
|
SUB_WITH_NAME = SUBSCRIPTION_1.copy()
|
2019-07-31 19:25:30 +00:00
|
|
|
SUB_WITH_NAME["name"] = "test device"
|
2019-01-12 04:44:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.html5.notify.save_json") as mock_save:
|
2019-01-12 04:44:29 +00:00
|
|
|
resp = await client.post(REGISTER_URL, data=json.dumps(SUB_WITH_NAME))
|
|
|
|
|
|
|
|
assert resp.status == 200
|
|
|
|
assert len(mock_save.mock_calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_save.mock_calls[0][1][1] == {"test device": SUBSCRIPTION_1}
|
2019-01-12 04:44:29 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_registering_new_device_expiration_view(hass, hass_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test that the HTML view works."""
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await mock_client(hass, hass_client)
|
2017-06-05 05:46:18 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.html5.notify.save_json") as mock_save:
|
2018-03-05 02:35:07 +00:00
|
|
|
resp = await client.post(REGISTER_URL, data=json.dumps(SUBSCRIPTION_4))
|
2017-06-05 05:46:18 +00:00
|
|
|
|
2018-02-15 21:06:14 +00:00
|
|
|
assert resp.status == 200
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_save.mock_calls[0][1][1] == {"unnamed device": SUBSCRIPTION_4}
|
2018-01-05 22:29:27 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_registering_new_device_fails_view(hass, hass_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test subs. are not altered when registering a new device fails."""
|
|
|
|
registrations = {}
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await mock_client(hass, hass_client, registrations)
|
2018-01-05 22:29:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.html5.notify.save_json",
|
|
|
|
side_effect=HomeAssistantError(),
|
|
|
|
):
|
2018-03-05 02:35:07 +00:00
|
|
|
resp = await client.post(REGISTER_URL, data=json.dumps(SUBSCRIPTION_4))
|
2018-01-05 22:29:27 +00:00
|
|
|
|
2020-04-08 21:20:03 +00:00
|
|
|
assert resp.status == HTTP_INTERNAL_SERVER_ERROR
|
2018-02-15 21:06:14 +00:00
|
|
|
assert registrations == {}
|
2018-01-05 22:29:27 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_registering_existing_device_view(hass, hass_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test subscription is updated when registering existing device."""
|
|
|
|
registrations = {}
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await mock_client(hass, hass_client, registrations)
|
2018-01-05 22:29:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.html5.notify.save_json") as mock_save:
|
2018-03-05 02:35:07 +00:00
|
|
|
await client.post(REGISTER_URL, data=json.dumps(SUBSCRIPTION_1))
|
|
|
|
resp = await client.post(REGISTER_URL, data=json.dumps(SUBSCRIPTION_4))
|
2018-01-05 22:29:27 +00:00
|
|
|
|
2018-02-15 21:06:14 +00:00
|
|
|
assert resp.status == 200
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_save.mock_calls[0][1][1] == {"unnamed device": SUBSCRIPTION_4}
|
|
|
|
assert registrations == {"unnamed device": SUBSCRIPTION_4}
|
2018-01-05 22:29:27 +00:00
|
|
|
|
|
|
|
|
2019-01-12 04:44:29 +00:00
|
|
|
async def test_registering_existing_device_view_with_name(hass, hass_client):
|
|
|
|
"""Test subscription is updated when reg'ing existing device with name."""
|
|
|
|
registrations = {}
|
|
|
|
client = await mock_client(hass, hass_client, registrations)
|
|
|
|
|
|
|
|
SUB_WITH_NAME = SUBSCRIPTION_1.copy()
|
2019-07-31 19:25:30 +00:00
|
|
|
SUB_WITH_NAME["name"] = "test device"
|
2019-01-12 04:44:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.html5.notify.save_json") as mock_save:
|
2019-01-12 04:44:29 +00:00
|
|
|
await client.post(REGISTER_URL, data=json.dumps(SUB_WITH_NAME))
|
|
|
|
resp = await client.post(REGISTER_URL, data=json.dumps(SUBSCRIPTION_4))
|
|
|
|
|
|
|
|
assert resp.status == 200
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_save.mock_calls[0][1][1] == {"test device": SUBSCRIPTION_4}
|
|
|
|
assert registrations == {"test device": SUBSCRIPTION_4}
|
2019-01-12 04:44:29 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_registering_existing_device_fails_view(hass, hass_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test sub. is not updated when registering existing device fails."""
|
|
|
|
registrations = {}
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await mock_client(hass, hass_client, registrations)
|
2018-01-05 22:29:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.html5.notify.save_json") as mock_save:
|
2018-03-05 02:35:07 +00:00
|
|
|
await client.post(REGISTER_URL, data=json.dumps(SUBSCRIPTION_1))
|
2018-02-15 21:06:14 +00:00
|
|
|
mock_save.side_effect = HomeAssistantError
|
2018-03-05 02:35:07 +00:00
|
|
|
resp = await client.post(REGISTER_URL, data=json.dumps(SUBSCRIPTION_4))
|
2018-01-05 22:29:27 +00:00
|
|
|
|
2020-04-08 21:20:03 +00:00
|
|
|
assert resp.status == HTTP_INTERNAL_SERVER_ERROR
|
2019-07-31 19:25:30 +00:00
|
|
|
assert registrations == {"unnamed device": SUBSCRIPTION_1}
|
2018-02-15 21:06:14 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_registering_new_device_validation(hass, hass_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test various errors when registering a new device."""
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await mock_client(hass, hass_client)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await client.post(
|
|
|
|
REGISTER_URL,
|
|
|
|
data=json.dumps({"browser": "invalid browser", "subscription": "sub info"}),
|
|
|
|
)
|
2018-02-15 21:06:14 +00:00
|
|
|
assert resp.status == 400
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await client.post(REGISTER_URL, data=json.dumps({"browser": "chrome"}))
|
2018-02-15 21:06:14 +00:00
|
|
|
assert resp.status == 400
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.html5.notify.save_json", return_value=False):
|
|
|
|
resp = await client.post(
|
|
|
|
REGISTER_URL,
|
|
|
|
data=json.dumps({"browser": "chrome", "subscription": "sub info"}),
|
|
|
|
)
|
2018-02-15 21:06:14 +00:00
|
|
|
assert resp.status == 400
|
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_unregistering_device_view(hass, hass_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test that the HTML unregister view works."""
|
2019-07-31 19:25:30 +00:00
|
|
|
registrations = {"some device": SUBSCRIPTION_1, "other device": SUBSCRIPTION_2}
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await mock_client(hass, hass_client, registrations)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.html5.notify.save_json") as mock_save:
|
|
|
|
resp = await client.delete(
|
|
|
|
REGISTER_URL,
|
|
|
|
data=json.dumps({"subscription": SUBSCRIPTION_1["subscription"]}),
|
|
|
|
)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
|
|
|
assert resp.status == 200
|
|
|
|
assert len(mock_save.mock_calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert registrations == {"other device": SUBSCRIPTION_2}
|
2018-02-15 21:06:14 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_unregister_device_view_handle_unknown_subscription(hass, hass_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test that the HTML unregister view handles unknown subscriptions."""
|
|
|
|
registrations = {}
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await mock_client(hass, hass_client, registrations)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.html5.notify.save_json") as mock_save:
|
|
|
|
resp = await client.delete(
|
|
|
|
REGISTER_URL,
|
|
|
|
data=json.dumps({"subscription": SUBSCRIPTION_3["subscription"]}),
|
|
|
|
)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
|
|
|
assert resp.status == 200, resp.response
|
|
|
|
assert registrations == {}
|
|
|
|
assert len(mock_save.mock_calls) == 0
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_unregistering_device_view_handles_save_error(hass, hass_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test that the HTML unregister view handles save errors."""
|
2019-07-31 19:25:30 +00:00
|
|
|
registrations = {"some device": SUBSCRIPTION_1, "other device": SUBSCRIPTION_2}
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await mock_client(hass, hass_client, registrations)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.html5.notify.save_json",
|
|
|
|
side_effect=HomeAssistantError(),
|
|
|
|
):
|
|
|
|
resp = await client.delete(
|
|
|
|
REGISTER_URL,
|
|
|
|
data=json.dumps({"subscription": SUBSCRIPTION_1["subscription"]}),
|
|
|
|
)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2020-04-08 21:20:03 +00:00
|
|
|
assert resp.status == HTTP_INTERNAL_SERVER_ERROR, resp.response
|
2018-02-15 21:06:14 +00:00
|
|
|
assert registrations == {
|
2019-07-31 19:25:30 +00:00
|
|
|
"some device": SUBSCRIPTION_1,
|
|
|
|
"other device": SUBSCRIPTION_2,
|
2018-02-15 21:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_callback_view_no_jwt(hass, hass_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test that the notification callback view works without JWT."""
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await mock_client(hass, hass_client)
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await client.post(
|
|
|
|
PUBLISH_URL,
|
|
|
|
data=json.dumps(
|
|
|
|
{"type": "push", "tag": "3bc28d69-0921-41f1-ac6a-7a627ba0aa72"}
|
|
|
|
),
|
|
|
|
)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
assert resp.status == 401
|
2018-02-15 21:06:14 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_callback_view_with_jwt(hass, hass_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test that the notification callback view works with JWT."""
|
2019-07-31 19:25:30 +00:00
|
|
|
registrations = {"device": SUBSCRIPTION_1}
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await mock_client(hass, hass_client, registrations)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2019-10-17 19:17:23 +00:00
|
|
|
with patch("homeassistant.components.html5.notify.WebPusher") as mock_wp:
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"notify",
|
|
|
|
"notify",
|
|
|
|
{"message": "Hello", "target": ["device"], "data": {"icon": "beer.png"}},
|
|
|
|
blocking=True,
|
|
|
|
)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
|
|
|
assert len(mock_wp.mock_calls) == 3
|
|
|
|
|
|
|
|
# WebPusher constructor
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_1["subscription"]
|
2018-02-15 21:06:14 +00:00
|
|
|
# Third mock_call checks the status_code of the response.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"
|
2018-02-15 21:06:14 +00:00
|
|
|
|
|
|
|
# Call to send
|
|
|
|
push_payload = json.loads(mock_wp.mock_calls[1][1][0])
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert push_payload["body"] == "Hello"
|
|
|
|
assert push_payload["icon"] == "beer.png"
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
bearer_token = "Bearer {}".format(push_payload["data"]["jwt"])
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await client.post(
|
|
|
|
PUBLISH_URL, json={"type": "push"}, headers={AUTHORIZATION: bearer_token}
|
|
|
|
)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
|
|
|
assert resp.status == 200
|
2018-03-05 02:35:07 +00:00
|
|
|
body = await resp.json()
|
2018-02-15 21:06:14 +00:00
|
|
|
assert body == {"event": "push", "status": "ok"}
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_send_fcm_without_targets(hass, hass_client):
|
|
|
|
"""Test that the notification is send with FCM without targets."""
|
2019-07-31 19:25:30 +00:00
|
|
|
registrations = {"device": SUBSCRIPTION_5}
|
2019-05-06 23:37:05 +00:00
|
|
|
await mock_client(hass, hass_client, registrations)
|
2019-10-17 19:17:23 +00:00
|
|
|
with patch("homeassistant.components.html5.notify.WebPusher") as mock_wp:
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"notify",
|
|
|
|
"notify",
|
|
|
|
{"message": "Hello", "target": ["device"], "data": {"icon": "beer.png"}},
|
|
|
|
blocking=True,
|
|
|
|
)
|
2019-05-06 23:37:05 +00:00
|
|
|
|
|
|
|
assert len(mock_wp.mock_calls) == 3
|
|
|
|
|
|
|
|
# WebPusher constructor
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_5["subscription"]
|
2019-05-06 23:37:05 +00:00
|
|
|
# Third mock_call checks the status_code of the response.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"
|