Exemple #1
0
def notify_news_admins():
    """Notify news admins."""
    from pybossa.repositories import UserRepository

    user_repo = UserRepository(db)
    admins = user_repo.filter_by(admin=True)

    for admin in admins:
        key = NOTIFY_ADMIN + str(admin.id)
        sentinel.master.set(key, 1)
Exemple #2
0
def setup_repositories(app):
    """Setup repositories."""
    from pybossa.repositories import UserRepository
    from pybossa.repositories import ProjectRepository
    from pybossa.repositories import ProjectStatsRepository
    from pybossa.repositories import AnnouncementRepository
    from pybossa.repositories import BlogRepository
    from pybossa.repositories import TaskRepository
    from pybossa.repositories import AuditlogRepository
    from pybossa.repositories import WebhookRepository
    from pybossa.repositories import ResultRepository
    from pybossa.repositories import HelpingMaterialRepository
    global user_repo
    global project_repo
    global project_stats_repo
    global announcement_repo
    global blog_repo
    global task_repo
    global auditlog_repo
    global webhook_repo
    global result_repo
    global helping_repo
    language = app.config.get('FULLTEXTSEARCH_LANGUAGE')
    user_repo = UserRepository(db)
    project_repo = ProjectRepository(db)
    project_stats_repo = ProjectStatsRepository(db)
    announcement_repo = AnnouncementRepository(db)
    blog_repo = BlogRepository(db)
    task_repo = TaskRepository(db, language)
    auditlog_repo = AuditlogRepository(db)
    webhook_repo = WebhookRepository(db)
    result_repo = ResultRepository(db)
    helping_repo = HelpingMaterialRepository(db)
Exemple #3
0
def setup_repositories():
    """Setup repositories."""
    from pybossa.repositories import UserRepository
    from pybossa.repositories import ProjectRepository
    from pybossa.repositories import BlogRepository
    from pybossa.repositories import TaskRepository
    from pybossa.repositories import AuditlogRepository
    from pybossa.repositories import WebhookRepository
    from pybossa.repositories import ResultRepository
    from pybossa.repositories import UserScoreRepository
    global user_repo
    global project_repo
    global blog_repo
    global task_repo
    global auditlog_repo
    global webhook_repo
    global result_repo
    global user_score_repo
    user_repo = UserRepository(db)
    project_repo = ProjectRepository(db)
    blog_repo = BlogRepository(db)
    task_repo = TaskRepository(db)
    auditlog_repo = AuditlogRepository(db)
    webhook_repo = WebhookRepository(db)
    result_repo = ResultRepository(db)
    user_score_repo = UserScoreRepository(db)
Exemple #4
0
def setup_repositories():
    from pybossa.repositories import UserRepository
    from pybossa.repositories import ProjectRepository
    from pybossa.repositories import BlogRepository
    from pybossa.repositories import TaskRepository
    from pybossa.repositories import AuditlogRepository
    global user_repo
    global project_repo
    global blog_repo
    global task_repo
    global auditlog_repo
    user_repo = UserRepository(db)
    project_repo = ProjectRepository(db)
    blog_repo = BlogRepository(db)
    task_repo = TaskRepository(db)
    auditlog_repo = AuditlogRepository(db)
Exemple #5
0
def setup_repositories(app):
    """Setup repositories."""
    from pybossa.repositories import UserRepository
    from pybossa.repositories import ProjectRepository
    from pybossa.repositories import ProjectStatsRepository
    from pybossa.repositories import AnnouncementRepository
    from pybossa.repositories import BlogRepository
    from pybossa.repositories import TaskRepository
    from pybossa.repositories import AuditlogRepository
    from pybossa.repositories import WebhookRepository
    from pybossa.repositories import ResultRepository
    from pybossa.repositories import HelpingMaterialRepository
    from pybossa.repositories import PerformanceStatsRepository
    global user_repo
    global project_repo
    global project_stats_repo
    global announcement_repo
    global blog_repo
    global task_repo
    global auditlog_repo
    global webhook_repo
    global result_repo
    global helping_repo
    global performance_stats_repo
    language = app.config.get('FULLTEXTSEARCH_LANGUAGE')
    rdancy_upd_exp = app.config.get('REDUNDANCY_UPDATE_EXPIRATION', 30)
    user_repo = UserRepository(db)
    project_repo = ProjectRepository(db)
    project_stats_repo = ProjectStatsRepository(db)
    announcement_repo = AnnouncementRepository(db)
    blog_repo = BlogRepository(db)
    task_repo = TaskRepository(db, language, rdancy_upd_exp)
    auditlog_repo = AuditlogRepository(db)
    webhook_repo = WebhookRepository(db)
    result_repo = ResultRepository(db)
    helping_repo = HelpingMaterialRepository(db)
    performance_stats_repo = PerformanceStatsRepository(db)
Exemple #6
0
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PYBOSSA.  If not, see <http://www.gnu.org/licenses/>.
import json
from default import db, Test, with_context
from collections import namedtuple
from factories import ProjectFactory, TaskFactory, UserFactory, CategoryFactory
from helper import web

from pybossa.repositories import UserRepository
from pybossa.repositories import AuditlogRepository
from mock import patch, MagicMock

auditlog_repo = AuditlogRepository(db)
user_repo = UserRepository(db)

FakeRequest = namedtuple('FakeRequest', ['text', 'status_code', 'headers'])


class TestAuditlogAPI(Test):
    @with_context
    def test_project_create(self):
        """Test Auditlog API project create works."""
        CategoryFactory.create()
        user = UserFactory.create()

        data = {
            'name': 'New Name',
            'short_name': 'new_short_name',
            'description': 'new_description',
 def setUp(self):
     super(TestUserRepository, self).setUp()
     self.user_repo = UserRepository(db)
class TestUserRepository(Test):
    def setUp(self):
        super(TestUserRepository, self).setUp()
        self.user_repo = UserRepository(db)

    @with_context
    def test_get_return_none_if_no_user(self):
        """Test get method returns None if there is no user with the
        specified id"""

        user = self.user_repo.get(200)

        assert user is None, user

    @with_context
    def test_get_returns_user(self):
        """Test get method returns a user if exists"""

        user = UserFactory.create()

        retrieved_user = self.user_repo.get(user.id)

        assert user == retrieved_user, retrieved_user

    @with_context
    def test_get_by_name_return_none_if_no_user(self):
        """Test get_by_name returns None when a user with the specified
        name does not exist"""

        user = self.user_repo.get_by_name('thisuserdoesnotexist')

        assert user is None, user

    @with_context
    def test_get_by_name_returns_the_user(self):
        """Test get_by_name returns a user if exists"""

        user = UserFactory.create()

        retrieved_user = self.user_repo.get_by_name(user.name)

        assert user == retrieved_user, retrieved_user

    @with_context
    def test_get_by(self):
        """Test get_by returns a user with the specified attribute"""

        user = UserFactory.create(name='Jon Snow')

        retrieved_user = self.user_repo.get_by(name=user.name)

        assert user == retrieved_user, retrieved_user

    @with_context
    def test_get_by_returns_none_if_no_user(self):
        """Test get_by returns None if no user matches the query"""

        UserFactory.create(name='Tyrion Lannister')

        user = self.user_repo.get_by(name='no_name')

        assert user is None, user

    @with_context
    def get_all_returns_list_of_all_users(self):
        """Test get_all returns a list of all the existing users"""

        users = UserFactory.create_batch(3)

        retrieved_users = self.user_repo.get_all()

        assert isinstance(retrieved_users, list)
        assert len(retrieved_users) == len(users), retrieved_users
        for user in retrieved_users:
            assert user in users, user

    @with_context
    def test_filter_by_no_matches(self):
        """Test filter_by returns an empty list if no users match the query"""

        UserFactory.create(name='reek', fullname='Theon Greyjoy')

        retrieved_users = self.user_repo.filter_by(name='asha')

        assert isinstance(retrieved_users, list)
        assert len(retrieved_users) == 0, retrieved_users

    @with_context
    def test_filter_by_one_condition(self):
        """Test filter_by returns a list of users that meet the filtering
        condition"""

        UserFactory.create_batch(3, locale='es')
        should_be_missing = UserFactory.create(locale='fr')

        retrieved_users = self.user_repo.filter_by(locale='es')

        assert len(retrieved_users) == 3, retrieved_users
        assert should_be_missing not in retrieved_users, retrieved_users

    @with_context
    def test_filter_by_multiple_conditions(self):
        """Test filter_by supports multiple-condition queries"""

        UserFactory.create_batch(2, locale='es', privacy_mode=True)
        user = UserFactory.create(locale='es', privacy_mode=False)

        retrieved_users = self.user_repo.filter_by(locale='es',
                                                   privacy_mode=False)

        assert len(retrieved_users) == 1, retrieved_users
        assert user in retrieved_users, retrieved_users

    @with_context
    def test_filter_by_limit_offset(self):
        """Test that filter_by supports limit and offset options"""

        UserFactory.create_batch(4)
        all_users = self.user_repo.filter_by()

        first_two = self.user_repo.filter_by(limit=2)
        last_two = self.user_repo.filter_by(limit=2, offset=2)

        assert len(first_two) == 2, first_two
        assert len(last_two) == 2, last_two
        assert first_two == all_users[:2]
        assert last_two == all_users[2:]

    @with_context
    def test_search_by_name_returns_list(self):
        """Test search_by_name returns a list with search results"""

        search = self.user_repo.search_by_name('')

        assert isinstance(search, list), search.__class__

    @with_context
    def test_search_by_name(self):
        """Test search_by_name returns a list with the user if searching by
        either its name or fullname"""

        user = UserFactory.create(name='greenseer', fullname='Jojen Reed')

        search_by_name = self.user_repo.search_by_name('greenseer')
        search_by_fullname = self.user_repo.search_by_name('Jojen Reed')

        assert user in search_by_name, search_by_name
        assert user in search_by_fullname, search_by_fullname

    @with_context
    def test_search_by_name_capital_lower_letters(self):
        """Test search_by_name works the same with capital or lower letters"""

        user_capitals = UserFactory.create(name='JOJEN')
        user_lowers = UserFactory.create(name='meera')

        search_lower = self.user_repo.search_by_name('jojen')
        search_capital = self.user_repo.search_by_name('MEERA')

        assert user_capitals in search_lower, search_lower
        assert user_lowers in search_capital, search_capital

    @with_context
    def test_search_by_name_substrings(self):
        """Test search_by_name works when searching by a substring"""

        user = UserFactory.create(name='Hodor')

        search = self.user_repo.search_by_name('odo')

        assert user in search, search

    @with_context
    def test_search_by_name_empty_string(self):
        """Test search_by_name returns an empty list when searching by '' """

        user = UserFactory.create(name='Brandon')

        search = self.user_repo.search_by_name('')

        assert len(search) == 0, search

    @with_context
    def test_total_users_no_users(self):
        """Test total_users return 0 if there are no users"""

        count = self.user_repo.total_users()

        assert count == 0, count

    @with_context
    def test_total_users_count(self):
        """Test total_users return 1 if there is one user"""

        UserFactory.create()
        count = self.user_repo.total_users()

        assert count == 1, count

    @with_context
    def test_save(self):
        """Test save persist the user"""

        user = UserFactory.build()
        assert self.user_repo.get(user.id) is None

        self.user_repo.save(user)

        assert self.user_repo.get(user.id) == user, "User not saved"

    @with_context
    def test_save_fails_if_integrity_error(self):
        """Test save raises a DBIntegrityError if the instance to be saved lacks
        a required value"""

        user = UserFactory.build(name=None)

        assert_raises(DBIntegrityError, self.user_repo.save, user)

    @with_context
    def test_save_only_saves_users(self):
        """Test save raises a WrongObjectError when an object which is not
        a User instance is saved"""

        bad_object = dict()

        assert_raises(WrongObjectError, self.user_repo.save, bad_object)

    @with_context
    def test_update(self):
        """Test update persists the changes made to the user"""

        user = UserFactory.create(locale='en')
        user.locale = 'it'

        self.user_repo.update(user)
        updated_user = self.user_repo.get(user.id)

        assert updated_user.locale == 'it', updated_user

    @with_context
    def test_update_fails_if_integrity_error(self):
        """Test update raises a DBIntegrityError if the instance to be updated
        lacks a required value"""

        user = UserFactory.create()
        user.name = None

        assert_raises(DBIntegrityError, self.user_repo.update, user)

    @with_context
    def test_update_only_updates_users(self):
        """Test update raises a WrongObjectError when an object which is not
        a User instance is updated"""

        bad_object = dict()

        assert_raises(WrongObjectError, self.user_repo.update, bad_object)
 def setUp(self):
     super(TestUserRepository, self).setUp()
     self.user_repo = UserRepository(db)
class TestUserRepository(Test):

    def setUp(self):
        super(TestUserRepository, self).setUp()
        self.user_repo = UserRepository(db)


    @with_context
    def test_get_return_none_if_no_user(self):
        """Test get method returns None if there is no user with the
        specified id"""

        user = self.user_repo.get(200)

        assert user is None, user


    @with_context
    def test_get_returns_user(self):
        """Test get method returns a user if exists"""

        user = UserFactory.create()

        retrieved_user = self.user_repo.get(user.id)

        assert user == retrieved_user, retrieved_user


    @with_context
    def test_get_by_name_return_none_if_no_user(self):
        """Test get_by_name returns None when a user with the specified
        name does not exist"""

        user = self.user_repo.get_by_name('thisuserdoesnotexist')

        assert user is None, user


    @with_context
    def test_get_by_name_returns_the_user(self):
        """Test get_by_name returns a user if exists"""

        user = UserFactory.create()

        retrieved_user = self.user_repo.get_by_name(user.name)

        assert user == retrieved_user, retrieved_user


    @with_context
    def test_get_by(self):
        """Test get_by returns a user with the specified attribute"""

        user = UserFactory.create(name='Jon Snow')

        retrieved_user = self.user_repo.get_by(name=user.name)

        assert user == retrieved_user, retrieved_user


    @with_context
    def test_get_by_returns_none_if_no_user(self):
        """Test get_by returns None if no user matches the query"""

        UserFactory.create(name='Tyrion Lannister')

        user = self.user_repo.get_by(name='no_name')

        assert user is None, user


    @with_context
    def get_all_returns_list_of_all_users(self):
        """Test get_all returns a list of all the existing users"""

        users = UserFactory.create_batch(3)

        retrieved_users = self.user_repo.get_all()

        assert isinstance(retrieved_users, list)
        assert len(retrieved_users) == len(users), retrieved_users
        for user in retrieved_users:
            assert user in users, user


    @with_context
    def test_filter_by_no_matches(self):
        """Test filter_by returns an empty list if no users match the query"""

        UserFactory.create(name='reek', fullname='Theon Greyjoy')

        retrieved_users = self.user_repo.filter_by(name='asha')

        assert isinstance(retrieved_users, list)
        assert len(retrieved_users) == 0, retrieved_users


    @with_context
    def test_filter_by_one_condition(self):
        """Test filter_by returns a list of users that meet the filtering
        condition"""

        UserFactory.create_batch(3, locale='es')
        should_be_missing = UserFactory.create(locale='fr')

        retrieved_users = self.user_repo.filter_by(locale='es')

        assert len(retrieved_users) == 3, retrieved_users
        assert should_be_missing not in retrieved_users, retrieved_users


    @with_context
    def test_filter_by_multiple_conditions(self):
        """Test filter_by supports multiple-condition queries"""

        UserFactory.create_batch(2, locale='es', privacy_mode=True)
        user = UserFactory.create(locale='es', privacy_mode=False)

        retrieved_users = self.user_repo.filter_by(locale='es',
                                                   privacy_mode=False)

        assert len(retrieved_users) == 1, retrieved_users
        assert user in retrieved_users, retrieved_users


    @with_context
    def test_filter_by_limit_offset(self):
        """Test that filter_by supports limit and offset options"""

        UserFactory.create_batch(4)
        all_users = self.user_repo.filter_by()

        first_two = self.user_repo.filter_by(limit=2)
        last_two = self.user_repo.filter_by(limit=2, offset=2)

        assert len(first_two) == 2, first_two
        assert len(last_two) == 2, last_two
        assert first_two == all_users[:2]
        assert last_two == all_users[2:]


    @with_context
    def test_search_by_name_returns_list(self):
        """Test search_by_name returns a list with search results"""

        search = self.user_repo.search_by_name('')

        assert isinstance(search, list), search.__class__


    @with_context
    def test_search_by_name(self):
        """Test search_by_name returns a list with the user if searching by
        either its name or fullname"""

        user = UserFactory.create(name='greenseer', fullname='Jojen Reed')

        search_by_name = self.user_repo.search_by_name('greenseer')
        search_by_fullname = self.user_repo.search_by_name('Jojen Reed')

        assert user in search_by_name, search_by_name
        assert user in search_by_fullname, search_by_fullname


    @with_context
    def test_search_by_name_capital_lower_letters(self):
        """Test search_by_name works the same with capital or lower letters"""

        user_capitals = UserFactory.create(name='JOJEN')
        user_lowers = UserFactory.create(name='meera')

        search_lower = self.user_repo.search_by_name('jojen')
        search_capital = self.user_repo.search_by_name('MEERA')

        assert user_capitals in search_lower, search_lower
        assert user_lowers in search_capital, search_capital


    @with_context
    def test_search_by_name_substrings(self):
        """Test search_by_name works when searching by a substring"""

        user = UserFactory.create(name='Hodor')

        search = self.user_repo.search_by_name('odo')

        assert user in search, search


    @with_context
    def test_search_by_name_empty_string(self):
        """Test search_by_name returns an empty list when searching by '' """

        user = UserFactory.create(name='Brandon')

        search = self.user_repo.search_by_name('')

        assert len(search) == 0, search


    @with_context
    def test_total_users_no_users(self):
        """Test total_users return 0 if there are no users"""

        count = self.user_repo.total_users()

        assert count == 0, count


    @with_context
    def test_total_users_count(self):
        """Test total_users return 1 if there is one user"""

        UserFactory.create()
        count = self.user_repo.total_users()

        assert count == 1, count


    @with_context
    def test_save(self):
        """Test save persist the user"""

        user = UserFactory.build()
        assert self.user_repo.get(user.id) is None

        self.user_repo.save(user)

        assert self.user_repo.get(user.id) == user, "User not saved"


    @with_context
    def test_save_fails_if_integrity_error(self):
        """Test save raises a DBIntegrityError if the instance to be saved lacks
        a required value"""

        user = UserFactory.build(name=None)

        assert_raises(DBIntegrityError, self.user_repo.save, user)


    @with_context
    def test_save_only_saves_users(self):
        """Test save raises a WrongObjectError when an object which is not
        a User instance is saved"""

        bad_object = dict()

        assert_raises(WrongObjectError, self.user_repo.save, bad_object)


    @with_context
    def test_update(self):
        """Test update persists the changes made to the user"""

        user = UserFactory.create(locale='en')
        user.locale = 'it'

        self.user_repo.update(user)
        updated_user = self.user_repo.get(user.id)

        assert updated_user.locale == 'it', updated_user


    @with_context
    def test_update_fails_if_integrity_error(self):
        """Test update raises a DBIntegrityError if the instance to be updated
        lacks a required value"""

        user = UserFactory.create()
        user.name = None

        assert_raises(DBIntegrityError, self.user_repo.update, user)


    @with_context
    def test_update_only_updates_users(self):
        """Test update raises a WrongObjectError when an object which is not
        a User instance is updated"""

        bad_object = dict()

        assert_raises(WrongObjectError, self.user_repo.update, bad_object)


    @with_context
    def test_get_users_no_args(self):
        """Test get users by id returns empty list
        """
        assert self.user_repo.get_users(None) == []


    @with_context
    def test_get_users(self):

        tyrion = UserFactory.create(name='Tyrion Lannister')
        theon = UserFactory.create(name='reek', fullname='Theon Greyjoy')

        retrieved_users = self.user_repo.get_users([tyrion.id, theon.id])
        assert any(user == tyrion for user in retrieved_users)
        assert any(user == theon for user in retrieved_users)
Exemple #11
0
def make_subadmin_by(**attributes):
    user_repo = UserRepository(db)
    user = user_repo.get_by(**attributes)
    make_subadmin(user)
Exemple #12
0
def make_subadmin(user):
    user_repo = UserRepository(db)
    user.subadmin = True
    user_repo.save(user)