Arch/tests/test_auth.py

73 lines
2.7 KiB
Python
Raw Normal View History

2025-03-03 17:50:11 -03:00
import pytest
from flask import session, g
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
class TestAuth:
"""Test authentication functionality."""
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
def test_login_page(self, client):
"""Test that login page loads correctly."""
2025-03-04 06:38:19 -03:00
response = client.get("/auth/login")
2025-03-03 17:50:11 -03:00
assert response.status_code == 200
2025-03-04 06:38:19 -03:00
assert b"Iniciar sesi" in response.data # 'Iniciar sesión' in Spanish
2025-03-03 17:50:11 -03:00
def test_login_success(self, client):
"""Test successful login with correct credentials."""
response = client.post(
2025-03-04 06:38:19 -03:00
"/auth/login",
data={"username": "admin", "password": "admin123"},
follow_redirects=True,
2025-03-03 17:50:11 -03:00
)
assert response.status_code == 200
# Check that we're redirected to the right page after login
2025-03-04 06:38:19 -03:00
assert b"Panel" in response.data or b"Proyectos" in response.data
2025-03-03 17:50:11 -03:00
def test_login_invalid_credentials(self, client):
"""Test login with invalid credentials."""
response = client.post(
2025-03-04 06:38:19 -03:00
"/auth/login",
data={"username": "admin", "password": "wrongpassword"},
follow_redirects=True,
2025-03-03 17:50:11 -03:00
)
assert response.status_code == 200
2025-03-04 06:38:19 -03:00
assert (
b"credenciales" in response.data.lower()
) # Error message about credentials
2025-03-03 17:50:11 -03:00
def test_logout(self, auth, client):
"""Test logout functionality."""
# First login
auth.login()
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
# Then logout
response = auth.logout()
assert response.status_code == 200
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
# Check if logged out - try to access a protected page
2025-03-04 06:38:19 -03:00
response = client.get("/users/", follow_redirects=True)
assert b"iniciar sesi" in response.data.lower() # Should see login page
2025-03-03 17:50:11 -03:00
def test_access_protected_route(self, client):
"""Test accessing a protected route without login."""
# Try to access users list without login
2025-03-04 06:38:19 -03:00
response = client.get("/users/", follow_redirects=True)
2025-03-03 17:50:11 -03:00
assert response.status_code == 200
2025-03-04 06:38:19 -03:00
assert b"iniciar sesi" in response.data.lower() # Should be redirected to login
2025-03-03 17:50:11 -03:00
def test_access_protected_route_with_login(self, logged_in_client):
"""Test accessing a protected route with login."""
# Admin should be able to access users list
2025-03-04 06:38:19 -03:00
response = logged_in_client.get("/admin/dashboard")
2025-03-03 17:50:11 -03:00
assert response.status_code == 200
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
def test_permission_levels(self, client, auth):
"""Test different permission levels."""
# Login as regular user
2025-03-04 06:38:19 -03:00
auth.login(username="user1", password="admin123")
2025-03-03 17:50:11 -03:00
# Try to access admin-only page
2025-03-04 06:38:19 -03:00
response = client.get("/admin/dashboard", follow_redirects=True)
assert (
response.status_code == 403 or b"acceso denegado" in response.data.lower()
)