Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.9 KB
Raw Blame
# allow: no-sut-import — black-box HTTP test; drives real routes through the Flask test client
"""
Tests for chat page routes (GET /chat/ and GET /chat/<session_id>).

These tests verify:
- Page routes require login
- Page renders with and without session_id
- Invalid session_id is handled gracefully
- A link from another origin cannot carry ``?q=`` into the page
"""

import json

import pytest

# Sec-Fetch-Site values for a navigation that another origin started.
FOREIGN_SITES = ("cross-site", "same-site")


class TestChatPageRoutes:
    """Tests for chat page rendering endpoints."""

    def test_chat_page_requires_login(self, client):
        """Test that GET /chat/ requires authentication."""
        response = client.get("/chat/", follow_redirects=False)
        # Should redirect to login page
        assert response.status_code in (302, 303)
        assert (
            "/login" in response.headers.get("location", "")
            or response.status_code == 401
        )

    def test_chat_page_renders_without_session_id(self, authenticated_client):
        """Test that GET /chat/ renders successfully for authenticated users."""
        response = authenticated_client.get("/chat/")
        assert response.status_code == 200
        # The response should be HTML (from render_template)
        assert b"<!DOCTYPE html>" in response.data or b"<html" in response.data

    def test_chat_page_renders_with_valid_session_id(
        self, authenticated_client
    ):
        """Test that GET /chat/<session_id> renders with a session ID."""
        # First create a session
        create_response = authenticated_client.post(
            "/api/chat/sessions",
            json={"initial_query": "Test query"},
            content_type="application/json",
        )
        session_id = json.loads(create_response.data)["session_id"]

        # Now load the chat page with that session
        response = authenticated_client.get(f"/chat/{session_id}")
        assert response.status_code == 200
        assert b"<!DOCTYPE html>" in response.data or b"<html" in response.data

    def test_chat_page_handles_invalid_session_id(self, authenticated_client):
        """Test that GET /chat/<session_id> with invalid session_id still renders.

        The page should render (status 200) because session validation happens
        client-side via JavaScript API calls, not server-side during page load.
        """
        invalid_session_id = "non-existent-session-id-12345"
        response = authenticated_client.get(f"/chat/{invalid_session_id}")
        # Page should still render - session validation is client-side
        assert response.status_code == 200

    def test_chat_page_with_session_requires_login(self, client):
        """Test that GET /chat/<session_id> requires authentication."""
        response = client.get("/chat/some-session-id", follow_redirects=False)
        # Should redirect to login page
        assert response.status_code in (302, 303)
        assert (
            "/login" in response.headers.get("location", "")
            or response.status_code == 401
        )


class TestChatPageLinkQuery:
    """chat.js only pre-fills the chat box from ``?q=``; nothing is sent
    until the user presses Send.

    The SameSite=Lax session cookie lets a link from another site open the
    chat page signed in, so the route also redirects ``q`` away when the
    browser sends ``Sec-Fetch-Site`` saying another origin started the
    navigation.
    """

    @pytest.mark.parametrize("site", FOREIGN_SITES)
    @pytest.mark.parametrize(
        ("path", "expected"),
        [
            ("/chat/?q=linked+text&mode=quick", "/chat/?mode=quick"),
            ("/chat/some-session-id?q=linked+text", "/chat/some-session-id"),
            ("/chat/a%20b?q=", "/chat/a%20b"),
        ],
    )
    def test_foreign_link_loses_q_before_the_login_check(
        self, client, site, path, expected
    ):
        """Signed out, this redirect still comes before the login bounce,
        so the bounce's ``next`` does not carry this link's ``q``. (A link
        straight to ``/auth/login?next=/chat/?q=...`` skips this route; the
        login page drops a chat ``q`` itself:
        tests/web/test_redirect_and_url_generation.py.)"""
        response = client.get(
            path, headers={"Sec-Fetch-Site": site}, follow_redirects=False
        )
        assert response.status_code == 303
        assert response.headers["location"] == expected

    def test_signed_in_foreign_link_opens_the_page_without_q(
        self, authenticated_client
    ):
        response = authenticated_client.get(
            "/chat/?q=linked+text",
            headers={"Sec-Fetch-Site": "cross-site"},
            follow_redirects=False,
        )
        assert response.status_code == 303
        assert response.headers["location"] == "/chat/"

        # External links still open the chat signed in (#6668).
        page = authenticated_client.get(
            response.headers["location"],
            headers={"Sec-Fetch-Site": "cross-site"},
            follow_redirects=False,
        )
        assert page.status_code == 200
        assert b'id="chat-input"' in page.data

    @pytest.mark.parametrize("site", [None, "same-origin", "none"])
    def test_same_origin_typed_or_unmarked_navigation_keeps_q(
        self, authenticated_client, site
    ):
        """``none`` is a typed or bookmarked URL, or a link opened from
        another app such as a mail or chat client; no header at all is what
        a plain-HTTP origin other than localhost gets. Each keeps ``q``, so
        the text is pre-filled and nothing more: chat.js sends nothing until
        the user presses Send."""
        headers = {"Sec-Fetch-Site": site} if site else {}
        response = authenticated_client.get(
            "/chat/?q=linked+text", headers=headers, follow_redirects=False
        )
        assert response.status_code == 200
        assert b'id="chat-input"' in response.data