76 lines
3.2 KiB
TypeScript
76 lines
3.2 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest';
|
||
import { mount } from '@vue/test-utils';
|
||
import { createPinia, setActivePinia } from 'pinia';
|
||
import { createVuetify } from 'vuetify';
|
||
import { createRouter, createMemoryHistory } from 'vue-router';
|
||
import TwoFactorView from '../../resources/js/views/auth/TwoFactorView.vue';
|
||
import { useAuthStore } from '../../resources/js/stores/auth';
|
||
|
||
const mountTwoFactor = async () => {
|
||
setActivePinia(createPinia());
|
||
// Эмулируем pending-2FA state — иначе onMounted редиректит на /login.
|
||
const auth = useAuthStore();
|
||
auth.requires2fa = true;
|
||
|
||
const router = createRouter({
|
||
history: createMemoryHistory(),
|
||
routes: [
|
||
{ path: '/2fa', component: TwoFactorView },
|
||
{ path: '/login', component: { template: '<div>stub</div>' } },
|
||
{ path: '/dashboard', component: { template: '<div>stub</div>' } },
|
||
{ path: '/recovery', component: { template: '<div>stub</div>' } },
|
||
{ path: '/recovery-use', component: { template: '<div>stub</div>' } },
|
||
],
|
||
});
|
||
await router.push('/2fa');
|
||
await router.isReady();
|
||
return mount(TwoFactorView, {
|
||
global: { plugins: [createVuetify(), router] },
|
||
});
|
||
};
|
||
|
||
describe('TwoFactorView.vue', () => {
|
||
it('монтируется и содержит заголовок', async () => {
|
||
const wrapper = await mountTwoFactor();
|
||
expect(wrapper.text()).toContain('Двухфакторная проверка');
|
||
});
|
||
|
||
it('содержит ровно 6 input-cell для кода', async () => {
|
||
const wrapper = await mountTwoFactor();
|
||
const cells = wrapper.findAll('.code-cell');
|
||
expect(cells).toHaveLength(6);
|
||
// Каждая cell — numeric inputmode + maxlength=1.
|
||
cells.forEach((cell) => {
|
||
expect(cell.attributes('inputmode')).toBe('numeric');
|
||
expect(cell.attributes('maxlength')).toBe('1');
|
||
});
|
||
});
|
||
|
||
it('содержит ссылку на резервный код', async () => {
|
||
const wrapper = await mountTwoFactor();
|
||
const links = wrapper.findAll('a').map((a) => a.text());
|
||
expect(links.some((t) => t.includes('резервный код'))).toBe(true);
|
||
});
|
||
|
||
it('A6: показывает реальный обратный отсчёт TOTP-окна (30 с)', async () => {
|
||
vi.useFakeTimers();
|
||
vi.setSystemTime(new Date(10_000)); // epoch 10 c → 30 - (10 % 30) = 20
|
||
try {
|
||
const wrapper = await mountTwoFactor();
|
||
const el = wrapper.find('[data-testid="totp-countdown"]');
|
||
expect(el.exists()).toBe(true);
|
||
expect(el.text()).toBe('00:20');
|
||
|
||
// Устанавливаем время так, чтобы после срабатывания интервала (+1000ms)
|
||
// Date.now() оказался на 15000 ms: 15000 - 1000 = 14000.
|
||
// epoch 15 c → 30 - (15 % 30) = 15
|
||
vi.setSystemTime(new Date(14_000));
|
||
vi.advanceTimersByTime(1000); // interval fires, Date.now() → 15000
|
||
await wrapper.vm.$nextTick();
|
||
expect(el.text()).toBe('00:15');
|
||
} finally {
|
||
vi.useRealTimers();
|
||
}
|
||
});
|
||
});
|