import sqlite3 import importlib import pytest server_app_module = importlib.import_module("server.app") # Expose app and init_db from the imported module app = server_app_module.app init_db = server_app_module.init_db @pytest.fixture def client(): with app.test_client() as client: yield client def test_get_app_settings_api_requires_auth(client): """Test that getting app settings requires authentication.""" resp = client.get("/admin/api/settings") assert resp.status_code == 302 assert resp.headers["Location"] == "/auth/login" def test_get_app_settings_api_with_auth(client): """Test getting app settings via API when authenticated.""" # Login first client.post("/auth/login", data={"username": "admin", "password": "admin"}) resp = client.get("/admin/api/settings") assert resp.status_code == 200 data = resp.get_json() assert data["status"] == "ok" assert "settings" in data assert isinstance(data["settings"], dict) def test_update_app_setting_api_requires_auth(client): """Test that updating app settings requires authentication.""" resp = client.put("/admin/api/settings/test_key", json={"value": "test_value"}) assert resp.status_code == 302 assert resp.headers["Location"] == "/auth/login" def test_update_app_setting_api_with_auth(client): """Test updating app settings via API when authenticated.""" # Login first client.post("/auth/login", data={"username": "admin", "password": "admin"}) # Update a setting resp = client.put("/admin/api/settings/test_key", json={"value": "test_value"}) assert resp.status_code == 200 data = resp.get_json() assert data["status"] == "ok" assert "updated successfully" in data["message"] # Verify it was saved resp = client.get("/admin/api/settings") data = resp.get_json() assert data["settings"]["test_key"] == "test_value" def test_delete_app_setting_api_with_auth(client): """Test deleting app settings via API when authenticated.""" # Login first client.post("/auth/login", data={"username": "admin", "password": "admin"}) # Add a setting first client.put("/admin/api/settings/delete_test", json={"value": "to_delete"}) # Delete the setting resp = client.delete("/admin/api/settings/delete_test") assert resp.status_code == 200 data = resp.get_json() assert data["status"] == "ok" assert "deleted successfully" in data["message"]