File size: 2,326 Bytes
176ee12
 
 
 
 
e360c25
 
 
 
176ee12
 
e360c25
176ee12
e360c25
176ee12
 
e360c25
176ee12
e360c25
 
 
176ee12
e360c25
176ee12
e360c25
176ee12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e360c25
176ee12
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from __future__ import annotations

from app.links import allowed_links, sanitize_links
from app.models import KnowledgeSource

# exact is the CANONICAL form produced by allowed_links (www/scheme/slash-less)
EXACT = {"flexigobe.com", "flexigobe.com/catalogo.pdf"}
HOSTS = {"flexigobe.com"}


def test_sanitize_keeps_real_drops_fake_and_fixes_path():
    # real allowlisted link kept exactly
    assert "https://flexigobe.com/catalogo.pdf" in sanitize_links(
        "Catálogo: https://flexigobe.com/catalogo.pdf", EXACT, HOSTS
    )
    # invented path on the known store domain → downgraded to the real homepage
    out = sanitize_links("Más info: https://flexigobe.com/about", EXACT, HOSTS)
    assert "/about" not in out and "https://flexigobe.com" in out
    # www variant of a known host is recognized (not treated as foreign)
    out_www = sanitize_links("Web: https://www.flexigobe.com/inventado", EXACT, HOSTS)
    assert "flexigobe.com" in out_www and "/inventado" not in out_www
    # foreign/unknown domain → removed
    assert "evil.example.com" not in sanitize_links("Visita https://evil.example.com/x", EXACT, HOSTS)
    # markdown link with invented path → label kept, link fixed
    out3 = sanitize_links("Lee [Sobre nosotros](https://flexigobe.com/about) aquí", EXACT, HOSTS)
    assert "Sobre nosotros" in out3 and "/about" not in out3


def test_sanitize_noop_without_links():
    assert sanitize_links("Sin enlaces aquí.", {"x"}, {"x"}) == "Sin enlaces aquí."


async def test_allowed_links_built_from_source_urls(db_session):
    db_session.add(
        KnowledgeSource(
            tenant_id=7, kind="url", name="web", location="https://shop.example",
            urls=["https://shop.example", "https://shop.example/faq"],
        )
    )
    await db_session.flush()
    exact, hosts = await allowed_links(db_session, 7)
    assert "shop.example/faq" in exact  # canonical form
    assert "shop.example" in hosts
    # another tenant's links must not leak in
    exact2, _ = await allowed_links(db_session, 999)
    assert exact2 == set()


def test_extract_urls_from_text():
    from app.rag.extract import extract_urls_from_text

    urls = extract_urls_from_text("Mira https://a.com/x y tambien https://a.com/y.pdf, gracias.")
    assert urls == ["https://a.com/x", "https://a.com/y.pdf"]