Ejemplo n.º 1
0
def test_create_link_with_user_id_unique_constraint(db_session):
    link_one = Link(hashid='hashone', user_id=1, url='http://google.com/test')
    link_two = Link(hashid='hashtwo', user_id=1, url='http://google.com/test')
    db_session.add(link_one)
    db_session.add(link_two)
    with pytest.raises(IntegrityError) as excinfo:
        db_session.flush()
    assert 'violates unique constraint "unique_url_and_user_id"' in str(
        excinfo.value)
Ejemplo n.º 2
0
def test_ban_url_netloc(db_session, link_svc):
    l1 = Link(hashid='aaaa', url='http://google.com/test')
    l2 = Link(hashid='bbbb', url='http://google.com/random')
    l3 = Link(hashid='cccc', url='http://google.com/test?v=1')
    db_session.add(l1)
    db_session.add(l2)
    db_session.add(l3)
    db_session.flush()
    link_svc.ban_url('http://google.com/test', mode='netloc')
    assert l1.is_banned and l2.is_banned and l3.is_banned
Ejemplo n.º 3
0
def test_get_link_by_hashid(db_session, link_svc):
    l1 = Link(hashid='1234', url='http://google.com/test')
    db_session.add(l1)
    db_session.flush()
    l2 = link_svc.get_link_by_hashid('1234')
    assert l2 is not None
    assert l1 == l2
Ejemplo n.º 4
0
 def __init__(self):
     self._link = Link(id=1,
                       url='https://google.com',
                       hashid='1234',
                       meta={
                           "title": "TEST",
                           "screenshot": "screenshots/1234.png"
                       })
     self._created = True
Ejemplo n.º 5
0
def test_get_link_by_url_with_user_id(db_session, link_svc):
    l1 = Link(hashid='1234', user_id=1, url='http://google.com/test')
    db_session.add(l1)
    db_session.flush()
    l2 = link_svc.get_link_by_url('http://google.com/test')
    assert l2 is None
    l3 = link_svc.get_link_by_url('http://google.com/test', user_id=1)
    assert l3 is not None
    assert l1 == l3
Ejemplo n.º 6
0
def test_create_link(db_session):
    link = Link(hashid='test', url='http://google.com/test')
    db_session.add(link)
    assert db_session.query(Link).filter(
        Link.url == 'http://google.com/test').count() == 1
    assert link._hash == sha1('http://google.com/test').hexdigest()
    # Testing the hashes attributes are created
    assert link._hash_path == sha1('google.com/test').hexdigest()
    assert link._hash_netloc == sha1('google.com').hexdigest()
    assert link._hash_netloc == sha1('google.com').hexdigest()
Ejemplo n.º 7
0
def test_add_click(db_session, stat_svc):
    link = Link(url='http://google.com/test', hashid='1234')
    db_session.add(link)
    db_session.flush()
    stat_svc.add_click(link.id)
    assert db_session.query(Stat).count() == 1
    assert db_session.query(ClickCounter).count() == 1
    assert db_session.query(ClickCounter).get(link.id).counter == 1
    stat_svc.add_click(link.id)
    assert db_session.query(ClickCounter).count() == 1
    assert db_session.query(ClickCounter).get(link.id).counter == 2
Ejemplo n.º 8
0
def test_ban_link_api_view(db_session, dummy_request, fake_link_service,
                           fake_task_service, fake_file_service):
    from suma.api.views import LinkView
    l = Link(url="http://google.com", hashid='1234')
    db_session.add(l)
    db_session.flush()
    dummy_request.method = 'POST'
    dummy_request.post = '{"mode": "url"}'
    dummy_request.json_body = {"mode": "url"}
    view = LinkView(LinkResource(l), dummy_request)
    response = view.ban()
    assert response.status_code == 201
Ejemplo n.º 9
0
    def create_link(self, url, user_id=None):
        created = False
        link = self.get_link_by_url(url, user_id)
        if link:
            return link, created

        nextid = self.dbsession.execute(Sequence("link_id_seq"))
        hashid = self.hashid.encode(nextid)
        link = Link(id=nextid, user_id=user_id, url=url, hashid=hashid)
        self.dbsession.begin_nested()
        try:
            self.dbsession.add(link)
            self.dbsession.commit()
            created = True
        except IntegrityError:
            self.dbsession.rollback()
            link = self.get_link_by_url(url, user_id)

        return link, created
Ejemplo n.º 10
0
def link(db_session):
    link = Link(hashid='1234', url='http://google.com')
    db_session.add(link)
    db_session.flush()
    return link
Ejemplo n.º 11
0
def test_get_link_by_id_or_hashid_with_unknown_hashid(db_session, link_svc):
    l1 = Link(hashid='1234', url='http://google.com/test')
    db_session.add(l1)
    db_session.flush()
    l2 = link_svc.get_link_by_id_or_hashid(-1)
    assert l2 is None
Ejemplo n.º 12
0
def link():
    return Link(
        hashid='12345',
        url='http://google.com/test'
    )
Ejemplo n.º 13
0
def test_link_url_not_null_constraint(db_session):
    link = Link(hashid='12345')
    db_session.add(link)
    with pytest.raises(IntegrityError) as excinfo:
        db_session.flush()
    assert 'violates not-null constraint' in str(excinfo.value)
Ejemplo n.º 14
0
def test_link_hashid_not_null_contraint(db_session):
    link = Link(url='http://google.com/test')
    db_session.add(link)
    with pytest.raises(IntegrityError) as excinfo:
        db_session.flush()
    assert 'violates not-null constraint' in str(excinfo.value)
Ejemplo n.º 15
0
def test_create_link_with_user_id(db_session):
    link = Link(hashid='test', user_id=1, url='http://google.com/test')
    db_session.add(link)
    assert db_session.query(Link.url == 'http://google.com/test').count() == 1