"""E2E tests for the comment submission flow.""" from __future__ import annotations import pytest from playwright.sync_api import Page, expect ARTICLE_SLUG = "nightly-playwright-journey" def _go_to_article(page: Page, base_url: str) -> None: page.goto(f"{base_url}/articles/{ARTICLE_SLUG}/", wait_until="networkidle") def _submit_comment(page: Page, *, name: str = "E2E Tester", email: str = "e2e@example.com", body: str) -> None: """Fill and submit the main (non-reply) comment form.""" form = page.locator("form[data-comment-form]") form.locator('input[name="author_name"]').fill(name) form.locator('input[name="author_email"]').fill(email) form.locator('textarea[name="body"]').fill(body) form.get_by_role("button", name="Post comment").click() @pytest.mark.e2e def test_valid_comment_shows_moderation_message(page: Page, base_url: str) -> None: """Successful comment submission must show the awaiting-moderation banner.""" _go_to_article(page, base_url) _submit_comment(page, body="This is a test comment from Playwright.") page.wait_for_url(lambda url: "commented=1" in url, timeout=10_000) expect(page.get_by_text("Your comment is awaiting moderation")).to_be_visible() @pytest.mark.e2e def test_valid_comment_not_immediately_visible(page: Page, base_url: str) -> None: """Submitted comment must NOT appear in the comments list before moderation.""" _go_to_article(page, base_url) unique_body = "Unique unmoderated comment body xq7z" _submit_comment(page, body=unique_body) page.wait_for_url(lambda url: "commented=1" in url, timeout=10_000) expect(page.get_by_text(unique_body)).not_to_be_visible() @pytest.mark.e2e def test_empty_body_shows_form_errors(page: Page, base_url: str) -> None: _go_to_article(page, base_url) _submit_comment(page, body=" ") # whitespace-only body page.wait_for_load_state("networkidle") expect(page.locator('[aria-label="Comment form errors"]')).to_be_visible() assert "commented=1" not in page.url @pytest.mark.e2e def test_missing_name_shows_form_errors(page: Page, base_url: str) -> None: _go_to_article(page, base_url) form = page.locator("form[data-comment-form]") form.locator('input[name="author_name"]').fill("") form.locator('input[name="author_email"]').fill("e2e@example.com") form.locator('textarea[name="body"]').fill("Comment without a name.") form.get_by_role("button", name="Post comment").click() page.wait_for_load_state("networkidle") assert "commented=1" not in page.url @pytest.mark.e2e def test_reply_form_visible_on_approved_comment(page: Page, base_url: str) -> None: """An approved seeded comment must display a reply form.""" _go_to_article(page, base_url) # The seeded approved comment should be visible expect(page.get_by_text("E2E Approved Commenter")).to_be_visible() # And a Reply button for it expect(page.get_by_role("button", name="Reply")).to_be_visible() @pytest.mark.e2e def test_reply_submission_redirects(page: Page, base_url: str) -> None: """Submitting a reply to an approved comment should redirect with commented=1.""" _go_to_article(page, base_url) # The reply form is always visible below the approved seeded comment reply_form = page.locator("form[action]").filter(has=page.get_by_role("button", name="Reply")).first reply_form.locator('input[name="author_name"]').fill("E2E Replier") reply_form.locator('input[name="author_email"]').fill("replier@example.com") reply_form.locator('textarea[name="body"]').fill("This is a test reply.") reply_form.get_by_role("button", name="Reply").click() page.wait_for_url(lambda url: "commented=1" in url, timeout=10_000) expect(page.get_by_text("Your comment is awaiting moderation")).to_be_visible() @pytest.mark.e2e def test_comments_section_absent_when_disabled(page: Page, base_url: str) -> None: """Article with comments_enabled=False must not show the comments section.""" response = page.goto(f"{base_url}/articles/e2e-no-comments/", wait_until="networkidle") assert response is not None and response.status == 200, ( f"Expected 200 for e2e-no-comments article, got {response and response.status}" ) expect(page.get_by_role("heading", level=1)).to_have_text("No Comments Article") expect(page.get_by_role("heading", name="Comments", exact=True)).to_have_count(0) expect(page.get_by_role("button", name="Post comment")).to_have_count(0)