fix: clear reset secrets and isolate recovery tests
This commit is contained in:
1 parent
63db81e498
commit
62a23fb04c
2 files changed
+35
-18
No files matched your search
+16
-2
@@ -38,7 +38,13 @@ export default function Modals({ modal, onClose, reportPostId }) {
|
|||||||
if (modal === 'register') {
|
if (modal === 'register') {
|
||||||
API.getCaptcha().then((c) => setRegCaptcha(c)).catch(() => {});
|
API.getCaptcha().then((c) => setRegCaptcha(c)).catch(() => {});
|
||||||
}
|
}
|
||||||
if (modal !== 'login') setAuthMode('login');
|
if (modal !== 'login') {
|
||||||
|
setAuthMode('login');
|
||||||
|
setResetEmail('');
|
||||||
|
setResetRecoveryCode('');
|
||||||
|
setResetPassword('');
|
||||||
|
setResetPasswordConfirm('');
|
||||||
|
}
|
||||||
}, [modal]);
|
}, [modal]);
|
||||||
|
|
||||||
if (!modal) return null;
|
if (!modal) return null;
|
||||||
@@ -91,6 +97,14 @@ export default function Modals({ modal, onClose, reportPostId }) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const returnToLogin = () => {
|
||||||
|
setAuthMode('login');
|
||||||
|
setResetEmail('');
|
||||||
|
setResetRecoveryCode('');
|
||||||
|
setResetPassword('');
|
||||||
|
setResetPasswordConfirm('');
|
||||||
|
};
|
||||||
|
|
||||||
const handleRegister = async () => {
|
const handleRegister = async () => {
|
||||||
if (!regUsername.trim() || !regEmail.trim() || !regPassword) { toast('请填写完整信息', 'warning'); return; }
|
if (!regUsername.trim() || !regEmail.trim() || !regPassword) { toast('请填写完整信息', 'warning'); return; }
|
||||||
if (regPassword.length < 6) { toast('密码至少6位', 'warning'); return; }
|
if (regPassword.length < 6) { toast('密码至少6位', 'warning'); return; }
|
||||||
@@ -169,7 +183,7 @@ export default function Modals({ modal, onClose, reportPostId }) {
|
|||||||
onKeyDown={(e) => { if (e.key === 'Enter') handleResetPassword(); }}
|
onKeyDown={(e) => { if (e.key === 'Enter') handleResetPassword(); }}
|
||||||
/>
|
/>
|
||||||
<button className="btn-primary" style={{ width: '100%' }} onClick={handleResetPassword}>{t('auth.resetPassword')}</button>
|
<button className="btn-primary" style={{ width: '100%' }} onClick={handleResetPassword}>{t('auth.resetPassword')}</button>
|
||||||
<button type="button" className="auth-forgot-link" onClick={() => setAuthMode('login')}>
|
<button type="button" className="auth-forgot-link" onClick={returnToLogin}>
|
||||||
{t('auth.backToLogin')}
|
{t('auth.backToLogin')}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+19
-16
@@ -22,13 +22,13 @@ async function api(path, options = {}) {
|
|||||||
let adminToken = null;
|
let adminToken = null;
|
||||||
let adminId = null;
|
let adminId = null;
|
||||||
let userId = null;
|
let userId = null;
|
||||||
let userToken = null;
|
|
||||||
let postId = null;
|
let postId = null;
|
||||||
let postNumber = null;
|
let postNumber = null;
|
||||||
let replyId = null;
|
let replyId = null;
|
||||||
|
|
||||||
const adminUser = { email: `admin${suffix}@test.com`, password: '123456', username: `admin${suffix}` };
|
const adminUser = { email: `admin${suffix}@test.com`, password: '123456', username: `admin${suffix}` };
|
||||||
const normalUser = { email: `user${suffix}@test.com`, password: '123456', username: `user${suffix}` };
|
const normalUser = { email: `user${suffix}@test.com`, password: '123456', username: `user${suffix}` };
|
||||||
|
const resetUser = { email: `reset${suffix}@test.com`, password: '123456', username: `reset${suffix}` };
|
||||||
|
|
||||||
test('注册第一个用户自动成为管理员', async () => {
|
test('注册第一个用户自动成为管理员', async () => {
|
||||||
const { status, data } = await api('/auth/register', {
|
const { status, data } = await api('/auth/register', {
|
||||||
@@ -68,20 +68,23 @@ test('注册普通用户', async () => {
|
|||||||
userId = data.user.id;
|
userId = data.user.id;
|
||||||
});
|
});
|
||||||
|
|
||||||
test('普通用户登录', async () => {
|
|
||||||
const { status, data } = await api('/auth/login', {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify({ email: normalUser.email, password: normalUser.password }),
|
|
||||||
});
|
|
||||||
assert.equal(status, 200);
|
|
||||||
assert.ok(data.token);
|
|
||||||
userToken = data.token;
|
|
||||||
});
|
|
||||||
|
|
||||||
test('恢复码可重置密码且只能使用一次', async () => {
|
test('恢复码可重置密码且只能使用一次', async () => {
|
||||||
|
const registered = await api('/auth/register', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(resetUser),
|
||||||
|
});
|
||||||
|
assert.equal(registered.status, 200);
|
||||||
|
|
||||||
|
const loggedIn = await api('/auth/login', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ email: resetUser.email, password: resetUser.password }),
|
||||||
|
});
|
||||||
|
assert.equal(loggedIn.status, 200);
|
||||||
|
assert.ok(loggedIn.data.token);
|
||||||
|
|
||||||
const generated = await api('/auth/recovery-codes/generate', {
|
const generated = await api('/auth/recovery-codes/generate', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { Authorization: `Bearer ${userToken}` },
|
headers: { Authorization: `Bearer ${loggedIn.data.token}` },
|
||||||
});
|
});
|
||||||
assert.equal(generated.status, 200);
|
assert.equal(generated.status, 200);
|
||||||
assert.equal(generated.data.codes.length, 10);
|
assert.equal(generated.data.codes.length, 10);
|
||||||
@@ -90,25 +93,25 @@ test('恢复码可重置密码且只能使用一次', async () => {
|
|||||||
const newPassword = '654321';
|
const newPassword = '654321';
|
||||||
const reset = await api('/auth/reset-password', {
|
const reset = await api('/auth/reset-password', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ email: normalUser.email, recoveryCode, newPassword }),
|
body: JSON.stringify({ email: resetUser.email, recoveryCode, newPassword }),
|
||||||
});
|
});
|
||||||
assert.equal(reset.status, 200);
|
assert.equal(reset.status, 200);
|
||||||
|
|
||||||
const oldLogin = await api('/auth/login', {
|
const oldLogin = await api('/auth/login', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ email: normalUser.email, password: normalUser.password }),
|
body: JSON.stringify({ email: resetUser.email, password: resetUser.password }),
|
||||||
});
|
});
|
||||||
assert.equal(oldLogin.status, 401);
|
assert.equal(oldLogin.status, 401);
|
||||||
|
|
||||||
const newLogin = await api('/auth/login', {
|
const newLogin = await api('/auth/login', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ email: normalUser.email, password: newPassword }),
|
body: JSON.stringify({ email: resetUser.email, password: newPassword }),
|
||||||
});
|
});
|
||||||
assert.equal(newLogin.status, 200);
|
assert.equal(newLogin.status, 200);
|
||||||
|
|
||||||
const reused = await api('/auth/reset-password', {
|
const reused = await api('/auth/reset-password', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ email: normalUser.email, recoveryCode, newPassword: normalUser.password }),
|
body: JSON.stringify({ email: resetUser.email, recoveryCode, newPassword: resetUser.password }),
|
||||||
});
|
});
|
||||||
assert.equal(reused.status, 400);
|
assert.equal(reused.status, 400);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user