From 3ff5bbe0fd9c4fd113d82193af0ca02b9f580af1 Mon Sep 17 00:00:00 2001 From: Ludeeus Date: Mon, 24 Jan 2022 12:02:11 +0000 Subject: [PATCH] Fix current tests --- .../components/github/coordinator.py | 11 ++++---- homeassistant/components/github/strings.json | 4 +++ .../components/github/translations/en.json | 4 +++ tests/components/github/conftest.py | 4 ++- tests/components/github/test_config_flow.py | 28 +++++++++++++++++++ 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/github/coordinator.py b/homeassistant/components/github/coordinator.py index 81c3e1376a..d2d61485e6 100644 --- a/homeassistant/components/github/coordinator.py +++ b/homeassistant/components/github/coordinator.py @@ -64,11 +64,6 @@ class GitHubBaseDataUpdateCoordinator(DataUpdateCoordinator[T]): async def _async_update_data(self) -> T: try: response = await self.fetch_data() - scope = ( - "repo" if self.config_entry.options.get(CONF_REPO_SCOPE, False) else "" - ) - if scope != response.headers.x_oauth_scopes: - raise ConfigEntryAuthFailed("Invalid OAuth scopes") except GitHubNotModifiedException: LOGGER.debug( "Content for %s with %s not modified", @@ -92,7 +87,11 @@ class RepositoryInformationDataUpdateCoordinator( async def fetch_data(self) -> GitHubResponseModel[GitHubRepositoryModel]: """Get the latest data from GitHub.""" - return await self._client.repos.get(self.repository, **{"etag": self._etag}) + response = await self._client.repos.get(self.repository, **{"etag": self._etag}) + scope = "repo" if self.config_entry.options.get(CONF_REPO_SCOPE, False) else "" + if scope != response.headers.x_oauth_scopes: + raise ConfigEntryAuthFailed("Invalid OAuth scopes") + return response class RepositoryReleaseDataUpdateCoordinator( diff --git a/homeassistant/components/github/strings.json b/homeassistant/components/github/strings.json index 6b025664b6..6834f6e2e5 100644 --- a/homeassistant/components/github/strings.json +++ b/homeassistant/components/github/strings.json @@ -12,6 +12,10 @@ "data": { "repositories": "Select repositories to track." } + }, + "reauth_confirm": { + "title": "[%key:common::config_flow::title::reauth%]", + "description": "The GitHub integration needs to re-authenticate your account to match the selected scopes." } }, "progress": { diff --git a/homeassistant/components/github/translations/en.json b/homeassistant/components/github/translations/en.json index 91f599cfd2..dfb01d7a27 100644 --- a/homeassistant/components/github/translations/en.json +++ b/homeassistant/components/github/translations/en.json @@ -9,6 +9,10 @@ "wait_for_device": "1. Open {url} \n2.Paste the following key to authorize the integration: \n```\n{code}\n```\n" }, "step": { + "reauth_confirm": { + "description": "The GitHub integration needs to re-authenticate your account to match the selected scopes.", + "title": "Reauthenticate Integration" + }, "repositories": { "data": { "repositories": "Select repositories to track." diff --git a/tests/components/github/conftest.py b/tests/components/github/conftest.py index 04b53da6b9..82cee3aefc 100644 --- a/tests/components/github/conftest.py +++ b/tests/components/github/conftest.py @@ -6,6 +6,7 @@ import pytest from homeassistant.components.github.const import ( CONF_ACCESS_TOKEN, + CONF_REPO_SCOPE, CONF_REPOSITORIES, DOMAIN, ) @@ -23,8 +24,9 @@ def mock_config_entry() -> MockConfigEntry: return MockConfigEntry( title="", domain=DOMAIN, + unique_id=DOMAIN, data={CONF_ACCESS_TOKEN: MOCK_ACCESS_TOKEN}, - options={CONF_REPOSITORIES: [TEST_REPOSITORY]}, + options={CONF_REPOSITORIES: [TEST_REPOSITORY], CONF_REPO_SCOPE: False}, ) diff --git a/tests/components/github/test_config_flow.py b/tests/components/github/test_config_flow.py index dad9747262..a5be8a721d 100644 --- a/tests/components/github/test_config_flow.py +++ b/tests/components/github/test_config_flow.py @@ -7,6 +7,7 @@ from homeassistant import config_entries from homeassistant.components.github.config_flow import starred_repositories from homeassistant.components.github.const import ( CONF_ACCESS_TOKEN, + CONF_REPO_SCOPE, CONF_REPOSITORIES, DEFAULT_REPOSITORIES, DOMAIN, @@ -62,6 +63,17 @@ async def test_full_user_flow_implementation( context={"source": config_entries.SOURCE_USER}, ) + assert result["step_id"] == "user" + assert result["type"] == "form" + assert "flow_id" in result + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_REPO_SCOPE: False, + }, + ) + assert result["step_id"] == "device" assert result["type"] == RESULT_TYPE_SHOW_PROGRESS assert "flow_id" in result @@ -92,10 +104,19 @@ async def test_flow_with_registration_failure( "https://github.com/login/device/code", exc=GitHubException("Registration failed"), ) + result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}, ) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_REPO_SCOPE: False, + }, + ) + assert result["type"] == RESULT_TYPE_ABORT assert result.get("reason") == "could_not_register" @@ -124,6 +145,13 @@ async def test_flow_with_activation_failure( DOMAIN, context={"source": config_entries.SOURCE_USER}, ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_REPO_SCOPE: False, + }, + ) + assert result["step_id"] == "device" assert result["type"] == RESULT_TYPE_SHOW_PROGRESS