-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
140 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Tests for the aws component.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |