Files
anidev-design-system/tests/identity.mjs
T

380 lines
14 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { chromium } from "playwright";
import assert from "node:assert/strict";
import { mkdir, writeFile } from "node:fs/promises";
import { tokensFor } from "../concepts/registry.js";
import { identityStates } from "../patterns/demo-identity.js";
const base = process.env.BASE_URL || "http://127.0.0.1:4173";
const b = await chromium.launch({
executablePath:
process.env.PLAYWRIGHT_EXECUTABLE_PATH ||
"C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe",
headless: true,
});
const ctx = await b.newContext(),
p = await ctx.newPage();
p.setDefaultTimeout(7000);
const failures = [],
errors = [];
let passed = 0;
p.on("pageerror", (e) => errors.push(e.message));
const path = (page = "profile", theme = "light", locale = "ru", extra = "") =>
`${base}/playground/frame.html?concept=dock&page=${page}&theme=${theme}&locale=${locale}&density=compact${extra}`;
async function check(name, run) {
try {
await run();
passed++;
} catch (error) {
failures.push({ name, error: error.message });
console.log("FAIL", name, error.message);
}
}
const reset = async () => {
await p.goto(path("identity"));
await p.locator("[data-identity=reset]").click();
};
await mkdir("references/identity", { recursive: true });
try {
for (const theme of ["light", "dark"])
for (const locale of ["ru", "en"])
for (const [device, width, height] of [
["desktop", 1440, 900],
["tablet", 768, 1024],
["mobile", 390, 844],
])
for (const page of [
"profile",
"security",
"sessions",
"applications",
"appearance",
"identity",
]) {
await check(`${page}/${theme}/${locale}/${device}`, async () => {
await p.setViewportSize({ width, height });
await p.goto(path(page, theme, locale));
await p.locator("h1").waitFor();
assert.ok(
await p.evaluate(
() => document.documentElement.scrollWidth <= innerWidth + 1,
),
);
if (locale === "en")
assert.equal(
/[А-Яа-яЁё]/.test(await p.locator("body").innerText()),
false,
);
if (locale === "ru" && device !== "tablet") {
await p.waitForTimeout(320);
await p.screenshot({
path: `references/identity/${page}-${theme}-${device}.png`,
fullPage: true,
});
}
});
}
await p.setViewportSize({ width: 1440, height: 900 });
await check("Profile update, stable subject and cancel", async () => {
await p.goto(path());
await p.locator("[name=firstName]").fill("Anya");
await p.locator("#identity-profile [type=submit]").click();
assert.equal(
await p.locator(".identity-profile-summary h2").innerText(),
"Anya Volkova",
);
assert.match(
await p.locator(".identity-footnote").innerText(),
/demo-anna/,
);
await p.reload();
assert.equal(await p.locator("[name=firstName]").inputValue(), "Anya");
await p.locator("[name=firstName]").fill("Unsaved");
await p.locator("#identity-profile [type=reset]").click();
assert.equal(await p.locator("[name=firstName]").inputValue(), "Anya");
await reset();
});
await check("Profile markup is escaped", async () => {
await p.goto(path());
await p.locator("[name=firstName]").fill("<img src=x onerror=alert(1)>");
await p.locator("#identity-profile [type=submit]").click();
assert.equal(await p.locator(".identity-profile-summary img").count(), 0);
assert.match(
await p.locator(".identity-profile-summary").innerText(),
/<img/,
);
await reset();
});
await check("Avatar preview and persistence", async () => {
await p.goto(path());
const png = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aT90AAAAASUVORK5CYII=",
"base64",
);
await p.locator("#identity-avatar-file").setInputFiles({
name: "avatar.png",
mimeType: "image/png",
buffer: png,
});
await p.locator("#avatar-preview img").waitFor();
await p.locator("#identity-profile [type=submit]").click();
await p.reload();
await p.locator(".identity-profile-summary img").waitFor();
assert.equal(
await p
.locator(".identity-profile-summary img")
.evaluate((e) => getComputedStyle(e).objectFit),
"cover",
);
await reset();
});
await check("Email change request stays a demo", async () => {
await p.goto(path("security"));
await p.locator("[data-identity=password-link]").click();
assert.match(await p.locator("#dialog").innerText(), /1 час/);
await p.locator("[data-identity=email-sent]").click();
assert.match(await p.locator("#toast").innerText(), /не отправлено/);
});
await check("Passkey creation and removal", async () => {
await p.locator("[data-identity=add-passkey]").click();
await p.locator("#identity-passkey input").fill("Laptop demo");
await p.locator("#identity-passkey [type=submit]").click();
assert.equal(await p.locator("[data-identity=remove-passkey]").count(), 2);
await p.locator("[data-identity=remove-passkey]").last().click();
await p.locator("[data-identity=confirm-remove-key]").click();
assert.equal(await p.locator("[data-identity=remove-passkey]").count(), 1);
});
await check("Session revocation preserves current session", async () => {
await p.goto(path("sessions"));
await p.locator("[data-identity=revoke-session][data-session=s-2]").click();
await p.locator("[data-identity=confirm-revoke]").click();
assert.equal(await p.locator(".identity-session").count(), 2);
await p.locator("[data-identity=revoke-others]").click();
await p.locator("[data-identity=confirm-revoke-others]").click();
assert.equal(await p.locator(".identity-session").count(), 1);
assert.match(
await p.locator(".identity-session").innerText(),
/Текущий сеанс/,
);
await reset();
});
await check(
"SSO roles, custom role, bulk suspension, add grant and resume",
async () => {
await p.goto(path("users"));
await p.locator("#user-search").fill("anna@anidev.ru");
await p.locator('[data-edit="1"]').click();
await p.locator("#custom-role").fill("Release Manager");
await p.locator("[data-identity=add-role]").click();
await p.locator("[name=ssoRole][value=Auditor]").check();
await p.locator("#edit-user [type=submit]").click();
await p.locator('[data-edit="1"]').click();
assert.equal(
await p.locator('[name=ssoRole][value="Release Manager"]').isChecked(),
true,
);
assert.equal(
await p.locator("[name=ssoRole][value=Auditor]").isChecked(),
true,
);
await p.locator("#grant-all").check();
await p.locator(".grant-bulk [data-identity=grant-suspend]").click();
await p.locator("[name=reason]").fill("Maintenance window");
await p.locator("#show-reason").check();
await p.locator("#grant-suspend-form [type=submit]").click();
assert.equal(await p.locator(".identity-grants .error-text").count(), 3);
await p.waitForTimeout(350);
await p.screenshot({
path: "references/identity/user-access-light-desktop.png",
});
await p.keyboard.press("Escape");
await p.goto(path("applications"));
assert.equal(
await p.locator("[data-identity=open-app]:disabled").count(),
3,
);
assert.match(
await p.locator(".identity-apps").innerText(),
/Maintenance window/,
);
await p.goto(path("users"));
await p.locator("#user-search").fill("anna@anidev.ru");
await p.locator('[data-edit="1"]').click();
await p.locator("[data-identity=grant-add]").click();
await p.locator("#grant-add-form [type=submit]").click();
assert.equal(await p.locator("[data-grant]").count(), 4);
await p.locator(".grant-actions summary").first().click();
await p.locator("[data-identity=grant-resume]").first().click();
assert.equal(await p.locator(".identity-grants .status").count(), 2);
await p.keyboard.press("Escape");
await reset();
},
);
for (const status of identityStates.filter((s) => s !== "authenticated"))
await check(`Gate ${status} hides private content`, async () => {
await p.goto(path("profile", "dark", "ru", "&identityState=" + status));
await p.locator(".identity-gate").waitFor();
const text = await p.locator("body").innerText();
assert.equal(/Anna Volkova|anna@anidev.ru|demo-anna/.test(text), false);
assert.equal(await p.locator("#identity-profile").count(), 0);
});
await check("TOTP errors remain in the same dialog", async () => {
await p.goto(
path("identity", "light", "ru", "&identityState=factor_required"),
);
await p.locator("[data-identity=verify-factor]").click();
await p.locator("#identity-factor input").fill("000000");
assert.match(await p.locator("#factor-error").innerText(), /Неверный код/);
await p.locator("#identity-factor input").fill("123456");
assert.equal(await p.locator("#dialog").evaluate((e) => e.open), false);
await p.locator("#identity-state").waitFor();
assert.equal(
await p.locator("#identity-state").inputValue(),
"authenticated",
);
});
await check(
"Account change signs out and hides previous account, then loads new scope",
async () => {
await reset();
await p.goto(path());
await p.locator(".dock-account").click();
await p.locator("[data-identity=switch]").click();
assert.equal(await p.locator("[data-account]").count(), 0);
await p.locator("[data-identity=confirm-switch]").click();
await p.locator("#auth-form").waitFor();
assert.equal(
/Anna Volkova|anna@anidev.ru/.test(await p.locator("#app").innerText()),
false,
);
await p.locator("[name=email]").fill("mikhail.orlov");
await p.locator("[name=password]").fill("wrong-password");
await p.locator("#auth-form [type=submit]").click();
await p
.locator("#auth-error")
.filter({ hasText: "Неверный пароль" })
.waitFor();
assert.equal(
await p.locator("[name=email]").inputValue(),
"mikhail.orlov",
);
assert.equal(await p.locator("[name=password]").inputValue(), "");
await p.locator("[name=password]").fill("demo-password");
await p.locator("#auth-form [type=submit]").click();
await p.locator("#identity-profile").waitFor();
assert.match(
await p.locator(".identity-profile-summary").innerText(),
/Mikhail Orlov/,
);
await p.goto(path("sessions"));
assert.equal(await p.locator(".identity-session").count(), 1);
assert.match(await p.locator(".identity-session").innerText(), /Linux/);
await p.goto(path("users"));
await p.locator(".identity-gate").waitFor();
assert.equal(await p.locator("#user-search").count(), 0);
},
);
await check(
"Missing email requires verification before password request",
async () => {
await p.goto(path("security"));
await p.locator("[data-identity=password-link]").click();
await p.locator("#identity-email").waitFor();
await p
.locator("#identity-email [name=email]")
.fill("mikhail@example.com");
await p.locator("#identity-email [type=submit]").click();
await p.locator("[data-identity=password-link]").click();
assert.match(
await p.locator("#dialog").innerText(),
/mikhail@example.com/,
);
await p.keyboard.press("Escape");
await reset();
},
);
await check("Appearance theme and gallery synchronization", async () => {
await p.goto(path("appearance"));
await p.locator("[data-identity=theme][data-theme=dark]").click();
assert.equal(await p.locator("body").getAttribute("data-theme"), "dark");
await p.goto(base + "/playground/?concept=dock");
await p.locator("#page").selectOption("appearance");
await p
.frameLocator("#preview-a")
.locator("[data-identity=theme][data-theme=dark]")
.click();
await p.waitForFunction(
() => document.querySelector("#theme").value === "dark",
);
await p
.frameLocator("#preview-a")
.locator("body[data-theme=dark]")
.waitFor();
assert.equal(
(
await p
.frameLocator("#preview-a")
.locator("html")
.evaluate((el) => el.style.getPropertyValue("--color-accent"))
).trim(),
tokensFor("dock", "dark")["color.accent"],
);
const download = p.waitForEvent("download");
await p.locator("#export").click();
await (await download).saveAs("test-results/identity-export.zip");
});
await check(
"Identity change in comparison clears an open private dialog",
async () => {
await p.goto(base + "/playground/?concept=dock&page=security");
await p.locator("#compare").check();
const first = p.frameLocator("#preview-a"),
second = p.frameLocator("#preview-b");
await second.locator("h1").waitFor();
await first.locator("[data-identity=password-link]").click();
await first
.locator("#dialog")
.filter({ hasText: "anna@anidev.ru" })
.waitFor({ state: "visible" });
await second.locator("body").evaluate(() => {
const key = "anidev.identity.demo.v2";
const data = JSON.parse(sessionStorage.getItem(key));
data.status = "account_changed";
data.revision++;
sessionStorage.setItem(key, JSON.stringify(data));
});
await first.locator(".identity-gate").waitFor();
assert.equal(
await first.locator("#dialog").evaluate((d) => d.open),
false,
);
assert.equal(
/Anna Volkova|anna@anidev.ru/.test(
await first.locator("body").innerText(),
),
false,
);
},
);
await check("No identity runtime errors", async () =>
assert.deepEqual(errors, []),
);
} finally {
await writeFile(
"references/identity/qa.json",
JSON.stringify(
{
date: new Date().toISOString(),
passed,
failed: failures.length,
failures,
errors,
},
null,
2,
),
);
await ctx.close();
await b.close();
}
console.log(`Identity QA: ${passed} passed, ${failures.length} failed`);
if (failures.length) process.exitCode = 1;