Refine runtime config and redirect settings

This commit is contained in:
2026-04-20 17:36:05 +02:00
parent 982af62f4f
commit fe0409dafe
12 changed files with 66 additions and 41 deletions
+4 -2
View File
@@ -58,7 +58,7 @@ def test_login_success_sets_session_cookie_and_allows_admin_access(client: TestC
assert "New Password" in config_response.text
assert "Save Config" in config_response.text
assert "当前用户" in config_response.text
assert "Fill in TickTick Client ID, Client Secret, and Redirect URI before starting OAuth." in config_response.text
assert "Fill in App Hostname, TickTick Client ID, and TickTick Client Secret before starting OAuth." in config_response.text
assert 'aria-disabled="true">Authorize TickTick<' in config_response.text
@@ -200,9 +200,10 @@ def test_config_page_shows_ticktick_oauth_link_when_ticktick_is_configured(
auth_database,
monkeypatch,
) -> None:
monkeypatch.setenv("APP_ENV", "production")
monkeypatch.setenv("APP_HOSTNAME", "localhost:8000")
monkeypatch.setenv("TICKTICK_CLIENT_ID", "ticktick-client-id")
monkeypatch.setenv("TICKTICK_CLIENT_SECRET", "ticktick-client-secret")
monkeypatch.setenv("TICKTICK_REDIRECT_URI", "http://localhost:8000/ticktick/auth/code")
get_settings.cache_clear()
reset_auth_db_caches()
@@ -224,6 +225,7 @@ def test_config_page_shows_ticktick_oauth_link_when_ticktick_is_configured(
assert config_response.status_code == 200
assert "Use the saved TickTick client settings to start the authorization flow." in config_response.text
assert "Redirect URI: https://localhost:8000/ticktick/auth/code" in config_response.text
assert 'href="/ticktick/auth/start">Authorize TickTick<' in config_response.text
+14
View File
@@ -5,6 +5,7 @@ def test_settings_support_two_independent_database_urls(monkeypatch) -> None:
monkeypatch.setenv("APP_DATABASE_URL", "sqlite:///./data/app.db")
monkeypatch.setenv("LOCATION_DATABASE_URL", "sqlite:///./data/locationRecorder.db")
monkeypatch.setenv("POO_DATABASE_URL", "sqlite:///./data/pooRecorder.db")
monkeypatch.setenv("APP_HOSTNAME", "home.example.com")
monkeypatch.setenv("POO_WEBHOOK_ID", "poo-hook")
monkeypatch.setenv("POO_SENSOR_ENTITY_NAME", "sensor.test_poo_status")
monkeypatch.setenv("POO_SENSOR_FRIENDLY_NAME", "Poo Status")
@@ -28,6 +29,9 @@ def test_settings_support_two_independent_database_urls(monkeypatch) -> None:
assert settings.home_assistant_base_url == "http://ha.local:8123"
assert settings.home_assistant_auth_token == "token"
assert settings.home_assistant_timeout_seconds == 2.5
assert settings.app_hostname == "home.example.com"
assert settings.app_base_url == "https://home.example.com"
assert settings.ticktick_redirect_uri == "https://home.example.com/ticktick/auth/code"
assert settings.auth_bootstrap_username == "admin"
assert settings.auth_bootstrap_password == "secret"
assert settings.auth_session_cookie_name == "auth_cookie"
@@ -39,3 +43,13 @@ def test_settings_support_two_independent_database_urls(monkeypatch) -> None:
assert settings.poo_sqlite_path is not None
assert settings.poo_sqlite_path.name == "pooRecorder.db"
assert settings.auth_cookie_secure is True
def test_settings_derive_development_ticktick_redirect_uri(monkeypatch) -> None:
monkeypatch.setenv("APP_ENV", "development")
monkeypatch.setenv("APP_HOSTNAME", "localhost:11001")
settings = Settings(_env_file=None)
assert settings.app_base_url == "http://localhost:11001"
assert settings.ticktick_redirect_uri == "http://localhost:11001/ticktick/auth/code"
+8 -7
View File
@@ -38,9 +38,10 @@ class _FakeJsonResponse:
def _configured_settings(**overrides) -> Settings:
payload = {
"app_env": "development",
"app_hostname": "localhost:8000",
"ticktick_client_id": "ticktick-client-id",
"ticktick_client_secret": "ticktick-client-secret",
"ticktick_redirect_uri": "http://localhost:8000/ticktick/auth/code",
"ticktick_token": "ticktick-access-token",
"home_assistant_action_task_project_id": "project-123",
}
@@ -105,9 +106,9 @@ def test_exchange_authorization_code_trims_ticktick_config_values(monkeypatch: p
captured = {}
client = TickTickClient(
settings=_configured_settings(
app_hostname=" localhost:8000 ",
ticktick_client_id=" ticktick-client-id ",
ticktick_client_secret=" ticktick-client-secret ",
ticktick_redirect_uri=" http://localhost:8000/ticktick/auth/code ",
)
)
default_auth_state_store.pending_state = "trimmed-state"
@@ -214,9 +215,9 @@ def test_homeassistant_publish_creates_ticktick_action_task(
auth_database,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("APP_HOSTNAME", "localhost:8000")
monkeypatch.setenv("TICKTICK_CLIENT_ID", "ticktick-client-id")
monkeypatch.setenv("TICKTICK_CLIENT_SECRET", "ticktick-client-secret")
monkeypatch.setenv("TICKTICK_REDIRECT_URI", "http://localhost:8000/ticktick/auth/code")
monkeypatch.setenv("TICKTICK_TOKEN", "ticktick-access-token")
monkeypatch.setenv("HOME_ASSISTANT_ACTION_TASK_PROJECT_ID", "project-123")
get_settings.cache_clear()
@@ -260,9 +261,9 @@ def test_ticktick_auth_start_redirects_authenticated_user(
auth_database,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("APP_HOSTNAME", "localhost:8000")
monkeypatch.setenv("TICKTICK_CLIENT_ID", "ticktick-client-id")
monkeypatch.setenv("TICKTICK_CLIENT_SECRET", "ticktick-client-secret")
monkeypatch.setenv("TICKTICK_REDIRECT_URI", "http://localhost:8000/ticktick/auth/code")
get_settings.cache_clear()
reset_auth_db_caches()
monkeypatch.setattr("app.integrations.ticktick.secrets.token_hex", lambda _: "state-redirect")
@@ -296,9 +297,9 @@ def test_ticktick_auth_callback_persists_token(
auth_database,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("APP_HOSTNAME", "localhost:8000")
monkeypatch.setenv("TICKTICK_CLIENT_ID", "ticktick-client-id")
monkeypatch.setenv("TICKTICK_CLIENT_SECRET", "ticktick-client-secret")
monkeypatch.setenv("TICKTICK_REDIRECT_URI", "http://localhost:8000/ticktick/auth/code")
get_settings.cache_clear()
reset_auth_db_caches()
default_auth_state_store.pending_state = "callback-state"
@@ -337,9 +338,9 @@ def test_ticktick_auth_callback_redirects_on_invalid_state(
auth_database,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("APP_HOSTNAME", "localhost:8000")
monkeypatch.setenv("TICKTICK_CLIENT_ID", "ticktick-client-id")
monkeypatch.setenv("TICKTICK_CLIENT_SECRET", "ticktick-client-secret")
monkeypatch.setenv("TICKTICK_REDIRECT_URI", "http://localhost:8000/ticktick/auth/code")
get_settings.cache_clear()
reset_auth_db_caches()
default_auth_state_store.pending_state = "expected-state"
@@ -361,9 +362,9 @@ def test_ticktick_auth_callback_redirects_when_token_exchange_fails(
auth_database,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("APP_HOSTNAME", "localhost:8000")
monkeypatch.setenv("TICKTICK_CLIENT_ID", "ticktick-client-id")
monkeypatch.setenv("TICKTICK_CLIENT_SECRET", "ticktick-client-secret")
monkeypatch.setenv("TICKTICK_REDIRECT_URI", "http://localhost:8000/ticktick/auth/code")
get_settings.cache_clear()
reset_auth_db_caches()
default_auth_state_store.pending_state = "callback-state"