Exemple #1
0
def ignore_redirect(db: DatabaseHandler, url: str, redirect_url: Optional[str]) -> bool:
    """Return true if we should ignore redirects to the target media source.

    This is usually to avoid redirects to domain resellers for previously valid and important but now dead links."""
    if redirect_url is None or url == redirect_url:
        return False

    medium_url = generate_medium_url_and_name_from_url(redirect_url)[0]

    u = normalize_url_lossy(medium_url)

    match = db.query("select 1 from topic_ignore_redirects where url = %(a)s", {'a': u}).hash()

    return match is not None
def test_ignore_redirect():
    db = connect_to_db()

    # redirect_url = None
    assert not ignore_redirect(db, 'http://foo.com', None)

    # url = redirect_url
    assert not ignore_redirect(db, 'http://foo.com', 'http://foo.com')

    # empty topic_ignore_redirects
    assert not ignore_redirect(db, 'http://foo.com', 'http://bar.com')

    # match topic_ingnore_redirects
    redirect_url = 'http://foo.com/foo.bar'
    medium_url = generate_medium_url_and_name_from_url(redirect_url)[0]
    nu = normalize_url_lossy(medium_url)

    db.create('topic_ignore_redirects', {'url': nu})

    assert ignore_redirect(db, 'http://bar.com', redirect_url)

    # no match
    assert not ignore_redirect(db, 'http://bar.com', 'http://bat.com')
def test_generate_medium_url_and_name_from_url() -> None:
    (url, name) = generate_medium_url_and_name_from_url('http://foo.com/bar')
    assert url == 'http://foo.com/'
    assert name == 'foo.com'