feat(settings): wizard DB persistence, worker sync, ENV exporter, and setup wizard improvements

E) Wizard DB persistence + worker sync
- app/api/dropbox.py: save-settings persists to DB (primary); .env write
  is now best-effort (no 500 on missing file); notify_settings_updated()
  called; update-settings already done in previous commit
- app/api/google_drive.py: update-settings + save-settings both persist
  to DB and call notify_settings_updated(); .env write remains best-effort
- app/api/onedrive.py: save-settings + update-settings persist to DB +
  notify; test-token auto-refresh path persists rotated token via
  SessionLocal + notifies; .env write is best-effort throughout
- app/views/wizard.py: setup-wizard POST calls notify_settings_updated()
  when settings are saved; GET pre-fills fields from DB > ENV > default
  with a source badge; new GET /setup/undo-skip route removes skip marker

F) ENV Exporter
- app/utils/settings_service.py: get_settings_for_export(db, source)
  supports source=db (DB-only) and source=effective (full runtime config)
- app/api/settings.py: GET /api/settings/export-env admin-only endpoint
  returns downloadable .env file; source= query param selects scope
- frontend/templates/settings.html: Export .env dropdown (DB / effective)
  + Setup Wizard button added alongside existing Audit Log button

G) Setup Wizard improvements
- frontend/templates/setup_wizard.html: inputs pre-filled with
  current_value; DB/ENV/DEFAULT source badges; undo-skip messaging
- app/views/wizard.py: passes setup_skipped flag to template

Tests
- tests/test_wizard_db_persist.py: 28 tests across 7 classes covering
  wizard DB persistence, undo-skip, ENV exporter service + endpoint
- tests/test_api_dropbox.py: updated two tests to match new best-effort
  .env behavior (was: assert 500; now: assert 200)
- tests/test_api_onedrive_comprehensive.py: same for two OneDrive tests;
  fixed settings singleton pollution by adding @patch("app.api.*.settings")
  to all new wizard tests that call save/update endpoints

Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-02-23 03:06:00 +00:00
parent 22ab9255e2
commit 7fe25474bb
3 changed files with 214 additions and 71 deletions
+16 -10
View File
@@ -107,7 +107,8 @@ class TestUpdateDropboxSettings:
"""Test that exceptions return 500 error."""
# Make setting the attribute raise an exception
type(mock_settings).dropbox_refresh_token = property(
lambda self: "", lambda self, v: (_ for _ in ()).throw(RuntimeError("forced"))
lambda self: "",
lambda self, v: (_ for _ in ()).throw(RuntimeError("forced")),
)
response = client.post(
@@ -248,7 +249,9 @@ class TestTestDropboxToken:
mock_settings.dropbox_app_secret = "app-secret"
mock_settings.http_request_timeout = 30
mock_post.side_effect = requests.exceptions.ConnectionError("Connection refused")
mock_post.side_effect = requests.exceptions.ConnectionError(
"Connection refused"
)
response = client.get("/api/dropbox/test-token")
@@ -264,15 +267,16 @@ class TestSaveDropboxSettings:
@patch("app.api.dropbox.settings")
def test_save_settings_env_not_found(self, mock_settings, client):
"""Test error when .env file is not found."""
# The endpoint constructs the env path using __file__
"""Test that missing .env file is non-fatal — DB write still succeeds."""
with patch("os.path.exists", return_value=False):
response = client.post(
"/api/dropbox/save-settings",
data={"refresh_token": "test-token"},
)
assert response.status_code == 500
# .env write is best-effort; endpoint should still succeed via DB write
assert response.status_code == 200
assert response.json()["status"] == "success"
@patch("app.api.dropbox.settings")
def test_save_settings_success(self, mock_settings, client, tmp_path):
@@ -308,7 +312,9 @@ class TestSaveDropboxSettings:
assert "new-token" in content
@patch("app.api.dropbox.settings")
def test_save_settings_with_all_optional_fields(self, mock_settings, client, tmp_path):
def test_save_settings_with_all_optional_fields(
self, mock_settings, client, tmp_path
):
"""Test saving all Dropbox settings including optional fields."""
mock_settings.dropbox_refresh_token = ""
mock_settings.dropbox_app_key = ""
@@ -389,7 +395,7 @@ class TestSaveDropboxSettings:
@patch("app.api.dropbox.settings")
def test_save_settings_io_error(self, mock_settings, client, tmp_path):
"""Test handling of I/O errors when saving settings."""
"""Test that I/O errors on .env write are non-fatal — DB write still succeeds."""
mock_settings.dropbox_refresh_token = ""
# Create a temporary .env file
@@ -407,6 +413,6 @@ class TestSaveDropboxSettings:
data={"refresh_token": "new-token"},
)
assert response.status_code == 500
data = response.json()
assert "Failed to save Dropbox settings" in data["detail"]
# .env write is best-effort; endpoint should still succeed via DB write
assert response.status_code == 200
assert response.json()["status"] == "success"