| 1 |
import {expect, test} from "@playwright/test"; |
| 2 |
import {login} from "../helpers/auth.mjs"; |
| 3 |
|
| 4 |
async function logAlertOnError(page, locator, fn) { |
| 5 |
try { |
| 6 |
await fn(); |
| 7 |
} catch (e) { |
| 8 |
const alertText = await locator.textContent(); |
| 9 |
// eslint-disable-next-line no-console |
| 10 |
console.log("[E2E] Alert error text:", alertText); |
| 11 |
throw e; |
| 12 |
} |
| 13 |
} |
| 14 |
|
| 15 |
// Helper function for sequentially filling in fields |
| 16 |
function fillSequentially(elements, values) { |
| 17 |
return elements.reduce((promise, el, i) => promise.then(() => el.fill(values[i])), Promise.resolve()); |
| 18 |
} |
| 19 |
|
| 20 |
test("Config page: always checks order error and valid save for actions", async ({page}, testInfo) => { |
| 21 |
const {enablePassword} = testInfo.project.use.rspamdPasswords; |
| 22 |
await login(page, enablePassword); |
| 23 |
|
| 24 |
await page.locator("#configuration_nav").click(); |
| 25 |
await expect(page.locator("#actionsFormField")).toBeVisible({timeout: 10000}); |
| 26 |
|
| 27 |
function getInputs() { return page.locator("#actionsFormField input[data-id='action']"); } |
| 28 |
const alert = page.locator(".alert-danger, .alert-modal.alert-danger"); |
| 29 |
|
| 30 |
const inputs = getInputs(); |
| 31 |
const count = await inputs.count(); |
| 32 |
expect(count).toBeGreaterThan(0); |
| 33 |
await Promise.all( |
| 34 |
Array.from({length: count}, (_, i) => expect(inputs.nth(i)).toBeVisible()) |
| 35 |
); |
| 36 |
|
| 37 |
// Save the original values |
| 38 |
const values = await Promise.all(Array.from({length: count}, (_, i) => inputs.nth(i).inputValue())); |
| 39 |
|
| 40 |
// Determine only the fields actually available for input (not disabled, not readonly) |
| 41 |
const fillableChecks = Array.from({length: count}, (_, i) => (async () => { |
| 42 |
const input = inputs.nth(i); |
| 43 |
const isDisabled = await input.isDisabled(); |
| 44 |
const isReadOnly = await input.evaluate((el) => el.hasAttribute("readonly")); |
| 45 |
return !isDisabled && !isReadOnly ? i : null; |
| 46 |
})()); |
| 47 |
const fillableIndices = (await Promise.all(fillableChecks)).filter((i) => i !== null); |
| 48 |
|
| 49 |
const fillableInputs = fillableIndices.map((i) => inputs.nth(i)); |
| 50 |
|
| 51 |
// 1. Correct order: strictly decreasing sequence |
| 52 |
const correctOrder = fillableIndices.map((_, idx) => (idx * 10).toString()); |
| 53 |
|
| 54 |
await fillSequentially(fillableInputs, correctOrder); |
| 55 |
|
| 56 |
await page.locator("#saveActionsBtn").click(); |
| 57 |
|
| 58 |
await logAlertOnError(page, alert, async () => { |
| 59 |
await expect(alert).not.toBeVisible({timeout: 2000}); |
| 60 |
}); |
| 61 |
|
| 62 |
// Reload the configuration and make sure the new value has been saved |
| 63 |
await page.locator("#refresh").click(); |
| 64 |
await page.locator("#configuration_nav").click(); |
| 65 |
|
| 66 |
const reloadedInputs = getInputs(); |
| 67 |
const reloadedCount = await reloadedInputs.count(); |
| 68 |
|
| 69 |
// Recalculate the fillable fields after reload |
| 70 |
const reloadedFillableChecks = Array.from({length: reloadedCount}, (_, i) => (async () => { |
| 71 |
const input = reloadedInputs.nth(i); |
| 72 |
const isDisabled = await input.isDisabled(); |
| 73 |
const isReadOnly = await input.evaluate((el) => el.hasAttribute("readonly")); |
| 74 |
return !isDisabled && !isReadOnly ? i : null; |
| 75 |
})()); |
| 76 |
const reloadedFillableIndices = (await Promise.all(reloadedFillableChecks)).filter((i) => i !== null); |
| 77 |
const reloadedFillableInputs = reloadedFillableIndices.map((i) => reloadedInputs.nth(i)); |
| 78 |
|
| 79 |
await Promise.all(reloadedFillableInputs.map((input) => expect(input).toBeVisible())); |
| 80 |
|
| 81 |
const saved = await Promise.all(reloadedFillableInputs.map((input) => input.inputValue())); |
| 82 |
expect(saved).toEqual(correctOrder); |
| 83 |
|
| 84 |
// 2. Break the order: increasing sequence |
| 85 |
const wrongOrder = reloadedFillableIndices.map((_, idx) => ((reloadedFillableIndices.length - idx) * 10).toString()); |
| 86 |
|
| 87 |
await fillSequentially(reloadedFillableInputs, wrongOrder); |
| 88 |
|
| 89 |
await page.locator("#saveActionsBtn").click(); |
| 90 |
|
| 91 |
await expect(alert).toBeVisible({timeout: 10000}); |
| 92 |
const alertText = await alert.textContent(); |
| 93 |
expect(alertText).toContain("Incorrect order of actions thresholds"); |
| 94 |
|
| 95 |
// Restore the original values |
| 96 |
await fillSequentially(reloadedFillableInputs, values); |
| 97 |
|
| 98 |
await page.locator("#saveActionsBtn").click(); |
| 99 |
}); |