Skip to content

Commit

Permalink
Fix tests, add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
watkins-matt committed Jan 31, 2024
1 parent 95b5598 commit 7259e81
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 18 deletions.
6 changes: 6 additions & 0 deletions custom_components/google_keep_sync/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ async def async_step_user(
# Show an error if the same username has already been configured
except AbortFlow:
errors["base"] = "already_configured"
return self.async_show_form(
step_id="user",
data_schema=SCHEMA_USER_DATA_STEP,
errors=errors,
description_placeholders={"invalid_auth_url": INVALID_AUTH_URL},
)

# Validate the user input for any issues
errors = await self.handle_user_input(user_input)
Expand Down
141 changes: 123 additions & 18 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def mock_google_keep_api():

@pytest.mark.asyncio
async def test_user_form_setup(hass: HomeAssistant, mock_google_keep_api):
"""Test the initial user setup form."""
"""Test the initial user setup form, with only a username and password."""
user_name = "[email protected]"
user_password = "testpass"
user_token = "testtoken"
user_token = ""

# Initiate the config flow
initial_form_result = await hass.config_entries.flow.async_init(
Expand Down Expand Up @@ -86,10 +86,100 @@ async def test_user_form_setup(hass: HomeAssistant, mock_google_keep_api):
}


@pytest.mark.asyncio
async def test_user_form_blank_username(hass: HomeAssistant, mock_google_keep_api):
"""Test handling of a blank username."""
user_input = {"username": " ", "password": "wrongpass"}

# Get the mock instance and set authenticate to return False
mock_instance = mock_google_keep_api.return_value
mock_instance.authenticate.return_value = False

initial_form_result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)

auth_fail_result = await hass.config_entries.flow.async_configure(
initial_form_result["flow_id"], user_input=user_input
)

assert auth_fail_result["type"] == "form"
assert auth_fail_result["errors"] == {"base": "blank_username"}


@pytest.mark.asyncio
async def test_user_form_password_and_token(hass: HomeAssistant, mock_google_keep_api):
"""Test handling of a blank username."""
user_input = {
"username": "[email protected]",
"password": "password",
"token": "token",
}

# Get the mock instance and set authenticate to return False
mock_instance = mock_google_keep_api.return_value
mock_instance.authenticate.return_value = False

initial_form_result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)

auth_fail_result = await hass.config_entries.flow.async_configure(
initial_form_result["flow_id"], user_input=user_input
)

assert auth_fail_result["type"] == "form"
assert auth_fail_result["errors"] == {"base": "both_password_and_token"}


@pytest.mark.asyncio
async def test_user_form_invalid_email(hass: HomeAssistant, mock_google_keep_api):
"""Test handling of an invalid email address."""
user_input = {"username": "testuser", "password": "wrongpass"}

# Get the mock instance and set authenticate to return False
mock_instance = mock_google_keep_api.return_value
mock_instance.authenticate.return_value = False

initial_form_result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)

auth_fail_result = await hass.config_entries.flow.async_configure(
initial_form_result["flow_id"], user_input=user_input
)

assert auth_fail_result["type"] == "form"
assert auth_fail_result["errors"] == {"base": "invalid_email"}


@pytest.mark.asyncio
async def test_user_form_neither_password_nor_token(
hass: HomeAssistant, mock_google_keep_api
):
"""Test handling of a missing password and token."""
user_input = {"username": "[email protected]", "password": "", "token": ""}

# Get the mock instance and set authenticate to return False
mock_instance = mock_google_keep_api.return_value
mock_instance.authenticate.return_value = False

initial_form_result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)

auth_fail_result = await hass.config_entries.flow.async_configure(
initial_form_result["flow_id"], user_input=user_input
)

assert auth_fail_result["type"] == "form"
assert auth_fail_result["errors"] == {"base": "neither_password_nor_token"}


@pytest.mark.asyncio
async def test_invalid_auth_handling(hass: HomeAssistant, mock_google_keep_api):
"""Test handling of invalid authentication."""
user_input = {"username": "testuser", "password": "wrongpass"}
user_input = {"username": "testuser@example.com", "password": "wrongpass"}

# Get the mock instance and set authenticate to return False
mock_instance = mock_google_keep_api.return_value
Expand All @@ -107,6 +197,31 @@ async def test_invalid_auth_handling(hass: HomeAssistant, mock_google_keep_api):
assert auth_fail_result["errors"] == {"base": "invalid_auth"}


@pytest.mark.asyncio
async def test_invalid_token(hass: HomeAssistant, mock_google_keep_api):
"""Test handling of an invalid token."""
user_input = {
"username": "[email protected]",
"password": "",
"token": "invalidtoken",
}

# Get the mock instance and set authenticate to return False
mock_instance = mock_google_keep_api.return_value
mock_instance.authenticate.return_value = False

initial_form_result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)

auth_fail_result = await hass.config_entries.flow.async_configure(
initial_form_result["flow_id"], user_input=user_input
)

assert auth_fail_result["type"] == "form"
assert auth_fail_result["errors"] == {"base": "invalid_token_format"}


@pytest.mark.asyncio
async def test_user_input_handling(hass: HomeAssistant, mock_google_keep_api):
"""Test user input handling."""
Expand All @@ -120,16 +235,6 @@ async def test_user_input_handling(hass: HomeAssistant, mock_google_keep_api):
assert result["step_id"] == "options"


@pytest.mark.asyncio
async def test_invalid_user_input(hass: HomeAssistant):
"""Test invalid user input."""
user_input = {"username": "", "password": ""}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=user_input
)
assert result["errors"] == {"base": "invalid_auth"}


@pytest.mark.asyncio
async def test_unexpected_exception_handling(hass: HomeAssistant, mock_google_keep_api):
"""Test handling of unexpected exceptions."""
Expand All @@ -141,7 +246,7 @@ async def test_unexpected_exception_handling(hass: HomeAssistant, mock_google_ke
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data={"username": "user", "password": "pass"},
data={"username": "user@example.com", "password": "pass"},
)

# Assert that an unknown error is handled
Expand Down Expand Up @@ -291,20 +396,20 @@ async def test_empty_username_or_password(hass: HomeAssistant):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=user_input
)
assert result["errors"] == {"base": "invalid_auth"}
assert result["errors"] == {"base": "blank_username"}

# Test with empty password
user_input = {"username": "username", "password": ""}
user_input = {"username": "username@example.com", "password": ""}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=user_input
)
assert result["errors"] == {"base": "invalid_auth"}
assert result["errors"] == {"base": "neither_password_nor_token"}


@pytest.mark.asyncio
async def test_authentication_network_issue(hass: HomeAssistant, mock_google_keep_api):
"""Test network issues during authentication."""
user_input = {"username": "testuser", "password": "testpass"}
user_input = {"username": "testuser@example.com", "password": "testpass"}

# Simulate network issue
mock_instance = mock_google_keep_api.return_value
Expand Down

0 comments on commit 7259e81

Please sign in to comment.