Esempio n. 1
0
async def test_on_link_failed(hass: HomeAssistant):
    """Test when we have errors during linking the router."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN,
        context={"source": SOURCE_USER},
        data={CONF_HOST: MOCK_HOST, CONF_PORT: MOCK_PORT},
    )

    with patch(
        "homeassistant.components.freebox.router.Freepybox.open",
        side_effect=AuthorizationError(),
    ):
        result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
        assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
        assert result["errors"] == {"base": "register_failed"}

    with patch(
        "homeassistant.components.freebox.router.Freepybox.open",
        side_effect=HttpRequestError(),
    ):
        result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
        assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
        assert result["errors"] == {"base": "cannot_connect"}

    with patch(
        "homeassistant.components.freebox.router.Freepybox.open",
        side_effect=InvalidTokenError(),
    ):
        result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
        assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
        assert result["errors"] == {"base": "unknown"}
Esempio n. 2
0
    async def _perform_request(self, verb, end_url, **kwargs):
        """
        Perform the given request, refreshing the session token if needed
        """
        if not self.session_token:
            await self._refresh_session_token()

        url = urljoin(self.base_url, end_url)
        request_params = {
            **kwargs,
            "headers": self._get_headers(),
            "timeout": self.timeout,
        }
        r = await verb(url, **request_params)

        # Return response if content is not json
        if r.content_type != "application/json":
            return r
        else:
            resp = await r.json()

            if resp.get("error_code") in ["auth_required", "invalid_session"]:
                logger.debug("Invalid session")
                await self._refresh_session_token()
                request_params["headers"] = self._get_headers()
                r = await verb(url, **request_params)
                resp = await r.json()

            if not resp["success"]:
                err_msg = "Request failed (APIResponse: {})".format(
                    json.dumps(resp))
                if resp.get("error_code") == "insufficient_rights":
                    raise InsufficientPermissionsError(err_msg)
                else:
                    raise HttpRequestError(err_msg)

            return resp["result"] if "result" in resp else None