43 lines
2.4 KiB
TypeScript
43 lines
2.4 KiB
TypeScript
import { expect, test } from '@playwright/test'
|
|
|
|
test('username login, private empty space, logout and history protection', async ({ page }) => {
|
|
const user = { id: 42, username: 'fictional-reader', role: 'learner' }
|
|
await page.route('**/api/v1/**', async route => {
|
|
const path = new URL(route.request().url()).pathname
|
|
let data: unknown = null
|
|
if (path.endsWith('/login')) {
|
|
expect(route.request().postDataJSON()).toEqual({ username: user.username, password: 'fictional-password' })
|
|
data = { token: 'fictional-session', user }
|
|
} else if (path.endsWith('/me')) data = user
|
|
else if (path.endsWith('/space')) data = { ownerId: user.id, language: 'en' }
|
|
await route.fulfill({ json: { code: 200, data } })
|
|
})
|
|
await page.goto('/')
|
|
await expect(page.getByRole('heading', { name: '欢迎回来' })).toBeVisible()
|
|
await page.getByLabel('账号').fill(user.username)
|
|
await page.getByLabel('密码', { exact: true }).fill('fictional-password')
|
|
await page.getByRole('button', { name: '登录', exact: true }).click()
|
|
await expect(page.getByRole('heading', { name: '我的书库' })).toBeVisible()
|
|
await expect(page.getByText('书库还是空的')).toBeVisible()
|
|
await expect(page.getByText(user.username, { exact: true })).toBeVisible()
|
|
await page.reload()
|
|
await expect(page.getByRole('heading', { name: '我的书库' })).toBeVisible()
|
|
await page.goto('/?history=1')
|
|
await expect(page.getByRole('heading', { name: '我的书库' })).toBeVisible()
|
|
await page.getByRole('button', { name: '退出登录' }).click()
|
|
await expect(page.getByRole('heading', { name: '欢迎回来' })).toBeVisible()
|
|
await page.goBack()
|
|
await expect(page.getByText(user.username, { exact: true })).toHaveCount(0)
|
|
await expect(page.getByRole('heading', { name: '欢迎回来' })).toBeVisible()
|
|
})
|
|
|
|
test('invalid credentials stay on login with accessible feedback', async ({ page }) => {
|
|
await page.route('**/api/v1/login', route => route.fulfill({ status: 401, json: { code: 401, msg: '账号或密码错误' } }))
|
|
await page.goto('/login')
|
|
await page.getByLabel('账号').fill('fictional-reader')
|
|
await page.getByLabel('密码', { exact: true }).fill('fictional-wrong-password')
|
|
await page.getByRole('button', { name: '登录', exact: true }).click()
|
|
await expect(page.getByRole('alert')).toHaveText('账号或密码错误')
|
|
await expect(page.getByRole('heading', { name: '我的书库' })).toHaveCount(0)
|
|
})
|