Fixes notify.html5 for notifications on FireFox (#12993)

* Only pass the gcm_key when using Google Cloud Messaging as endpoint.

* Test if the gcm_key is only included for GCM endpoints.
pull/12996/head
corneyl 2018-03-09 02:50:17 +01:00 committed by Paulus Schoutsen
parent 19a529e917
commit 2ee73ca911
2 changed files with 44 additions and 3 deletions

View File

@ -404,8 +404,14 @@ class HTML5NotificationService(BaseNotificationService):
jwt_token = jwt.encode(jwt_claims, jwt_secret).decode('utf-8')
payload[ATTR_DATA][ATTR_JWT] = jwt_token
# Only pass the gcm key if we're actually using GCM
# If we don't, notifications break on FireFox
gcm_key = self._gcm_key \
if 'googleapis.com' in info[ATTR_SUBSCRIPTION][ATTR_ENDPOINT] \
else None
response = WebPusher(info[ATTR_SUBSCRIPTION]).send(
json.dumps(payload), gcm_key=self._gcm_key, ttl='86400')
json.dumps(payload), gcm_key=gcm_key, ttl='86400'
)
# pylint: disable=no-member
if response.status_code == 410:

View File

@ -12,7 +12,7 @@ CONFIG_FILE = 'file.conf'
SUBSCRIPTION_1 = {
'browser': 'chrome',
'subscription': {
'endpoint': 'https://google.com',
'endpoint': 'https://googleapis.com',
'keys': {'auth': 'auth', 'p256dh': 'p256dh'}
},
}
@ -39,7 +39,7 @@ SUBSCRIPTION_3 = {
SUBSCRIPTION_4 = {
'browser': 'chrome',
'subscription': {
'endpoint': 'https://google.com',
'endpoint': 'https://googleapis.com',
'expirationTime': None,
'keys': {'auth': 'auth', 'p256dh': 'p256dh'}
},
@ -115,6 +115,41 @@ class TestHtml5Notify(object):
assert payload['body'] == 'Hello'
assert payload['icon'] == 'beer.png'
@patch('pywebpush.WebPusher')
def test_gcm_key_include(self, mock_wp):
"""Test if the gcm_key is only included for GCM endpoints."""
hass = MagicMock()
data = {
'chrome': SUBSCRIPTION_1,
'firefox': SUBSCRIPTION_2
}
m = mock_open(read_data=json.dumps(data))
with patch('homeassistant.util.json.open', m, create=True):
service = html5.get_service(hass, {
'gcm_sender_id': '100',
'gcm_api_key': 'Y6i0JdZ0mj9LOaSI'
})
assert service is not None
service.send_message('Hello', target=['chrome', 'firefox'])
assert len(mock_wp.mock_calls) == 6
# WebPusher constructor
assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_1['subscription']
assert mock_wp.mock_calls[3][1][0] == SUBSCRIPTION_2['subscription']
# Third mock_call checks the status_code of the response.
assert mock_wp.mock_calls[2][0] == '().send().status_code.__eq__'
assert mock_wp.mock_calls[5][0] == '().send().status_code.__eq__'
# Get the keys passed to the WebPusher's send method
assert mock_wp.mock_calls[1][2]['gcm_key'] is not None
assert mock_wp.mock_calls[4][2]['gcm_key'] is None
async def test_registering_new_device_view(hass, test_client):
"""Test that the HTML view works."""