Files
shumai/packages/webui/routes/reset-password.test.ts
T
Yiling-J 19eed521ae fix(webui): prevent unwanted redirects to login on reset-password page (#487)
* fix(webui): allow reset-password route with token when stale auth exists

* fix(webui): prevent 401 redirect to login on reset-password page
2026-10-21 12:12:25 +08:00

40 lines
1.5 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'vitest'
import { useAuthStore } from '@/ui/stores/auth'
import { Route } from './reset-password'
describe('Reset Password Route beforeLoad guard', () => {
beforeEach(() => {
useAuthStore.getState().clearAuth()
})
it('redirects to / when user is authenticated in store and no reset token is present', () => {
useAuthStore.getState().setUser({ id: 'user-1', email: 'test@example.com' })
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Route.options.beforeLoad?.({ search: { token: '' } } as any)
}).toThrow()
})
it('does NOT redirect and clears stale auth when a reset token is present in search params', () => {
useAuthStore.getState().setUser({ id: 'user-1', email: 'test@example.com' })
// When the user has stale auth in localStorage but provides a token,
// they should NOT be redirected to / (which would 401 and redirect to login)
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Route.options.beforeLoad?.({ search: { token: 'valid-token-123' } } as any)
}).not.toThrow()
// And the stale auth store should be cleared
expect(useAuthStore.getState().user).toBeNull()
})
it('does not redirect when user is not logged in and no token is present', () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Route.options.beforeLoad?.({ search: { token: '' } } as any)
}).not.toThrow()
})
})