feat(webui): strengthen password change guidance
Use consistent field labels, validation, and strength guidance in both password-change flows. Show requirements only while the new-password field is active and surface validation failures clearly.
This commit is contained in:
8 files changed
+1210
-132
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import { renderToStaticMarkup } from 'react-dom/server';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { FloatingLabelInput } from '../src/components/interior/floating-label';
|
||||
import { InlineValidation } from '../src/components/interior/inline-validation';
|
||||
import {
|
||||
PasswordStrength,
|
||||
type PasswordRule,
|
||||
} from '../src/components/interior/password-strength';
|
||||
|
||||
const RULES: readonly PasswordRule[] = [
|
||||
{ id: 'length', label: '长度不少于 10 位', test: (value) => value.length >= 10 },
|
||||
{ id: 'upper', label: '包含大写字母', test: (value) => /[A-Z]/.test(value) },
|
||||
];
|
||||
|
||||
describe('password input guidance', () => {
|
||||
it('renders an accessible floating password label', () => {
|
||||
const markup = renderToStaticMarkup(
|
||||
<FloatingLabelInput
|
||||
id="password"
|
||||
label="新密码"
|
||||
type="password"
|
||||
value=""
|
||||
required
|
||||
onChange={() => {}}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(markup).toContain('id="password"');
|
||||
expect(markup).toContain('for="password"');
|
||||
expect(markup).toContain('新密码');
|
||||
expect(markup).toContain('aria-required="true"');
|
||||
});
|
||||
|
||||
it('keeps the strength details out of the tree while hidden', () => {
|
||||
const markup = renderToStaticMarkup(
|
||||
<PasswordStrength value="SnowLuma-Test" rules={RULES} visible={false} />,
|
||||
);
|
||||
|
||||
expect(markup).not.toContain('role="meter"');
|
||||
expect(markup).not.toContain('长度不少于 10 位');
|
||||
});
|
||||
|
||||
it('shows the evaluated strength details while visible', () => {
|
||||
const markup = renderToStaticMarkup(
|
||||
<PasswordStrength value="SnowLuma-Test" rules={RULES} visible />,
|
||||
);
|
||||
|
||||
expect(markup).toContain('role="meter"');
|
||||
expect(markup).toContain('aria-valuenow="2"');
|
||||
expect(markup).toContain('长度不少于 10 位');
|
||||
expect(markup).toContain('包含大写字母');
|
||||
});
|
||||
|
||||
it('renders inline validation with its hint and status channel', () => {
|
||||
const markup = renderToStaticMarkup(
|
||||
<InlineValidation
|
||||
id="confirm-password"
|
||||
label="确认新密码"
|
||||
value=""
|
||||
onChange={() => {}}
|
||||
validate={(value) => value ? null : '请再次输入新密码'}
|
||||
hint="需要与新密码完全一致"
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(markup).toContain('确认新密码');
|
||||
expect(markup).toContain('需要与新密码完全一致');
|
||||
expect(markup).toContain('role="status"');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { createApiClient } from '../src/lib/api/client';
|
||||
|
||||
const tokenStore = {
|
||||
load: () => 'test-token',
|
||||
save: () => undefined,
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('password strength API client', () => {
|
||||
it('returns the server-owned rule evaluation', async () => {
|
||||
const rules = [
|
||||
{ id: 'length', label: '长度不少于 10 位', ok: true },
|
||||
{ id: 'no-space', label: '不包含空格', ok: true },
|
||||
];
|
||||
const fetchMock = vi.fn(async () => new Response(JSON.stringify({
|
||||
rules,
|
||||
valid: true,
|
||||
}), {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
}));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const client = createApiClient({ tokenStore });
|
||||
await expect(client.checkPasswordStrength('Strong-password')).resolves.toEqual({
|
||||
rules,
|
||||
valid: true,
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/auth/check-strength', expect.objectContaining({
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ password: 'Strong-password' }),
|
||||
headers: {
|
||||
Authorization: 'Bearer test-token',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
signal: expect.any(AbortSignal),
|
||||
}));
|
||||
});
|
||||
|
||||
it('rejects malformed responses instead of silently disabling the form', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
|
||||
valid: false,
|
||||
}), {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
})));
|
||||
|
||||
const client = createApiClient({ tokenStore });
|
||||
await expect(client.checkPasswordStrength('candidate')).rejects.toMatchObject({
|
||||
status: 502,
|
||||
message: '密码强度接口返回了无效响应',
|
||||
});
|
||||
});
|
||||
|
||||
it('propagates request failures for observable inline validation', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
|
||||
message: 'strength service unavailable',
|
||||
}), {
|
||||
status: 503,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
})));
|
||||
|
||||
const client = createApiClient({ tokenStore });
|
||||
await expect(client.checkPasswordStrength('candidate')).rejects.toMatchObject({
|
||||
status: 503,
|
||||
message: 'strength service unavailable',
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user