Add support for EventBridge to aws integration (#77573)

* Added EventBridge support to aws integration

* Added type hints for all aws notification services + Added unit tests for EventBridge AWS integration

* Increase line coverage for unit tests for aws integration.
This commit is contained in:
Parham Ghazanfari 2022-10-25 07:21:25 -04:00 committed by GitHub
parent aea0067e49
commit dbfca8def8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 169 additions and 5 deletions

View file

@ -1,5 +1,6 @@
"""Tests for the aws component config and setup."""
from unittest.mock import AsyncMock, MagicMock, patch as async_patch
import json
from unittest.mock import AsyncMock, MagicMock, call, patch as async_patch
from homeassistant.setup import async_setup_component
@ -13,6 +14,7 @@ class MockAioSession:
self.invoke = AsyncMock()
self.publish = AsyncMock()
self.send_message = AsyncMock()
self.put_events = AsyncMock()
def create_client(self, *args, **kwargs):
"""Create a mocked client."""
@ -23,6 +25,7 @@ class MockAioSession:
invoke=self.invoke, # lambda
publish=self.publish, # sns
send_message=self.send_message, # sqs
put_events=self.put_events, # events
)
),
__aexit__=AsyncMock(),
@ -289,3 +292,111 @@ async def test_service_call_extra_data(hass):
"AWS.SNS.SMS.SenderID": {"StringValue": "HA-notify", "DataType": "String"}
},
)
async def test_events_service_call(hass):
"""Test events service (EventBridge) call works as expected."""
mock_session = MockAioSession()
with async_patch(
"homeassistant.components.aws.AioSession", return_value=mock_session
):
await async_setup_component(
hass,
"aws",
{
"aws": {
"notify": [
{
"service": "events",
"name": "Events Test",
"region_name": "us-east-1",
}
]
}
},
)
await hass.async_block_till_done()
assert hass.services.has_service("notify", "events_test") is True
mock_session.put_events.return_value = {
"Entries": [{"EventId": "", "ErrorCode": 0, "ErrorMessage": "test-error"}]
}
await hass.services.async_call(
"notify",
"events_test",
{
"message": "test",
"target": "ARN",
"data": {},
},
blocking=True,
)
mock_session.put_events.assert_called_once_with(
Entries=[
{
"EventBusName": "ARN",
"Detail": json.dumps({"message": "test"}),
"DetailType": "",
"Source": "homeassistant",
"Resources": [],
}
]
)
async def test_events_service_call_10_targets(hass):
"""Test events service (EventBridge) call works with more than 10 targets."""
mock_session = MockAioSession()
with async_patch(
"homeassistant.components.aws.AioSession", return_value=mock_session
):
await async_setup_component(
hass,
"aws",
{
"aws": {
"notify": [
{
"service": "events",
"name": "Events Test",
"region_name": "us-east-1",
}
]
}
},
)
await hass.async_block_till_done()
assert hass.services.has_service("notify", "events_test") is True
await hass.services.async_call(
"notify",
"events_test",
{
"message": "",
"target": [f"eventbus{i}" for i in range(11)],
"data": {
"detail_type": "test_event",
"detail": {"eventkey": "eventvalue"},
"source": "HomeAssistant-test",
"resources": ["resource1", "resource2"],
},
},
blocking=True,
)
entry = {
"Detail": json.dumps({"eventkey": "eventvalue"}),
"DetailType": "test_event",
"Source": "HomeAssistant-test",
"Resources": ["resource1", "resource2"],
}
mock_session.put_events.assert_has_calls(
[
call(Entries=[entry | {"EventBusName": f"eventbus{i}"} for i in range(10)]),
call(Entries=[entry | {"EventBusName": "eventbus10"}]),
]
)