buster/apps/web/playwright-tests/auth-utils/auth-utils.ts

173 lines
5.1 KiB
TypeScript
Raw Normal View History

Biome linter test (#336) * update files for use with biom * fix prettier formats * minor biome fixes * fix additional files * update linting rules * fix additional linting errors * fix linting errors * update lib * run check in the lib direcotyr * update some linting errors * fix problems * move addtional files to stricter linting * fix imports and linting errors * update some biome settings * fix query parser * quick wins * update files * fix addtional things * fix context files * update additional biome files * fix additional files * biome fixes * fixin files * fix broken * fix additional files and problems * fix biome buster stuff * fix helpers and * stricter linting * fixed * fix missing types * fix linting * remove missing imports * update more biome test stuff * update fixes * update imports * more updates * fix some linting errors * fix broken children as prop errors * lint ci * update lint check * Update package.json * Update next.config.mjs * fix some broken tests * update some tests * chore(versions): bump api to v0.1.16; bump web to v0.1.16; bump cli to v0.1.16 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-unit-tests.yml * fix some tests * chore(versions): bump api to v0.1.17; bump web to v0.1.17; bump cli to v0.1.17 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * update ignore files * chore(versions): bump api to v0.1.18; bump web to v0.1.18; bump cli to v0.1.18 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-lint.yml * chore(versions): bump api to v0.1.19; bump web to v0.1.19; bump cli to v0.1.19 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * update boime package settings * chore(versions): bump api to v0.1.20; bump web to v0.1.20; bump cli to v0.1.20 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * save to dropdown bug fixes * update console logs * update chart animation * chore(versions): bump api to v0.1.21; bump web to v0.1.21; bump cli to v0.1.21 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * update build * chore(versions): bump api to v0.1.22; bump web to v0.1.22; bump cli to v0.1.22 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * additinal updates for biome 2 * update imports * chore(versions): bump api to v0.1.23; bump web to v0.1.23; bump cli to v0.1.23 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * final updates * chore(versions): bump api to v0.1.24; bump web to v0.1.24; bump cli to v0.1.24 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update dashboard-updates.test.ts * chore(versions): bump api to v0.1.25; bump web to v0.1.25; bump cli to v0.1.25 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * crazy fix * chore(versions): bump api to v0.1.26; bump web to v0.1.26; bump cli to v0.1.26 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * more attempts for stable tests * chore(versions): bump api to v0.1.27; bump web to v0.1.27; bump cli to v0.1.27 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * update tests * split the test off * chore(versions): bump api to v0.1.28; bump web to v0.1.28; bump cli to v0.1.28 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update pie-styling-updates.spec.ts * Create web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.29; bump web to v0.1.29; bump cli to v0.1.29 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * update tests * chore(versions): bump api to v0.1.30; bump web to v0.1.30; bump cli to v0.1.30 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.31; bump web to v0.1.31; bump cli to v0.1.31 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.32; bump web to v0.1.32; bump cli to v0.1.32 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.33; bump web to v0.1.33; bump cli to v0.1.33 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.34; bump web to v0.1.34; bump cli to v0.1.34 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * Update web-e2e-tests-optimized.yml * Revert "Update web-e2e-tests-optimized.yml" This reverts commit dfc9263a2621f1c00dee30e28dbb01a9f8f914ef. * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.35; bump web to v0.1.35; bump cli to v0.1.35 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.36; bump web to v0.1.36; bump cli to v0.1.36 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-05-31 01:16:48 +08:00
import * as fs from 'node:fs';
import * as path from 'node:path';
import type { Page } from '@playwright/test';
2025-05-03 05:37:07 +08:00
import { expect } from '@playwright/test';
Biome linter test (#336) * update files for use with biom * fix prettier formats * minor biome fixes * fix additional files * update linting rules * fix additional linting errors * fix linting errors * update lib * run check in the lib direcotyr * update some linting errors * fix problems * move addtional files to stricter linting * fix imports and linting errors * update some biome settings * fix query parser * quick wins * update files * fix addtional things * fix context files * update additional biome files * fix additional files * biome fixes * fixin files * fix broken * fix additional files and problems * fix biome buster stuff * fix helpers and * stricter linting * fixed * fix missing types * fix linting * remove missing imports * update more biome test stuff * update fixes * update imports * more updates * fix some linting errors * fix broken children as prop errors * lint ci * update lint check * Update package.json * Update next.config.mjs * fix some broken tests * update some tests * chore(versions): bump api to v0.1.16; bump web to v0.1.16; bump cli to v0.1.16 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-unit-tests.yml * fix some tests * chore(versions): bump api to v0.1.17; bump web to v0.1.17; bump cli to v0.1.17 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * update ignore files * chore(versions): bump api to v0.1.18; bump web to v0.1.18; bump cli to v0.1.18 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-lint.yml * chore(versions): bump api to v0.1.19; bump web to v0.1.19; bump cli to v0.1.19 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * update boime package settings * chore(versions): bump api to v0.1.20; bump web to v0.1.20; bump cli to v0.1.20 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * save to dropdown bug fixes * update console logs * update chart animation * chore(versions): bump api to v0.1.21; bump web to v0.1.21; bump cli to v0.1.21 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * update build * chore(versions): bump api to v0.1.22; bump web to v0.1.22; bump cli to v0.1.22 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * additinal updates for biome 2 * update imports * chore(versions): bump api to v0.1.23; bump web to v0.1.23; bump cli to v0.1.23 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * final updates * chore(versions): bump api to v0.1.24; bump web to v0.1.24; bump cli to v0.1.24 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update dashboard-updates.test.ts * chore(versions): bump api to v0.1.25; bump web to v0.1.25; bump cli to v0.1.25 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * crazy fix * chore(versions): bump api to v0.1.26; bump web to v0.1.26; bump cli to v0.1.26 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * more attempts for stable tests * chore(versions): bump api to v0.1.27; bump web to v0.1.27; bump cli to v0.1.27 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * update tests * split the test off * chore(versions): bump api to v0.1.28; bump web to v0.1.28; bump cli to v0.1.28 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update pie-styling-updates.spec.ts * Create web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.29; bump web to v0.1.29; bump cli to v0.1.29 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * update tests * chore(versions): bump api to v0.1.30; bump web to v0.1.30; bump cli to v0.1.30 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.31; bump web to v0.1.31; bump cli to v0.1.31 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.32; bump web to v0.1.32; bump cli to v0.1.32 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.33; bump web to v0.1.33; bump cli to v0.1.33 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.34; bump web to v0.1.34; bump cli to v0.1.34 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * Update web-e2e-tests-optimized.yml * Revert "Update web-e2e-tests-optimized.yml" This reverts commit dfc9263a2621f1c00dee30e28dbb01a9f8f914ef. * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.35; bump web to v0.1.35; bump cli to v0.1.35 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] * Update web-e2e-tests-optimized.yml * chore(versions): bump api to v0.1.36; bump web to v0.1.36; bump cli to v0.1.36 [skip ci] * chore: update tag_info.json with potential release versions [skip ci] --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-05-31 01:16:48 +08:00
import { jwtDecode } from 'jwt-decode';
import isEmpty from 'lodash/isEmpty';
2025-05-02 05:31:05 +08:00
// Path to the authentication state file
export const authFile = path.join(__dirname, 'auth.json');
/**
* Checks if valid authentication data exists
*/
export function hasValidAuth(): boolean {
try {
if (!fs.existsSync(authFile)) {
return false;
}
const authData = JSON.parse(fs.readFileSync(authFile, 'utf-8'));
if (
isEmpty(authData) ||
isEmpty(authData.cookies) ||
isEmpty(authData.localStorage) ||
isEmpty(authData.sessionStorage)
) {
return false;
}
// Check if JWT is valid
if (authData.localStorage) {
const storage = JSON.parse(authData.localStorage);
const token = storage.buster_token || storage.token;
if (token) {
try {
const decoded = jwtDecode(token);
const expTime = decoded.exp ? decoded.exp * 1000 : 0; // Convert to milliseconds
if (expTime && expTime < Date.now()) {
return false;
}
} catch (error) {
return false;
}
}
}
return true;
} catch (error) {
return false;
}
}
/**
* Performs login and saves authentication state
*/
export async function login(page: Page) {
await page.goto('http://localhost:3000/auth/login');
// Add your login logic here, for example:
await page.getByText('Sign in').click();
2025-05-03 05:37:07 +08:00
await page.getByText(`Don't already have an account?`).click();
2025-05-02 05:31:05 +08:00
await page.getByRole('textbox', { name: 'What is your email address?' }).fill('chad@buster.so');
await page.getByRole('textbox', { name: 'What is your email address?' }).press('Tab');
await page.getByRole('textbox', { name: 'Password' }).fill('password');
2025-05-03 05:37:07 +08:00
expect(page.getByRole('textbox', { name: 'Confirm passowrd' })).not.toBeVisible();
2025-05-02 05:31:05 +08:00
await page.getByRole('button', { name: 'Sign in' }).click();
2025-05-03 06:42:34 +08:00
2025-05-03 05:37:07 +08:00
//expect "Invalid email or password" to not be visible
expect(page.getByText('Invalid email or password')).not.toBeVisible();
await page.waitForLoadState('networkidle');
await page.waitForLoadState('domcontentloaded');
await page.waitForTimeout(250);
2025-05-02 05:31:05 +08:00
await page.waitForURL('http://localhost:3000/app/home');
2025-05-03 06:42:34 +08:00
await page.waitForTimeout(250);
await page.goto('http://localhost:3000/app/new-user');
await page.getByRole('button', { name: 'Get Started' }).click();
await page.getByRole('textbox', { name: 'What is your full name' }).dblclick();
await page.getByRole('textbox', { name: 'What is your full name' }).fill('Chad');
2025-05-03 05:37:07 +08:00
await page.waitForTimeout(20);
await page.getByRole('button', { name: 'Create your account' }).click();
2025-05-03 05:37:07 +08:00
await page.waitForTimeout(550);
await page.waitForLoadState('networkidle');
await page.waitForLoadState('domcontentloaded');
await page.waitForURL('http://localhost:3000/app/home');
// Wait for the page to be fully loaded before accessing storage
await page.waitForLoadState('networkidle');
await page.waitForLoadState('domcontentloaded');
2025-05-02 05:31:05 +08:00
// Save authentication data
const authData = {
cookies: await page.context().cookies(),
localStorage: await page.evaluate(() => JSON.stringify(localStorage)),
sessionStorage: await page.evaluate(() => JSON.stringify(sessionStorage))
};
try {
fs.writeFileSync(authFile, JSON.stringify(authData));
return authData;
} catch (error) {
console.error('Failed to save authentication data:', error);
return authData;
}
}
/**
* Applies saved authentication state to a page
*/
export async function applyAuth(page: Page): Promise<boolean> {
if (!hasValidAuth()) {
return false;
}
try {
const authData = JSON.parse(fs.readFileSync(authFile, 'utf-8'));
// Add cookies
await page.context().addCookies(authData.cookies || []);
// Set localStorage and sessionStorage
if (!isEmpty(authData.localStorage) || !isEmpty(authData.sessionStorage)) {
await page.goto('http://localhost:3000');
if (authData.localStorage) {
await page.evaluate((storageData) => {
const storage = JSON.parse(storageData);
for (const [key, value] of Object.entries(storage)) {
localStorage.setItem(key, value as string);
}
}, authData.localStorage);
}
if (authData.sessionStorage) {
await page.evaluate((storageData) => {
const storage = JSON.parse(storageData);
for (const [key, value] of Object.entries(storage)) {
sessionStorage.setItem(key, value as string);
}
}, authData.sessionStorage);
}
} else {
return false;
}
return true;
} catch (error) {
console.error('Failed to apply authentication:', error);
return false;
}
}
/**
* Clears saved authentication data
*/
export function clearAuth() {
try {
if (fs.existsSync(authFile)) {
fs.unlinkSync(authFile);
return true;
}
return false;
} catch (error) {
console.error('Failed to clear authentication data:', error);
return false;
}
}