Exemplo n.º 1
0
    def test_info_public_keys(self):
        """Test DomainObject to_public_json method works."""
        user = User()
        user.name = 'daniel'
        user.info = dict(container='3', avatar='img.png', token='secret')
        user_dict = user.dictize()
        json = user.to_public_json()
        err_msg = "Wrong value"
        assert json['name'] == user.name, err_msg
        err_msg = "Missing fields"
        assert json.keys().sort() == user.public_attributes().sort(), err_msg
        err_msg = "There should be info keys"
        assert json['info']['container'] == '3', err_msg
        assert json['info']['avatar'] == 'img.png', err_msg
        err_msg = "This key should be missing"
        assert json['info'].get('token') is None, err_msg

        json = user.to_public_json(data=user_dict)
        err_msg = "Wrong value"
        assert json['name'] == user.name, err_msg
        err_msg = "Missing fields"
        assert json.keys().sort() == user.public_attributes().sort(), err_msg
        err_msg = "There should be info keys"
        assert json['info']['container'] == '3', err_msg
        assert json['info']['avatar'] == 'img.png', err_msg
        err_msg = "This key should be missing"
        assert json['info'].get('token') is None, err_msg
Exemplo n.º 2
0
def get_users_page(page, per_page=24):
    """Return users with a paginator."""
    offset = (page - 1) * per_page
    sql = text('''SELECT "user".id, "user".name,
               "user".fullname, "user".email_addr,
               "user".created, "user".info, COUNT(task_run.id) AS task_runs
               FROM task_run, "user"
               WHERE "user".id=task_run.user_id GROUP BY "user".id
               ORDER BY "user".created DESC LIMIT :limit OFFSET :offset''')
    results = session.execute(sql, dict(limit=per_page, offset=offset))
    accounts = []

    u = User()

    for row in results:
        user = dict(id=row.id,
                    name=row.name,
                    fullname=row.fullname,
                    email_addr=row.email_addr,
                    created=row.created,
                    task_runs=row.task_runs,
                    info=row.info,
                    registered_ago=pretty_date(row.created))
        tmp = u.to_public_json(data=user)
        accounts.append(tmp)
    return accounts
Exemplo n.º 3
0
    def test_user_public_attributes(self):
        """Test public attributes works."""
        user = User(email_addr="*****@*****.**",
                    name="johndoe",
                    pro=1,
                    fullname="John Doe",
                    locale="en")
        public_attributes = [
            'created', 'name', 'fullname', 'locale', 'info', 'n_answers',
            'registered_ago', 'rank', 'score'
        ]

        user.set_password("juandiso")
        print(sorted(public_attributes))
        print(sorted(user.public_attributes()))
        assert sorted(public_attributes) == sorted(user.public_attributes())
        data = user.to_public_json()
        err_msg = "There are some keys that should not be public"
        assert sorted(list(data.keys())) == sorted(public_attributes), err_msg
        all_attributes = list(user.dictize().keys())
        s = set(public_attributes)
        private_attributes = [x for x in all_attributes if x not in s]
        for attr in private_attributes:
            err_msg = "This attribute should be private %s" % attr
            assert data.get(attr) is None, err_msg
Exemplo n.º 4
0
    def test_to_public_json(self):
        """Test DomainObject to_public_json method works."""
        user = User()
        user.name = 'daniel'
        user_dict = user.dictize()
        json = user.to_public_json()
        err_msg = "Wrong value"
        assert json['name'] == user.name, err_msg
        err_msg = "Missing fields"
        assert json.keys().sort() == user.public_attributes().sort(), err_msg

        json = user.to_public_json(data=user_dict)
        err_msg = "Wrong value"
        assert json['name'] == user.name, err_msg
        err_msg = "Missing fields"
        assert json.keys().sort() == user.public_attributes().sort(), err_msg
Exemplo n.º 5
0
def public_get_user_summary(name):
    """Sanitize user summary for public usage"""
    private_user = get_user_summary(name)
    public_user = None
    if private_user is not None:
        u = User()
        public_user = u.to_public_json(data=private_user)
    return public_user
Exemplo n.º 6
0
def public_get_user_summary(name):
    """Sanitize user summary for public usage"""
    private_user = get_user_summary(name)
    public_user = None
    if private_user is not None:
        u = User()
        public_user = u.to_public_json(data=private_user)
    return public_user
Exemplo n.º 7
0
    def test_info_public_keys(self):
        """Test DomainObject to_public_json method works."""
        user = User()
        user.name = 'daniel'
        user.info = dict(container='3',
                         avatar='img.png',
                         token='secret',
                         badges=['awesome.png', 'incredible.png'],
                         hidden=True)
        user_dict = user.dictize()
        json = user.to_public_json()
        err_msg = "Wrong value"
        assert json['name'] == user.name, err_msg
        err_msg = "Missing fields"
        assert list(
            json.keys()).sort() == user.public_attributes().sort(), err_msg
        err_msg = "There should be info keys"
        assert json['info']['container'] == '3', err_msg
        assert json['info']['avatar'] == 'img.png', err_msg
        err_msg = "This key should be missing"
        assert json['info'].get('token') is None, err_msg

        json = user.to_public_json(data=user_dict)
        err_msg = "Wrong value"
        assert json['name'] == user.name, err_msg
        err_msg = "Missing fields"
        assert list(
            json.keys()).sort() == user.public_attributes().sort(), err_msg
        err_msg = "There should be info keys"
        assert json['info']['container'] == '3', err_msg
        assert json['info']['avatar'] == 'img.png', err_msg
        err_msg = "This key should be missing"
        assert json['info'].get('token') is None, err_msg

        with patch.dict(self.flask_app.config,
                        {'USER_INFO_PUBLIC_FIELDS': ['badges']}):
            json = user.to_public_json()
            assert list(json['info'].keys()).sort() == User().public_info_keys(
            ).sort(), err_msg
            assert 'badges' in list(json['info'].keys())
            assert 'hidden' not in list(json['info'].keys())
Exemplo n.º 8
0
    def test_user_public_info_keys(self):
        """Test public info keys works."""
        user = User(
            email_addr="*****@*****.**",
            name="johndoe",
            fullname="John Doe",
            info=dict(avatar='image.png', container='foldr3', token='security'),
            locale="en")
        public_info_keys = ['avatar', 'container']
        user.set_password("juandiso")
        assert public_info_keys.sort() == user.public_info_keys().sort()

        data = user.to_public_json()
        err_msg = "There are some keys that should not be public"
        assert data.get('info').keys().sort() == public_info_keys.sort(), err_msg
Exemplo n.º 9
0
    def test_user_public_info_keys(self):
        """Test public info keys works."""
        user = User(email_addr="*****@*****.**",
                    name="johndoe",
                    fullname="John Doe",
                    info=dict(avatar='image.png',
                              container='foldr3',
                              token='security'),
                    locale="en")
        public_info_keys = ['avatar', 'container']
        user.set_password("juandiso")
        assert public_info_keys.sort() == user.public_info_keys().sort()

        data = user.to_public_json()
        err_msg = "There are some keys that should not be public"
        assert list(
            data.get('info').keys()).sort() == public_info_keys.sort(), err_msg
Exemplo n.º 10
0
    def test_user_public_attributes(self):
        """Test public attributes works."""
        user = User(
            email_addr="*****@*****.**",
            name="johndoe",
            pro=1,
            fullname="John Doe",
            locale="en")
        public_attributes = ['created', 'name', 'fullname', 'locale', 'info',
                             'task_runs', 'registered_ago', 'rank', 'score']

        user.set_password("juandiso")
        assert public_attributes.sort() == user.public_attributes().sort()
        data = user.to_public_json()
        err_msg = "There are some keys that should not be public"
        assert data.keys().sort() == public_attributes.sort(), err_msg
        all_attributes = user.dictize().keys()
        s = set(public_attributes)
        private_attributes = [x for x in all_attributes if x not in s]
        for attr in private_attributes:
            err_msg = "This attribute should be private %s" % attr
            assert data.get(attr) is None, err_msg
Exemplo n.º 11
0
def get_users_page(page, per_page=24):
    """Return users with a paginator."""
    offset = (page - 1) * per_page
    sql = text('''SELECT "user".id, "user".name,
               "user".fullname, "user".email_addr,
               "user".created, "user".info, COUNT(task_run.id) AS task_runs
               FROM task_run, "user"
               WHERE "user".id=task_run.user_id GROUP BY "user".id
               ORDER BY "user".created DESC LIMIT :limit OFFSET :offset''')
    results = session.execute(sql, dict(limit=per_page, offset=offset))
    accounts = []

    u = User()

    for row in results:
        user = dict(id=row.id, name=row.name, fullname=row.fullname,
                    email_addr=row.email_addr, created=row.created,
                    task_runs=row.task_runs, info=row.info,
                    registered_ago=pretty_date(row.created))
        tmp = u.to_public_json(data=user)
        accounts.append(tmp)
    return accounts
Exemplo n.º 12
0
def get_leaderboard(n, user_id=None):
    """Return the top n users with their rank."""
    sql = text('''
               WITH global_rank AS (
                    WITH scores AS (
                        SELECT user_id, COUNT(*) AS score FROM task_run
                        WHERE user_id IS NOT NULL GROUP BY user_id)
                    SELECT user_id, score, rank() OVER (ORDER BY score desc)
                    FROM scores)
               SELECT rank, id, name, fullname, email_addr, info, created,
               score FROM global_rank
               JOIN public."user" on (user_id=public."user".id) ORDER BY rank
               LIMIT :limit;
               ''')

    results = session.execute(sql, dict(limit=n))

    u = User()

    top_users = []
    user_in_top = False
    for row in results:
        if (row.id == user_id):
            user_in_top = True
        user = dict(rank=row.rank,
                    id=row.id,
                    name=row.name,
                    fullname=row.fullname,
                    email_addr=row.email_addr,
                    info=row.info,
                    created=row.created,
                    score=row.score)
        tmp = u.to_public_json(data=user)
        top_users.append(tmp)
    if (user_id is not None):
        if not user_in_top:
            sql = text('''
                       WITH global_rank AS (
                            WITH scores AS (
                                SELECT user_id, COUNT(*) AS score FROM task_run
                                WHERE user_id IS NOT NULL GROUP BY user_id)
                            SELECT user_id, score, rank() OVER
                                (ORDER BY score desc)
                            FROM scores)
                       SELECT rank, id, name, fullname, email_addr, info, created,
                              score FROM global_rank
                       JOIN public."user" on (user_id=public."user".id)
                       WHERE user_id=:user_id ORDER BY rank;
                       ''')
            user_rank = session.execute(sql, dict(user_id=user_id))
            u = User.query.get(user_id)
            # Load by default user data with no rank
            user = dict(rank=-1,
                        id=u.id,
                        name=u.name,
                        fullname=u.fullname,
                        email_addr=u.email_addr,
                        info=u.info,
                        created=u.created,
                        score=-1)
            user = u.to_public_json(data=user)
            for row in user_rank:  # pragma: no cover
                user = dict(rank=row.rank,
                            id=row.id,
                            name=row.name,
                            fullname=row.fullname,
                            email_addr=row.email_addr,
                            info=row.info,
                            created=row.created,
                            score=row.score)
                user = u.to_public_json(data=user)
            top_users.append(user)

    return top_users