from __future__ import annotations from uuid import uuid4 from playwright.sync_api import Page, expect from tests.e2e.helpers import expect_flash, expect_path, login def _unique_suffix() -> str: return uuid4().hex[:8] def test_admin_can_create_edit_reset_password_and_toggle_account( page: Page, base_url: str, admin_credentials: dict[str, str], ) -> None: suffix = _unique_suffix() username = f"e2e-user-{suffix}" updated_display_name = f"更新显示名{suffix}" login( page, base_url, username=admin_credentials["username"], password=admin_credentials["password"], expected_path="/", ) page.get_by_role("link", name="账号管理").click() expect_path(page, base_url, "/admin/accounts") expect(page.get_by_role("heading", name="账号管理")).to_be_visible() page.get_by_role("link", name="新增账号").click() expect_path(page, base_url, "/admin/accounts/new") page.locator("#username").fill(username) page.locator("#display_name").fill(f"初始显示名{suffix}") page.locator("#role").select_option("entry_only") page.locator("#password").fill("EntryPass123!") page.locator("#confirm_password").fill("EntryPass123!") page.get_by_role("button", name="创建账号").click() expect_path(page, base_url, "/admin/accounts") expect_flash(page, f"已创建账号 {username}。") account_row = page.locator("tr", has_text=username) expect(account_row.get_by_role("cell", name=username)).to_be_visible() expect(account_row.get_by_role("cell", name=f"初始显示名{suffix}")).to_be_visible() account_row.get_by_role("link", name="编辑").click() expect(page.get_by_role("heading", name="编辑账号")).to_be_visible() page.locator("#display_name").fill(updated_display_name) page.locator("#role").select_option("editor") page.get_by_role("button", name="保存账号信息").click() expect_path(page, base_url, "/admin/accounts") expect_flash(page, f"已更新账号 {username}。") account_row = page.locator("tr", has_text=username) expect(account_row.get_by_text(updated_display_name)).to_be_visible() expect(account_row.get_by_text("editor")).to_be_visible() account_row.get_by_role("link", name="重置密码").click() expect(page.get_by_role("heading", name="重置密码")).to_be_visible() page.locator("#password").fill("ResetPass123!") page.locator("#confirm_password").fill("ResetPass123!") page.get_by_role("button", name="确认重置").click() expect_path(page, base_url, "/admin/accounts") expect_flash(page, f"已重置账号 {username} 的密码。") account_row = page.locator("tr", has_text=username) account_row.get_by_role("button", name="停用").click() expect_flash(page, f"已将账号 {username} 设为停用。") account_row = page.locator("tr", has_text=username) expect(account_row.get_by_text("停用")).to_be_visible() expect(account_row.get_by_role("button", name="启用")).to_be_visible() account_row.get_by_role("button", name="启用").click() expect_flash(page, f"已将账号 {username} 设为启用。") account_row = page.locator("tr", has_text=username) expect(account_row.get_by_text("启用")).to_be_visible() expect(account_row.get_by_role("button", name="停用")).to_be_visible()