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:
+16
-10
@@ -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"
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -8,7 +8,6 @@ import pytest
|
||||
|
||||
from app.utils.settings_service import get_setting_from_db, save_setting_to_db
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# TestSetupWizardDbPersist
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -49,7 +48,9 @@ class TestSetupWizardDbPersist:
|
||||
|
||||
@patch("app.views.wizard.notify_settings_updated")
|
||||
@patch("app.views.wizard.save_setting_to_db")
|
||||
def test_notify_not_called_when_no_settings_saved(self, mock_save, mock_notify, client):
|
||||
def test_notify_not_called_when_no_settings_saved(
|
||||
self, mock_save, mock_notify, client
|
||||
):
|
||||
"""Test that notify_settings_updated is NOT called when saved_count == 0."""
|
||||
mock_save.return_value = False
|
||||
|
||||
@@ -64,7 +65,9 @@ class TestSetupWizardDbPersist:
|
||||
@patch("app.views.wizard.notify_settings_updated")
|
||||
@patch("app.views.wizard.secrets.token_hex")
|
||||
@patch("app.views.wizard.save_setting_to_db")
|
||||
def test_auto_generate_session_secret(self, mock_save, mock_token, mock_notify, client):
|
||||
def test_auto_generate_session_secret(
|
||||
self, mock_save, mock_token, mock_notify, client
|
||||
):
|
||||
"""Test that session_secret auto-generate path produces a real token."""
|
||||
mock_save.return_value = True
|
||||
mock_token.return_value = "deadbeef" * 8
|
||||
@@ -130,9 +133,12 @@ class TestSetupWizardUndoSkip:
|
||||
class TestDropboxSaveSettingsDbPersist:
|
||||
"""Unit tests for save_dropbox_settings DB persistence."""
|
||||
|
||||
@patch("app.api.dropbox.settings")
|
||||
@patch("app.api.dropbox.notify_settings_updated")
|
||||
@patch("app.api.dropbox.save_setting_to_db")
|
||||
def test_db_written_even_when_env_missing(self, mock_save, mock_notify, client):
|
||||
def test_db_written_even_when_env_missing(
|
||||
self, mock_save, mock_notify, mock_settings, client
|
||||
):
|
||||
"""Test that DB is written even when .env doesn't exist (no exception)."""
|
||||
mock_save.return_value = True
|
||||
|
||||
@@ -148,9 +154,12 @@ class TestDropboxSaveSettingsDbPersist:
|
||||
assert data["status"] == "success"
|
||||
mock_save.assert_called()
|
||||
|
||||
@patch("app.api.dropbox.settings")
|
||||
@patch("app.api.dropbox.notify_settings_updated")
|
||||
@patch("app.api.dropbox.save_setting_to_db")
|
||||
def test_notify_settings_updated_called(self, mock_save, mock_notify, client):
|
||||
def test_notify_settings_updated_called(
|
||||
self, mock_save, mock_notify, mock_settings, client
|
||||
):
|
||||
"""Test that notify_settings_updated is called."""
|
||||
mock_save.return_value = True
|
||||
|
||||
@@ -163,9 +172,12 @@ class TestDropboxSaveSettingsDbPersist:
|
||||
|
||||
mock_notify.assert_called_once()
|
||||
|
||||
@patch("app.api.dropbox.settings")
|
||||
@patch("app.api.dropbox.notify_settings_updated")
|
||||
@patch("app.api.dropbox.save_setting_to_db")
|
||||
def test_all_provided_values_persisted(self, mock_save, mock_notify, client):
|
||||
def test_all_provided_values_persisted(
|
||||
self, mock_save, mock_notify, mock_settings, client
|
||||
):
|
||||
"""Test that all provided values are persisted to DB."""
|
||||
mock_save.return_value = True
|
||||
|
||||
@@ -197,9 +209,12 @@ class TestDropboxSaveSettingsDbPersist:
|
||||
class TestGoogleDriveUpdateSettingsDbPersist:
|
||||
"""Unit tests for update_google_drive_settings DB persistence."""
|
||||
|
||||
@patch("app.api.google_drive.settings")
|
||||
@patch("app.api.google_drive.notify_settings_updated")
|
||||
@patch("app.api.google_drive.save_setting_to_db")
|
||||
def test_db_written_for_each_provided_field(self, mock_save, mock_notify, client):
|
||||
def test_db_written_for_each_provided_field(
|
||||
self, mock_save, mock_notify, mock_settings, client
|
||||
):
|
||||
"""Test that DB is written for each provided field."""
|
||||
mock_save.return_value = True
|
||||
|
||||
@@ -223,9 +238,12 @@ class TestGoogleDriveUpdateSettingsDbPersist:
|
||||
assert "google_drive_folder_id" in keys_saved
|
||||
assert "google_drive_use_oauth" in keys_saved
|
||||
|
||||
@patch("app.api.google_drive.settings")
|
||||
@patch("app.api.google_drive.notify_settings_updated")
|
||||
@patch("app.api.google_drive.save_setting_to_db")
|
||||
def test_use_oauth_saved_as_lowercase_string(self, mock_save, mock_notify, client):
|
||||
def test_use_oauth_saved_as_lowercase_string(
|
||||
self, mock_save, mock_notify, mock_settings, client
|
||||
):
|
||||
"""Test that use_oauth is saved as 'true' or 'false' string."""
|
||||
mock_save.return_value = True
|
||||
|
||||
@@ -235,13 +253,18 @@ class TestGoogleDriveUpdateSettingsDbPersist:
|
||||
follow_redirects=False,
|
||||
)
|
||||
|
||||
use_oauth_calls = [call for call in mock_save.call_args_list if call[0][1] == "google_drive_use_oauth"]
|
||||
use_oauth_calls = [
|
||||
call
|
||||
for call in mock_save.call_args_list
|
||||
if call[0][1] == "google_drive_use_oauth"
|
||||
]
|
||||
assert len(use_oauth_calls) == 1
|
||||
assert use_oauth_calls[0][0][2] in ("true", "false")
|
||||
|
||||
@patch("app.api.google_drive.settings")
|
||||
@patch("app.api.google_drive.notify_settings_updated")
|
||||
@patch("app.api.google_drive.save_setting_to_db")
|
||||
def test_notify_called(self, mock_save, mock_notify, client):
|
||||
def test_notify_called(self, mock_save, mock_notify, mock_settings, client):
|
||||
"""Test that notify_settings_updated is called."""
|
||||
mock_save.return_value = True
|
||||
|
||||
@@ -263,9 +286,12 @@ class TestGoogleDriveUpdateSettingsDbPersist:
|
||||
class TestOneDriveSaveSettingsDbPersist:
|
||||
"""Unit tests for save_onedrive_settings DB persistence."""
|
||||
|
||||
@patch("app.api.onedrive.settings")
|
||||
@patch("app.api.onedrive.notify_settings_updated")
|
||||
@patch("app.api.onedrive.save_setting_to_db")
|
||||
def test_db_written_even_without_env_file(self, mock_save, mock_notify, client):
|
||||
def test_db_written_even_without_env_file(
|
||||
self, mock_save, mock_notify, mock_settings, client
|
||||
):
|
||||
"""Test that DB is written even when .env file does not exist."""
|
||||
mock_save.return_value = True
|
||||
|
||||
@@ -281,9 +307,10 @@ class TestOneDriveSaveSettingsDbPersist:
|
||||
assert data["status"] == "success"
|
||||
mock_save.assert_called()
|
||||
|
||||
@patch("app.api.onedrive.settings")
|
||||
@patch("app.api.onedrive.notify_settings_updated")
|
||||
@patch("app.api.onedrive.save_setting_to_db")
|
||||
def test_all_fields_persisted(self, mock_save, mock_notify, client):
|
||||
def test_all_fields_persisted(self, mock_save, mock_notify, mock_settings, client):
|
||||
"""Test that all provided fields are persisted to DB."""
|
||||
mock_save.return_value = True
|
||||
|
||||
@@ -307,9 +334,10 @@ class TestOneDriveSaveSettingsDbPersist:
|
||||
assert "onedrive_tenant_id" in keys_saved
|
||||
assert "onedrive_folder_path" in keys_saved
|
||||
|
||||
@patch("app.api.onedrive.settings")
|
||||
@patch("app.api.onedrive.notify_settings_updated")
|
||||
@patch("app.api.onedrive.save_setting_to_db")
|
||||
def test_notify_called(self, mock_save, mock_notify, client):
|
||||
def test_notify_called(self, mock_save, mock_notify, mock_settings, client):
|
||||
"""Test that notify_settings_updated is called."""
|
||||
mock_save.return_value = True
|
||||
|
||||
@@ -344,7 +372,7 @@ class TestGetSettingsForExport:
|
||||
|
||||
def test_source_effective_includes_metadata_keys(self, db_session):
|
||||
"""Test that source=effective includes keys from SETTING_METADATA."""
|
||||
from app.utils.settings_service import SETTING_METADATA, get_settings_for_export
|
||||
from app.utils.settings_service import get_settings_for_export
|
||||
|
||||
result = get_settings_for_export(db_session, source="effective")
|
||||
|
||||
@@ -407,7 +435,9 @@ class TestExportEnvEndpoint:
|
||||
mock_request = MagicMock()
|
||||
mock_admin = {"id": "admin", "is_admin": True}
|
||||
|
||||
result = asyncio.run(export_env_settings(mock_request, db_session, mock_admin, source="db"))
|
||||
result = asyncio.run(
|
||||
export_env_settings(mock_request, db_session, mock_admin, source="db")
|
||||
)
|
||||
assert result.media_type == "text/plain"
|
||||
|
||||
def test_content_disposition_header(self, db_session):
|
||||
@@ -419,7 +449,9 @@ class TestExportEnvEndpoint:
|
||||
mock_request = MagicMock()
|
||||
mock_admin = {"id": "admin", "is_admin": True}
|
||||
|
||||
result = asyncio.run(export_env_settings(mock_request, db_session, mock_admin, source="db"))
|
||||
result = asyncio.run(
|
||||
export_env_settings(mock_request, db_session, mock_admin, source="db")
|
||||
)
|
||||
cd = result.headers.get("content-disposition", "")
|
||||
assert "attachment" in cd
|
||||
assert ".env" in cd
|
||||
@@ -436,7 +468,11 @@ class TestExportEnvEndpoint:
|
||||
mock_admin = {"id": "admin", "is_admin": True}
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
asyncio.run(export_env_settings(mock_request, db_session, mock_admin, source="invalid"))
|
||||
asyncio.run(
|
||||
export_env_settings(
|
||||
mock_request, db_session, mock_admin, source="invalid"
|
||||
)
|
||||
)
|
||||
assert exc_info.value.status_code == 400
|
||||
|
||||
def test_default_source_is_db(self, db_session):
|
||||
@@ -461,7 +497,11 @@ class TestExportEnvEndpoint:
|
||||
mock_request = MagicMock()
|
||||
mock_admin = {"id": "admin", "is_admin": True}
|
||||
|
||||
result = asyncio.run(export_env_settings(mock_request, db_session, mock_admin, source="effective"))
|
||||
result = asyncio.run(
|
||||
export_env_settings(
|
||||
mock_request, db_session, mock_admin, source="effective"
|
||||
)
|
||||
)
|
||||
assert result.media_type == "text/plain"
|
||||
|
||||
def test_output_contains_docuelevate_header(self, db_session):
|
||||
@@ -473,5 +513,7 @@ class TestExportEnvEndpoint:
|
||||
mock_request = MagicMock()
|
||||
mock_admin = {"id": "admin", "is_admin": True}
|
||||
|
||||
result = asyncio.run(export_env_settings(mock_request, db_session, mock_admin, source="db"))
|
||||
result = asyncio.run(
|
||||
export_env_settings(mock_request, db_session, mock_admin, source="db")
|
||||
)
|
||||
assert b"DocuElevate" in result.body
|
||||
|
||||
Reference in New Issue
Block a user