Skip to content

Commit 6dc30bb

Browse files
committed
FE: enhance settings tests to verify API persistence of PLUGINS_KEEP_HIST setting
1 parent 206c2e7 commit 6dc30bb

3 files changed

Lines changed: 64 additions & 25 deletions

File tree

front/plugins/sync/hub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function jsonResponse($status, $data = '', $message = '') {
4848
$apiRoot = getenv('NETALERTX_API') ?: '/tmp/api';
4949
$file_path = rtrim($apiRoot, '/') . '/table_devices.json';
5050

51-
$data = file_get_contents($file_path);
51+
$data = file_get_contents($file_path);
5252

5353
// Prepare the data to return as a JSON response
5454
$response_data = base64_encode($data);
@@ -75,7 +75,7 @@ function jsonResponse($status, $data = '', $message = '') {
7575
// // check location
7676
// if (!is_dir($storage_path)) {
7777
// echo "Could not open folder: {$storage_path}";
78-
// write_notification("[Plugin: SYNC] Could not open folder: {$storage_path}", "alert");
78+
// write_notification("[Plugin: SYNC] Could not open folder: {$storage_path}", "alert");
7979
// http_response_code(500);
8080
// exit;
8181
// }

test/ui/test_helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def get_api_token():
3030
return None
3131

3232

33+
# Load API_TOKEN at module initialization
34+
API_TOKEN = get_api_token()
35+
36+
3337
def get_driver(download_dir=None):
3438
"""Create a Selenium WebDriver for Chrome/Chromium
3539

test/ui/test_ui_settings.py

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import time
88
import os
9+
import requests
910
from selenium.webdriver.common.by import By
1011
from selenium.webdriver.support.ui import WebDriverWait
1112
from selenium.webdriver.support import expected_conditions as EC
@@ -14,7 +15,7 @@
1415
# Add test directory to path
1516
sys.path.insert(0, os.path.dirname(__file__))
1617

17-
from test_helpers import BASE_URL # noqa: E402 [flake8 lint suppression]
18+
from test_helpers import BASE_URL, API_TOKEN # noqa: E402 [flake8 lint suppression]
1819

1920

2021
def test_settings_page_loads(driver):
@@ -156,41 +157,75 @@ def test_save_settings_no_loss_of_data(driver):
156157
157158
This test verifies that the saveSettings() function properly:
158159
1. Loads all settings
159-
2. Preserves settings that weren't modified
160-
3. Saves without data loss
160+
2. Update PLUGINS_KEEP_HIST <input> - set to 333
161+
3. Saves
162+
4. Check API endpoint that the setting is updated correctly
161163
"""
162164
driver.get(f"{BASE_URL}/settings.php")
163165
time.sleep(3)
164166

165-
# Count the total number of setting inputs before save
166-
inputs_before = driver.find_elements(By.CSS_SELECTOR, "input, select, textarea")
167-
initial_count = len(inputs_before)
168-
169-
if initial_count == 0:
170-
assert True, "No settings inputs found"
167+
# Find the PLUGINS_KEEP_HIST input field
168+
plugins_keep_hist_input = None
169+
try:
170+
plugins_keep_hist_input = WebDriverWait(driver, 10).until(
171+
EC.presence_of_element_located((By.ID, "PLUGINS_KEEP_HIST"))
172+
)
173+
except:
174+
assert True, "PLUGINS_KEEP_HIST input not found, skipping test"
171175
return
172176

173-
print(f"Found {initial_count} settings inputs")
177+
# Get original value
178+
original_value = plugins_keep_hist_input.get_attribute("value")
179+
print(f"PLUGINS_KEEP_HIST original value: {original_value}")
174180

175-
# Click save without modifying anything
181+
# Set new value
182+
new_value = "333"
183+
plugins_keep_hist_input.clear()
184+
plugins_keep_hist_input.send_keys(new_value)
185+
time.sleep(1)
186+
187+
# Click save
176188
save_btn = driver.find_element(By.CSS_SELECTOR, "button#save")
177189
driver.execute_script("arguments[0].click();", save_btn)
178190
time.sleep(3)
179191

180-
# Reload the page
181-
driver.get(f"{BASE_URL}/settings.php")
182-
time.sleep(3)
192+
# Check for errors after save
193+
error_elements = driver.find_elements(By.CSS_SELECTOR, ".alert-danger, .error-message, .callout-danger")
194+
has_visible_error = False
195+
for elem in error_elements:
196+
if elem.is_displayed():
197+
error_text = elem.text
198+
if error_text and len(error_text) > 0:
199+
print(f"Found error message: {error_text}")
200+
has_visible_error = True
201+
break
202+
203+
assert not has_visible_error, "No error messages should be displayed after save"
204+
205+
# Verify via API endpoint /settings/<setKey>
206+
# Extract backend API URL from BASE_URL
207+
api_base = BASE_URL.replace('/front', '').replace(':20211', ':20212') # Switch to backend port
208+
api_url = f"{api_base}/settings/PLUGINS_KEEP_HIST"
183209

184-
# Count settings again
185-
inputs_after = driver.find_elements(By.CSS_SELECTOR, "input, select, textarea")
186-
final_count = len(inputs_after)
210+
headers = {
211+
"Authorization": f"Bearer {API_TOKEN}"
212+
}
187213

188-
# Should have the same number of settings (within 10% tolerance for dynamic elements)
189-
tolerance = max(1, int(initial_count * 0.1))
190-
assert abs(initial_count - final_count) <= tolerance, \
191-
f"Settings count should be preserved. Before: {initial_count}, After: {final_count}"
214+
try:
215+
response = requests.get(api_url, headers=headers, timeout=5)
216+
assert response.status_code == 200, f"API returned {response.status_code}: {response.text}"
192217

193-
print(f"✅ Settings preservation verified: {initial_count} -> {final_count}")
218+
data = response.json()
219+
assert data.get("success") == True, f"API returned success=false: {data}"
194220

221+
saved_value = str(data.get("value"))
222+
print(f"API /settings/PLUGINS_KEEP_HIST returned: {saved_value}")
223+
assert saved_value == new_value, \
224+
f"Setting not persisted correctly. Expected: {new_value}, Got: {saved_value}"
225+
226+
except requests.exceptions.RequestException as e:
227+
assert False, f"Error calling settings API: {e}"
228+
except Exception as e:
229+
assert False, f"Error verifying setting via API: {e}"
195230

196-
# Settings endpoint doesn't exist in Flask API - settings are managed via PHP/config files
231+
print(f"✅ Settings update verified via API: PLUGINS_KEEP_HIST changed to {new_value}")

0 commit comments

Comments
 (0)