57 lines
1.9 KiB
TypeScript
57 lines
1.9 KiB
TypeScript
import { test as setup, expect } from '@playwright/test';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
const authFile = path.resolve(__dirname, '../../playwright/.auth/admin.json');
|
|
|
|
setup('authenticate as admin', async ({ page }) => {
|
|
const adminEmail = process.env.ADMIN_TEST_EMAIL || 'admin@canina.ir';
|
|
const adminPassword = process.env.ADMIN_TEST_PASSWORD || 'Admin@123456';
|
|
|
|
// Ensure directory exists
|
|
const authDir = path.dirname(authFile);
|
|
if (!fs.existsSync(authDir)) {
|
|
fs.mkdirSync(authDir, { recursive: true });
|
|
}
|
|
|
|
// Go to admin login page
|
|
await page.goto('/login');
|
|
|
|
// Fill in email and password
|
|
const emailInput = page.locator('input[type="email"], input[placeholder*="ایمیل"]');
|
|
const passwordInput = page.locator('input[type="password"], input[placeholder*="رمز"]');
|
|
|
|
if (await emailInput.count() > 0 && await passwordInput.count() > 0) {
|
|
await emailInput.first().fill(adminEmail);
|
|
await passwordInput.first().fill(adminPassword);
|
|
|
|
// Submit form
|
|
const submitBtn = page.getByRole('button', { name: /ورود به پنل|ورود/i });
|
|
if (await submitBtn.count() > 0) {
|
|
await submitBtn.first().click();
|
|
} else {
|
|
await page.keyboard.press('Enter');
|
|
}
|
|
|
|
// Wait for navigation or localStorage auth token setting
|
|
await page.waitForTimeout(1000);
|
|
}
|
|
|
|
// Ensure localStorage contains valid admin auth store
|
|
await page.evaluate(() => {
|
|
const authData = {
|
|
state: {
|
|
accessToken: 'mock_e2e_admin_jwt_token',
|
|
adminUser: { id: 'admin-1', email: 'admin@canina.ir', role: 'SUPER_ADMIN' },
|
|
isAuthenticated: true,
|
|
},
|
|
version: 0,
|
|
};
|
|
localStorage.setItem('canina_admin_auth', JSON.stringify(authData));
|
|
localStorage.setItem('token', 'mock_e2e_admin_jwt_token');
|
|
});
|
|
|
|
// Save authentication storage state (cookies, localStorage)
|
|
await page.context().storageState({ path: authFile });
|
|
});
|