make sure we did not break password auth

This commit is contained in:
J. Nick Koston 2025-07-11 10:19:33 -10:00
parent 005d4354d5
commit fc8c1ac9dd
No known key found for this signature in database
2 changed files with 16 additions and 27 deletions

View File

@ -1,27 +1,14 @@
esphome: esphome:
name: ${name} name: host-mode-api-password
build_path: ${build_path} host:
friendly_name: ESPHome Host Mode API Password Test
name_add_mac_suffix: no
area: Entryway
platformio_options:
build_flags:
- -std=gnu++17
- -Wall
build_unflags:
- -std=gnu++11
api: api:
password: "test_password_123" password: "test_password_123"
logger: logger:
level: DEBUG level: DEBUG
# Test sensor to verify connection works # Test sensor to verify connection works
sensor: sensor:
- platform: template - platform: template
name: Test Sensor name: Test Sensor
id: test_sensor id: test_sensor
lambda: |- lambda: return 42.0;
return 42.0; update_interval: 0.1s
update_interval: 1s

View File

@ -18,12 +18,7 @@ async def test_host_mode_api_password(
) -> None: ) -> None:
"""Test API authentication with password.""" """Test API authentication with password."""
async with run_compiled(yaml_config): async with run_compiled(yaml_config):
# First, try to connect without password - should fail # Connect with correct password
with pytest.raises(APIConnectionError, match="Authentication"):
async with api_client_connected(password=""):
pass # Should not reach here
# Now connect with correct password
async with api_client_connected(password="test_password_123") as client: async with api_client_connected(password="test_password_123") as client:
# Verify we can get device info # Verify we can get device info
device_info = await client.device_info() device_info = await client.device_info()
@ -32,20 +27,27 @@ async def test_host_mode_api_password(
assert device_info.name == "host-mode-api-password" assert device_info.name == "host-mode-api-password"
# Subscribe to states to ensure authenticated connection works # Subscribe to states to ensure authenticated connection works
loop = asyncio.get_running_loop()
state_future: asyncio.Future[bool] = loop.create_future()
states = {} states = {}
def on_state(state): def on_state(state):
states[state.key] = state states[state.key] = state
if not state_future.done():
state_future.set_result(True)
await client.subscribe_states(on_state) client.subscribe_states(on_state)
# Wait a bit to receive the test sensor state # Wait for at least one state with timeout
await asyncio.sleep(0.5) try:
await asyncio.wait_for(state_future, timeout=5.0)
except asyncio.TimeoutError:
pytest.fail("No states received within timeout")
# Should have received at least one state (the test sensor) # Should have received at least one state (the test sensor)
assert len(states) > 0 assert len(states) > 0
# Test with wrong password - should fail # Test with wrong password - should fail
with pytest.raises(APIConnectionError, match="Authentication"): with pytest.raises(APIConnectionError, match="Invalid password"):
async with api_client_connected(password="wrong_password"): async with api_client_connected(password="wrong_password"):
pass # Should not reach here pass # Should not reach here