core/tests/components/monzo/conftest.py

126 lines
3.1 KiB
Python
Raw Normal View History

Add Monzo integration (#101731) * Initial monzo implementation * Tests and fixes * Extracted api to pypi package * Add app confirmation step * Corrected data path for accounts * Removed useless check * Improved tests * Exclude partially tested files from coverage check * Use has_entity_name naming * Bumped monzopy to 1.0.10 * Remove commented out code * Remove reauth from initial PR * Remove useless code * Correct comment * Remove reauth tests * Remove device triggers from intial PR * Set attr outside constructor * Remove f-strings where no longer needed in entity.py * Rename field to make clearer it's a Callable * Correct native_unit_of_measurement * Remove pot transfer service from intial PR * Remove reauth string * Remove empty fields in manifest.json * Freeze SensorEntityDescription and remove Mixin Also use list comprehensions for producing sensor lists * Use consts in application_credentials.py * Revert "Remove useless code" Apparently this wasn't useless This reverts commit c6b7109e47202f866c766ea4c16ce3eb0588795b. * Ruff and pylint style fixes * Bumped monzopy to 1.1.0 Adds support for joint/business/etc account pots * Update test snapshot * Rename AsyncConfigEntryAuth * Use dataclasses instead of dictionaries * Move OAuth constants to application_credentials.py * Remove remaining constants and dependencies for services from this PR * Remove empty manifest entry * Fix comment * Set device entry_type to service * ACC_SENSORS -> ACCOUNT_SENSORS * Make value_fn of sensors return StateType * Rename OAuthMonzoAPI again * Fix tests * Patch API instead of integration for unavailable test * Move pot constant to sensor.py * Improve type safety in async_get_monzo_api_data() * Update async_oauth_create_entry() docstring --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-05-07 18:38:58 +00:00
"""Fixtures for tests."""
import time
from unittest.mock import AsyncMock, patch
from monzopy.monzopy import UserAccount
import pytest
from homeassistant.components.application_credentials import (
ClientCredential,
async_import_client_credential,
)
from homeassistant.components.monzo.api import AuthenticatedMonzoAPI
from homeassistant.components.monzo.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
CLIENT_ID = "1234"
CLIENT_SECRET = "5678"
TEST_ACCOUNTS = [
{
"id": "acc_curr",
"name": "Current Account",
"type": "uk_retail",
"balance": {"balance": 123, "total_balance": 321},
},
{
"id": "acc_flex",
"name": "Flex",
"type": "uk_monzo_flex",
"balance": {"balance": 123, "total_balance": 321},
},
]
TEST_POTS = [
{
"id": "pot_savings",
"name": "Savings",
"style": "savings",
"balance": 134578,
"currency": "GBP",
"type": "instant_access",
}
]
TITLE = "jake"
USER_ID = 12345
@pytest.fixture(autouse=True)
async def setup_credentials(hass: HomeAssistant) -> None:
"""Fixture to setup credentials."""
assert await async_setup_component(hass, "application_credentials", {})
await async_import_client_credential(
hass,
DOMAIN,
ClientCredential(CLIENT_ID, CLIENT_SECRET, DOMAIN),
)
@pytest.fixture(name="expires_at")
def mock_expires_at() -> int:
"""Fixture to set the oauth token expiration time."""
return time.time() + 3600
@pytest.fixture
def polling_config_entry(expires_at: int) -> MockConfigEntry:
"""Create Monzo entry in Home Assistant."""
return MockConfigEntry(
domain=DOMAIN,
title=TITLE,
unique_id=str(USER_ID),
data={
"auth_implementation": DOMAIN,
"token": {
"status": 0,
"userid": str(USER_ID),
"access_token": "mock-access-token",
"refresh_token": "mock-refresh-token",
"expires_in": 60,
"expires_at": time.time() + 1000,
},
"profile": TITLE,
},
)
@pytest.fixture(name="basic_monzo")
def mock_basic_monzo():
"""Mock monzo with one pot."""
mock = AsyncMock(spec=AuthenticatedMonzoAPI)
mock_user_account = AsyncMock(spec=UserAccount)
mock_user_account.accounts.return_value = []
mock_user_account.pots.return_value = TEST_POTS
mock.user_account = mock_user_account
with patch(
"homeassistant.components.monzo.AuthenticatedMonzoAPI",
return_value=mock,
):
yield mock
@pytest.fixture(name="monzo")
def mock_monzo():
"""Mock monzo."""
mock = AsyncMock(spec=AuthenticatedMonzoAPI)
mock_user_account = AsyncMock(spec=UserAccount)
mock_user_account.accounts.return_value = TEST_ACCOUNTS
mock_user_account.pots.return_value = TEST_POTS
mock.user_account = mock_user_account
with patch(
"homeassistant.components.monzo.AuthenticatedMonzoAPI",
return_value=mock,
):
yield mock