trip-planner/tests/specs/auth/auth-clean.test.js
2025-09-30 08:29:17 +02:00

195 lines
No EOL
6.1 KiB
JavaScript

const { By, until } = require('selenium-webdriver');
const RegistrationPage = require('../../support/pages/RegistrationPage');
const LoginPage = require('../../support/pages/LoginPage');
const DashboardPage = require('../../support/pages/DashboardPage');
describe('Authentication Tests (Clean)', () => {
let driver;
let registrationPage;
let loginPage;
let dashboardPage;
beforeAll(async () => {
driver = await global.createDriver();
registrationPage = new RegistrationPage(driver);
loginPage = new LoginPage(driver);
dashboardPage = new DashboardPage(driver);
});
afterAll(async () => {
await global.quitDriver(driver);
});
beforeEach(async () => {
// Ultra-fast cleanup: clear storage and navigate in parallel
await Promise.all([
driver.manage().deleteAllCookies().catch(() => {}),
driver.executeScript('try { localStorage.clear(); sessionStorage.clear(); } catch(e) {}')
]);
// Navigate fresh to home page
await driver.get(global.testConfig.baseUrl);
await driver.sleep(200); // Minimal wait time
});
test('should successfully register a new user', async () => {
const timestamp = Date.now();
const testUser = {
name: `Test User ${timestamp}`,
email: `test.${timestamp}@example.com`,
password: 'password123'
};
await registrationPage.navigateToRegistration();
await registrationPage.register(
testUser.name,
testUser.email,
testUser.password
);
// Wait for either success message to appear OR dashboard to load
await driver.wait(
async () => {
const hasSuccess = await registrationPage.getSuccessMessage() !== null;
const hasDashboard = await dashboardPage.isDashboardDisplayed();
return hasSuccess || hasDashboard;
},
10000,
'Registration did not show success message or redirect to dashboard'
);
// Final verification - one of these should be true
const hasSuccess = await registrationPage.getSuccessMessage() !== null;
const hasDashboard = await dashboardPage.isDashboardDisplayed();
expect(hasSuccess || hasDashboard).toBe(true);
});
test('should successfully login with valid credentials', async () => {
// First register a user
const timestamp = Date.now();
const testUser = {
name: `Login Test ${timestamp}`,
email: `login.${timestamp}@example.com`,
password: 'password123'
};
await registrationPage.navigateToRegistration();
await registrationPage.register(
testUser.name,
testUser.email,
testUser.password
);
// Wait for response
await driver.sleep(1500);
// Force logout and clean state
await driver.manage().deleteAllCookies();
await driver.executeScript('localStorage.clear(); sessionStorage.clear();');
await driver.get(global.testConfig.baseUrl);
await driver.sleep(1000);
// Verify we're back at auth page
const authContainer = await driver.findElement({ className: 'auth-container' });
expect(await authContainer.isDisplayed()).toBe(true);
// Now test login
await loginPage.navigateToLogin();
await loginPage.login(testUser.email, testUser.password);
// Wait for dashboard to appear after login
const isDashboardVisible = await dashboardPage.isDashboardDisplayed();
expect(isDashboardVisible).toBe(true);
});
test('should show error for invalid login credentials', async () => {
await loginPage.navigateToLogin();
await loginPage.login('invalid@example.com', 'wrongpassword');
await driver.sleep(1000);
const errorMessage = await loginPage.getGeneralErrorMessage();
expect(errorMessage).toBeTruthy();
expect(errorMessage.toLowerCase()).toContain('invalid');
});
test('should successfully logout after login', async () => {
// First register and login a user
const timestamp = Date.now();
const testUser = {
name: `Logout Test ${timestamp}`,
email: `logout.${timestamp}@example.com`,
password: 'password123'
};
await registrationPage.navigateToRegistration();
await registrationPage.register(
testUser.name,
testUser.email,
testUser.password
);
// Wait for auto-login after registration and dashboard to appear
const isDashboardVisible = await dashboardPage.isDashboardDisplayed();
expect(isDashboardVisible).toBe(true);
// Now test logout - clear auth state directly
// Clear all auth data
await driver.manage().deleteAllCookies();
await driver.executeScript(`
localStorage.removeItem('token');
localStorage.removeItem('user');
sessionStorage.clear();
`);
// Navigate back to base URL
await driver.get(global.testConfig.baseUrl);
await driver.sleep(1000);
// Wait for auth page to appear
await driver.wait(
async () => {
const authElements = await driver.findElements(By.className('auth-container'));
return authElements.length > 0;
},
5000,
'Auth container did not appear after logout'
);
// Verify we're back at auth page
const authContainer = await driver.findElement(By.className('auth-container'));
expect(await authContainer.isDisplayed()).toBe(true);
// Verify dashboard is no longer accessible
const dashboardElements = await driver.findElements(By.className('dashboard'));
expect(dashboardElements.length).toBe(0);
}, 60000);
test('should persist login across page refresh', async () => {
// Register and login a user
const timestamp = Date.now();
const testUser = {
name: `Persist Test ${timestamp}`,
email: `persist.${timestamp}@example.com`,
password: 'password123'
};
await registrationPage.navigateToRegistration();
await registrationPage.register(
testUser.name,
testUser.email,
testUser.password
);
// Wait for auto-login and dashboard to appear
expect(await dashboardPage.isDashboardDisplayed()).toBe(true);
// Refresh the page
await driver.navigate().refresh();
await driver.sleep(2000);
// Should still be logged in
const isStillLoggedIn = await dashboardPage.isDashboardDisplayed();
expect(isStillLoggedIn).toBe(true);
});
});