Esempio n. 1
0
 def test_generate_no_duplicate():
     assert Short.query.count() == 0
     one = Short.generate(target="test")
     assert Short.query.count() == 1
     two = Short.generate(target="test")
     assert Short.query.count() == 1
     assert one.symbol == two.symbol
Esempio n. 2
0
    def test_search_by_param(visitor):
        assert Short.query.count() == 0
        one = Short.generate(target="___one")
        two = Short.generate(target="two___")
        assert Short.query.count() == 2

        org = visitor(ENDPOINT)
        res_one = visitor(ENDPOINT, query_string="q=one")
        res_two = visitor(ENDPOINT, query_string="q=two")
        res_all = visitor(ENDPOINT, query_string="q=___")
        exp_one = 'value="one"'
        exp_two = 'value="two"'
        exp_all = 'value="___"'

        assert exp_one not in org.text
        assert exp_two not in org.text
        assert exp_all not in org.text

        assert exp_one in res_one.text
        assert one.target in res_one.text

        assert exp_two in res_two.text
        assert two.target in res_two.text

        assert exp_all in res_all.text
        assert one.target in res_all.text
        assert two.target in res_all.text
Esempio n. 3
0
 def test_generate_create():
     assert Short.query.count() == 0
     Short.create(symbol="demo", target="test")
     assert Short.query.count() == 1
     short = Short.generate(target="test")
     assert Short.query.count() == 1
     assert short.symbol == "demo"
Esempio n. 4
0
 def test_generate_duplicate_explicit():
     assert Short.query.count() == 0
     one = Short.generate(target="test", delay=23)
     assert Short.query.count() == 1
     two = Short.generate(target="test", delay=42)
     assert Short.query.count() == 2
     assert one.symbol != two.symbol
Esempio n. 5
0
    def test_symbol_unique():
        one = Short.create(symbol="sym", target="one", _commit=False)
        assert one.save(_commit=True)

        two = Short.create(symbol="sym", target="two", _commit=False)
        with raises(IntegrityError):
            assert two.save(_commit=True)
Esempio n. 6
0
    def test_len_symbol_empty():
        assert Short.len_symbol(1) == 1
        assert Short.len_symbol(2) == 2

        assert Short.len_symbol(1) == 1

        assert Short.len_symbol(-2) == 2
        assert Short.len_symbol(-1) == 1
Esempio n. 7
0
    def test_generate_symbol_length():
        for num in range(1, 1 + 23):
            sym = Short.generate_symbol(length=num)
            assert sym
            assert len(sym) == num

        assert Short.generate_symbol(length=0) == ""
        assert len(Short.generate_symbol(length=-1)) == 1
        assert len(Short.generate_symbol(length=-2)) == 2
Esempio n. 8
0
    def test_generate_symbol_basic():
        one = Short.generate_symbol(LEN)
        two = Short.generate_symbol(LEN)

        assert one
        assert two
        assert one != two

        assert one != Short.generate_symbol(LEN)
        assert two != Short.generate_symbol(LEN)
Esempio n. 9
0
    def test_ordered_query():
        assert Short.query.count() == 0
        Short.generate(target="one", delay=0)
        Short.generate(target="two", delay=0)
        Short.generate(target="drop", delay=1)
        Short.generate(target="nope", delay=2)
        assert Short.query.count() == 4

        assert Short.ordered(None).count() == 4
        query = Short.query.filter(Short.delay == 0)
        assert query.count() == 2
        assert Short.ordered(None, query=query).count() == 2
Esempio n. 10
0
    def test_len_symbol_fact():
        assert Short.len_symbol(1) == 1

        for a_sym in SYM_POOL:
            for b_sym in SYM_POOL:
                sym = a_sym + b_sym
                Short.create(symbol=sym, target=sym, _commit=False)
            Short.create(symbol=a_sym, target=a_sym, _commit=False)

        assert Short.len_symbol(0) == 3
        assert Short.len_symbol(1) == 3
        assert Short.len_symbol(2) == 3
        assert Short.len_symbol(3) == 3
        assert Short.len_symbol(4) == 4
Esempio n. 11
0
    def test_blocked():
        blocklist = [re_compile(r"^.+example\.com$", IGNORECASE)]
        assert Short.query.count() == 0
        Short.generate(target="http://www.example.org")
        Short.generate(target="https://example.org")
        one = Short.generate(target="http://www.example.com")
        two = Short.generate(target="https://example.com")
        assert Short.query.count() == 4

        assert Short.blocked([]).all() == []
        assert Short.blocked(blocklist).all() == [one, two]
        assert Short.blocked(blocklist, rev=True).all() == [two, one]
Esempio n. 12
0
    def test_nullables(session):
        short = Short.create(_commit=False)

        assert short.prime is None
        assert short.symbol is None
        assert short.target is None
        assert short.active is None
        assert short.created is None
        assert short.visited is None

        with raises(IntegrityError):
            short.save(_commit=True)
        session.rollback()

        short.update(symbol="sym", _commit=False)

        with raises(IntegrityError):
            short.save(_commit=True)
        session.rollback()

        short.update(target="tgt", _commit=False)

        short.save(_commit=True)

        assert short.prime == 1
        assert short.symbol == "sym"
        assert short.target == "tgt"
        assert short.active is True
        assert short.created is not None
        assert short.visited == 0
def _sho(target="test", delay=23):
    return Short.create(
        symbol="".join(choice(SYM_POOL) for _ in range(SYM_MINI)),
        target=target,
        delay=delay,
        _commit=False,
    )
Esempio n. 14
0
    def action(self):
        if not self.validate():
            return None

        return Short.generate(target=self.target.data,
                              delay=self.delay.data,
                              _commit=True)
Esempio n. 15
0
    def test_make_symbol():
        pool, delim, miss = SYM_POOL.partition(SYM_POOL[-2])
        for sym in pool:
            Short.create(symbol=sym, target=sym, _commit=False)

        symbol = Short.make_symbol(1)
        assert symbol in delim + miss
        Short.create(symbol=symbol, target=symbol, _commit=False)

        symbol = Short.make_symbol(1)
        assert symbol in delim + miss
        Short.create(symbol=symbol, target=symbol, _commit=False)

        assert len(Short.make_symbol(1)) == 2
Esempio n. 16
0
    def test_default_fields():
        start = datetime.utcnow()
        short = Short.create(symbol="abc", target="def")

        assert short.prime == 1
        assert short.symbol == "abc"
        assert short.target == "def"
        assert short.active is True
        assert start <= short.created
        assert short.created <= datetime.utcnow()
        assert short.visited == 0
Esempio n. 17
0
    def test_content(visitor):
        assert Short.query.count() == 0
        obj = Short.create(symbol="symbol", target="target")
        assert Short.query.count() == 1

        res = visitor(ENDPOINT)
        txt = res.text
        assert str(obj.prime) in txt
        assert obj.symbol in txt
        assert obj.target in txt
        assert str(obj.created) in txt
Esempio n. 18
0
    def test_display(visitor, monkeypatch):
        assert Short.query.count() == 0
        one = Short.generate(target="one")
        two = Short.generate(target="two")
        thr = Short.generate(target="three")
        assert Short.query.count() == 3

        org = visitor(ENDPOINT)
        blocklist = [re_compile(r"^t.+", IGNORECASE)]
        monkeypatch.setattr(plus, "BLOCKLIST", blocklist)
        res = visitor(ENDPOINT)

        assert "nothing there" in org.text.lower()
        assert "nothing there" not in res.text.lower()
        assert f"<td>{one.prime}</td>" not in res.text
        assert f"<td>{two.prime}</td>" in res.text
        assert f"<td>{thr.prime}</td>" in res.text
        assert one.target not in res.text
        assert two.target in res.text
        assert thr.target in res.text
Esempio n. 19
0
    def test_increase_visit_fallback():
        short = Short.create(symbol="abc", target="def")
        assert short.visited == 0

        short.increase_visit()
        assert short.visited == 1

        short.update(visited="wrong", _commit=False)
        assert short.visited == "wrong"

        short.increase_visit()
        assert short.visited == 1
Esempio n. 20
0
def block(page=None, field=None, sort=None):
    query = Short.blocked(BLOCKLIST, field=field, rev=sort == "desc")
    if not query:
        abort(404)

    return render_template(
        "plus/show.html",
        title="Blocked",
        elements=query.paginate(
            page=page, per_page=current_app.config["PAGINATION"]
        ),
    )
Esempio n. 21
0
    def test_len_symbol():
        assert Short.len_symbol(1) == 1

        for sym in SYM_POOL:
            Short.create(symbol=sym, target=sym, _commit=False)

        assert Short.len_symbol(0) == 2
        assert Short.len_symbol(1) == 2
        assert Short.len_symbol(2) == 2
        assert Short.len_symbol(3) == 3
Esempio n. 22
0
    def test_by_symbol_active():
        act = Short.create(symbol="act", target="act", active=True)
        ina = Short.create(symbol="ina", target="ina", active=False)

        assert Short.by_symbol_active("lol") is None
        assert Short.by_symbol_active("act") == act
        assert Short.by_symbol_active("ina") is None
        assert Short.by_symbol("ina") == ina
Esempio n. 23
0
    def test_by_symbol():
        one = Short.create(symbol="one", target="1")
        two = Short.create(symbol="two", target="2")

        assert Short.by_symbol("null") is None
        assert Short.by_symbol("one") == one
        assert Short.by_symbol("two") == two
        assert Short.by_symbol("three") is None
Esempio n. 24
0
def show(page=None, field=None, sort=None):
    term = request.args.get("q", None)
    form = ShortFindForm(term=term)
    if form.validate_on_submit():
        target = form.action()
        if target:
            return redirect(target)

    query = (
        Short.searched(term, field=field, rev=sort == "desc")
        if term is not None
        else Short.ordered(field=field, rev=sort == "desc")
    )
    if not query:
        abort(404)

    return render_template(
        "plus/show.html",
        title="Show",
        form=form,
        elements=query.paginate(
            page=page, per_page=current_app.config["PAGINATION"]
        ),
    )
Esempio n. 25
0
def short(symb):
    if is_botagent(request.user_agent):
        abort(403)

    item = Short.by_symbol_active(symb)
    if not item:
        abort(404)

    item.increase_visit()
    resp = make_response(
        render_template(
            "short.html",
            title=item.symbol,
            short=item,
        ))

    resp.headers.add("X-Robots-Tag", "noindex, nofollow")
    return resp
Esempio n. 26
0
 def test_generate_no_duplicate_on_multi():
     assert Short.query.count() == 0
     one = Short.generate(target="test", delay=1)
     two = Short.generate(target="test", delay=2)
     thr = Short.generate(target="test", delay=3)
     assert Short.query.count() == 3
     g_thr = Short.generate(target="test", delay=3)
     g_two = Short.generate(target="test", delay=2)
     g_one = Short.generate(target="test", delay=1)
     assert Short.query.count() == 3
     assert g_one.symbol == one.symbol
     assert g_two.symbol == two.symbol
     assert g_thr.symbol == thr.symbol
Esempio n. 27
0
    def test_searched():
        assert Short.query.count() == 0
        one = Short.create(symbol="one", target="_cherry", delay=3)
        two = Short.create(symbol="two", target="_eggplant", delay=2)
        thr = Short.create(symbol="thr", target="_peach", delay=1)
        assert Short.query.count() == 3

        assert Short.searched("one").all() == [one]
        assert Short.searched("two").all() == [two]

        assert Short.searched("err").all() == [one]
        assert Short.searched("egg").all() == [two]

        assert Short.searched("t").all() == [two, thr]
        assert Short.searched("p").all() == [two, thr]
        assert Short.searched("_").all() == [one, two, thr]
        assert Short.searched("_", rev=True).all() == [thr, two, one]

        assert Short.searched("_", field="delay", rev=False).all() == [
            thr,
            two,
            one,
        ]
        assert Short.searched("_", field="delay", rev=True).all() == [
            one,
            two,
            thr,
        ]
Esempio n. 28
0
    def test_ordered():
        assert Short.query.count() == 0
        now = datetime.utcnow()

        one = Short.generate(
            target="one",
            delay=2,
            created=now - timedelta(seconds=1),
        )
        two = Short.generate(
            target="two",
            delay=1,
            created=now - timedelta(seconds=3),
        )
        thr = Short.generate(
            target="thr",
            delay=3,
            created=now - timedelta(seconds=2),
        )
        assert Short.query.count() == 3

        assert Short.ordered("PRIME") is None
        assert Short.ordered("BANANA") is None
        assert Short.ordered(None).all() == [one, two, thr]  # same as prime

        assert Short.ordered("prime").all() == [one, two, thr]
        assert Short.ordered("prime", rev=True).all() == [thr, two, one]

        assert Short.ordered("delay").all() == [two, one, thr]
        assert Short.ordered("delay", rev=True).all() == [thr, one, two]

        assert Short.ordered("target").all() == [one, thr, two]
        assert Short.ordered("target", rev=True).all() == [two, thr, one]

        assert Short.ordered("created").all() == [two, thr, one]
        assert Short.ordered("created", rev=True).all() == [one, thr, two]

        assert Short.ordered("active").all() == [one, two, thr]
        assert Short.ordered("active", rev=True).all() == [thr, two, one]
Esempio n. 29
0
def _sho(target="test"):
    return Short.create(symbol=_sym(), target=target)
Esempio n. 30
0
 def test_increase_visit():
     short = Short.create(symbol="abc", target="def")
     assert short.visited == 0
     for num in range(1, 1 + 23):
         short.increase_visit()
         assert short.visited == num