Example #1
0
async def test_full_flow(
    hass, aiohttp_client, aioclient_mock, current_request_with_host
):
    """Check full flow."""
    assert await setup.async_setup_component(
        hass,
        DOMAIN,
        {
            DOMAIN: {
                "project_id": PROJECT_ID,
                "subscriber_id": SUBSCRIBER_ID,
                CONF_CLIENT_ID: CLIENT_ID,
                CONF_CLIENT_SECRET: CLIENT_SECRET,
            },
            "http": {"base_url": "https://example.com"},
        },
    )

    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER}
    )
    state = config_entry_oauth2_flow._encode_jwt(
        hass,
        {
            "flow_id": result["flow_id"],
            "redirect_uri": "https://example.com/auth/external/callback",
        },
    )

    oauth_authorize = OAUTH2_AUTHORIZE.format(project_id=PROJECT_ID)
    assert result["url"] == (
        f"{oauth_authorize}?response_type=code&client_id={CLIENT_ID}"
        "&redirect_uri=https://example.com/auth/external/callback"
        f"&state={state}&scope=https://www.googleapis.com/auth/sdm.service"
        "+https://www.googleapis.com/auth/pubsub"
        "&access_type=offline&prompt=consent"
    )

    client = await aiohttp_client(hass.http.app)
    resp = await client.get(f"/auth/external/callback?code=abcd&state={state}")
    assert resp.status == 200
    assert resp.headers["content-type"] == "text/html; charset=utf-8"

    aioclient_mock.post(
        OAUTH2_TOKEN,
        json={
            "refresh_token": "mock-refresh-token",
            "access_token": "mock-access-token",
            "type": "Bearer",
            "expires_in": 60,
        },
    )

    with patch(
        "homeassistant.components.nest.async_setup_entry", return_value=True
    ) as mock_setup:
        await hass.config_entries.flow.async_configure(result["flow_id"])

    assert len(hass.config_entries.async_entries(DOMAIN)) == 1
    assert len(mock_setup.mock_calls) == 1
Example #2
0
 def authorize_url(self, state: str, redirect_url: str) -> str:
     """Generate the expected authorization url."""
     oauth_authorize = OAUTH2_AUTHORIZE.format(project_id=PROJECT_ID)
     return (
         f"{oauth_authorize}?response_type=code&client_id={CLIENT_ID}"
         f"&redirect_uri={redirect_url}"
         f"&state={state}&scope=https://www.googleapis.com/auth/sdm.service"
         "+https://www.googleapis.com/auth/pubsub"
         "&access_type=offline&prompt=consent")
    async def async_oauth_flow(self, result):
        """Invoke the oauth flow with fake responses."""
        state = config_entry_oauth2_flow._encode_jwt(
            self.hass,
            {
                "flow_id": result["flow_id"],
                "redirect_uri": "https://example.com/auth/external/callback",
            },
        )

        oauth_authorize = OAUTH2_AUTHORIZE.format(project_id=PROJECT_ID)
        assert result["type"] == "external"
        assert result["url"] == (
            f"{oauth_authorize}?response_type=code&client_id={CLIENT_ID}"
            "&redirect_uri=https://example.com/auth/external/callback"
            f"&state={state}&scope=https://www.googleapis.com/auth/sdm.service"
            "+https://www.googleapis.com/auth/pubsub"
            "&access_type=offline&prompt=consent")

        client = await self.aiohttp_client(self.hass.http.app)
        resp = await client.get(
            f"/auth/external/callback?code=abcd&state={state}")
        assert resp.status == 200
        assert resp.headers["content-type"] == "text/html; charset=utf-8"

        self.aioclient_mock.post(
            OAUTH2_TOKEN,
            json={
                "refresh_token": "mock-refresh-token",
                "access_token": "mock-access-token",
                "type": "Bearer",
                "expires_in": 60,
            },
        )

        with patch("homeassistant.components.nest.async_setup_entry",
                   return_value=True) as mock_setup:
            await self.hass.config_entries.flow.async_configure(
                result["flow_id"])
            assert len(mock_setup.mock_calls) == 1