2026-05-16 22:08:55 +03:00
|
|
|
|
import { describe, it, expect, vi } from 'vitest';
|
2026-05-08 17:09:56 +03:00
|
|
|
|
import { mount } from '@vue/test-utils';
|
2026-05-08 20:14:33 +03:00
|
|
|
|
import { createPinia, setActivePinia } from 'pinia';
|
2026-05-08 17:09:56 +03:00
|
|
|
|
import { createVuetify } from 'vuetify';
|
|
|
|
|
|
import { createRouter, createMemoryHistory } from 'vue-router';
|
|
|
|
|
|
import TwoFactorView from '../../resources/js/views/auth/TwoFactorView.vue';
|
2026-05-08 20:14:33 +03:00
|
|
|
|
import { useAuthStore } from '../../resources/js/stores/auth';
|
2026-05-08 17:09:56 +03:00
|
|
|
|
|
|
|
|
|
|
const mountTwoFactor = async () => {
|
2026-05-08 20:14:33 +03:00
|
|
|
|
setActivePinia(createPinia());
|
|
|
|
|
|
// Эмулируем pending-2FA state — иначе onMounted редиректит на /login.
|
|
|
|
|
|
const auth = useAuthStore();
|
|
|
|
|
|
auth.requires2fa = true;
|
|
|
|
|
|
|
2026-05-08 17:09:56 +03:00
|
|
|
|
const router = createRouter({
|
|
|
|
|
|
history: createMemoryHistory(),
|
|
|
|
|
|
routes: [
|
|
|
|
|
|
{ path: '/2fa', component: TwoFactorView },
|
2026-05-08 20:14:33 +03:00
|
|
|
|
{ path: '/login', component: { template: '<div>stub</div>' } },
|
|
|
|
|
|
{ path: '/dashboard', component: { template: '<div>stub</div>' } },
|
2026-05-08 17:09:56 +03:00
|
|
|
|
{ path: '/recovery', component: { template: '<div>stub</div>' } },
|
2026-05-12 20:23:51 +03:00
|
|
|
|
{ path: '/recovery-use', component: { template: '<div>stub</div>' } },
|
2026-05-08 17:09:56 +03:00
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
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);
|
|
|
|
|
|
});
|
2026-05-16 22:08:55 +03:00
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2026-05-08 17:09:56 +03:00
|
|
|
|
});
|