Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Nov 25, 2018
1 parent 428fb89 commit 34e1f5f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
4 changes: 2 additions & 2 deletions homeassistant/components/cloud/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def __init__(self, cloud):

async def async_publish_webhooks(self):
"""Inform the Relayer of the webhooks that we expect."""
webhooks = self.cloud.prefs.webhooks
await self.cloud.iot.async_send_message('webhook-register', {
'webhook_ids': [info['cloud_id'] for info
in self.cloud.prefs.webhooks.values()]
'webhook_ids': [info['cloud_id'] for info in webhooks.values()]
})

async def async_enable(self, webhook_id):
Expand Down
68 changes: 60 additions & 8 deletions tests/components/cloud/test_webhooks.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,69 @@
"""Test cloud webhooks."""
from unittest.mock import Mock, patch

import pytest

from homeassistant.components.cloud import prefs, webhooks

from tests.common import mock_coro


def test_publish_webhooks_on_connect():
"""Test that we publish webhooks when connected to cloud."""
assert False
@pytest.fixture
def mock_webhooks(hass):
cloud = Mock()
cloud.hass = hass
cloud.hass.async_add_executor_job = Mock(return_value=mock_coro())
cloud.iot = Mock(async_send_message=Mock(return_value=mock_coro()))
cloud.webhook_create_url = 'https://webhook-create.url'
cloud.prefs = prefs.CloudPreferences(hass)
hass.loop.run_until_complete(cloud.prefs.async_initialize(True))
return webhooks.Webhooks(cloud)


def test_enable():
async def test_enable(mock_webhooks, aioclient_mock):
"""Test enabling webhooks."""
assert False
aioclient_mock.post('https://webhook-create.url', json={
'webhook_id': 'mock-cloud-id',
'url': 'https://hooks.nabu.casa/ZXCZCXZ',
})

hook = {
'webhook_id': 'mock-webhook-id',
'cloud_id': 'mock-cloud-id',
'cloud_url': 'https://hooks.nabu.casa/ZXCZCXZ',
}

assert hook == await mock_webhooks.async_enable('mock-webhook-id')

assert mock_webhooks.cloud.prefs.webhooks == {
'mock-webhook-id': hook
}

publish_calls = mock_webhooks.cloud.iot.async_send_message.mock_calls
assert len(publish_calls) == 1
assert publish_calls[0][1][0] == 'webhook-register'
assert publish_calls[0][1][1] == {
'webhook_ids': ['mock-cloud-id']
}


async def test_disable(mock_webhooks):
"""Test disabling webhooks."""
mock_webhooks.cloud.prefs._prefs['webhooks'] = {
'mock-webhook-id': {
'webhook_id': 'mock-webhook-id',
'cloud_id': 'mock-cloud-id',
'cloud_url': 'https://hooks.nabu.casa/ZXCZCXZ',
}
}

await mock_webhooks.async_disable('mock-webhook-id')

assert mock_webhooks.cloud.prefs.webhooks == {}

def test_disable():
"""Test webhooks."""
assert False
publish_calls = mock_webhooks.cloud.iot.async_send_message.mock_calls
assert len(publish_calls) == 1
assert publish_calls[0][1][0] == 'webhook-register'
assert publish_calls[0][1][1] == {
'webhook_ids': []
}

0 comments on commit 34e1f5f

Please sign in to comment.