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
+137 -42
View File
@@ -72,7 +72,9 @@ class TestExchangeOneDriveToken:
@patch("app.api.onedrive.exchange_oauth_token")
def test_exchange_token_error(self, mock_exchange, client: TestClient):
"""Test token exchange with error from OAuth provider."""
mock_exchange.side_effect = HTTPException(status_code=400, detail="Invalid authorization code")
mock_exchange.side_effect = HTTPException(
status_code=400, detail="Invalid authorization code"
)
response = client.post(
"/api/onedrive/exchange-token",
@@ -107,7 +109,9 @@ class TestTestOneDriveToken:
@patch("requests.post")
@patch("requests.get")
@patch("app.config.settings")
def test_test_token_success(self, mock_settings, mock_get, mock_post, client: TestClient):
def test_test_token_success(
self, mock_settings, mock_get, mock_post, client: TestClient
):
"""Test successful token validation with properly mocked responses."""
# Configure settings with property mocking
type(mock_settings).onedrive_refresh_token = "test_refresh_token"
@@ -119,13 +123,19 @@ class TestTestOneDriveToken:
# Mock token refresh response
mock_post_response = Mock()
mock_post_response.status_code = 200
mock_post_response.json.return_value = {"access_token": "test_access_token", "expires_in": 3600}
mock_post_response.json.return_value = {
"access_token": "test_access_token",
"expires_in": 3600,
}
mock_post.return_value = mock_post_response
# Mock user info response
mock_get_response = Mock()
mock_get_response.status_code = 200
mock_get_response.json.return_value = {"displayName": "Test User", "userPrincipalName": "test@example.com"}
mock_get_response.json.return_value = {
"displayName": "Test User",
"userPrincipalName": "test@example.com",
}
mock_get.return_value = mock_get_response
response = client.get("/api/onedrive/test-token")
@@ -152,7 +162,9 @@ class TestTestOneDriveToken:
@patch("requests.post")
@patch("app.config.settings")
def test_test_token_refresh_failed(self, mock_settings, mock_post, client: TestClient):
def test_test_token_refresh_failed(
self, mock_settings, mock_post, client: TestClient
):
"""Test when token refresh fails."""
type(mock_settings).onedrive_refresh_token = "invalid_token"
type(mock_settings).onedrive_client_id = "test_client_id"
@@ -177,7 +189,9 @@ class TestTestOneDriveToken:
@patch("requests.post")
@patch("requests.get")
@patch("app.config.settings")
def test_test_token_new_refresh_token_issued(self, mock_settings, mock_get, mock_post, client: TestClient):
def test_test_token_new_refresh_token_issued(
self, mock_settings, mock_get, mock_post, client: TestClient
):
"""Test when Microsoft issues a new refresh token."""
type(mock_settings).onedrive_refresh_token = "old_refresh_token"
type(mock_settings).onedrive_client_id = "test_client_id"
@@ -198,7 +212,10 @@ class TestTestOneDriveToken:
# Mock user info
mock_get_response = Mock()
mock_get_response.status_code = 200
mock_get_response.json.return_value = {"displayName": "Test User", "userPrincipalName": "test@example.com"}
mock_get_response.json.return_value = {
"displayName": "Test User",
"userPrincipalName": "test@example.com",
}
mock_get.return_value = mock_get_response
with patch("os.path.exists", return_value=False):
@@ -209,12 +226,23 @@ class TestTestOneDriveToken:
@patch("requests.post")
@patch("requests.get")
@patch("builtins.open", new_callable=mock_open, read_data="ONEDRIVE_REFRESH_TOKEN=old_token\n")
@patch(
"builtins.open",
new_callable=mock_open,
read_data="ONEDRIVE_REFRESH_TOKEN=old_token\n",
)
@patch("os.path.exists")
@patch("os.path.dirname")
@patch("app.config.settings")
def test_test_token_updates_env_file(
self, mock_settings, mock_dirname, mock_exists, mock_file, mock_get, mock_post, client: TestClient
self,
mock_settings,
mock_dirname,
mock_exists,
mock_file,
mock_get,
mock_post,
client: TestClient,
):
"""Test that new refresh token is saved to .env file."""
mock_settings.onedrive_refresh_token = "old_token"
@@ -238,7 +266,10 @@ class TestTestOneDriveToken:
# Mock user info
mock_get_response = Mock()
mock_get_response.status_code = 200
mock_get_response.json.return_value = {"displayName": "Test User", "userPrincipalName": "test@example.com"}
mock_get_response.json.return_value = {
"displayName": "Test User",
"userPrincipalName": "test@example.com",
}
mock_get.return_value = mock_get_response
response = client.get("/api/onedrive/test-token")
@@ -247,7 +278,9 @@ class TestTestOneDriveToken:
@patch("requests.post")
@patch("requests.get")
@patch("app.config.settings")
def test_test_token_user_info_failed(self, mock_settings, mock_get, mock_post, client: TestClient):
def test_test_token_user_info_failed(
self, mock_settings, mock_get, mock_post, client: TestClient
):
"""Test when user info request fails."""
mock_settings.onedrive_refresh_token = "test_token"
mock_settings.onedrive_client_id = "test_client_id"
@@ -258,7 +291,10 @@ class TestTestOneDriveToken:
# Mock successful refresh
mock_post_response = Mock()
mock_post_response.status_code = 200
mock_post_response.json.return_value = {"access_token": "test_access_token", "expires_in": 3600}
mock_post_response.json.return_value = {
"access_token": "test_access_token",
"expires_in": 3600,
}
mock_post.return_value = mock_post_response
# Mock failed user info
@@ -320,7 +356,9 @@ class TestSaveOneDriveSettings:
@patch("os.path.exists")
@patch("os.path.dirname")
@patch("app.config.settings")
def test_save_settings_success(self, mock_settings, mock_dirname, mock_exists, mock_file, client: TestClient):
def test_save_settings_success(
self, mock_settings, mock_dirname, mock_exists, mock_file, client: TestClient
):
"""Test successful save to .env file."""
mock_exists.return_value = True
mock_dirname.return_value = "/app"
@@ -342,18 +380,27 @@ class TestSaveOneDriveSettings:
@patch("os.path.exists")
@patch("os.path.dirname")
def test_save_settings_env_file_not_found(self, mock_dirname, mock_exists, client: TestClient):
"""Test save when .env file doesn't exist."""
def test_save_settings_env_file_not_found(
self, mock_dirname, mock_exists, client: TestClient
):
"""Test that missing .env file is non-fatal — DB write still succeeds."""
mock_exists.return_value = False
mock_dirname.return_value = "/app"
response = client.post("/api/onedrive/save-settings", data={"refresh_token": "token", "tenant_id": "common"})
response = client.post(
"/api/onedrive/save-settings",
data={"refresh_token": "token", "tenant_id": "common"},
)
assert response.status_code == 500
data = response.json()
assert "could not find .env file" in data["detail"].lower()
# .env write is best-effort; endpoint should still succeed via DB write
assert response.status_code == 200
assert response.json()["status"] == "success"
@patch("builtins.open", new_callable=mock_open, read_data="ONEDRIVE_REFRESH_TOKEN=old_token\n")
@patch(
"builtins.open",
new_callable=mock_open,
read_data="ONEDRIVE_REFRESH_TOKEN=old_token\n",
)
@patch("os.path.exists")
@patch("os.path.dirname")
@patch("app.config.settings")
@@ -365,12 +412,17 @@ class TestSaveOneDriveSettings:
mock_dirname.return_value = "/app"
response = client.post(
"/api/onedrive/save-settings", data={"refresh_token": "updated_token", "tenant_id": "common"}
"/api/onedrive/save-settings",
data={"refresh_token": "updated_token", "tenant_id": "common"},
)
assert response.status_code == 200
@patch("builtins.open", new_callable=mock_open, read_data="# ONEDRIVE_CLIENT_ID=commented\n")
@patch(
"builtins.open",
new_callable=mock_open,
read_data="# ONEDRIVE_CLIENT_ID=commented\n",
)
@patch("os.path.exists")
@patch("os.path.dirname")
@patch("app.config.settings")
@@ -383,7 +435,11 @@ class TestSaveOneDriveSettings:
response = client.post(
"/api/onedrive/save-settings",
data={"refresh_token": "token", "client_id": "new_client_id", "tenant_id": "common"},
data={
"refresh_token": "token",
"client_id": "new_client_id",
"tenant_id": "common",
},
)
assert response.status_code == 200
@@ -401,26 +457,39 @@ class TestSaveOneDriveSettings:
response = client.post(
"/api/onedrive/save-settings",
data={"refresh_token": "new_token", "folder_path": "/New/Path", "tenant_id": "common"},
data={
"refresh_token": "new_token",
"folder_path": "/New/Path",
"tenant_id": "common",
},
)
assert response.status_code == 200
def test_save_settings_missing_required_field(self, client: TestClient):
"""Test save without required refresh_token."""
response = client.post("/api/onedrive/save-settings", data={"tenant_id": "common"})
response = client.post(
"/api/onedrive/save-settings", data={"tenant_id": "common"}
)
assert response.status_code == 422 # Validation error
@patch("os.path.exists")
@patch("os.path.dirname")
def test_save_settings_exception_handling(self, mock_dirname, mock_exists, client: TestClient):
"""Test exception handling in save settings."""
def test_save_settings_exception_handling(
self, mock_dirname, mock_exists, client: TestClient
):
"""Test that exceptions in .env write are non-fatal — DB write still succeeds."""
mock_exists.side_effect = Exception("Unexpected error")
response = client.post("/api/onedrive/save-settings", data={"refresh_token": "token", "tenant_id": "common"})
response = client.post(
"/api/onedrive/save-settings",
data={"refresh_token": "token", "tenant_id": "common"},
)
assert response.status_code == 500
# .env write exception is caught; endpoint succeeds via DB write
assert response.status_code == 200
assert response.json()["status"] == "success"
@pytest.mark.unit
@@ -429,7 +498,9 @@ class TestUpdateOneDriveSettings:
@patch("app.tasks.upload_to_onedrive.get_onedrive_token")
@patch("app.config.settings")
def test_update_settings_success(self, mock_settings, mock_get_token, client: TestClient):
def test_update_settings_success(
self, mock_settings, mock_get_token, client: TestClient
):
"""Test successful settings update in memory."""
mock_get_token.return_value = "test_token"
@@ -450,24 +521,30 @@ class TestUpdateOneDriveSettings:
@patch("app.tasks.upload_to_onedrive.get_onedrive_token")
@patch("app.config.settings")
def test_update_settings_minimal(self, mock_settings, mock_get_token, client: TestClient):
def test_update_settings_minimal(
self, mock_settings, mock_get_token, client: TestClient
):
"""Test update with only required fields."""
mock_get_token.return_value = "test_token"
response = client.post(
"/api/onedrive/update-settings", data={"refresh_token": "new_token", "tenant_id": "common"}
"/api/onedrive/update-settings",
data={"refresh_token": "new_token", "tenant_id": "common"},
)
assert response.status_code == 200
@patch("app.tasks.upload_to_onedrive.get_onedrive_token")
@patch("app.config.settings")
def test_update_settings_token_test_fails(self, mock_settings, mock_get_token, client: TestClient):
def test_update_settings_token_test_fails(
self, mock_settings, mock_get_token, client: TestClient
):
"""Test update when token test fails."""
mock_get_token.side_effect = Exception("Token invalid")
response = client.post(
"/api/onedrive/update-settings", data={"refresh_token": "bad_token", "tenant_id": "common"}
"/api/onedrive/update-settings",
data={"refresh_token": "bad_token", "tenant_id": "common"},
)
assert response.status_code == 200
@@ -477,18 +554,26 @@ class TestUpdateOneDriveSettings:
def test_update_settings_missing_required_field(self, client: TestClient):
"""Test update without required refresh_token."""
response = client.post("/api/onedrive/update-settings", data={"tenant_id": "common"})
response = client.post(
"/api/onedrive/update-settings", data={"tenant_id": "common"}
)
assert response.status_code == 422
@patch("app.config.settings")
def test_update_settings_exception_handling(self, mock_settings, client: TestClient):
def test_update_settings_exception_handling(
self, mock_settings, client: TestClient
):
"""Test exception handling in update settings."""
mock_settings.onedrive_refresh_token = None
with patch("app.tasks.upload_to_onedrive.get_onedrive_token", side_effect=Exception("Fatal error")):
with patch(
"app.tasks.upload_to_onedrive.get_onedrive_token",
side_effect=Exception("Fatal error"),
):
response = client.post(
"/api/onedrive/update-settings", data={"refresh_token": "token", "tenant_id": "common"}
"/api/onedrive/update-settings",
data={"refresh_token": "token", "tenant_id": "common"},
)
# Should still update settings even if test fails
@@ -534,7 +619,9 @@ class TestGetOneDriveFullConfig:
assert "status" in data
@patch("app.config.settings")
def test_get_full_config_exception_handling(self, mock_settings, client: TestClient):
def test_get_full_config_exception_handling(
self, mock_settings, client: TestClient
):
"""Test exception handling in get full config."""
# Even with exception, endpoint catches it
response = client.get("/api/onedrive/get-full-config")
@@ -578,7 +665,10 @@ class TestOneDriveIntegration:
with patch("app.tasks.upload_to_onedrive.get_onedrive_token"):
response = client.post(
"/api/onedrive/update-settings",
data={"refresh_token": token_data["refresh_token"], "tenant_id": "common"},
data={
"refresh_token": token_data["refresh_token"],
"tenant_id": "common",
},
)
assert response.status_code == 200
@@ -586,7 +676,9 @@ class TestOneDriveIntegration:
@patch("requests.post")
@patch("requests.get")
@patch("app.config.settings")
def test_token_refresh_rotation(self, mock_settings, mock_get, mock_post, client: TestClient):
def test_token_refresh_rotation(
self, mock_settings, mock_get, mock_post, client: TestClient
):
"""Test token refresh with automatic rotation."""
type(mock_settings).onedrive_refresh_token = "old_token"
type(mock_settings).onedrive_client_id = "test_client_id"
@@ -606,7 +698,10 @@ class TestOneDriveIntegration:
mock_get_response = Mock()
mock_get_response.status_code = 200
mock_get_response.json.return_value = {"displayName": "Test User", "userPrincipalName": "test@example.com"}
mock_get_response.json.return_value = {
"displayName": "Test User",
"userPrincipalName": "test@example.com",
}
mock_get.return_value = mock_get_response
with patch("os.path.exists", return_value=False):