|
| 1 | +/** |
| 2 | + * Copyright since 2025 Mifos Initiative |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + */ |
| 8 | +import { test as setup, expect } from '@playwright/test'; |
| 9 | +import fs from 'fs'; |
| 10 | +import path from 'path'; |
| 11 | + |
| 12 | +const authFile = 'playwright/.auth/user.json'; |
| 13 | +const emptyState: { cookies: never[]; origins: never[] } = { cookies: [], origins: [] }; |
| 14 | + |
| 15 | +setup('authenticate', async ({ page, browser }) => { |
| 16 | + const authPath = path.resolve(authFile); |
| 17 | + const authDir = path.dirname(authPath); |
| 18 | + if (!fs.existsSync(authDir)) { |
| 19 | + fs.mkdirSync(authDir, { recursive: true }); |
| 20 | + } |
| 21 | + if (fs.existsSync(authPath)) { |
| 22 | + fs.unlinkSync(authPath); |
| 23 | + } |
| 24 | + |
| 25 | + const username = process.env.E2E_USERNAME || 'mifos'; |
| 26 | + const password = process.env.E2E_PASSWORD || 'password'; |
| 27 | + |
| 28 | + await page.goto('/#/login'); |
| 29 | + await page.locator('input[formcontrolname="username"]').waitFor({ state: 'visible', timeout: 60000 }); |
| 30 | + |
| 31 | + await page.locator('input[formcontrolname="username"]').fill(username); |
| 32 | + await page.locator('input[formcontrolname="password"]').fill(password); |
| 33 | + await page.getByRole('button', { name: /login/i }).click(); |
| 34 | + |
| 35 | + try { |
| 36 | + await expect(page).not.toHaveURL(/.*login.*/, { timeout: 30000 }); |
| 37 | + await expect(page.locator('mat-toolbar')).toBeVisible({ timeout: 10000 }); |
| 38 | + } catch { |
| 39 | + console.log('Auth setup: login failed (no Fineract backend). Writing empty storageState for CI.'); |
| 40 | + fs.writeFileSync(authPath, JSON.stringify(emptyState)); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + console.log('Auth setup: copying mifosXCredentials from sessionStorage → localStorage'); |
| 45 | + const credsCopied = await page.evaluate(() => { |
| 46 | + const creds = sessionStorage.getItem('mifosXCredentials'); |
| 47 | + if (!creds) return false; |
| 48 | + localStorage.setItem('mifosXCredentials', creds); |
| 49 | + return true; |
| 50 | + }); |
| 51 | + |
| 52 | + if (!credsCopied) { |
| 53 | + throw new Error('CRITICAL: mifosXCredentials not found in sessionStorage. ' + 'Did the auth storage key change?'); |
| 54 | + } |
| 55 | + |
| 56 | + await page.context().storageState({ path: authFile }); |
| 57 | + console.log('Auth setup: storageState saved to', authFile); |
| 58 | + |
| 59 | + const verifyContext = await browser.newContext({ storageState: authFile }); |
| 60 | + const verifyPage = await verifyContext.newPage(); |
| 61 | + await verifyPage.goto('/#/'); |
| 62 | + await expect(verifyPage).not.toHaveURL(/.*login.*/, { timeout: 30000 }); |
| 63 | + await verifyContext.close(); |
| 64 | + console.log('Auth setup: storageState verification passed ✓'); |
| 65 | +}); |
0 commit comments