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.

6.2 KB
Raw Blame
import json
from collections.abc import Generator
from io import StringIO
from unittest.mock import patch

import apprise
import pytest
from loguru import logger as loguru_logger

from local_deep_research.notifications.exceptions import ServiceError
from local_deep_research.notifications.service import NotificationService
from local_deep_research.security.notification_validator import (
    NotificationURLValidator,
)


BOT_ID = "123456789"
TOKEN_SECRET = "Token_abc-123"
BOT_TOKEN = f"{BOT_ID}:{TOKEN_SECRET}"
CHAT_ID = 987654321
TARGET_ID = str(CHAT_ID)
MALFORMED_TELEGRAM_URLS = (
    f"tgram://bot{BOT_ID}:{TOKEN_SECRET}.evil/{TARGET_ID}",
    f"tgram://bot{BOT_ID}%3A{TOKEN_SECRET}.evil/{TARGET_ID}",
)
SECRET_PARTS = (BOT_ID, TOKEN_SECRET, TARGET_ID)


@pytest.fixture
def loguru_output() -> Generator[StringIO, None, None]:
    output = StringIO()
    sink_id = loguru_logger.add(
        output,
        format="{message}",
        level="DEBUG",
        diagnose=False,
    )
    loguru_logger.enable("local_deep_research")
    try:
        yield output
    finally:
        loguru_logger.remove(sink_id)
        loguru_logger.disable("local_deep_research")


@pytest.mark.parametrize(
    "url",
    (
        f"tgram://{BOT_TOKEN}/{CHAT_ID}",
        "tgram://bot123456789%3AToken_abc-123?detect=no",
        f"tgram://{BOT_TOKEN}",
    ),
    ids=("literal-colon", "encoded-colon", "authority-only"),
)
def test_canonical_apprise_telegram_urls_validate_without_dns(url: str) -> None:
    with patch(
        "local_deep_research.security.notification_validator.socket.getaddrinfo"
    ) as dns_lookup:
        validation_result = NotificationURLValidator.validate_service_url(url)

    apprise_result = apprise.Apprise().add(url)
    alias_result = apprise.Apprise().add(
        url.replace("tgram://", "telegram://", 1)
    )

    assert apprise_result is True
    assert alias_result is False
    assert validation_result == (True, None)
    dns_lookup.assert_not_called()


@pytest.mark.parametrize(
    "url",
    (
        "tgram://bot_token/chat_id",
        "tgram://123456789/Token_abc-123/987654321",
        "tgram://123456789::Token_abc-123/987654321",
        "tgram://123456789%3A/987654321",
        "tgram://user@123456789:Token_abc-123/987654321",
        "tgram://123456789:Token_abc-123.evil/987654321",
        "tgram://123456789:Token_abc-123#fragment",
    ),
)
def test_noncanonical_apprise_telegram_authorities_are_rejected(
    url: str,
) -> None:
    with patch(
        "local_deep_research.security.notification_validator.socket.getaddrinfo"
    ) as dns_lookup:
        result = NotificationURLValidator.validate_service_url(url)

    assert result == (
        False,
        "Invalid Telegram service URL; expected "
        "tgram://<bot_id>:<token>/<chat_id>",
    )
    dns_lookup.assert_not_called()


@pytest.mark.parametrize(
    "notification_url",
    (
        f"tgram://bot{BOT_TOKEN}/{CHAT_ID}",
        "tgram://bot123456789%3AToken_abc-123/987654321",
    ),
    ids=("literal-colon", "encoded-colon"),
)
def test_real_apprise_telegram_notify_uses_only_the_pinned_endpoint(
    notification_url: str,
) -> None:
    instance = apprise.Apprise()
    assert instance.add(notification_url) is True

    with patch("apprise.plugins.telegram.requests.post") as post:
        post.return_value.status_code = 200

        result = instance.notify(title="Test", body="Network-free message")

    assert result is True
    post.assert_called_once()
    request_url = post.call_args.args[0]
    payload = json.loads(post.call_args.kwargs["data"])
    assert request_url == (
        "https://api.telegram.org/bot123456789:Token_abc-123/sendMessage"
    )
    assert payload["chat_id"] == CHAT_ID
    assert str(CHAT_ID) not in request_url


@pytest.mark.parametrize(
    "service_url",
    MALFORMED_TELEGRAM_URLS,
    ids=("literal-colon", "encoded-colon"),
)
def test_validate_multiple_urls_omits_malformed_telegram_url(
    service_url: str,
) -> None:
    is_valid, error = NotificationURLValidator.validate_multiple_urls(
        service_url
    )

    assert is_valid is False
    assert error is not None
    for secret in SECRET_PARTS:
        assert secret not in error
    assert error == (
        "Invalid notification service URL: Invalid Telegram service URL; "
        "expected tgram://<bot_id>:<token>/<chat_id>"
    )


@pytest.mark.parametrize(
    "service_url",
    MALFORMED_TELEGRAM_URLS,
    ids=("literal-colon", "encoded-colon"),
)
def test_send_validation_failure_omits_malformed_telegram_url(
    service_url: str,
    loguru_output: StringIO,
) -> None:
    service = NotificationService(outbound_allowed=True)
    with (
        patch(
            "apprise.plugins.telegram.requests.post",
            side_effect=AssertionError("Telegram POST must not occur"),
        ) as post,
        pytest.raises(ServiceError) as raised,
    ):
        service.send(
            title="Test",
            body="Network-free message",
            service_urls=service_url,
        )

    post.assert_not_called()
    exception_text = str(raised.value)
    output = loguru_output.getvalue()
    for secret in SECRET_PARTS:
        assert secret not in output
        assert secret not in exception_text
    assert "Service URL validation failed" in output
    assert "Invalid Telegram service URL" in exception_text


@pytest.mark.parametrize(
    "service_url",
    MALFORMED_TELEGRAM_URLS,
    ids=("literal-colon", "encoded-colon"),
)
def test_test_service_warning_omits_malformed_telegram_url(
    service_url: str,
    loguru_output: StringIO,
) -> None:
    service = NotificationService(outbound_allowed=True)
    with patch(
        "apprise.plugins.telegram.requests.post",
        side_effect=AssertionError("Telegram POST must not occur"),
    ) as post:
        result = service.test_service(service_url)

    post.assert_not_called()
    assert result["success"] is False
    user_error = result["error"]
    assert isinstance(user_error, str)
    output = loguru_output.getvalue()
    for secret in SECRET_PARTS:
        assert secret not in output
        assert secret not in user_error
    assert (
        "Test service URL validation failed: Invalid Telegram service URL"
        in output
    )
    assert "Invalid Telegram service URL" in user_error