优化
This commit is contained in:
@@ -6,6 +6,7 @@ import { createMemoryHistory, createRouter } from 'vue-router'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { useAccessPreviewStore } from '@/core/access/accessPreview'
|
||||
import { useAuthStore } from '@/core/auth/auth'
|
||||
import { ApiResponseError } from '@/shared/api/http'
|
||||
import LoginView from './LoginView.vue'
|
||||
|
||||
const loginSource = readFileSync(resolve(process.cwd(), 'src/modules/auth/pages/LoginView.vue'), 'utf8')
|
||||
@@ -177,4 +178,50 @@ describe('LoginView authentication lifecycle', () => {
|
||||
expect(initialize).not.toHaveBeenCalled()
|
||||
expect(replace).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('shows the backend credential error and diagnostic identifiers', async () => {
|
||||
const pinia = createPinia()
|
||||
setActivePinia(pinia)
|
||||
const auth = useAuthStore()
|
||||
const router = createRouter({
|
||||
history: createMemoryHistory(),
|
||||
routes: [
|
||||
{ path: '/login', component: LoginView },
|
||||
{ path: '/', component: { template: '<p>home</p>' } },
|
||||
],
|
||||
})
|
||||
await router.push('/login')
|
||||
await router.isReady()
|
||||
vi.spyOn(auth, 'login').mockRejectedValue(new ApiResponseError(
|
||||
'用户名或密码错误',
|
||||
'A0401',
|
||||
undefined,
|
||||
null,
|
||||
'request-login',
|
||||
'trace-login',
|
||||
))
|
||||
const wrapper = mount(LoginView, {
|
||||
global: {
|
||||
plugins: [pinia, router],
|
||||
stubs: {
|
||||
'el-form': formStub,
|
||||
'el-form-item': { template: '<label><slot /></label>' },
|
||||
'el-input': { props: ['modelValue'], emits: ['update:modelValue'], template: '<input :value="modelValue" @input="$emit(\'update:modelValue\', $event.target.value)" />' },
|
||||
'el-button': { emits: ['click'], template: '<button type="button" @click="$emit(\'click\')"><slot /></button>' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const inputs = wrapper.findAll('input')
|
||||
await inputs[0]!.setValue('platform_admin')
|
||||
await inputs[1]!.setValue('incorrect-password')
|
||||
await wrapper.get('form').trigger('submit')
|
||||
await flushPromises()
|
||||
|
||||
const alert = wrapper.get('[role="alert"]').text()
|
||||
expect(alert).toContain('用户名或密码错误')
|
||||
expect(alert).toContain('request-login')
|
||||
expect(alert).toContain('trace-login')
|
||||
expect(alert).not.toContain('登录状态已失效')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -41,7 +41,11 @@ async function submit() {
|
||||
: requestedPath
|
||||
await router.replace(targetPath)
|
||||
} catch (error) {
|
||||
loginError.value = getApiErrorDetails(error, '登录失败,请检查账号和密码后重试')
|
||||
loginError.value = getApiErrorDetails(
|
||||
error,
|
||||
'登录失败,请检查账号和密码后重试',
|
||||
{ preserveAuthenticationMessage: true },
|
||||
)
|
||||
ElMessage.error(loginError.value.message)
|
||||
} finally {
|
||||
loading.value = false
|
||||
|
||||
Reference in New Issue
Block a user