Skip to content

Commit

Permalink
add tests for privacy and public user info
Browse files Browse the repository at this point in the history
  • Loading branch information
therealmarv committed Jan 18, 2017
1 parent a3dd59b commit ae3632c
Showing 1 changed file with 198 additions and 2 deletions.
200 changes: 198 additions & 2 deletions test/test_cache/test_cache_users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf8 -*-
# This file is part of PYBOSSA.
#
# Copyright (C) 2015 Scifabric LTD.
# Copyright (C) 2017 Scifabric LTD.
#
# PYBOSSA is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -35,6 +35,14 @@ def test_get_user_summary_nousers(self):
assert user is None, user


def test_public_get_user_summary_nousers(self):
"""Test public CACHE USERS get_user_summary returns None if no user exists with
the name requested"""
user = cached_users.public_get_user_summary('nouser')

assert user is None, user


def test_get_user_summary_user_exists(self):
"""Test CACHE USERS get_user_summary returns a dict with the user data
if the user exists"""
Expand All @@ -46,6 +54,18 @@ def test_get_user_summary_user_exists(self):
assert type(zizou) is dict, type(zizou)
assert zizou != None, zizou

def test_public_get_user_summary_user_exists(self):
"""Test public CACHE USERS get_user_summary returns a dict with the user data
if the user exists"""
UserFactory.create(name='zidane')
UserFactory.create(name='figo')

zizou = cached_users.public_get_user_summary('zidane')

assert type(zizou) is dict, type(zizou)
assert zizou != None, zizou


def test_get_user_summary_returns_fields(self):
"""Test CACHE USERS get_user_summary all the fields in the dict"""
UserFactory.create(name='user')
Expand All @@ -57,6 +77,19 @@ def test_get_user_summary_returns_fields(self):
for field in fields:
assert field in user.keys(), field

def test_public_get_user_summary_returns_fields(self):
"""Test CACHE USERS public_get_user_summary all the fields in the dict"""
UserFactory.create(name='user')
public_fields = ('name', 'info', 'fullname', 'created', 'rank', 'score')
private_fields = ('id', 'api_key', 'twitter_user_id', 'google_user_id',
'facebook_user_id', 'email_addr', 'total')
user = cached_users.public_get_user_summary('user')

for field in public_fields:
assert field in user.keys(), field

for field in private_fields:
assert field not in user.keys(), field

def test_rank_and_score(self):
"""Test CACHE USERS rank_and_score returns the correct rank and score"""
Expand All @@ -77,7 +110,6 @@ def test_rank_and_score(self):
assert last_in_rank['score'] == 1, last_in_rank['score']



def test_projects_contributed_no_contributions(self):
"""Test CACHE USERS projects_contributed returns empty list if the user has
not contributed to any project"""
Expand All @@ -88,6 +120,36 @@ def test_projects_contributed_no_contributions(self):
assert projects_contributed == [], projects_contributed


def test_projects_contributed_no_contributions_cached(self):
"""Test CACHE USERS projects_contributed_cached returns empty list if the user has
not contributed to any project"""
user = UserFactory.create()

projects_contributed = cached_users.projects_contributed_cached(user.id)

assert projects_contributed == [], projects_contributed


def test_public_projects_contributed_no_contributions(self):
"""Test public CACHE USERS projects_contributed returns empty list if the user has
not contributed to any project"""
user = UserFactory.create()

projects_contributed = cached_users.public_projects_contributed(user.id)

assert projects_contributed == [], projects_contributed


def test_public_projects_contributed_no_contributions_cached(self):
"""Test public CACHE USERS projects_contributed_cached returns empty list if the user has
not contributed to any project"""
user = UserFactory.create()

projects_contributed = cached_users.public_projects_contributed_cached(user.id)

assert projects_contributed == [], projects_contributed


def test_projects_contributed_contributions(self):
"""Test CACHE USERS projects_contributed returns a list of projects that has
contributed to"""
Expand All @@ -103,6 +165,62 @@ def test_projects_contributed_contributions(self):
assert projects_contributed[0]['short_name'] == project_contributed.short_name, projects_contributed


def test_projects_contributed_contributions_cached(self):
"""Test CACHE USERS projects_contributed_cached returns a list of projects that has
contributed to"""
user = UserFactory.create()
project_contributed = ProjectFactory.create()
task = TaskFactory.create(project=project_contributed)
TaskRunFactory.create(task=task, user=user)
another_project = ProjectFactory.create()

projects_contributed = cached_users.projects_contributed_cached(user.id)

assert len(projects_contributed) == 1
assert projects_contributed[0]['short_name'] == project_contributed.short_name, projects_contributed

def test_public_projects_contributed_contributions(self):
"""Test CACHE USERS public projects_contributed returns a list of projects that has
contributed to"""
user = UserFactory.create()
project_contributed = ProjectFactory.create()
task = TaskFactory.create(project=project_contributed)
TaskRunFactory.create(task=task, user=user)
another_project = ProjectFactory.create()

projects_contributed = cached_users.public_projects_contributed(user.id)

assert len(projects_contributed) == 1
assert projects_contributed[0]['short_name'] == project_contributed.short_name, projects_contributed

# check privacy
err_msg = 'private information is in public record'
assert 'id' not in projects_contributed[0], err_msg
assert 'owner_id' not in projects_contributed[0], err_msg
assert 'task_presenter' not in projects_contributed[0]['info']


def test_public_projects_contributed_contributions_cached(self):
"""Test CACHE USERS public cached projects_contributed returns a list of projects that has
contributed to"""
user = UserFactory.create()
project_contributed = ProjectFactory.create()
task = TaskFactory.create(project=project_contributed)
TaskRunFactory.create(task=task, user=user)
another_project = ProjectFactory.create()

projects_contributed = cached_users.public_projects_contributed_cached(user.id)

assert len(projects_contributed) == 1
assert projects_contributed[0]['short_name'] == project_contributed.short_name, projects_contributed

# check privacy
err_msg = 'private information is in public record'
assert 'id' not in projects_contributed[0], err_msg
assert 'owner_id' not in projects_contributed[0], err_msg
assert 'task_presenter' not in projects_contributed[0]['info']


def test_projects_contributed_returns_fields(self):
"""Test CACHE USERS projects_contributed returns the info of the projects with
the required fields"""
Expand All @@ -129,6 +247,36 @@ def test_published_projects_no_projects(self):
assert projects_published == [], projects_published


def test_published_projects_no_projects_cached(self):
"""Test CACHE USERS published_projects_cached returns empty list if the user has
not created any project"""
user = UserFactory.create()

projects_published = cached_users.published_projects_cached(user.id)

assert projects_published == [], projects_published


def test_public_published_projects_no_projects(self):
"""Test public CACHE USERS published_projects returns empty list if the user has
not created any project"""
user = UserFactory.create()

projects_published = cached_users.public_published_projects(user.id)

assert projects_published == [], projects_published


def test_public_published_projects_no_projects_cached(self):
"""Test public CACHE USERS published_projects_cached returns empty list if the user has
not created any project"""
user = UserFactory.create()

projects_published = cached_users.public_published_projects_cached(user.id)

assert projects_published == [], projects_published


def test_published_projects_returns_published(self):
"""Test CACHE USERS published_projects returns a list with the projects that
are published by the user"""
Expand All @@ -141,6 +289,18 @@ def test_published_projects_returns_published(self):
assert projects_published[0]['short_name'] == published_project.short_name, projects_published


def test_public_published_projects_returns_published(self):
"""Test public CACHE USERS published_projects returns a list with the projects that
are published by the user"""
user = UserFactory.create()
published_project = ProjectFactory.create(owner=user, published=True)

projects_published = cached_users.public_published_projects(user.id)

assert len(projects_published) == 1, projects_published
assert projects_published[0]['short_name'] == published_project.short_name, projects_published


def test_published_projects_only_returns_published(self):
"""Test CACHE USERS published_projects does not return draft
or another user's projects"""
Expand All @@ -167,6 +327,42 @@ def test_published_projects_returns_fields(self):
assert field in projects_published[0].keys(), field


def test_public_published_projects_returns_fields(self):
"""Test CACHE USERS published_projects returns the info of the projects with
the required fields"""
user = UserFactory.create()
published_project = ProjectFactory.create(owner=user, published=True)
private_fields = ('id', 'owner_id')
public_fields = ('name', 'short_name', 'description',
'overall_progress', 'n_tasks', 'n_volunteers', 'info')

projects_published = cached_users.public_published_projects(user.id)

for field in public_fields:
assert field in projects_published[0].keys(), field

for field in private_fields:
assert field not in projects_published[0].keys(), field


def test_public_published_projects_cached_returns_fields(self):
"""Test CACHE USERS published_projects_cached returns the info of the projects with
the required fields"""
user = UserFactory.create()
published_project = ProjectFactory.create(owner=user, published=True)
private_fields = ('id', 'owner_id')
public_fields = ('name', 'short_name', 'description',
'overall_progress', 'n_tasks', 'n_volunteers', 'info')

projects_published = cached_users.public_published_projects_cached(user.id)

for field in public_fields:
assert field in projects_published[0].keys(), field

for field in private_fields:
assert field not in projects_published[0].keys(), field


def test_draft_projects_no_projects(self):
"""Test CACHE USERS draft_projects returns an empty list if the user has no
draft projects"""
Expand Down

0 comments on commit ae3632c

Please sign in to comment.