Files
blog.pyaqa.ru/tests/e2e/test_profile_and_theme.py
Sergey Vanyushkin cf4982c0e5 test(e2e): add TC-E2E-003/004/005/007/008/009/010 — delete, pagination, errors, profile, theme
- test_post_deletion.py: user delete own, admin delete any, 403 for other's
- test_pagination.py: navigation across pages, boundary on last page
- test_errors.py: 404 nonexistent post, 404 for other user's draft
- test_post_lifecycle.py: draft-to-publish via edit flow
- test_post_ownership.py: user can edit own post
- test_profile_and_theme.py: profile page rendering, theme toggle with localStorage
- fix(web): remove infinite pagination for USER role (routes.py)
- fix(e2e): stabilize all publish() calls with expect_navigation
- fix(e2e): add _unique_title() to avoid slug collisions at scale
- docs: update FEATURE_POST_LIFECYCLE.md and TEST_MODEL.md coverage
2026-05-08 20:25:01 +03:00

115 lines
3.7 KiB
Python

"""End-to-end tests for profile page and theme toggle.
Tests that authenticated users can view their profile with correct data
and that the theme switcher toggles between light and dark modes with
localStorage persistence.
"""
from __future__ import annotations
import pytest
from playwright.sync_api import Page
@pytest.mark.e2e
def test_user_profile_page_renders_correctly(
user_page: Page,
admin_page: Page,
base_url: str,
) -> None:
"""Test that profile page displays user info, role badge, and actions.
Steps:
1. User navigates to /web/profile.
2. Verify username, role badge, email, and user ID are visible.
3. Admin navigates to /web/profile.
4. Verify admin sees ADMIN role badge with primary CSS class.
5. Guest attempts to access /web/profile and is redirected to login.
Args:
user_page: Playwright page authenticated as regular user.
admin_page: Playwright page authenticated as admin.
base_url: Application base URL.
"""
user_page.goto(f"{base_url}/web/profile")
user_page.wait_for_selector('[data-testid="profile-username"]')
username = user_page.locator('[data-testid="profile-username"]').text_content()
assert username
assert len(username) > 0
role = user_page.locator('[data-testid="profile-role"]').text_content()
assert "USER" in role
role_class = user_page.locator('[data-testid="profile-role"]').evaluate(
"el => el.className",
)
assert "badge-success" in role_class
user_id = user_page.locator('[data-testid="profile-value-userid"]').text_content()
assert user_id
assert len(user_id) > 0
assert user_page.locator('[data-testid="btn-create-post-profile"]').is_visible()
admin_page.goto(f"{base_url}/web/profile")
admin_page.wait_for_selector('[data-testid="profile-username"]')
admin_role = admin_page.locator('[data-testid="profile-role"]').text_content()
assert "ADMIN" in admin_role
admin_role_class = admin_page.locator('[data-testid="profile-role"]').evaluate(
"el => el.className",
)
assert "badge-primary" in admin_role_class
@pytest.mark.e2e
def test_theme_toggle_switches_between_light_and_dark(
user_page: Page,
base_url: str,
) -> None:
"""Test that the theme toggle switches between light and dark modes.
Steps:
1. Open home page and read initial theme from html data-theme.
2. Click the theme toggle button.
3. Verify the theme attribute flips and localStorage updates.
4. Click again and verify it flips back.
5. Verify icon visibility changes accordingly.
Args:
user_page: Playwright page authenticated as regular user.
base_url: Application base URL.
"""
user_page.goto(f"{base_url}/web/")
user_page.wait_for_selector('[data-testid="theme-toggle"]')
initial_theme = user_page.evaluate(
"() => document.documentElement.getAttribute('data-theme')",
)
assert initial_theme in ("light", "dark")
user_page.locator('[data-testid="theme-toggle"]').click()
user_page.wait_for_timeout(300)
new_theme = user_page.evaluate(
"() => document.documentElement.getAttribute('data-theme')",
)
assert new_theme != initial_theme
assert new_theme in ("light", "dark")
stored = user_page.evaluate("() => localStorage.getItem('blog-theme')")
assert stored == new_theme
user_page.locator('[data-testid="theme-toggle"]').click()
user_page.wait_for_timeout(300)
restored_theme = user_page.evaluate(
"() => document.documentElement.getAttribute('data-theme')",
)
assert restored_theme == initial_theme
stored_restored = user_page.evaluate("() => localStorage.getItem('blog-theme')")
assert stored_restored == initial_theme