import { test, expect } from '../../fixtures'; test.describe('سناریوی اعتبارسنجی احراز هویت پیامکی و بازیابی رمز عبور (Auth OTP & Password Recovery)', () => { test('درخواست کد بازیابی پیامکی -> ورود کد OTP -> تنظیم رمز عبور جدید -> لاگین موفق با رمز جدید', async ({ page }) => { let resetPasswordCalled = false; // Intercept and mock Auth API endpoints await page.route('**/api/auth/send-otp*', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ success: true, message: 'کد تایید ارسال شد', code: '54321', }), }); }); await page.route('**/api/auth/verify-otp*', async (route) => { const postData = route.request().postDataJSON?.() || {}; if (postData.code === '54321') { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ success: true, data: { resetToken: 'mock_reset_token_xyz' }, }), }); } else { await route.fulfill({ status: 400, contentType: 'application/json', body: JSON.stringify({ success: false, message: 'کد تایید نامعتبر است', }), }); } }); await page.route('**/api/users/profile*', async (route) => { if (route.request().method() === 'PUT' || route.request().method() === 'PATCH') { resetPasswordCalled = true; await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ success: true, data: { id: 'usr-1', mobile: '09123456789', firstName: 'پارسا' }, message: 'رمز عبور با موفقیت تغییر کرد', }), }); } else { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ id: 'usr-1', mobile: '09123456789', firstName: 'پارسا', }), }); } }); // 1. ورود به صفحه اصلی و باز کردن مودال ورود await page.goto('/', { waitUntil: 'domcontentloaded' }); const headerLoginBtn = page.locator('[data-testid="header-login-btn"]').first(); await expect(headerLoginBtn).toBeVisible({ timeout: 15000 }); await headerLoginBtn.click(); // 2. کلیک روی لینک فراموشی رمز عبور const forgotLink = page.locator('[data-testid="forgot-password-link"]').first(); await expect(forgotLink).toBeVisible({ timeout: 10000 }); await forgotLink.click(); // 3. پر کردن شماره موبایل و ارسال درخواست OTP const phoneInput = page.locator('input[placeholder="۰۹۱۲۳۴۵۶۷۸۹"]').first(); await expect(phoneInput).toBeVisible({ timeout: 10000 }); await phoneInput.fill('09123456789'); const sendOtpBtn = page.locator('button[type="submit"]').filter({ hasText: /ارسال کد بازیابی/i }).first(); await expect(sendOtpBtn).toBeVisible(); await sendOtpBtn.click(); // 4. راستی‌آزمایی ورود به مرحله forgot-otp و وارد کردن کد OTP const otpInput = page.locator('input[placeholder="کد ۵ رقمی"]').first(); await expect(otpInput).toBeVisible({ timeout: 10000 }); await otpInput.fill('54321'); const verifyOtpBtn = page.locator('button[type="submit"]').filter({ hasText: /تایید و ادامه/i }).first(); await expect(verifyOtpBtn).toBeVisible(); await verifyOtpBtn.click(); // 5. راستی‌آزمایی ورود به مرحله تنظیم رمز عبور جدید const newPassInput = page.locator('input[placeholder="حداقل ۶ کاراکتر"]').first(); await expect(newPassInput).toBeVisible({ timeout: 10000 }); await newPassInput.fill('NewSecret@2026'); const submitNewPassBtn = page.locator('button[type="submit"]').filter({ hasText: /ثبت رمز عبور جدید/i }).first(); await expect(submitNewPassBtn).toBeVisible(); await submitNewPassBtn.click(); // 6. راستی‌آزمایی قطعی فراخوانی موفق API ریست پسورد await expect.poll(() => resetPasswordCalled, { timeout: 10000 }).toBe(true); }); });