Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

refactor: extract auth helper constants and helper methods

LearningCircuit2e72a6e3e46d
diff --git a/tests/ui_tests/auth_helper.js b/tests/ui_tests/auth_helper.js
index 822a56f1a8..b7ab70c950 100644
--- a/tests/ui_tests/auth_helper.js
+++ b/tests/ui_tests/auth_helper.js
@@ -3,12 +3,43 @@
* Handles login and registration for Puppeteer tests
*/
+const crypto = require('crypto');
+
const DEFAULT_TEST_USER = {
username: 'testuser',
password: 'T3st!Secure#2024$LDR'
};
-const crypto = require('crypto');
+// Configuration constants - single source of truth for auth helper settings
+const AUTH_CONFIG = {
+ // Route paths
+ paths: {
+ login: '/auth/login',
+ register: '/auth/register',
+ logout: '/auth/logout'
+ },
+ // Timeouts (ms)
+ timeouts: {
+ navigation: 30000,
+ formSelector: process.env.CI ? 10000 : 5000,
+ submitNavigation: process.env.CI ? 10000 : 90000,
+ urlCheck: 5000,
+ errorCheck: 2000,
+ logout: 10000
+ },
+ // Delays (ms)
+ delays: {
+ retryNavigation: 1000,
+ afterRegistration: 3000,
+ beforeRetry: 5000,
+ afterLogout: 1000
+ },
+ // CI-specific settings
+ ci: {
+ waitUntil: 'domcontentloaded',
+ maxLoginAttempts: 5
+ }
+};
// Generate random username for each test to avoid conflicts
function generateRandomUsername() {
@@ -30,6 +61,48 @@ class AuthHelper {
constructor(page, baseUrl = 'http://127.0.0.1:5000') {
this.page = page;
this.baseUrl = baseUrl;
+ this.isCI = !!process.env.CI;
+ }
+
+ /**
+ * Helper method for delays
+ */
+ async _delay(ms) {
+ return new Promise(resolve => setTimeout(resolve, ms));
+ }
+
+ /**
+ * Navigate to an auth page with CI-aware retry logic
+ * @param {string} path - The path to navigate to (e.g., AUTH_CONFIG.paths.login)
+ * @param {string} expectedPathSegment - Path segment to verify arrival (e.g., '/auth/login')
+ * @returns {string} The URL we arrived at
+ */
+ async _navigateToAuthPage(path, expectedPathSegment) {
+ const targetUrl = `${this.baseUrl}${path}`;
+ const waitUntil = this.isCI ? AUTH_CONFIG.ci.waitUntil : 'networkidle2';
+
+ console.log(` Navigating to ${path}...`);
+ await this.page.goto(targetUrl, {
+ waitUntil,
+ timeout: AUTH_CONFIG.timeouts.navigation
+ });
+
+ let arrivedUrl = this.page.url();
+ console.log(` Arrived at: ${arrivedUrl}`);
+
+ // In CI, verify we're on the correct page and retry if needed
+ if (this.isCI && !arrivedUrl.includes(expectedPathSegment)) {
+ console.log(` CI: Not on ${expectedPathSegment}, retrying navigation...`);
+ await this._delay(AUTH_CONFIG.delays.retryNavigation);
+ await this.page.goto(targetUrl, {
+ waitUntil: AUTH_CONFIG.ci.waitUntil,
+ timeout: AUTH_CONFIG.timeouts.navigation
+ });
+ arrivedUrl = this.page.url();
+ console.log(` CI: Second attempt arrived at: ${arrivedUrl}`);
+ }
+
+ return arrivedUrl;
}
/**
@@ -41,7 +114,7 @@ class AuthHelper {
const url = this.page.url();
console.log('Checking login status at URL:', url);
- if (url.includes('/auth/login')) {
+ if (url.includes(AUTH_CONFIG.paths.login)) {
console.log('On login page - not logged in');
return false;
}
@@ -103,34 +176,13 @@ class AuthHelper {
// Navigate to login page only if not already there
const currentUrl = this.page.url();
console.log(` Current URL: ${currentUrl}`);
- if (!currentUrl.includes('/auth/login')) {
- console.log(` Navigating to login page...`);
- // In CI, use domcontentloaded for faster initial response
- const waitUntil = process.env.CI ? 'domcontentloaded' : 'networkidle2';
- await this.page.goto(`${this.baseUrl}/auth/login`, {
- waitUntil: waitUntil,
- timeout: 30000
- });
-
- // In CI, verify we're on the login page and retry if needed
- let arrivedUrl = this.page.url();
- console.log(` Arrived at: ${arrivedUrl}`);
-
- if (process.env.CI && !arrivedUrl.includes('/auth/login')) {
- console.log(` CI: Not on login page, retrying navigation...`);
- await new Promise(resolve => setTimeout(resolve, 1000));
- await this.page.goto(`${this.baseUrl}/auth/login`, {
- waitUntil: 'domcontentloaded',
- timeout: 30000
- });
- arrivedUrl = this.page.url();
- console.log(` CI: Second attempt arrived at: ${arrivedUrl}`);
- }
+ if (!currentUrl.includes(AUTH_CONFIG.paths.login)) {
+ await this._navigateToAuthPage(AUTH_CONFIG.paths.login, AUTH_CONFIG.paths.login);
}
// Wait for login form
console.log(' Waiting for login form...');
- await this.page.waitForSelector('input[name="username"]', { timeout: 5000 });
+ await this.page.waitForSelector('input[name="username"]', { timeout: AUTH_CONFIG.timeouts.formSelector });
// Check what's on the page
const formAction = await this.page.$eval('form', form => form.action).catch(() => 'no form found');
@@ -175,7 +227,7 @@ class AuthHelper {
try {
// In CI, try a different approach - click and wait for URL change
- if (process.env.CI) {
+ if (this.isCI) {
console.log(' Using CI-specific login approach (wait for redirect)');
// Click the submit button
@@ -184,14 +236,14 @@ class AuthHelper {
// Wait for either navigation or timeout
let redirected = false;
for (let i = 0; i < 30; i++) {
- await new Promise(resolve => setTimeout(resolve, 1000));
+ await this._delay(AUTH_CONFIG.delays.retryNavigation);
// Try to get URL with a timeout
let currentUrl;
try {
currentUrl = await Promise.race([
this.page.evaluate(() => window.location.href),
- new Promise((_, reject) => setTimeout(() => reject(new Error('URL check timeout')), 5000))
+ new Promise((_, reject) => setTimeout(() => reject(new Error('URL check timeout')), AUTH_CONFIG.timeouts.urlCheck))
]);
console.log(` Checking URL (${i+1}/30): ${currentUrl}`);
} catch (urlError) {
@@ -199,7 +251,7 @@ class AuthHelper {
continue; // Skip this check and try again
}
- if (currentUrl && !currentUrl.includes('/auth/login')) {
+ if (currentUrl && !currentUrl.includes(AUTH_CONFIG.paths.login)) {
console.log(' ✅ Redirected away from login page');
redirected = true;
break;
@@ -209,7 +261,7 @@ class AuthHelper {
try {
const errorElement = await Promise.race([
this.page.$('.alert-danger, .error-message, .flash-message'),
- new Promise(resolve => setTimeout(() => resolve(null), 2000))
+ new Promise(resolve => setTimeout(() => resolve(null), AUTH_CONFIG.timeouts.errorCheck))
]);
if (errorElement) {
const errorText = await this.page.evaluate(el => el.textContent, errorElement);
@@ -222,10 +274,10 @@ class AuthHelper {
// Otherwise ignore the error check failure
}
- // In CI mode, give up after 5 attempts if we're still on login page
+ // In CI mode, give up after configured attempts if we're still on login page
// This likely means the user doesn't exist
- if (i >= 5 && currentUrl && currentUrl.includes('/auth/login')) {
- console.log(' Login not succeeding after 5 attempts, user likely does not exist');
+ if (i >= AUTH_CONFIG.ci.maxLoginAttempts && currentUrl && currentUrl.includes(AUTH_CONFIG.paths.login)) {
+ console.log(` Login not succeeding after ${AUTH_CONFIG.ci.maxLoginAttempts} attempts, user likely does not exist`);
throw new Error('Login failed - user does not exist');
}
}
@@ -237,7 +289,7 @@ class AuthHelper {
const sessionCookie = cookies.find(c => c.name === 'session');
if (sessionCookie) {
console.log(' Session cookie exists, manually navigating to home');
- await this.page.goto(this.baseUrl, { waitUntil: 'domcontentloaded' });
+ await this.page.goto(this.baseUrl, { waitUntil: AUTH_CONFIG.ci.waitUntil });
} else {
throw new Error('Login failed - no redirect and no session cookie');
}
@@ -249,7 +301,7 @@ class AuthHelper {
await Promise.all([
this.page.waitForNavigation({
waitUntil: 'networkidle2',
- timeout: 90000
+ timeout: AUTH_CONFIG.timeouts.submitNavigation
}),
this.page.click('button[type="submit"]')
]);
@@ -275,7 +327,7 @@ class AuthHelper {
const finalUrl = this.page.url();
console.log(` Final URL: ${finalUrl}`);
- if (finalUrl.includes('/auth/login')) {
+ if (finalUrl.includes(AUTH_CONFIG.paths.login)) {
// Still on login page - check for error
const error = await this.page.$('.alert-danger, .error-message, .alert');
if (error) {
@@ -305,38 +357,16 @@ class AuthHelper {
async register(username = DEFAULT_TEST_USER.username, password = DEFAULT_TEST_USER.password) {
console.log(`📝 Attempting registration for ${username}...`);
- // Navigate to registration page
- // In CI, use domcontentloaded for faster response
- const waitUntil = process.env.CI ? 'domcontentloaded' : 'networkidle2';
- await this.page.goto(`${this.baseUrl}/auth/register`, {
- waitUntil: waitUntil,
- timeout: 30000
- });
+ // Navigate to registration page using the helper
+ const arrivedUrl = await this._navigateToAuthPage(AUTH_CONFIG.paths.register, AUTH_CONFIG.paths.register);
- // Log where we arrived
- let arrivedUrl = this.page.url();
- console.log(` Arrived at: ${arrivedUrl}`);
-
- // In CI, verify we're on the registration page
- if (process.env.CI && !arrivedUrl.includes('/auth/register')) {
- console.log(` CI: Not on registration page, retrying...`);
- await new Promise(resolve => setTimeout(resolve, 1000));
- await this.page.goto(`${this.baseUrl}/auth/register`, {
- waitUntil: 'domcontentloaded',
- timeout: 30000
- });
- arrivedUrl = this.page.url();
- console.log(` CI: Second attempt arrived at: ${arrivedUrl}`);
-
- // If still redirected (e.g., to login), registration might be disabled
- if (arrivedUrl.includes('/auth/login')) {
- throw new Error('Registration page redirected to login - registrations may be disabled');
- }
+ // If redirected to login, registration might be disabled
+ if (arrivedUrl.includes(AUTH_CONFIG.paths.login)) {
+ throw new Error('Registration page redirected to login - registrations may be disabled');
}
- // Wait for registration form with longer timeout in CI
- const formTimeout = process.env.CI ? 10000 : 5000;
- await this.page.waitForSelector('input[name="username"]', { timeout: formTimeout });
+ // Wait for registration form
+ await this.page.waitForSelector('input[name="username"]', { timeout: AUTH_CONFIG.timeouts.formSelector });
// Fill in registration form
await this.page.type('input[name="username"]', username);
@@ -354,22 +384,22 @@ class AuthHelper {
await Promise.all([
this.page.waitForNavigation({
waitUntil: 'domcontentloaded',
- timeout: process.env.CI ? 10000 : 90000 // Shorter timeout in CI
+ timeout: AUTH_CONFIG.timeouts.submitNavigation
}),
this.page.click('button[type="submit"]')
]);
} catch (navError) {
// In CI, navigation errors are expected due to frame detachment
- if (process.env.CI && navError.message.includes('detached')) {
+ if (this.isCI && navError.message.includes('detached')) {
console.log(' CI: Navigation error (expected):', navError.message);
// Wait for registration to complete server-side
- await new Promise(resolve => setTimeout(resolve, 3000));
+ await this._delay(AUTH_CONFIG.delays.afterRegistration);
// Navigate back to home page after registration
try {
await this.page.goto(this.baseUrl, {
- waitUntil: 'domcontentloaded',
- timeout: 10000
+ waitUntil: AUTH_CONFIG.ci.waitUntil,
+ timeout: AUTH_CONFIG.timeouts.formSelector
});
} catch (gotoError) {
console.log(' CI: Could not navigate after registration:', gotoError.message);
@@ -383,7 +413,7 @@ class AuthHelper {
// Check if registration was successful
const currentUrl = this.page.url();
- if (currentUrl.includes('/auth/register')) {
+ if (currentUrl.includes(AUTH_CONFIG.paths.register)) {
// Still on registration page - check for actual errors (not warnings)
const error = await this.page.$('.alert-danger:not(.alert-warning), .error-message');
if (error) {
@@ -448,7 +478,7 @@ class AuthHelper {
if (attempt < retries && registerError.message.includes('timeout')) {
console.log(`⚠️ Timeout error, retrying (${attempt}/${retries})...`);
- await new Promise(resolve => setTimeout(resolve, 5000)); // Wait 5 seconds before retry
+ await this._delay(AUTH_CONFIG.delays.beforeRetry);
continue;
}
@@ -476,8 +506,8 @@ class AuthHelper {
await Promise.all([
this.page.waitForNavigation({
waitUntil: 'networkidle2',
- timeout: 10000
- }).catch(err => {
+ timeout: AUTH_CONFIG.timeouts.logout
+ }).catch(() => {
console.log(' Navigation wait timed out, checking URL...');
}),
this.page.evaluate(() => {
@@ -492,24 +522,24 @@ class AuthHelper {
await Promise.all([
this.page.waitForNavigation({
waitUntil: 'networkidle2',
- timeout: 10000
- }).catch(err => {
+ timeout: AUTH_CONFIG.timeouts.logout
+ }).catch(() => {
console.log(' Navigation wait timed out, checking URL...');
}),
this.page.click('a.logout-btn')
]);
} else {
// Last resort: navigate directly to logout URL
- console.log(' No logout form/button found, navigating directly to /auth/logout...');
- await this.page.goto(`${this.page.url().split('/').slice(0, 3).join('/')}/auth/logout`, {
+ console.log(` No logout form/button found, navigating directly to ${AUTH_CONFIG.paths.logout}...`);
+ await this.page.goto(`${this.page.url().split('/').slice(0, 3).join('/')}${AUTH_CONFIG.paths.logout}`, {
waitUntil: 'networkidle2',
- timeout: 10000
+ timeout: AUTH_CONFIG.timeouts.logout
});
}
}
// Give it a moment for any redirects
- await new Promise(resolve => setTimeout(resolve, 1000));
+ await this._delay(AUTH_CONFIG.delays.afterLogout);
// Ensure we're on the login page or logged out
const currentUrl = this.page.url();
@@ -517,17 +547,17 @@ class AuthHelper {
// Check if we're logged out by looking for login form
const loginForm = await this.page.$('form[action*="login"], input[name="username"]');
- if (loginForm || currentUrl.includes('/auth/login')) {
+ if (loginForm || currentUrl.includes(AUTH_CONFIG.paths.login)) {
console.log('✅ Logged out successfully');
} else {
// Double-check by trying to access a protected page
await this.page.goto(`${this.page.url().split('/').slice(0, 3).join('/')}/settings/`, {
waitUntil: 'networkidle2',
- timeout: 5000
+ timeout: AUTH_CONFIG.timeouts.formSelector
}).catch(() => {});
const finalUrl = this.page.url();
- if (finalUrl.includes('/auth/login')) {
+ if (finalUrl.includes(AUTH_CONFIG.paths.login)) {
console.log('✅ Logged out successfully (verified via protected page)');
} else {
console.log(`Warning: May not be fully logged out. Current URL: ${finalUrl}`);