2017-08-06 18:19:47 +00:00
|
|
|
"""The tests for the mailbox component."""
|
|
|
|
from hashlib import sha1
|
2021-10-22 21:54:36 +00:00
|
|
|
from http import HTTPStatus
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.bootstrap import async_setup_component
|
|
|
|
import homeassistant.components.mailbox as mailbox
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2018-12-02 15:32:53 +00:00
|
|
|
def mock_http_client(hass, hass_client):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Start the Home Assistant HTTP component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {mailbox.DOMAIN: {"platform": "demo"}}
|
|
|
|
hass.loop.run_until_complete(async_setup_component(hass, mailbox.DOMAIN, config))
|
2018-12-02 15:32:53 +00:00
|
|
|
return hass.loop.run_until_complete(hass_client())
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
async def test_get_platforms_from_mailbox(mock_http_client):
|
2017-08-06 18:19:47 +00:00
|
|
|
"""Get platforms from mailbox."""
|
|
|
|
url = "/api/mailbox/platforms"
|
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
req = await mock_http_client.get(url)
|
2021-10-22 21:54:36 +00:00
|
|
|
assert req.status == HTTPStatus.OK
|
2020-01-01 23:13:04 +00:00
|
|
|
result = await req.json()
|
2021-03-20 12:55:10 +00:00
|
|
|
assert len(result) == 1
|
|
|
|
assert result[0].get("name") == "DemoMailbox"
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
async def test_get_messages_from_mailbox(mock_http_client):
|
2017-08-06 18:19:47 +00:00
|
|
|
"""Get messages from mailbox."""
|
|
|
|
url = "/api/mailbox/messages/DemoMailbox"
|
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
req = await mock_http_client.get(url)
|
2021-10-22 21:54:36 +00:00
|
|
|
assert req.status == HTTPStatus.OK
|
2020-01-01 23:13:04 +00:00
|
|
|
result = await req.json()
|
2017-08-06 18:19:47 +00:00
|
|
|
assert len(result) == 10
|
|
|
|
|
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
async def test_get_media_from_mailbox(mock_http_client):
|
2017-08-06 18:19:47 +00:00
|
|
|
"""Get audio from mailbox."""
|
|
|
|
mp3sha = "3f67c4ea33b37d1710f772a26dd3fb43bb159d50"
|
2020-01-02 19:17:10 +00:00
|
|
|
msgtxt = "Message 1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
|
2019-07-31 19:25:30 +00:00
|
|
|
msgsha = sha1(msgtxt.encode("utf-8")).hexdigest()
|
2017-08-06 18:19:47 +00:00
|
|
|
|
2021-07-12 22:12:55 +00:00
|
|
|
url = f"/api/mailbox/media/DemoMailbox/{msgsha}"
|
2020-01-01 23:13:04 +00:00
|
|
|
req = await mock_http_client.get(url)
|
2021-10-22 21:54:36 +00:00
|
|
|
assert req.status == HTTPStatus.OK
|
2020-01-01 23:13:04 +00:00
|
|
|
data = await req.read()
|
2017-08-06 18:19:47 +00:00
|
|
|
assert sha1(data).hexdigest() == mp3sha
|
|
|
|
|
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
async def test_delete_from_mailbox(mock_http_client):
|
2017-08-06 18:19:47 +00:00
|
|
|
"""Get audio from mailbox."""
|
2020-01-02 19:17:10 +00:00
|
|
|
msgtxt1 = "Message 1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
|
|
|
|
msgtxt2 = "Message 3. Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
|
2019-07-31 19:25:30 +00:00
|
|
|
msgsha1 = sha1(msgtxt1.encode("utf-8")).hexdigest()
|
|
|
|
msgsha2 = sha1(msgtxt2.encode("utf-8")).hexdigest()
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
for msg in [msgsha1, msgsha2]:
|
2021-07-12 22:12:55 +00:00
|
|
|
url = f"/api/mailbox/delete/DemoMailbox/{msg}"
|
2020-01-01 23:13:04 +00:00
|
|
|
req = await mock_http_client.delete(url)
|
2021-10-22 21:54:36 +00:00
|
|
|
assert req.status == HTTPStatus.OK
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
url = "/api/mailbox/messages/DemoMailbox"
|
2020-01-01 23:13:04 +00:00
|
|
|
req = await mock_http_client.get(url)
|
2021-10-22 21:54:36 +00:00
|
|
|
assert req.status == HTTPStatus.OK
|
2020-01-01 23:13:04 +00:00
|
|
|
result = await req.json()
|
2017-08-06 18:19:47 +00:00
|
|
|
assert len(result) == 8
|
|
|
|
|
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
async def test_get_messages_from_invalid_mailbox(mock_http_client):
|
2017-08-06 18:19:47 +00:00
|
|
|
"""Get messages from mailbox."""
|
|
|
|
url = "/api/mailbox/messages/mailbox.invalid_mailbox"
|
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
req = await mock_http_client.get(url)
|
2021-10-22 21:54:36 +00:00
|
|
|
assert req.status == HTTPStatus.NOT_FOUND
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
async def test_get_media_from_invalid_mailbox(mock_http_client):
|
2017-08-06 18:19:47 +00:00
|
|
|
"""Get messages from mailbox."""
|
|
|
|
msgsha = "0000000000000000000000000000000000000000"
|
2021-07-12 22:12:55 +00:00
|
|
|
url = f"/api/mailbox/media/mailbox.invalid_mailbox/{msgsha}"
|
2017-08-06 18:19:47 +00:00
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
req = await mock_http_client.get(url)
|
2021-10-22 21:54:36 +00:00
|
|
|
assert req.status == HTTPStatus.NOT_FOUND
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
async def test_get_media_from_invalid_msgid(mock_http_client):
|
2017-08-06 18:19:47 +00:00
|
|
|
"""Get messages from mailbox."""
|
|
|
|
msgsha = "0000000000000000000000000000000000000000"
|
2021-07-12 22:12:55 +00:00
|
|
|
url = f"/api/mailbox/media/DemoMailbox/{msgsha}"
|
2017-08-06 18:19:47 +00:00
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
req = await mock_http_client.get(url)
|
2021-10-22 21:54:36 +00:00
|
|
|
assert req.status == HTTPStatus.INTERNAL_SERVER_ERROR
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
async def test_delete_from_invalid_mailbox(mock_http_client):
|
2017-08-06 18:19:47 +00:00
|
|
|
"""Get audio from mailbox."""
|
|
|
|
msgsha = "0000000000000000000000000000000000000000"
|
2021-07-12 22:12:55 +00:00
|
|
|
url = f"/api/mailbox/delete/mailbox.invalid_mailbox/{msgsha}"
|
2017-08-06 18:19:47 +00:00
|
|
|
|
2020-01-01 23:13:04 +00:00
|
|
|
req = await mock_http_client.delete(url)
|
2021-10-22 21:54:36 +00:00
|
|
|
assert req.status == HTTPStatus.NOT_FOUND
|