async def test_full_flow_implementation(opp, aiohttp_client, aioclient_mock, current_request_with_host): """Test registering an integration and finishing flow works.""" await setup_component(opp) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "pick_implementation" # pylint: disable=protected-access state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) result2 = await opp.config_entries.flow.async_configure( result["flow_id"], {"implementation": "eneco"}) assert result2["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP assert result2["url"] == ( "https://api.toon.eu/authorize" "?response_type=code&client_id=client" "&redirect_uri=https://example.com/auth/external/callback" f"&state={state}" "&tenant_id=eneco&issuer=identity.toon.eu") client = await aiohttp_client(opp.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( "https://api.toon.eu/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) with patch("toonapi.Toon.agreements", return_value=[Agreement(agreement_id=123)]): result3 = await opp.config_entries.flow.async_configure( result["flow_id"]) assert result3["data"]["auth_implementation"] == "eneco" assert result3["data"]["agreement_id"] == 123 result3["data"]["token"].pop("expires_at") assert result3["data"]["token"] == { "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }
async def test_toon_abort(opp, aiohttp_client, aioclient_mock, current_request_with_host): """Test we abort on Toon error.""" await setup_component(opp) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}) # pylint: disable=protected-access state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) await opp.config_entries.flow.async_configure(result["flow_id"], {"implementation": "eneco"}) client = await aiohttp_client(opp.http.app) await client.get(f"/auth/external/callback?code=abcd&state={state}") aioclient_mock.post( "https://api.toon.eu/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) with patch("toonapi.Toon.agreements", side_effect=ToonError): result2 = await opp.config_entries.flow.async_configure( result["flow_id"]) assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result2["reason"] == "connection_error"
async def test_full_flow( opp: OpenPeerPower, aiohttp_client, aioclient_mock, current_request_with_host, ) -> None: """Check full flow.""" assert await setup.async_setup_component( opp, "NEW_DOMAIN", { "NEW_DOMAIN": { "client_id": CLIENT_ID, "client_secret": CLIENT_SECRET }, "http": { "base_url": "https://example.com" }, }, ) result = await opp.config_entries.flow.async_init( "NEW_DOMAIN", context={"source": config_entries.SOURCE_USER}) state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) assert result["url"] == ( f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}" "&redirect_uri=https://example.com/auth/external/callback" f"&state={state}") client = await aiohttp_client(opp.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("openpeerpower.components.NEW_DOMAIN.async_setup_entry", return_value=True) as mock_setup: await opp.config_entries.flow.async_configure(result["flow_id"]) assert len(opp.config_entries.async_entries(DOMAIN)) == 1 assert len(mock_setup.mock_calls) == 1
async def test_abort_if_oauth_error( opp, flow_handler, local_impl, aiohttp_client, aioclient_mock, current_request_with_host, ): """Check bad oauth token.""" flow_handler.async_register_implementation(opp, local_impl) config_entry_oauth2_flow.async_register_implementation( opp, TEST_DOMAIN, MockOAuth2Implementation()) result = await opp.config_entries.flow.async_init( TEST_DOMAIN, context={"source": config_entries.SOURCE_USER}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "pick_implementation" # Pick implementation result = await opp.config_entries.flow.async_configure( result["flow_id"], user_input={"implementation": TEST_DOMAIN}) state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP assert result["url"] == ( f"{AUTHORIZE_URL}?response_type=code&client_id={CLIENT_ID}" "&redirect_uri=https://example.com/auth/external/callback" f"&state={state}&scope=read+write") client = await aiohttp_client(opp.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( TOKEN_URL, json={ "refresh_token": REFRESH_TOKEN, "access_token": ACCESS_TOKEN_1, "type": "bearer", "expires_in": "badnumber", }, ) result = await opp.config_entries.flow.async_configure(result["flow_id"]) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "oauth_error"
async def test_reauth_account_mismatch( opp, aiohttp_client, aioclient_mock, current_request_with_host ): """Test Spotify reauthentication with different account.""" await setup.async_setup_component( opp, DOMAIN, { DOMAIN: {CONF_CLIENT_ID: "client", CONF_CLIENT_SECRET: "secret"}, "http": {"base_url": "https://example.com"}, }, ) old_entry = MockConfigEntry( domain=DOMAIN, unique_id=123, version=1, data={"id": "frenck", "auth_implementation": DOMAIN}, ) old_entry.add_to_opp(opp) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_REAUTH}, data=old_entry.data ) flows = opp.config_entries.flow.async_progress() result = await opp.config_entries.flow.async_configure(flows[0]["flow_id"], {}) # pylint: disable=protected-access state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) client = await aiohttp_client(opp.http.app) await client.get(f"/auth/external/callback?code=abcd&state={state}") aioclient_mock.post( "https://accounts.spotify.com/api/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) with patch("openpeerpower.components.spotify.config_flow.Spotify") as spotify_mock: spotify_mock.return_value.current_user.return_value = {"id": "fake_id"} result = await opp.config_entries.flow.async_configure(result["flow_id"]) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "reauth_account_mismatch"
async def test_full_user_flow( opp, aiohttp_client, aioclient_mock, current_request_with_host ): """Check full flow.""" assert await setup.async_setup_component( opp, DOMAIN, { DOMAIN: {CONF_CLIENT_ID: CLIENT_ID, CONF_CLIENT_SECRET: CLIENT_SECRET}, "http": {"base_url": "https://example.com"}, }, ) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) result = await opp.config_entries.flow.async_configure( result["flow_id"], {"environment": ENV_CLOUD} ) state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) client = await aiohttp_client(opp.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( TOKEN_URL["PRODUCTION"], json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) with patch( "openpeerpower.components.smappee.async_setup_entry", return_value=True ) as mock_setup: await opp.config_entries.flow.async_configure(result["flow_id"]) assert len(opp.config_entries.async_entries(DOMAIN)) == 1 assert len(mock_setup.mock_calls) == 1
async def test_full_flow(opp, aiohttp_client, aioclient_mock): """Check full flow.""" assert await setup.async_setup_component( opp, "almond", { "almond": { "type": "oauth2", "client_id": CLIENT_ID_VALUE, "client_secret": CLIENT_SECRET_VALUE, }, "http": {"base_url": "https://example.com"}, }, ) result = await opp.config_entries.flow.async_init( "almond", context={"source": config_entries.SOURCE_USER} ) state = config_entry_oauth2_flow._encode_jwt(opp, {"flow_id": result["flow_id"]}) assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP assert result["url"] == ( "https://almond.stanford.edu/me/api/oauth2/authorize" f"?response_type=code&client_id={CLIENT_ID_VALUE}" "&redirect_uri=https://example.com/auth/external/callback" f"&state={state}&scope=profile+user-read+user-read-results+user-exec-command" ) client = await aiohttp_client(opp.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( "https://almond.stanford.edu/me/api/oauth2/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) result = await opp.config_entries.flow.async_configure(result["flow_id"]) assert len(opp.config_entries.async_entries(DOMAIN)) == 1 entry = opp.config_entries.async_entries(DOMAIN)[0] assert entry.data["type"] == "oauth2" assert entry.data["host"] == "https://almond.stanford.edu/me"
async def test_import_migration(opp, aiohttp_client, aioclient_mock, current_request_with_host): """Test if importing step with migration works.""" old_entry = MockConfigEntry(domain=DOMAIN, unique_id=123, version=1) old_entry.add_to_opp(opp) await setup_component(opp) entries = opp.config_entries.async_entries(DOMAIN) assert len(entries) == 1 assert entries[0].version == 1 flows = opp.config_entries.flow.async_progress() assert len(flows) == 1 assert flows[0]["context"][CONF_MIGRATE] == old_entry.entry_id # pylint: disable=protected-access state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": flows[0]["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) await opp.config_entries.flow.async_configure(flows[0]["flow_id"], {"implementation": "eneco"}) client = await aiohttp_client(opp.http.app) await client.get(f"/auth/external/callback?code=abcd&state={state}") aioclient_mock.post( "https://api.toon.eu/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) with patch("toonapi.Toon.agreements", return_value=[Agreement(agreement_id=123)]): result = await opp.config_entries.flow.async_configure( flows[0]["flow_id"]) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY entries = opp.config_entries.async_entries(DOMAIN) assert len(entries) == 1 assert entries[0].version == 2
async def test_multiple_agreements(opp, aiohttp_client, aioclient_mock, current_request_with_host): """Test abort when there are no displays.""" await setup_component(opp) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}) # pylint: disable=protected-access state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) await opp.config_entries.flow.async_configure(result["flow_id"], {"implementation": "eneco"}) client = await aiohttp_client(opp.http.app) await client.get(f"/auth/external/callback?code=abcd&state={state}") aioclient_mock.post( "https://api.toon.eu/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) with patch( "toonapi.Toon.agreements", return_value=[ Agreement(agreement_id=1), Agreement(agreement_id=2) ], ): result3 = await opp.config_entries.flow.async_configure( result["flow_id"]) assert result3["type"] == data_entry_flow.RESULT_TYPE_FORM assert result3["step_id"] == "agreement" result4 = await opp.config_entries.flow.async_configure( result["flow_id"], {CONF_AGREEMENT: "None None, None"}) assert result4["data"]["auth_implementation"] == "eneco" assert result4["data"]["agreement_id"] == 1
async def test_abort_if_spotify_error( opp, aiohttp_client, aioclient_mock, current_request_with_host ): """Check Spotify errors causes flow to abort.""" await setup.async_setup_component( opp, DOMAIN, { DOMAIN: {CONF_CLIENT_ID: "client", CONF_CLIENT_SECRET: "secret"}, "http": {"base_url": "https://example.com"}, }, ) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} ) # pylint: disable=protected-access state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) client = await aiohttp_client(opp.http.app) await client.get(f"/auth/external/callback?code=abcd&state={state}") aioclient_mock.post( "https://accounts.spotify.com/api/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) with patch( "openpeerpower.components.spotify.config_flow.Spotify.current_user", side_effect=SpotifyException(400, -1, "message"), ): result = await opp.config_entries.flow.async_configure(result["flow_id"]) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "connection_error"
async def test_agreement_already_set_up(opp, aiohttp_client, aioclient_mock, current_request_with_host): """Test showing display form again if display already exists.""" await setup_component(opp) MockConfigEntry(domain=DOMAIN, unique_id=123).add_to_opp(opp) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}) # pylint: disable=protected-access state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) await opp.config_entries.flow.async_configure(result["flow_id"], {"implementation": "eneco"}) client = await aiohttp_client(opp.http.app) await client.get(f"/auth/external/callback?code=abcd&state={state}") aioclient_mock.post( "https://api.toon.eu/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) with patch("toonapi.Toon.agreements", return_value=[Agreement(agreement_id=123)]): result3 = await opp.config_entries.flow.async_configure( result["flow_id"]) assert result3["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result3["reason"] == "already_configured"
async def setup_profile(self, user_id: int) -> ConfigEntryWithingsApi: """Set up a user profile through config flows.""" profile_config = next( iter([ profile_config for profile_config in self._profile_configs if profile_config.user_id == user_id ])) api_mock: ConfigEntryWithingsApi = MagicMock( spec=ConfigEntryWithingsApi) ComponentFactory._setup_api_method( api_mock.user_get_device, profile_config.api_response_user_get_device) ComponentFactory._setup_api_method( api_mock.sleep_get_summary, profile_config.api_response_sleep_get_summary) ComponentFactory._setup_api_method( api_mock.measure_get_meas, profile_config.api_response_measure_get_meas) ComponentFactory._setup_api_method( api_mock.notify_list, profile_config.api_response_notify_list) ComponentFactory._setup_api_method( api_mock.notify_revoke, profile_config.api_response_notify_revoke) self._api_class_mock.reset_mocks() self._api_class_mock.return_value = api_mock # Get the withings config flow. result = await self._opp.config_entries.flow.async_init( const.DOMAIN, context={"source": SOURCE_USER}) assert result # pylint: disable=protected-access state = config_entry_oauth2_flow._encode_jwt( self._opp, { "flow_id": result["flow_id"], "redirect_uri": "http://127.0.0.1:8080/auth/external/callback", }, ) assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP assert result["url"] == ( "https://account.withings.com/oauth2_user/authorize2?" f"response_type=code&client_id={self._client_id}&" "redirect_uri=http://127.0.0.1:8080/auth/external/callback&" f"state={state}" "&scope=user.info,user.metrics,user.activity,user.sleepevents") # Simulate user being redirected from withings site. client: TestClient = await self._aiohttp_client(self._opp.http.app) resp = await client.get(f"{AUTH_CALLBACK_PATH}?code=abcd&state={state}" ) assert resp.status == 200 assert resp.headers["content-type"] == "text/html; charset=utf-8" self._aioclient_mock.clear_requests() self._aioclient_mock.post( "https://account.withings.com/oauth2/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, "userid": profile_config.user_id, }, ) # Present user with a list of profiles to choose from. result = await self._opp.config_entries.flow.async_configure( result["flow_id"]) assert result.get("type") == "form" assert result.get("step_id") == "profile" assert "profile" in result.get("data_schema").schema # Provide the user profile. result = await self._opp.config_entries.flow.async_configure( result["flow_id"], {const.PROFILE: profile_config.profile}) # Finish the config flow by calling it again. assert result.get("type") == "create_entry" assert result.get("result") config_data = result.get("result").data assert config_data.get(const.PROFILE) == profile_config.profile assert config_data.get("auth_implementation") == const.DOMAIN assert config_data.get("token") # Wait for remaining tasks to complete. await self._opp.async_block_till_done() # Mock the webhook. data_manager = get_data_manager_by_user_id(self._opp, user_id) self._aioclient_mock.clear_requests() self._aioclient_mock.request( "HEAD", data_manager.webhook_config.url, ) return self._api_class_mock.return_value
async def test_full_flow(opp, aiohttp_client, aioclient_mock, current_request_with_host): """Check full flow.""" assert await setup.async_setup_component( opp, DOMAIN, { DOMAIN: { CONF_CLIENT_ID: CLIENT_ID, CONF_CLIENT_SECRET: CLIENT_SECRET, }, DOMAIN_HTTP: { CONF_BASE_URL: "https://example.com" }, }, ) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}) state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP assert result["url"] == ( f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}" "&redirect_uri=https://example.com/auth/external/callback" f"&state={state}") client = await aiohttp_client(opp.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("openpeerpower.components.lyric.api.ConfigEntryLyricClient" ), patch("openpeerpower.components.lyric.async_setup_entry", return_value=True) as mock_setup: result = await opp.config_entries.flow.async_configure( result["flow_id"]) assert result["data"]["auth_implementation"] == DOMAIN result["data"]["token"].pop("expires_at") assert result["data"]["token"] == { "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, } assert DOMAIN in opp.config.components entry = opp.config_entries.async_entries(DOMAIN)[0] assert entry.state is config_entries.ConfigEntryState.LOADED assert len(opp.config_entries.async_entries(DOMAIN)) == 1 assert len(mock_setup.mock_calls) == 1
async def test_reauthentication_flow(opp, aiohttp_client, aioclient_mock, current_request_with_host): """Test reauthentication flow.""" await setup.async_setup_component( opp, DOMAIN, { DOMAIN: { CONF_CLIENT_ID: CLIENT_ID, CONF_CLIENT_SECRET: CLIENT_SECRET, }, DOMAIN_HTTP: { CONF_BASE_URL: "https://example.com" }, }, ) old_entry = MockConfigEntry( domain=DOMAIN, unique_id=DOMAIN, version=1, data={ "id": "timmo", "auth_implementation": DOMAIN }, ) old_entry.add_to_opp(opp) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=old_entry.data) flows = opp.config_entries.flow.async_progress() assert len(flows) == 1 result = await opp.config_entries.flow.async_configure( flows[0]["flow_id"], {}) # pylint: disable=protected-access state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) client = await aiohttp_client(opp.http.app) await client.get(f"/auth/external/callback?code=abcd&state={state}") aioclient_mock.post( OAUTH2_TOKEN, json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) with patch("openpeerpower.components.lyric.api.ConfigEntryLyricClient"): with patch("openpeerpower.components.lyric.async_setup_entry", return_value=True) as mock_setup: result = await opp.config_entries.flow.async_configure( result["flow_id"]) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "reauth_successful" assert len(mock_setup.mock_calls) == 1
async def test_full_flow(opp, aiohttp_client, aioclient_mock, current_request_with_host): """Check full flow.""" assert await setup.async_setup_component( opp, DOMAIN, { DOMAIN: { CONF_CLIENT_ID: CLIENT_ID_VALUE, CONF_CLIENT_SECRET: CLIENT_SECRET_VALUE, } }, ) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}) state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP assert result["url"] == ( "https://accounts.somfy.com/oauth/oauth/v2/auth" f"?response_type=code&client_id={CLIENT_ID_VALUE}" "&redirect_uri=https://example.com/auth/external/callback" f"&state={state}") client = await aiohttp_client(opp.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( "https://accounts.somfy.com/oauth/oauth/v2/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) with patch("openpeerpower.components.somfy.api.ConfigEntrySomfyApi"): result = await opp.config_entries.flow.async_configure( result["flow_id"]) assert result["data"]["auth_implementation"] == DOMAIN result["data"]["token"].pop("expires_at") assert result["data"]["token"] == { "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, } assert DOMAIN in opp.config.components entry = opp.config_entries.async_entries(DOMAIN)[0] assert entry.state is config_entries.ConfigEntryState.LOADED assert await opp.config_entries.async_unload(entry.entry_id) assert entry.state is config_entries.ConfigEntryState.NOT_LOADED
async def test_full_flow( opp, aiohttp_client, aioclient_mock, current_request_with_host ): """Check a full flow.""" assert await setup.async_setup_component( opp, DOMAIN, { DOMAIN: {CONF_CLIENT_ID: "client", CONF_CLIENT_SECRET: "secret"}, "http": {"base_url": "https://example.com"}, }, ) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} ) # pylint: disable=protected-access state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP assert result["url"] == ( "https://accounts.spotify.com/authorize" "?response_type=code&client_id=client" "&redirect_uri=https://example.com/auth/external/callback" f"&state={state}" "&scope=user-modify-playback-state,user-read-playback-state,user-read-private," "playlist-read-private,playlist-read-collaborative,user-library-read," "user-top-read,user-read-playback-position,user-read-recently-played,user-follow-read" ) client = await aiohttp_client(opp.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( "https://accounts.spotify.com/api/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) with patch("openpeerpower.components.spotify.config_flow.Spotify") as spotify_mock: spotify_mock.return_value.current_user.return_value = { "id": "fake_id", "display_name": "frenck", } result = await opp.config_entries.flow.async_configure(result["flow_id"]) assert result["data"]["auth_implementation"] == DOMAIN result["data"]["token"].pop("expires_at") assert result["data"]["name"] == "frenck" assert result["data"]["token"] == { "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }
async def test_config_reauth_profile(opp: OpenPeerPower, aiohttp_client, aioclient_mock, current_request_with_host) -> None: """Test reauth an existing profile re-creates the config entry.""" opp_config = { HA_DOMAIN: { CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_METRIC, CONF_EXTERNAL_URL: "http://127.0.0.1:8080/", }, const.DOMAIN: { CONF_CLIENT_ID: "my_client_id", CONF_CLIENT_SECRET: "my_client_secret", const.CONF_USE_WEBHOOK: False, }, } await async_process_op_core_config(opp, opp_config.get(HA_DOMAIN)) assert await async_setup_component(opp, const.DOMAIN, opp_config) await opp.async_block_till_done() config_entry = MockConfigEntry(domain=const.DOMAIN, data={const.PROFILE: "person0"}, unique_id="0") config_entry.add_to_opp(opp) result = await opp.config_entries.flow.async_init( const.DOMAIN, context={ "source": config_entries.SOURCE_REAUTH, "profile": "person0" }, ) assert result assert result["type"] == "form" assert result["step_id"] == "reauth" assert result["description_placeholders"] == {const.PROFILE: "person0"} result = await opp.config_entries.flow.async_configure( result["flow_id"], {}, ) # pylint: disable=protected-access state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) client: TestClient = await aiohttp_client(opp.http.app) resp = await client.get(f"{AUTH_CALLBACK_PATH}?code=abcd&state={state}") assert resp.status == 200 assert resp.headers["content-type"] == "text/html; charset=utf-8" aioclient_mock.clear_requests() aioclient_mock.post( "https://account.withings.com/oauth2/token", json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, "userid": "0", }, ) result = await opp.config_entries.flow.async_configure(result["flow_id"]) assert result assert result["type"] == "abort" assert result["reason"] == "already_configured" entries = opp.config_entries.async_entries(const.DOMAIN) assert entries assert entries[0].data["token"]["refresh_token"] == "mock-refresh-token"
async def test_reauth(opp: OpenPeerPower, aiohttp_client, aioclient_mock, current_request_with_host): """Test initialization of the reauth flow.""" assert await setup.async_setup_component( opp, "neato", { "neato": { "client_id": CLIENT_ID, "client_secret": CLIENT_SECRET }, "http": { "base_url": "https://example.com" }, }, ) MockConfigEntry( entry_id="my_entry", domain=NEATO_DOMAIN, data={ "username": "******", "password": "******", "vendor": "neato" }, ).add_to_opp(opp) # Should show form result = await opp.config_entries.flow.async_init( "neato", context={"source": config_entries.SOURCE_REAUTH}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "reauth_confirm" # Confirm reauth flow result2 = await opp.config_entries.flow.async_configure( result["flow_id"], {}) state = config_entry_oauth2_flow._encode_jwt( opp, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) client = await aiohttp_client(opp.http.app) resp = await client.get(f"/auth/external/callback?code=abcd&state={state}") assert resp.status == 200 aioclient_mock.post( OAUTH2_TOKEN, json={ "refresh_token": "mock-refresh-token", "access_token": "mock-access-token", "type": "Bearer", "expires_in": 60, }, ) # Update entry with patch("openpeerpower.components.neato.async_setup_entry", return_value=True) as mock_setup: result3 = await opp.config_entries.flow.async_configure( result2["flow_id"]) await opp.async_block_till_done() new_entry = opp.config_entries.async_get_entry("my_entry") assert result3["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result3["reason"] == "reauth_successful" assert new_entry.state == config_entries.ConfigEntryState.LOADED assert len(opp.config_entries.async_entries(NEATO_DOMAIN)) == 1 assert len(mock_setup.mock_calls) == 1