Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_get_gateway_fails_unauthorized(hass):
"""Failed call."""
with patch(
"pydeconz.DeconzSession.async_load_parameters",
side_effect=pydeconz.errors.Unauthorized,
), pytest.raises(deconz.errors.AuthenticationRequired):
assert (
await deconz.gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock())
is False
)
async def test_request_fails_raise_error() -> None:
"""Test a successful call of request."""
response = Mock()
response.content_type = "application/json"
response.json = CoroutineMock(
return_value=[
{"error": {"type": 1, "address": "address", "description": "description"}}
]
)
session = CoroutineMock(return_value=response)
with pytest.raises(errors.Unauthorized) as e_info:
await utils.async_request(session, "url")
assert str(e_info.value) == "address description"
session = aiohttp_client.async_get_clientsession(hass)
deconz = DeconzSession(
session,
config[CONF_HOST],
config[CONF_PORT],
config[CONF_API_KEY],
async_add_device=async_add_device_callback,
connection_status=async_connection_status_callback,
)
try:
with async_timeout.timeout(10):
await deconz.initialize()
return deconz
except errors.Unauthorized:
_LOGGER.warning("Invalid key for deCONZ at %s", config[CONF_HOST])
raise AuthenticationRequired
except (asyncio.TimeoutError, errors.RequestError):
_LOGGER.error("Error connecting to deCONZ gateway at %s", config[CONF_HOST])
raise CannotConnect
class Forbidden(pydeconzException):
"""The caller has no rights to access the requested URI."""
class ResourceNotFound(pydeconzException):
"""The requested resource (light, group, ...) was not found."""
class BridgeBusy(pydeconzException):
"""The Bridge is busy, too many requests (more than 20)."""
ERRORS = {
1: Unauthorized, # Unauthorized user
2: BadRequest, # Body contains invalid JSON
3: ResourceNotFound, # Resource not available
4: RequestError, # Method not available for resource
5: BadRequest, # Missing parameters in body
6: RequestError, # Parameter not available
7: RequestError, # Invalid value for parameter
8: RequestError, # Parameter is not modifiable
901: BridgeBusy, # May occur when sending too fast
}
def raise_error(error):
if error:
cls = ERRORS.get(error["type"], pydeconzException)
raise cls("{} {}".format(error["address"], error["description"]))