124 lines
4.5 KiB
Python
124 lines
4.5 KiB
Python
# tests/test_e2e_authentication.py
|
|
|
|
import pytest
|
|
from playwright.sync_api import Page, expect
|
|
|
|
from playwright_config import BASE_URL
|
|
|
|
|
|
class TestAuthentication:
|
|
"""Tests for authentication and login functionality"""
|
|
|
|
@pytest.mark.e2e
|
|
@pytest.mark.smoke
|
|
def test_unauthenticated_user_redirected_to_login(self, app_server, page: Page):
|
|
"""Test that when not logged in, the default page is the login page"""
|
|
# Navigate to the root URL
|
|
page.goto(BASE_URL)
|
|
|
|
# Wait for the page to fully load
|
|
page.wait_for_load_state("networkidle")
|
|
|
|
# Check that we're on the login page
|
|
# Option 1: Check URL contains login-related path
|
|
expect(page).to_have_url(f"{BASE_URL}/authlogin/login")
|
|
|
|
# Option 2: Check for login form elements
|
|
# Look for typical login form elements
|
|
login_indicators = [
|
|
"input[type='email']",
|
|
"input[type='password']",
|
|
"input[name*='username']",
|
|
"input[name*='email']",
|
|
"input[name*='password']",
|
|
"button[type='submit']",
|
|
"form"
|
|
]
|
|
|
|
# At least one login indicator should be present
|
|
login_form_found = False
|
|
for selector in login_indicators:
|
|
if page.locator(selector).count() > 0:
|
|
login_form_found = True
|
|
break
|
|
|
|
assert login_form_found, "No login form elements found on the page"
|
|
|
|
# Option 3: Check for login-related text content
|
|
page_content = page.content().lower()
|
|
login_keywords = ["login", "sign in", "authenticate", "username", "password", "email"]
|
|
|
|
has_login_content = any(keyword in page_content for keyword in login_keywords)
|
|
assert has_login_content, "Page does not contain login-related content"
|
|
|
|
# Option 4: Ensure we're not on a protected page
|
|
# Check that we don't see protected content like "dashboard", "logout", "profile", "settings"]
|
|
protected_content = ["dashboard", "logout", "profile", "settings"]
|
|
page_text = page.locator("body").inner_text().lower()
|
|
|
|
for protected_word in protected_content:
|
|
assert protected_word not in page_text, f"Found protected content '{protected_word}' when not logged in"
|
|
|
|
@pytest.mark.e2e
|
|
@pytest.mark.regression
|
|
def test_login_page_has_required_elements(self, app_server, page: Page):
|
|
"""Test that the login page contains all required elements"""
|
|
page.goto(BASE_URL)
|
|
page.wait_for_load_state("networkidle")
|
|
|
|
# Check for essential login form elements
|
|
expect(page.locator("form")).to_be_visible()
|
|
|
|
# Check for input fields (at least email/username and password)
|
|
username_input = page.locator("input[type='email'], input[name*='username'], input[name*='email']")
|
|
expect(username_input.first).to_be_visible()
|
|
|
|
password_input = page.locator("input[type='password'], input[name*='password']")
|
|
expect(password_input.first).to_be_visible()
|
|
|
|
# Check for submit button
|
|
submit_button = page.locator("button[type='submit'], input[type='submit']")
|
|
expect(submit_button.first).to_be_visible()
|
|
|
|
@pytest.mark.e2e
|
|
def test_page_loads_without_errors(self, app_server, page: Page):
|
|
"""Test that the login page loads without console errors"""
|
|
console_errors = []
|
|
|
|
def handle_console(msg):
|
|
if msg.type == "error":
|
|
console_errors.append(msg.text)
|
|
|
|
page.on("console", handle_console)
|
|
page.goto(BASE_URL)
|
|
page.wait_for_load_state("networkidle")
|
|
|
|
# Check for console errors
|
|
assert len(console_errors) == 0, f"Console errors found: {console_errors}"
|
|
|
|
# Check that the page actually loaded (not a 404 or 500 error)
|
|
expect(page.locator("body")).to_be_visible()
|
|
|
|
# Check that the page has a title
|
|
expect(page).to_have_title("My Managing Tools")
|
|
|
|
# New test to validate database isolation
|
|
@pytest.mark.e2e
|
|
@pytest.mark.smoke
|
|
def test_test_database_isolation(self, app_server, test_database, test_users):
|
|
"""Test that application uses isolated test database"""
|
|
import os
|
|
|
|
# Verify test environment variables are configured
|
|
assert os.environ.get("DB_PATH") == test_database
|
|
assert os.environ.get("ADMIN_EMAIL") == test_users["admin"]["email"]
|
|
assert os.environ.get("ADMIN_PASSWORD") == test_users["admin"]["password"]
|
|
|
|
# Verify temporary database file exists
|
|
assert os.path.exists(test_database), f"Test database file not found: {test_database}"
|
|
|
|
# Verify path contains 'test_mmt_' to confirm isolation
|
|
assert "test_mmt_" in test_database, "Database path should contain test prefix"
|
|
|
|
print(f"✅ Test database isolation confirmed: {test_database}")
|