Add and use HTTP_FORBIDDEN constant (#33839)
This commit is contained in:
1 parent
70ee9d7f26
commit
bc26be3c11
15 files changed
+60
-37
No files matched your search
@@ -7,7 +7,13 @@ from asynctest import patch
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.components.airly.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
CONF_LATITUDE,
|
||||
CONF_LONGITUDE,
|
||||
CONF_NAME,
|
||||
HTTP_FORBIDDEN,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
|
||||
@@ -33,7 +39,9 @@ async def test_invalid_api_key(hass):
|
||||
"""Test that errors are shown when API key is invalid."""
|
||||
with patch(
|
||||
"airly._private._RequestsHandler.get",
|
||||
side_effect=AirlyError(403, {"message": "Invalid authentication credentials"}),
|
||||
side_effect=AirlyError(
|
||||
HTTP_FORBIDDEN, {"message": "Invalid authentication credentials"}
|
||||
),
|
||||
):
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
|
||||
@@ -12,6 +12,7 @@ from homeassistant.const import (
|
||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||
CONCENTRATION_PARTS_PER_BILLION,
|
||||
CONCENTRATION_PARTS_PER_MILLION,
|
||||
HTTP_FORBIDDEN,
|
||||
HTTP_INTERNAL_SERVER_ERROR,
|
||||
TEMP_CELSIUS,
|
||||
UNIT_PERCENTAGE,
|
||||
@@ -71,7 +72,7 @@ async def test_setup_permanent_error(hass, aioclient_mock):
|
||||
"""Expected failures caused by permanent errors in API response."""
|
||||
fake_async_add_entities = MagicMock()
|
||||
|
||||
errors = [400, 401, 403]
|
||||
errors = [400, 401, HTTP_FORBIDDEN]
|
||||
for error in errors:
|
||||
aioclient_mock.get(re.compile("api.foobot.io/v2/owner/.*"), status=error)
|
||||
result = await foobot.async_setup_platform(
|
||||
|
||||
@@ -20,6 +20,7 @@ from homeassistant.components.http.ban import (
|
||||
setup_bans,
|
||||
)
|
||||
from homeassistant.components.http.view import request_handler_factory
|
||||
from homeassistant.const import HTTP_FORBIDDEN
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import mock_real_ip
|
||||
@@ -55,12 +56,16 @@ async def test_access_from_banned_ip(hass, aiohttp_client):
|
||||
for remote_addr in BANNED_IPS:
|
||||
set_real_ip(remote_addr)
|
||||
resp = await client.get("/")
|
||||
assert resp.status == 403
|
||||
assert resp.status == HTTP_FORBIDDEN
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"remote_addr, bans, status",
|
||||
list(zip(BANNED_IPS_WITH_SUPERVISOR, [1, 1, 0], [403, 403, 401])),
|
||||
list(
|
||||
zip(
|
||||
BANNED_IPS_WITH_SUPERVISOR, [1, 1, 0], [HTTP_FORBIDDEN, HTTP_FORBIDDEN, 401]
|
||||
)
|
||||
),
|
||||
)
|
||||
async def test_access_from_supervisor_ip(
|
||||
remote_addr, bans, status, hass, aiohttp_client, hassio_env
|
||||
@@ -78,7 +83,7 @@ async def test_access_from_supervisor_ip(
|
||||
mock_real_ip(app)(remote_addr)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.http.ban.async_load_ip_bans_config", return_value=[],
|
||||
"homeassistant.components.http.ban.async_load_ip_bans_config", return_value=[]
|
||||
):
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
@@ -151,7 +156,7 @@ async def test_ip_bans_file_creation(hass, aiohttp_client):
|
||||
m_open.assert_called_once_with(hass.config.path(IP_BANS_FILE), "a")
|
||||
|
||||
resp = await client.get("/")
|
||||
assert resp.status == 403
|
||||
assert resp.status == HTTP_FORBIDDEN
|
||||
assert m_open.call_count == 1
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.melcloud.const import DOMAIN
|
||||
from homeassistant.const import HTTP_INTERNAL_SERVER_ERROR
|
||||
from homeassistant.const import HTTP_FORBIDDEN, HTTP_INTERNAL_SERVER_ERROR
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
@@ -92,7 +92,7 @@ async def test_form_errors(hass, mock_login, mock_get_devices, error, reason):
|
||||
"error,message",
|
||||
[
|
||||
(401, "invalid_auth"),
|
||||
(403, "invalid_auth"),
|
||||
(HTTP_FORBIDDEN, "invalid_auth"),
|
||||
(HTTP_INTERNAL_SERVER_ERROR, "cannot_connect"),
|
||||
],
|
||||
)
|
||||
|
||||
@@ -6,6 +6,7 @@ import pytest
|
||||
|
||||
from homeassistant.components import onboarding
|
||||
from homeassistant.components.onboarding import const, views
|
||||
from homeassistant.const import HTTP_FORBIDDEN
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import mock_storage
|
||||
@@ -65,7 +66,7 @@ async def test_onboarding_user_already_done(hass, hass_storage, aiohttp_client):
|
||||
},
|
||||
)
|
||||
|
||||
assert resp.status == 403
|
||||
assert resp.status == HTTP_FORBIDDEN
|
||||
|
||||
|
||||
async def test_onboarding_user(hass, hass_storage, aiohttp_client):
|
||||
@@ -179,7 +180,7 @@ async def test_onboarding_user_race(hass, hass_storage, aiohttp_client):
|
||||
|
||||
res1, res2 = await asyncio.gather(resp1, resp2)
|
||||
|
||||
assert sorted([res1.status, res2.status]) == [200, 403]
|
||||
assert sorted([res1.status, res2.status]) == [200, HTTP_FORBIDDEN]
|
||||
|
||||
|
||||
async def test_onboarding_integration(hass, hass_storage, hass_client):
|
||||
|
||||
@@ -15,7 +15,7 @@ from homeassistant.components.smartthings.const import (
|
||||
CONF_REFRESH_TOKEN,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.const import HTTP_NOT_FOUND
|
||||
from homeassistant.const import HTTP_FORBIDDEN, HTTP_NOT_FOUND
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry, mock_coro
|
||||
@@ -103,7 +103,7 @@ async def test_token_forbidden(hass, smartthings_mock):
|
||||
|
||||
request_info = Mock(real_url="http://example.com")
|
||||
smartthings_mock.apps.side_effect = ClientResponseError(
|
||||
request_info=request_info, history=None, status=403
|
||||
request_info=request_info, history=None, status=HTTP_FORBIDDEN
|
||||
)
|
||||
|
||||
result = await flow.async_step_user({"access_token": str(uuid4())})
|
||||
|
||||
@@ -17,7 +17,7 @@ from homeassistant.components.smartthings.const import (
|
||||
SIGNAL_SMARTTHINGS_UPDATE,
|
||||
SUPPORTED_PLATFORMS,
|
||||
)
|
||||
from homeassistant.const import HTTP_INTERNAL_SERVER_ERROR
|
||||
from homeassistant.const import HTTP_FORBIDDEN, HTTP_INTERNAL_SERVER_ERROR
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.setup import async_setup_component
|
||||
@@ -155,7 +155,7 @@ async def test_scenes_unauthorized_loads_platforms(
|
||||
smartthings_mock.installed_app.return_value = installed_app
|
||||
smartthings_mock.devices.return_value = [device]
|
||||
smartthings_mock.scenes.side_effect = ClientResponseError(
|
||||
request_info=request_info, history=None, status=403
|
||||
request_info=request_info, history=None, status=HTTP_FORBIDDEN
|
||||
)
|
||||
mock_token = Mock()
|
||||
mock_token.access_token = str(uuid4())
|
||||
@@ -307,10 +307,10 @@ async def test_remove_entry_already_deleted(hass, config_entry, smartthings_mock
|
||||
request_info = Mock(real_url="http://example.com")
|
||||
# Arrange
|
||||
smartthings_mock.delete_installed_app.side_effect = ClientResponseError(
|
||||
request_info=request_info, history=None, status=403
|
||||
request_info=request_info, history=None, status=HTTP_FORBIDDEN
|
||||
)
|
||||
smartthings_mock.delete_app.side_effect = ClientResponseError(
|
||||
request_info=request_info, history=None, status=403
|
||||
request_info=request_info, history=None, status=HTTP_FORBIDDEN
|
||||
)
|
||||
# Act
|
||||
await smartthings.async_remove_entry(hass, config_entry)
|
||||
|
||||
@@ -8,6 +8,7 @@ from homeassistant.components.media_player.const import (
|
||||
SERVICE_PLAY_MEDIA,
|
||||
)
|
||||
import homeassistant.components.tts as tts
|
||||
from homeassistant.const import HTTP_FORBIDDEN
|
||||
from homeassistant.setup import setup_component
|
||||
|
||||
from tests.common import assert_setup_component, get_test_home_assistant, mock_service
|
||||
@@ -198,7 +199,7 @@ class TestTTSYandexPlatform:
|
||||
"speed": 1,
|
||||
}
|
||||
aioclient_mock.get(
|
||||
self._base_url, status=403, content=b"test", params=url_param
|
||||
self._base_url, status=HTTP_FORBIDDEN, content=b"test", params=url_param
|
||||
)
|
||||
|
||||
config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}}
|
||||
|
||||
Reference in new issue
Block a user