<commit_before># -*- coding: utf8 -*-
# This file is part of PYBOSSA.
#
# Copyright (C) 2015 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
# 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/>.
import json
from default import with_context, Test
from nose.tools import assert_raises
from werkzeug.exceptions import MethodNotAllowed
from pybossa.api.user import UserAPI
from test_api import TestAPI
from pybossa.core import db
from mock import patch, MagicMock

from factories import UserFactory


class TestUserAPI(Test):

    @with_context
    def test_user_get(self):
        """Test API User GET"""
        admin, expected_user, someone = UserFactory.create_batch(3, info=dict(extra='foo',
                                                                 badges=[1,2,3]))
        # Test GET all users
        res = self.app.get('/api/user')
        data = json.loads(res.data)
        user = data[0]
        assert len(data) == 3, data
        for datum in data:
            assert [u'locale', u'name'] == datum.keys(), datum.keys()

        # The output should have a mime-type: application/json
        assert res.mimetype == 'application/json', res

        # Test GETting a specific user by ID
        res = self.app.get('/api/user/%s' % expected_user.id)
        data = json.loads(res.data)
        user = data
        assert user['name'] == expected_user.name, (user['name'],
                                                    expected_user.name)
        assert 'info' not in user.keys(), user.keys()

        # Test GETting a specific user by ID as owner
        url = '/api/user/%s?api_key=%s' % (expected_user.id,
                                           expected_user.api_key)
        res = self.app.get(url)
        data = json.loads(res.data)
        user = data
        assert user['name'] == expected_user.name, data
        assert 'info' in user.keys(), user.keys()
        assert user['info']['extra'] == 'foo'
        assert user['info']['badges'] == [1,2,3]

        # Test GETting a specific user by ID as admin
        url = '/api/user/%s?api_key=%s' % (expected_user.id,
                                           admin.api_key)
        res = self.app.get(url)
        data = json.loads(res.data)
        user = data
        assert user['name'] == expected_user.name, data
        assert 'info' in user.keys(), user.keys()
        assert user['info']['extra'] == 'foo'
        assert user['info']['badges'] == [1,2,3]

        # Test GETting a specific user by ID as non owner non admin 
        url = '/api/user/%s?api_key=%s' % (expected_user.id,
                                           someone.api_key)
        res = self.app.get(url)
        data = json.loads(res.data)
        user = data
        assert user['name'] == expected_user.name, data
        assert 'info' not in user.keys(), user.keys()


        # Test a non-existant ID
        res = self.app.get('/api/user/3434209')
        err = json.loads(res.data)
        assert res.status_code == 404, err
        assert err['status'] == 'failed', err
        assert err['target'] == 'user', err
        assert err['exception_cls'] == 'NotFound', err
        assert err['action'] == 'GET', err


    @with_context
    def test_query_user(self):
        """Test API query for user endpoint works"""
        expected_user = UserFactory.create_batch(2)[0]
        # When querying with a valid existing field which is unique
        # It should return one correct result if exists
        res = self.app.get('/api/user?name=%s' % expected_user.name)
        data = json.loads(res.data)
        assert len(data) == 1, data
        assert data[0]['name'] == expected_user.name, data
        # And it should return no results if there are no matches
        res = self.app.get('/api/user?name=Godzilla')
        data = json.loads(res.data)
        assert len(data) == 0, data

        # When querying with a valid existing non-unique field
        res = self.app.get("/api/user?locale=en")
        data = json.loads(res.data)
        # It should return 3 results, as every registered user has locale=en by default
        assert len(data) == 2, data
        # And they should be the correct ones
        assert (data[0]['locale'] == data[1]['locale'] == 'en'
               and data[0] != data[1]), data<commit_msg>More tests.<commit_after># -*- coding: utf8 -*-
# This file is part of PYBOSSA.
#
# Copyright (C) 2015 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
# 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/>.
import json
from default import with_context, Test
from nose.tools import assert_raises
from werkzeug.exceptions import MethodNotAllowed
from pybossa.api.user import UserAPI
from test_api import TestAPI
from pybossa.core import db
from mock import patch, MagicMock

from factories import UserFactory


class TestUserAPI(Test):

    @with_context
    def test_user_get(self):
        """Test API User GET"""
        admin, expected_user, someone = UserFactory.create_batch(3,
                                                                 info=dict(extra='foo',
                                                                 badges=[1,2,3]))
        restricted = UserFactory.create(restrict=True)
        # Test GET all users
        res = self.app.get('/api/user')
        data = json.loads(res.data)
        user = data[0]
        assert len(data) == 3, data
        for datum in data:
            assert [u'locale', u'name'] == datum.keys(), datum.keys()

        # The output should have a mime-type: application/json
        assert res.mimetype == 'application/json', res

        # Test GETting a specific user by ID
        res = self.app.get('/api/user/%s' % expected_user.id)
        data = json.loads(res.data)
        user = data
        assert user['name'] == expected_user.name, (user['name'],
                                                    expected_user.name)
        assert 'info' not in user.keys(), user.keys()

        # Test GETting a specific user by ID as owner
        url = '/api/user/%s?api_key=%s' % (expected_user.id,
                                           expected_user.api_key)
        res = self.app.get(url)
        data = json.loads(res.data)
        user = data
        assert user['name'] == expected_user.name, data
        assert 'info' in user.keys(), user.keys()
        assert user['info']['extra'] == 'foo'
        assert user['info']['badges'] == [1,2,3]

        # Test GETting a specific user by ID as admin
        url = '/api/user/%s?api_key=%s' % (expected_user.id,
                                           admin.api_key)
        res = self.app.get(url)
        data = json.loads(res.data)
        user = data
        assert user['name'] == expected_user.name, data
        assert 'info' in user.keys(), user.keys()
        assert user['info']['extra'] == 'foo'
        assert user['info']['badges'] == [1,2,3]

        # Test GETting a specific user by ID as non owner non admin 
        url = '/api/user/%s?api_key=%s' % (expected_user.id,
                                           someone.api_key)
        res = self.app.get(url)
        data = json.loads(res.data)
        user = data
        assert user['name'] == expected_user.name, data
        assert 'info' not in user.keys(), user.keys()


        # Test a non-existant ID
        res = self.app.get('/api/user/3434209')
        err = json.loads(res.data)
        assert res.status_code == 404, err
        assert err['status'] == 'failed', err
        assert err['target'] == 'user', err
        assert err['exception_cls'] == 'NotFound', err
        assert err['action'] == 'GET', err

        # Test restricted by anon
        url = '/api/user/%s' % restricted.id
        res = self.app.get(url)
        assert res.status_code == 401, res.status_code

        # As user
        res = self.app.get(url + '?api_key=' + someone.api_key)
        assert res.status_code == 403, res.status_code

        # As admin
        res = self.app.get(url + '?api_key=' + admin.api_key)
        assert res.status_code == 403, res.status_code

        # As same user
        res = self.app.get(url + '?api_key=' + restricted.api_key)
        assert res.status_code == 200, res.status_code
        data = json.loads(res.data)
        assert data['id'] == restricted.id



    @with_context
    def test_query_user(self):
        """Test API query for user endpoint works"""
        expected_user = UserFactory.create_batch(2)[0]
        # When querying with a valid existing field which is unique
        # It should return one correct result if exists
        res = self.app.get('/api/user?name=%s' % expected_user.name)
        data = json.loads(res.data)
        assert len(data) == 1, data
        assert data[0]['name'] == expected_user.name, data
        # And it should return no results if there are no matches
        res = self.app.get('/api/user?name=Godzilla')
        data = json.loads(res.data)
        assert len(data) == 0, data

        # When querying with a valid existing non-unique field
        res = self.app.get("/api/user?locale=en")
        data = json.loads(res.data)
        # It should return 3 results, as every registered user has locale=en by default
        assert len(data) == 2, data
        # And they should be the correct ones
        assert (data[0]['locale'] == data[1]['locale'] == 'en'
               and data[0] != data[1]), data
{'fraction_non_alphanumeric': 0.11572926596758817, 'fraction_numerical': 0.008579599618684462, 'mean_word_length': 2.161844484629295, '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': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3373217', 'n_tokens_mistral': 3031, 'n_tokens_neox': 2966, 'n_words': 965}