2018-10-10 12:07:51 +00:00
|
|
|
"""The tests for the hassio component."""
|
|
|
|
|
2021-10-23 18:34:53 +00:00
|
|
|
from http import HTTPStatus
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import Mock, patch
|
2018-10-10 12:07:51 +00:00
|
|
|
|
2021-01-01 21:31:56 +00:00
|
|
|
from homeassistant.auth.providers.homeassistant import InvalidAuth
|
2018-10-10 12:07:51 +00:00
|
|
|
|
|
|
|
|
2020-01-14 22:49:56 +00:00
|
|
|
async def test_auth_success(hass, hassio_client_supervisor):
|
2018-10-10 12:07:51 +00:00
|
|
|
"""Test no auth needed for ."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.auth.providers.homeassistant."
|
|
|
|
"HassAuthProvider.async_validate_login",
|
|
|
|
) as mock_login:
|
2020-01-14 22:49:56 +00:00
|
|
|
resp = await hassio_client_supervisor.post(
|
2019-07-31 19:25:30 +00:00
|
|
|
"/api/hassio_auth",
|
|
|
|
json={"username": "test", "password": "123456", "addon": "samba"},
|
2018-10-10 12:07:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Check we got right response
|
2021-10-23 18:34:53 +00:00
|
|
|
assert resp.status == HTTPStatus.OK
|
2018-10-10 12:07:51 +00:00
|
|
|
mock_login.assert_called_with("test", "123456")
|
|
|
|
|
|
|
|
|
2020-01-14 22:49:56 +00:00
|
|
|
async def test_auth_fails_no_supervisor(hass, hassio_client):
|
|
|
|
"""Test if only supervisor can access."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.auth.providers.homeassistant."
|
|
|
|
"HassAuthProvider.async_validate_login",
|
|
|
|
) as mock_login:
|
|
|
|
resp = await hassio_client.post(
|
|
|
|
"/api/hassio_auth",
|
|
|
|
json={"username": "test", "password": "123456", "addon": "samba"},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check we got right response
|
2021-10-23 18:34:53 +00:00
|
|
|
assert resp.status == HTTPStatus.UNAUTHORIZED
|
2020-01-14 22:49:56 +00:00
|
|
|
assert not mock_login.called
|
|
|
|
|
|
|
|
|
|
|
|
async def test_auth_fails_no_auth(hass, hassio_noauth_client):
|
|
|
|
"""Test if only supervisor can access."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.auth.providers.homeassistant."
|
|
|
|
"HassAuthProvider.async_validate_login",
|
|
|
|
) as mock_login:
|
|
|
|
resp = await hassio_noauth_client.post(
|
|
|
|
"/api/hassio_auth",
|
|
|
|
json={"username": "test", "password": "123456", "addon": "samba"},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check we got right response
|
2021-10-23 18:34:53 +00:00
|
|
|
assert resp.status == HTTPStatus.UNAUTHORIZED
|
2020-01-14 22:49:56 +00:00
|
|
|
assert not mock_login.called
|
|
|
|
|
|
|
|
|
|
|
|
async def test_login_error(hass, hassio_client_supervisor):
|
2018-10-10 12:07:51 +00:00
|
|
|
"""Test no auth needed for error."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.auth.providers.homeassistant."
|
|
|
|
"HassAuthProvider.async_validate_login",
|
2020-08-25 12:22:50 +00:00
|
|
|
Mock(side_effect=InvalidAuth()),
|
2019-07-31 19:25:30 +00:00
|
|
|
) as mock_login:
|
2020-01-14 22:49:56 +00:00
|
|
|
resp = await hassio_client_supervisor.post(
|
2019-07-31 19:25:30 +00:00
|
|
|
"/api/hassio_auth",
|
|
|
|
json={"username": "test", "password": "123456", "addon": "samba"},
|
2018-10-10 12:07:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Check we got right response
|
2021-10-23 18:34:53 +00:00
|
|
|
assert resp.status == HTTPStatus.NOT_FOUND
|
2018-10-10 12:07:51 +00:00
|
|
|
mock_login.assert_called_with("test", "123456")
|
|
|
|
|
|
|
|
|
2020-01-14 22:49:56 +00:00
|
|
|
async def test_login_no_data(hass, hassio_client_supervisor):
|
2018-10-10 12:07:51 +00:00
|
|
|
"""Test auth with no data -> error."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.auth.providers.homeassistant."
|
|
|
|
"HassAuthProvider.async_validate_login",
|
2020-08-25 12:22:50 +00:00
|
|
|
Mock(side_effect=InvalidAuth()),
|
2019-07-31 19:25:30 +00:00
|
|
|
) as mock_login:
|
2020-01-14 22:49:56 +00:00
|
|
|
resp = await hassio_client_supervisor.post("/api/hassio_auth")
|
2018-10-10 12:07:51 +00:00
|
|
|
|
|
|
|
# Check we got right response
|
2021-10-23 18:34:53 +00:00
|
|
|
assert resp.status == HTTPStatus.BAD_REQUEST
|
2018-10-10 12:07:51 +00:00
|
|
|
assert not mock_login.called
|
|
|
|
|
|
|
|
|
2020-01-14 22:49:56 +00:00
|
|
|
async def test_login_no_username(hass, hassio_client_supervisor):
|
2018-10-10 12:07:51 +00:00
|
|
|
"""Test auth with no username in data -> error."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.auth.providers.homeassistant."
|
|
|
|
"HassAuthProvider.async_validate_login",
|
2020-08-25 12:22:50 +00:00
|
|
|
Mock(side_effect=InvalidAuth()),
|
2019-07-31 19:25:30 +00:00
|
|
|
) as mock_login:
|
2020-01-14 22:49:56 +00:00
|
|
|
resp = await hassio_client_supervisor.post(
|
2019-10-14 21:56:45 +00:00
|
|
|
"/api/hassio_auth", json={"password": "123456", "addon": "samba"}
|
2018-10-10 12:07:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Check we got right response
|
2021-10-23 18:34:53 +00:00
|
|
|
assert resp.status == HTTPStatus.BAD_REQUEST
|
2018-10-10 12:07:51 +00:00
|
|
|
assert not mock_login.called
|
2018-10-11 08:55:38 +00:00
|
|
|
|
|
|
|
|
2020-01-14 22:49:56 +00:00
|
|
|
async def test_login_success_extra(hass, hassio_client_supervisor):
|
2018-10-11 08:55:38 +00:00
|
|
|
"""Test auth with extra data."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.auth.providers.homeassistant."
|
|
|
|
"HassAuthProvider.async_validate_login",
|
|
|
|
) as mock_login:
|
2020-01-14 22:49:56 +00:00
|
|
|
resp = await hassio_client_supervisor.post(
|
2019-07-31 19:25:30 +00:00
|
|
|
"/api/hassio_auth",
|
2018-10-11 08:55:38 +00:00
|
|
|
json={
|
|
|
|
"username": "test",
|
|
|
|
"password": "123456",
|
|
|
|
"addon": "samba",
|
|
|
|
"path": "/share",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check we got right response
|
2021-10-23 18:34:53 +00:00
|
|
|
assert resp.status == HTTPStatus.OK
|
2018-10-11 08:55:38 +00:00
|
|
|
mock_login.assert_called_with("test", "123456")
|
2020-01-14 22:49:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_password_success(hass, hassio_client_supervisor):
|
|
|
|
"""Test no auth needed for ."""
|
|
|
|
with patch(
|
2020-08-25 12:22:50 +00:00
|
|
|
"homeassistant.auth.providers.homeassistant."
|
|
|
|
"HassAuthProvider.async_change_password",
|
2020-01-14 22:49:56 +00:00
|
|
|
) as mock_change:
|
|
|
|
resp = await hassio_client_supervisor.post(
|
|
|
|
"/api/hassio_auth/password_reset",
|
|
|
|
json={"username": "test", "password": "123456"},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check we got right response
|
2021-10-23 18:34:53 +00:00
|
|
|
assert resp.status == HTTPStatus.OK
|
2020-01-14 22:49:56 +00:00
|
|
|
mock_change.assert_called_with("test", "123456")
|
|
|
|
|
|
|
|
|
|
|
|
async def test_password_fails_no_supervisor(hass, hassio_client):
|
|
|
|
"""Test if only supervisor can access."""
|
2020-08-25 12:22:50 +00:00
|
|
|
resp = await hassio_client.post(
|
|
|
|
"/api/hassio_auth/password_reset",
|
|
|
|
json={"username": "test", "password": "123456"},
|
|
|
|
)
|
2020-01-14 22:49:56 +00:00
|
|
|
|
2020-08-25 12:22:50 +00:00
|
|
|
# Check we got right response
|
2021-10-23 18:34:53 +00:00
|
|
|
assert resp.status == HTTPStatus.UNAUTHORIZED
|
2020-01-14 22:49:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_password_fails_no_auth(hass, hassio_noauth_client):
|
|
|
|
"""Test if only supervisor can access."""
|
2020-08-25 12:22:50 +00:00
|
|
|
resp = await hassio_noauth_client.post(
|
|
|
|
"/api/hassio_auth/password_reset",
|
|
|
|
json={"username": "test", "password": "123456"},
|
|
|
|
)
|
2020-01-14 22:49:56 +00:00
|
|
|
|
2020-08-25 12:22:50 +00:00
|
|
|
# Check we got right response
|
2021-10-23 18:34:53 +00:00
|
|
|
assert resp.status == HTTPStatus.UNAUTHORIZED
|
2020-01-14 22:49:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_password_no_user(hass, hassio_client_supervisor):
|
2020-08-25 12:22:50 +00:00
|
|
|
"""Test changing password for invalid user."""
|
|
|
|
resp = await hassio_client_supervisor.post(
|
|
|
|
"/api/hassio_auth/password_reset",
|
|
|
|
json={"username": "test", "password": "123456"},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check we got right response
|
2021-10-23 18:34:53 +00:00
|
|
|
assert resp.status == HTTPStatus.NOT_FOUND
|