<commit_before># -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2014 SF Isle of Man Limited
#
# 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
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# 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/>.

from default import Test, assert_not_raises
from pybossa.auth import require
from nose.tools import assert_raises
from werkzeug.exceptions import Forbidden, Unauthorized
from mock import patch
from test_authorization import mock_current_user
from factories import AppFactory, UserFactory, TaskFactory
from factories import reset_all_pk_sequences



class TestTaskAuthorization(Test):

    mock_anonymous = mock_current_user()
    mock_authenticated = mock_current_user(anonymous=False, admin=False, id=2)
    mock_admin = mock_current_user(anonymous=False, admin=True, id=1)



    @patch('pybossa.auth.current_user', new=mock_anonymous)
    @patch('pybossa.auth.task.current_user', new=mock_anonymous)
    def test_anonymous_user_cannot_create(self):
        """Test anonymous users cannot create tasks"""
        assert_raises(Unauthorized, getattr(require, 'task').create)


    @patch('pybossa.auth.current_user', new=mock_authenticated)
    @patch('pybossa.auth.task.current_user', new=mock_authenticated)
    def test_authenticated_user_cannot_create(self):
        """Test authenticated users can't create tasks"""
        assert_raises(Exception, getattr(require, 'task').create)

    def test_project_owner_can_create(self):
        """Test project owner can create tasks"""
        admin = UserFactory.create()
        user = UserFactory.create()
        app = AppFactory.create(owner=user)
        task = TaskFactory.build(app=app)
        with patch('pybossa.auth.task.current_user', new=user):
            assert_not_raises(Exception, getattr(require, 'task').create, task)

    def test_not_project_owner_cannot_create(self):
        """Test authenticated user cannot create tasks"""
        user = UserFactory.create()
        user2 = UserFactory.create()
        app = AppFactory.create(owner=user)
        task = TaskFactory.build(app=app)
        with patch('pybossa.auth.task.current_user', new=user2):
            assert_raises(Exception, getattr(require, 'task').create, task)

    def test_admin_can_create(self):
        """Test admin user can create tasks"""
        user = UserFactory.create()
        user2 = UserFactory.create()
        app = AppFactory.create(owner=user2)
        task = TaskFactory.build(app=app)
        with patch('pybossa.auth.task.current_user', new=user):
            assert_not_raises(Exception, getattr(require, 'task').create, task)

#
#
#    @patch('pybossa.auth.current_user', new=mock_anonymous)
#    @patch('pybossa.auth.task.current_user', new=mock_anonymous)
#    def test_anonymous_user_can_read_all_projects(self):<commit_msg>Improvements for the tests.<commit_after># -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2014 SF Isle of Man Limited
#
# 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
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# 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/>.

from default import Test, assert_not_raises
from pybossa.auth import require
from nose.tools import assert_raises
from werkzeug.exceptions import Forbidden, Unauthorized
from mock import patch
from test_authorization import mock_current_user
from factories import AppFactory, UserFactory, TaskFactory
from factories import reset_all_pk_sequences



class TestTaskAuthorization(Test):

    mock_anonymous = mock_current_user()
    mock_authenticated = mock_current_user(anonymous=False, admin=False, id=2)
    mock_admin = mock_current_user(anonymous=False, admin=True, id=1)



    @patch('pybossa.auth.current_user', new=mock_anonymous)
    @patch('pybossa.auth.task.current_user', new=mock_anonymous)
    def test_anonymous_user_cannot_create(self):
        """Test anonymous users cannot create tasks"""
        assert_raises(Unauthorized, getattr(require, 'task').create)


    def test_project_owner_can_create(self):
        """Test project owner can create tasks"""
        admin = UserFactory.create()
        user = UserFactory.create()
        app = AppFactory.create(owner=user)
        task = TaskFactory.build(app=app)
        with patch('pybossa.auth.task.current_user', new=user):
            assert_not_raises(Forbidden, getattr(require, 'task').create, task)

    @patch('pybossa.auth.current_user', new=mock_authenticated)
    def test_not_project_owner_cannot_create(self):
        """Test authenticated user cannot create tasks"""
        user = UserFactory.create()
        user2 = UserFactory.create()
        app = AppFactory.create(owner=user)
        task = TaskFactory.build(app=app)
        with patch('pybossa.auth.task.current_user', new=user2):
            assert_raises(Forbidden, getattr(require, 'task').create, task)

    def test_admin_can_create(self):
        """Test admin user can create tasks"""
        user = UserFactory.create()
        user2 = UserFactory.create()
        app = AppFactory.create(owner=user2)
        task = TaskFactory.build(app=app)
        with patch('pybossa.auth.task.current_user', new=user):
            assert_not_raises(Forbidden, getattr(require, 'task').create, task)

#
#
#    @patch('pybossa.auth.current_user', new=mock_anonymous)
#    @patch('pybossa.auth.task.current_user', new=mock_anonymous)
#    def test_anonymous_user_can_read_all_projects(self):
{'fraction_non_alphanumeric': 0.09626992725584275, 'fraction_numerical': 0.0037145952638910386, 'mean_word_length': 4.100236779794791, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7308616', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1866, 'n_words': 531}