feat: add admin password reset

This commit is contained in:
2026-06-16 06:40:55 -06:00
parent 502afffa02
commit 979a70439b
8 changed files with 168 additions and 6 deletions
+36 -1
View File
@@ -14,7 +14,8 @@ vi.mock('../composables/useAuth', () => ({
describe('AdminPage', () => {
beforeEach(() => {
vi.clearAllMocks()
authedFetch.mockReset()
logout.mockReset()
authedFetch.mockResolvedValue([
{ id: 'u1', username: 'teacher', role: 'user', createdAt: '2026-01-01T00:00:00.000Z' },
])
@@ -33,8 +34,42 @@ describe('AdminPage', () => {
expect.arrayContaining(['ui-button', 'ui-button--primary']),
)
expect(wrapper.get('table').classes()).toContain('ui-table')
expect(wrapper.get('button[data-testid="reset-password-u1"]').classes()).toContain('ui-button')
expect(wrapper.get('button[data-testid="delete-user-u1"]').classes()).toEqual(
expect.arrayContaining(['ui-button', 'ui-button--danger']),
)
})
it('resets a user password after confirmation', async () => {
vi.spyOn(window, 'confirm').mockReturnValue(true)
authedFetch.mockResolvedValueOnce([
{ id: 'u1', username: 'teacher', role: 'user', createdAt: '2026-01-01T00:00:00.000Z' },
])
authedFetch.mockResolvedValueOnce({ ok: true })
const wrapper = mount(AdminPage)
await flushPromises()
await wrapper.get('button[data-testid="reset-password-u1"]').trigger('click')
await flushPromises()
expect(window.confirm).toHaveBeenCalledWith('确定要将该用户密码重置为 123456 吗?')
expect(authedFetch).toHaveBeenCalledWith('/api/admin/users/u1/reset-password', { method: 'POST' })
expect(wrapper.text()).toContain('已将密码重置为 123456。')
})
it('does not reset a password when confirmation is declined', async () => {
vi.spyOn(window, 'confirm').mockReturnValue(false)
authedFetch.mockResolvedValueOnce([
{ id: 'u1', username: 'teacher', role: 'user', createdAt: '2026-01-01T00:00:00.000Z' },
])
const wrapper = mount(AdminPage)
await flushPromises()
await wrapper.get('button[data-testid="reset-password-u1"]').trigger('click')
await flushPromises()
expect(authedFetch).toHaveBeenCalledTimes(1)
})
})