mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Simplify google calendar authentication setup (#67314)
Simplify google calendar authentication to combine some of the cases together, and reduce unecessarily checks. Make the tests share common authentication setup and reduce use of mocks by introducing a fake for holding on to credentials. This makes future refactoring simpler, so we don't have to care as much about the interactions with the credentials storage.
This commit is contained in:
@@ -53,28 +53,6 @@ async def mock_code_flow(
|
||||
yield mock_flow
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def token_scopes() -> list[str]:
|
||||
"""Fixture for scopes used during test."""
|
||||
return ["https://www.googleapis.com/auth/calendar"]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def creds(token_scopes: list[str]) -> OAuth2Credentials:
|
||||
"""Fixture that defines creds used in the test."""
|
||||
token_expiry = utcnow() + datetime.timedelta(days=7)
|
||||
return OAuth2Credentials(
|
||||
access_token="ACCESS_TOKEN",
|
||||
client_id="client-id",
|
||||
client_secret="client-secret",
|
||||
refresh_token="REFRESH_TOKEN",
|
||||
token_expiry=token_expiry,
|
||||
token_uri="http://example.com",
|
||||
user_agent="n/a",
|
||||
scopes=token_scopes,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mock_exchange(creds: OAuth2Credentials) -> YieldFixture[Mock]:
|
||||
"""Fixture for mocking out the exchange for credentials."""
|
||||
@@ -84,25 +62,6 @@ async def mock_exchange(creds: OAuth2Credentials) -> YieldFixture[Mock]:
|
||||
yield mock
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def mock_token_write(hass: HomeAssistant) -> None:
|
||||
"""Fixture to avoid writing token files to disk."""
|
||||
with patch(
|
||||
"homeassistant.components.google.os.path.isfile", return_value=True
|
||||
), patch("homeassistant.components.google.Storage.put"):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mock_token_read(
|
||||
hass: HomeAssistant,
|
||||
creds: OAuth2Credentials,
|
||||
) -> None:
|
||||
"""Fixture to populate an existing token file."""
|
||||
with patch("homeassistant.components.google.Storage.get", return_value=creds):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def calendars_config() -> list[dict[str, Any]]:
|
||||
"""Fixture for tests to override default calendar configuration."""
|
||||
|
||||
Reference in New Issue
Block a user