Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

153 linhas
6.6 KiB

  1. import type { Page, Route } from '@playwright/test'
  2. import { captureScreenshot, expect, test } from './fixtures'
  3. test.use({ trace: 'off', video: 'off' })
  4. const loginContext = {
  5. defaultRoleCode: 'admin',
  6. roles: [
  7. { code: 'teacher', label: '教员', accountLabel: '教员账号', organizationMode: 'CASCADE', organizationLabels: ['教学系', '教研室'] },
  8. { code: 'admin', label: '管理员', accountLabel: '管理员账号', organizationMode: 'NONE', organizationLabels: [] },
  9. ],
  10. departments: [
  11. {
  12. id: '100', code: 'TEST', name: '测试教学系', parentId: null,
  13. children: [{ id: '110', code: 'EMPTY', name: '空账号教研室', parentId: '100', children: [] }],
  14. },
  15. ],
  16. }
  17. const forcedPasswordProfile = {
  18. user: {
  19. id: '7', username: 'teacher.test', displayName: '测试教员',
  20. departmentId: '110', departmentName: '空账号教研室', mustChangePassword: true,
  21. },
  22. activeRoleId: '20',
  23. activeRole: { id: '20', code: 'teacher', name: '教员' },
  24. roles: [{ id: '20', code: 'teacher', name: '教员', status: 1 }],
  25. permissions: [],
  26. authorizationMode: 'SINGLE_ACTIVE',
  27. mustChangePassword: true,
  28. }
  29. const envelope = (data: unknown, message = '成功', code = 200) => JSON.stringify({
  30. code, message, data, timestamp: '2026-08-17T12:00:00+08:00', requestId: 'ute2e-zentao-auth',
  31. })
  32. const fulfill = (route: Route, data: unknown, status = 200, message = '成功', code = 200) => route.fulfill({
  33. status,
  34. contentType: 'application/json',
  35. body: envelope(data, message, code),
  36. })
  37. async function mockAuth(
  38. page: Page,
  39. options: { me?: 'forced' | 'expired'; initialPassword401?: boolean } = {},
  40. ) {
  41. let refreshRequests = 0
  42. let initialPasswordRequests = 0
  43. await page.route('**/api/auth/v1/**', async (route) => {
  44. const path = new URL(route.request().url()).pathname
  45. if (path.endsWith('/auth/login-context')) return fulfill(route, loginContext)
  46. if (path.endsWith('/auth/login-identities')) return fulfill(route, [])
  47. if (path.endsWith('/menus/navigation')) return fulfill(route, [])
  48. if (path.endsWith('/auth/me')) {
  49. if (options.me === 'expired') {
  50. return fulfill(route, null, 401, '内部会话详情不应展示', 40101)
  51. }
  52. return fulfill(route, forcedPasswordProfile)
  53. }
  54. if (path.endsWith('/auth/initial-password')) {
  55. initialPasswordRequests += 1
  56. if (options.initialPassword401) {
  57. return fulfill(route, null, 401, '账号已停用:internal-user-id=7', 40102)
  58. }
  59. return fulfill(route, {})
  60. }
  61. if (path.endsWith('/auth/refresh')) {
  62. refreshRequests += 1
  63. return fulfill(route, null, 401, 'refresh-token-internal-detail', 40101)
  64. }
  65. return fulfill(route, {})
  66. })
  67. return {
  68. refreshRequests: () => refreshRequests,
  69. initialPasswordRequests: () => initialPasswordRequests,
  70. }
  71. }
  72. async function selectOption(page: Page, select: ReturnType<Page['locator']>, text: string) {
  73. await select.locator('.el-select__wrapper').click()
  74. await page.locator('.el-select-dropdown:visible').getByRole('option', { name: text, exact: true }).click()
  75. }
  76. async function seedSession(page: Page, includeRefresh = false) {
  77. await page.addInitScript(({ refresh }) => {
  78. if (sessionStorage.getItem('zentao-auth-seeded') === '1') return
  79. sessionStorage.setItem('zentao-auth-seeded', '1')
  80. sessionStorage.setItem('unreal-tran:web:access-token:v1', 'zentao-access-token')
  81. if (refresh) sessionStorage.setItem('unreal-tran:web:refresh-token:v1', 'zentao-refresh-token')
  82. }, { refresh: includeRefresh })
  83. }
  84. test('859:部门没有可登录教员时账号选择器明确禁用且不可展开', async ({ page }, testInfo) => {
  85. await mockAuth(page)
  86. await page.goto('/login')
  87. await page.locator('[data-role-code="teacher"]').click()
  88. await selectOption(page, page.getByTestId('login-organization'), '测试教学系')
  89. await selectOption(page, page.getByTestId('login-department'), '空账号教研室')
  90. const identity = page.getByTestId('login-identity')
  91. await expect(page.getByText('当前条件下暂无可登录教员账号,请联系管理员。')).toBeVisible()
  92. await expect(identity).toBeDisabled()
  93. await identity.locator('.el-select__wrapper').click({ force: true })
  94. await expect(page.locator('.login-identity-popper:visible')).toHaveCount(0)
  95. await captureScreenshot(page, testInfo, '859-empty-teacher-account-disabled')
  96. })
  97. test('861:首次改密两次输入不一致时立即显示字段提示且不提交', async ({ page }, testInfo) => {
  98. await seedSession(page)
  99. const mocked = await mockAuth(page, { me: 'forced' })
  100. await page.goto('/change-password')
  101. const inputs = page.locator('.password-card input[autocomplete="new-password"]')
  102. await inputs.nth(0).fill('Strong#Password8')
  103. await inputs.nth(1).fill('Different#Pass9')
  104. await expect(page.getByText('两次输入的新密码不一致', { exact: true })).toBeVisible()
  105. await expect(page.getByRole('button', { name: '确认修改并登录' })).toBeDisabled()
  106. expect(mocked.initialPasswordRequests()).toBe(0)
  107. await captureScreenshot(page, testInfo, '861-initial-password-mismatch')
  108. })
  109. test('864:首次改密期间账号停用后仅显示一次固定登录提示且不刷新令牌', async ({ page }, testInfo) => {
  110. await seedSession(page, true)
  111. const mocked = await mockAuth(page, { me: 'forced', initialPassword401: true })
  112. await page.goto('/change-password')
  113. const inputs = page.locator('.password-card input[autocomplete="new-password"]')
  114. await inputs.nth(0).fill('Strong#Password8')
  115. await inputs.nth(1).fill('Strong#Password8')
  116. await page.getByRole('button', { name: '确认修改并登录' }).click()
  117. await expect(page).toHaveURL(/\/login\?redirect=/)
  118. await expect(page.locator('#login-error')).toHaveText('账号、密码或登录身份不匹配')
  119. await expect(page.getByText(/internal-user-id|refresh-token-internal-detail/)).toHaveCount(0)
  120. expect(mocked.initialPasswordRequests()).toBe(1)
  121. expect(mocked.refreshRequests()).toBe(0)
  122. await captureScreenshot(page, testInfo, '864-disabled-account-one-time-notice')
  123. await page.reload()
  124. await expect(page.locator('#login-error')).toHaveText('')
  125. })
  126. test('普通会话过期只跳转登录,不展示后端错误详情或停用账号提示', async ({ page }) => {
  127. await seedSession(page, true)
  128. const mocked = await mockAuth(page, { me: 'expired' })
  129. await page.goto('/dashboard')
  130. await expect(page).toHaveURL(/\/login\?redirect=/)
  131. await expect(page.locator('#login-error')).toHaveText('')
  132. await expect(page.getByText(/内部会话详情|refresh-token-internal-detail|账号、密码或登录身份不匹配/)).toHaveCount(0)
  133. expect(mocked.refreshRequests()).toBe(1)
  134. })