Files
XHChat/tests/account-settings.test.mjs

65 lines
3.5 KiB
JavaScript

import assert from 'node:assert/strict'
import { randomBytes } from 'node:crypto'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import test from 'node:test'
import { _electron, chromium } from 'playwright'
import electron from 'electron'
const base = process.env.XHCHAT_TEST_URL ?? 'http://127.0.0.1:8080'
const work = (page) => page.getByRole('navigation', { name: '工作区' })
test('Windows 与 Web 修改密码和退出其他会话', { timeout: 45000 }, async () => {
const browser = await chromium.launch({ channel: process.env.XHCHAT_TEST_BROWSER ?? 'msedge' })
const profile = await mkdtemp(join(tmpdir(), 'xhchat-security-'))
let desktop
try {
const web = await browser.newPage({ viewport: { width: 390, height: 844 } })
await web.goto(base)
desktop = await _electron.launch({ executablePath: electron, args: [join(import.meta.dirname, '..', 'Desktop'), '--user-data-dir=' + profile],
env: { ...process.env, XHCHAT_SERVER_URL: base } })
const windows = await desktop.firstWindow()
const username = 'secure_' + randomBytes(6).toString('hex')
const password = randomBytes(24).toString('hex')
const nextPassword = randomBytes(24).toString('hex')
async function login(page, pass) {
await page.getByLabel('用户名').fill(username)
await page.getByLabel('密码', { exact: true }).fill(pass)
await page.locator('button.primary').click()
await page.getByText('实时连接已建立').waitFor()
}
await web.getByRole('button', { name: '注册', exact: true }).click()
await login(web,password)
await login(windows,password)
await windows.locator('.device-row').filter({ hasText: '当前设备' }).getByRole('button', { name:'重命名' }).click()
await windows.getByLabel('设备新名称').fill('工作电脑')
await windows.getByRole('button', { name:'保存设备名称' }).click()
await windows.getByText('工作电脑', { exact: true }).waitFor()
await work(web).getByRole('link', { name:'账号安全' }).click()
await web.getByRole('region', { name:'登录记录', exact: true }).getByText('Windows 电脑', { exact: true }).waitFor()
assert.equal(await web.evaluate(() => document.documentElement.scrollWidth), 390)
await web.getByRole('button', { name:'退出其他登录会话' }).click()
await web.getByRole('button', { name:'确认退出其他会话' }).click()
await windows.getByRole('heading', { name:'登录 XHChat' }).waitFor()
await web.getByText('其他登录会话已退出').waitFor()
await login(windows,password)
await windows.getByText('工作电脑', { exact: true }).waitFor()
await web.getByLabel('当前密码').fill(password)
await web.getByLabel('新密码', { exact: true }).fill(nextPassword)
await web.getByLabel('确认新密码').fill(nextPassword+'x')
await web.getByRole('button', { name:'修改密码并重新登录' }).click()
await web.getByText('两次输入的新密码不一致').waitFor()
await web.getByLabel('确认新密码').fill(nextPassword)
await web.getByRole('button', { name:'修改密码并重新登录' }).click()
await web.getByRole('heading', { name:'登录 XHChat' }).waitFor()
await windows.getByRole('heading', { name:'登录 XHChat' }).waitFor()
await login(windows,nextPassword)
await windows.getByText('工作电脑', { exact: true }).waitFor()
} finally {
await desktop?.close()
await browser.close()
await rm(profile, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 })
}
})