From 227bbd6550fc0722abaeaaddc450ec036b1d7ae3 Mon Sep 17 00:00:00 2001 From: Urvish Date: Fri, 27 Nov 2026 16:01:59 +0530 Subject: [PATCH] test(e2e): add Phase 4 checkout, Razorpay, billing phone, and subscriptions tests --- tests/e2e/checkout-razorpay.spec.ts | 221 ++++++++++++++++++++++++++++ tests/e2e/subscriptions.spec.ts | 85 +++++++++++ 2 files changed, 306 insertions(+) create mode 100644 tests/e2e/checkout-razorpay.spec.ts create mode 100644 tests/e2e/subscriptions.spec.ts diff --git a/tests/e2e/checkout-razorpay.spec.ts b/tests/e2e/checkout-razorpay.spec.ts new file mode 100644 index 0000000..b82ca54 --- /dev/null +++ b/tests/e2e/checkout-razorpay.spec.ts @@ -0,0 +1,221 @@ +import { test, expect } from '@playwright/test'; +import { mockPublicStoreApis, mockAuthenticatedUser } from './fixtures/apiMocks'; + +test.describe('Phase 4: Checkout, Billing Phone & Razorpay Flow', () => { + test.beforeEach(async ({ page }) => { + await mockPublicStoreApis(page); + }); + + test('unauthenticated checkout opens AuthModal', async ({ page }) => { + await page.goto('./'); + await expect(page.getByText('Acu (EOV+Cat+Ext+MR)')).toBeVisible(); + + // Add item to cart and open cart drawer + await page.getByText('Acu (EOV+Cat+Ext+MR)').click(); + + await page.getByRole('button', { name: /Secure Payment with Razorpay/i }).click(); + + // AuthModal should appear in register mode + await expect(page.getByRole('heading', { name: 'Create Account' })).toBeVisible(); + }); + + test('missing billing phone opens BillingPhoneModal, validates, saves, and completes Razorpay payment', async ({ page }) => { + // Mock authenticated user with NO phone number + await mockAuthenticatedUser(page, { phone_number: '' }); + + let profilePutPayload: any = null; + await page.route('**/api/auth/profile', async (route) => { + if (route.request().method() === 'PUT') { + profilePutPayload = route.request().postDataJSON(); + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ status: 'success' }), + }); + } else { + await route.fallback(); + } + }); + + await page.route('**/api/checkout/create-order', async (route) => { + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ + razorpay_order_id: 'order_mock_001', + grand_total: 1000, + currency: 'INR', + }), + }); + }); + + await page.route('**/api/checkout/verify', async (route) => { + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ + status: 'success', + invoice_number: 'INV-2026-0042', + invoice_url: '/api/checkout/payment/pay_mock_12345/invoice/view', + message: 'Payment verified successfully and module entitlements unlocked!', + }), + }); + }); + + // Inject mock window.Razorpay that auto-succeeds on .open() + await page.addInitScript(() => { + (window as any).__openedUrls = []; + window.open = ((url: string) => { + (window as any).__openedUrls.push(url); + return null; + }) as any; + + (window as any).Razorpay = function (options: any) { + this.options = options; + this.events = {}; + this.on = (event: string, cb: Function) => { + this.events[event] = cb; + }; + this.open = () => { + setTimeout(() => { + options.handler({ + razorpay_order_id: options.order_id, + razorpay_payment_id: 'pay_mock_12345', + razorpay_signature: 'sig_mock_xyz', + }); + }, 50); + }; + }; + }); + + await page.goto('./'); + await expect(page.getByText('Acu (EOV+Cat+Ext+MR)')).toBeVisible(); + + await page.getByText('Acu (EOV+Cat+Ext+MR)').click(); + await page.getByRole('button', { name: /Secure Payment with Razorpay/i }).click(); + + // BillingPhoneModal should appear + await expect(page.getByText('Billing Contact Number')).toBeVisible(); + + // Test empty phone validation + await page.getByRole('button', { name: 'Save Phone Number' }).click(); + await expect(page.getByText(/Please enter your mobile number/i)).toBeVisible(); + + // Enter valid Indian 10-digit mobile number + const phoneInput = page.locator('input[type="tel"]').last(); + await phoneInput.fill('9876543210'); + await page.getByRole('button', { name: 'Save Phone Number' }).click(); + + // Should save phone via PUT /api/auth/profile and auto-proceed to Razorpay -> PaymentSuccessModal + await expect(page.getByText('Thank You for Your Purchase!')).toBeVisible({ timeout: 10000 }); + expect(profilePutPayload).toEqual({ + phone_number: '+919876543210', + country: 'India', + }); + + // Verify PaymentSuccessModal details + await expect(page.getByText('INV-2026-0042')).toBeVisible(); + await expect(page.getByText('pay_mock_12345')).toBeVisible(); + await expect(page.getByText('₹1000')).toBeVisible(); + + // Pause 10s auto-redirect + await page.getByRole('button', { name: /Stay on Web/i }).click(); + await expect(page.getByText(/Auto-redirect paused/i)).toBeVisible(); + + // Click Download Invoice PDF -> opens invoice URL & navigates to Subscriptions tab + await page.getByText('Download Invoice PDF').click(); + const openedUrls = await page.evaluate(() => (window as any).__openedUrls); + expect(openedUrls.some((u: string) => u.includes('/api/checkout/payment/pay_mock_12345/invoice/view'))).toBe(true); + }); + + test('Razorpay modal dismiss cancels cleanly without calling verify', async ({ page }) => { + await mockAuthenticatedUser(page, { phone_number: '+919876543210' }); + + let verifyCalled = false; + await page.route('**/api/checkout/create-order', async (route) => { + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ + razorpay_order_id: 'order_mock_cancel', + grand_total: 1000, + currency: 'INR', + }), + }); + }); + + await page.route('**/api/checkout/verify', async (route) => { + verifyCalled = true; + await route.fulfill({ status: 200, body: '{}' }); + }); + + await page.addInitScript(() => { + (window as any).Razorpay = function (options: any) { + this.on = () => {}; + this.open = () => { + setTimeout(() => { + options.modal.ondismiss(); + }, 50); + }; + }; + }); + + await page.goto('./'); + await page.getByText('Acu (EOV+Cat+Ext+MR)').click(); + await page.getByRole('button', { name: /Secure Payment with Razorpay/i }).click(); + + // Button should return to enabled state, verify not called, success modal not shown + await expect(page.getByRole('button', { name: /Secure Payment with Razorpay/i })).toBeEnabled(); + expect(verifyCalled).toBe(false); + await expect(page.getByText('Thank You for Your Purchase!')).not.toBeVisible(); + }); + + test('Razorpay payment.failed and verify signature failure show error feedback', async ({ page }) => { + await mockAuthenticatedUser(page, { phone_number: '+919876543210' }); + + await page.route('**/api/checkout/create-order', async (route) => { + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ + razorpay_order_id: 'order_mock_fail', + grand_total: 1000, + currency: 'INR', + }), + }); + }); + + await page.route('**/api/checkout/verify', async (route) => { + await route.fulfill({ + status: 400, + contentType: 'application/json', + body: JSON.stringify({ detail: 'Invalid Razorpay signature' }), + }); + }); + + await page.addInitScript(() => { + (window as any).Razorpay = function (options: any) { + this.events = {}; + this.on = (event: string, cb: Function) => { + this.events[event] = cb; + }; + this.open = () => { + setTimeout(() => { + options.handler({ + razorpay_order_id: options.order_id, + razorpay_payment_id: 'pay_bad_sig', + razorpay_signature: 'bad_sig', + }); + }, 50); + }; + }; + }); + + await page.goto('./'); + await page.getByText('Acu (EOV+Cat+Ext+MR)').click(); + await page.getByRole('button', { name: /Secure Payment with Razorpay/i }).click(); + + await expect(page.getByText('Verification Failed')).toBeVisible(); + await expect(page.getByText('Invalid Razorpay signature')).toBeVisible(); + }); +}); diff --git a/tests/e2e/subscriptions.spec.ts b/tests/e2e/subscriptions.spec.ts new file mode 100644 index 0000000..ca5bf7b --- /dev/null +++ b/tests/e2e/subscriptions.spec.ts @@ -0,0 +1,85 @@ +import { test, expect } from '@playwright/test'; +import { mockPublicStoreApis, mockAuthenticatedUser } from './fixtures/apiMocks'; + +test.describe('Phase 4: Subscriptions & Entitlements View', () => { + test.beforeEach(async ({ page }) => { + await mockPublicStoreApis(page); + }); + + test('unauthenticated user sees login prompt on Subscriptions tab', async ({ page }) => { + await page.goto('./'); + await page.getByRole('button', { name: /Subscriptions/i }).first().click(); + + await expect(page.getByText('Please register or log in to view active entitlements.')).toBeVisible(); + await page.getByRole('button', { name: 'Log In' }).click(); + await expect(page.getByText('Welcome Back!')).toBeVisible(); + }); + + test('authenticated user with no subscriptions sees empty state and can return to catalog', async ({ page }) => { + await mockAuthenticatedUser(page, { active_subscriptions: [] }); + + await page.goto('./'); + await page.getByRole('button', { name: /Subscriptions/i }).first().click(); + + await expect(page.getByText('No active subscriptions')).toBeVisible(); + await page.getByRole('button', { name: 'Explore Modules Catalog' }).click(); + await expect(page.getByText('Acu (EOV+Cat+Ext+MR)')).toBeVisible(); + }); + + test('populated subscriptions list renders passes, pagination, and invoice download', async ({ page }) => { + const futureDate = new Date(Date.now() + 180 * 24 * 60 * 60 * 1000).toISOString(); + const tenYearsDate = new Date(Date.now() + 3650 * 24 * 60 * 60 * 1000).toISOString(); + + const subs = [ + { + id: 1, + plan_code: 'LIFETIME_ALL', + status: 'ACTIVE', + started_at: '2026-01-01T00:00:00Z', + expires_at: tenYearsDate, + payment_id: 'pay_sub_1', + invoice_number: 'INV-2026-0001', + invoice_url: '/api/checkout/payment/pay_sub_1/invoice/view', + }, + ...Array.from({ length: 6 }, (_, i) => ({ + id: i + 2, + plan_code: `MODULE_${i + 2}`, + status: 'ACTIVE', + started_at: '2026-02-01T00:00:00Z', + expires_at: futureDate, + payment_id: `pay_sub_${i + 2}`, + invoice_number: `INV-2026-000${i + 2}`, + invoice_url: `/api/checkout/payment/pay_sub_${i + 2}/invoice/view`, + })), + ]; + + await page.addInitScript(() => { + (window as any).__openedUrls = []; + window.open = ((url: string) => { + (window as any).__openedUrls.push(url); + return null; + }) as any; + }); + + await mockAuthenticatedUser(page, { + active_subscriptions: subs, + unlocked_module_codes: subs.map((s) => s.plan_code), + }); + + await page.goto('./'); + await page.getByRole('button', { name: /Subscriptions/i }).first().click(); + + // First page shows 6 items, including LIFETIME_ALL ("10 Years Pass") + await expect(page.getByText('10 Years Pass')).toBeVisible(); + await expect(page.getByText('INV-2026-0001')).toBeVisible(); + + // Click Download Invoice on first card + await page.getByRole('button', { name: 'Download Invoice' }).first().click(); + const openedUrls = await page.evaluate(() => (window as any).__openedUrls); + expect(openedUrls.some((u: string) => u.includes('/api/checkout/payment/pay_sub_1/invoice/view'))).toBe(true); + + // Pagination is visible since totalCount = 7 > 6 + await page.getByRole('button', { name: '2', exact: true }).click(); + await expect(page.getByText('INV-2026-0007')).toBeVisible(); + }); +});