ElevenLabs invalid api key config flow testing (#133822)

This commit is contained in:
Simon 2024-12-23 11:05:31 +00:00 committed by GitHub
parent c5fe25a001
commit 386a722393
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 11 deletions

View File

@ -7,11 +7,7 @@ rules:
appropriate-polling: done
brands: done
common-modules: done
config-flow-test-coverage:
status: todo
comment: >
We should have every test end in either ABORT or CREATE_ENTRY.
test_invalid_api_key should assert the kind of error that is raised.
config-flow-test-coverage: done
config-flow: done
dependency-transparency: done
docs-actions: done

View File

@ -24,14 +24,19 @@ def mock_setup_entry() -> Generator[AsyncMock]:
yield mock_setup_entry
@pytest.fixture
def mock_async_client() -> Generator[AsyncMock]:
"""Override async ElevenLabs client."""
def _client_mock():
client_mock = AsyncMock()
client_mock.voices.get_all.return_value = GetVoicesResponse(voices=MOCK_VOICES)
client_mock.models.get_all.return_value = MOCK_MODELS
return client_mock
@pytest.fixture
def mock_async_client() -> Generator[AsyncMock]:
"""Override async ElevenLabs client."""
with patch(
"elevenlabs.AsyncElevenLabs", return_value=client_mock
"homeassistant.components.elevenlabs.config_flow.AsyncElevenLabs",
return_value=_client_mock(),
) as mock_async_client:
yield mock_async_client
@ -41,7 +46,7 @@ def mock_async_client_fail() -> Generator[AsyncMock]:
"""Override async ElevenLabs client."""
with patch(
"homeassistant.components.elevenlabs.config_flow.AsyncElevenLabs",
return_value=AsyncMock(),
return_value=_client_mock(),
) as mock_async_client:
mock_async_client.side_effect = ApiError
yield mock_async_client

View File

@ -73,10 +73,28 @@ async def test_invalid_api_key(
},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"]
assert result["errors"] == {"base": "invalid_api_key"}
mock_setup_entry.assert_not_called()
# Reset the side effect
mock_async_client_fail.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_API_KEY: "api_key",
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "ElevenLabs"
assert result["data"] == {
"api_key": "api_key",
}
assert result["options"] == {CONF_MODEL: DEFAULT_MODEL, CONF_VOICE: "voice1"}
mock_setup_entry.assert_called_once()
async def test_options_flow_init(
hass: HomeAssistant,