Skip to content

Commit

Permalink
Add basic tests for aws component
Browse files Browse the repository at this point in the history
  • Loading branch information
awarecan committed Mar 22, 2019
1 parent bea5961 commit f92db99
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 12 deletions.
21 changes: 9 additions & 12 deletions homeassistant/components/aws/__init__.py
Expand Up @@ -6,11 +6,7 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import (
ATTR_CREDENTIALS,
CONF_NAME,
CONF_PROFILE_NAME,
)
from homeassistant.const import ATTR_CREDENTIALS, CONF_NAME, CONF_PROFILE_NAME
from homeassistant.helpers import config_validation as cv, discovery

# Loading the config flow file will register the flow
Expand Down Expand Up @@ -39,13 +35,15 @@
}
)

DEFAULT_CREDENTIAL = [{CONF_NAME: "default", CONF_PROFILE_NAME: "default"}]

CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Optional(ATTR_CREDENTIALS): vol.All(
cv.ensure_list, [AWS_CREDENTIAL_SCHEMA]
),
vol.Optional(
ATTR_CREDENTIALS, default=DEFAULT_CREDENTIAL
): vol.All(cv.ensure_list, [AWS_CREDENTIAL_SCHEMA]),
vol.Optional(CONF_NOTIFY): vol.All(
cv.ensure_list, [NOTIFY_PLATFORM_SCHEMA]
),
Expand All @@ -63,9 +61,7 @@ async def async_setup(hass, config):
conf = config.get(DOMAIN)
if conf is None:
# create a default conf using default profile
conf = CONFIG_SCHEMA(
{CONF_NAME: "default", CONF_PROFILE_NAME: "default"}
)
conf = CONFIG_SCHEMA({ATTR_CREDENTIALS: DEFAULT_CREDENTIAL})

hass.data[DATA_CONFIG] = conf
hass.data[DATA_SESSIONS] = OrderedDict()
Expand Down Expand Up @@ -112,7 +108,8 @@ async def async_setup_entry(hass, entry):
name = conf[ATTR_CREDENTIALS][index][CONF_NAME]
if isinstance(result, Exception):
_LOGGER.error(
"Validating credential [%s] failed: %s", name, result
"Validating credential [%s] failed: %s",
name, result, exc_info=result
)
validation = False
else:
Expand Down
1 change: 1 addition & 0 deletions tests/components/aws/__init__.py
@@ -0,0 +1 @@
"""Tests for the aws component."""
130 changes: 130 additions & 0 deletions tests/components/aws/test_init.py
@@ -0,0 +1,130 @@
"""Tests for the aws component config and setup."""
from asynctest import patch as async_patch, MagicMock, CoroutineMock

from homeassistant.components import aws
from homeassistant.setup import async_setup_component


class MockAioSession:
"""Mock AioSession."""

def __init__(self, *args, **kwargs):
"""Init a mock session."""

def create_client(self, *args, **kwargs): # pylint: disable=no-self-use
"""Create a mocked client."""
return MagicMock(
__aenter__=CoroutineMock(return_value=CoroutineMock(
get_user=CoroutineMock(), # iam
invoke=CoroutineMock(), # lambda
publish=CoroutineMock(), # sns
send_message=CoroutineMock(), # sqs
)),
__aexit__=CoroutineMock()
)


async def test_empty_config(hass):
"""Test a default config will be create for empty config."""
with async_patch('aiobotocore.AioSession', new=MockAioSession):
await async_setup_component(hass, 'aws', {
'aws': {}
})
await hass.async_block_till_done()

sessions = hass.data[aws.DATA_SESSIONS]
assert sessions is not None
assert len(sessions) == 1
assert isinstance(sessions.get('default'), MockAioSession)


async def test_empty_credential(hass):
"""Test a default config will be create for empty credential section."""
with async_patch('aiobotocore.AioSession', new=MockAioSession):
await async_setup_component(hass, 'aws', {
'aws': {
'notify': [{
'service': 'lambda',
'name': 'New Lambda Test',
'region_name': 'us-east-1',
}]
}
})
await hass.async_block_till_done()

sessions = hass.data[aws.DATA_SESSIONS]
assert sessions is not None
assert len(sessions) == 1
assert isinstance(sessions.get('default'), MockAioSession)

assert hass.services.has_service('notify', 'new_lambda_test') is True
await hass.services.async_call(
'notify',
'new_lambda_test',
{'message': 'test', 'target': 'ARN'},
blocking=True
)


async def test_profile_credential(hass):
"""Test notify service can refer to credential name."""
with async_patch('aiobotocore.AioSession', new=MockAioSession):
await async_setup_component(hass, 'aws', {
'aws': {
'credentials': {
'name': 'test',
'profile_name': 'test-profile',
},
'notify': [{
'service': 'sns',
'credential_name': 'test',
'name': 'SNS Test',
'region_name': 'us-east-1',
}]
}
})
await hass.async_block_till_done()

sessions = hass.data[aws.DATA_SESSIONS]
assert sessions is not None
assert len(sessions) == 1
assert isinstance(sessions.get('test'), MockAioSession)

assert hass.services.has_service('notify', 'sns_test') is True
await hass.services.async_call(
'notify',
'sns_test',
{'title': 'test', 'message': 'test', 'target': 'ARN'},
blocking=True
)


async def test_notify_credential(hass):
"""Test notify service can refer to credential name."""
with async_patch('aiobotocore.AioSession', new=MockAioSession):
await async_setup_component(hass, 'aws', {
'aws': {
'notify': [{
'service': 'sqs',
'credential_name': 'test',
'name': 'SQS Test',
'region_name': 'us-east-1',
'aws_access_key_id': 'some-key',
'aws_secret_access_key': 'some-secret',
}]
}
})
await hass.async_block_till_done()

sessions = hass.data[aws.DATA_SESSIONS]
assert sessions is not None
assert len(sessions) == 1
assert isinstance(sessions.get('default'), MockAioSession)

assert hass.services.has_service('notify', 'sqs_test') is True
await hass.services.async_call(
'notify',
'sqs_test',
{'message': 'test', 'target': 'ARN'},
blocking=True
)

0 comments on commit f92db99

Please sign in to comment.