|
6 | 6 |
|
7 | 7 | import time |
8 | 8 | import os |
| 9 | +import requests |
9 | 10 | from selenium.webdriver.common.by import By |
10 | 11 | from selenium.webdriver.support.ui import WebDriverWait |
11 | 12 | from selenium.webdriver.support import expected_conditions as EC |
|
14 | 15 | # Add test directory to path |
15 | 16 | sys.path.insert(0, os.path.dirname(__file__)) |
16 | 17 |
|
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] |
18 | 19 |
|
19 | 20 |
|
20 | 21 | def test_settings_page_loads(driver): |
@@ -156,41 +157,75 @@ def test_save_settings_no_loss_of_data(driver): |
156 | 157 |
|
157 | 158 | This test verifies that the saveSettings() function properly: |
158 | 159 | 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 |
161 | 163 | """ |
162 | 164 | driver.get(f"{BASE_URL}/settings.php") |
163 | 165 | time.sleep(3) |
164 | 166 |
|
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" |
171 | 175 | return |
172 | 176 |
|
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}") |
174 | 180 |
|
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 |
176 | 188 | save_btn = driver.find_element(By.CSS_SELECTOR, "button#save") |
177 | 189 | driver.execute_script("arguments[0].click();", save_btn) |
178 | 190 | time.sleep(3) |
179 | 191 |
|
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" |
183 | 209 |
|
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 | + } |
187 | 213 |
|
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}" |
192 | 217 |
|
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}" |
194 | 220 |
|
| 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}" |
195 | 230 |
|
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