<commit_before># -*- coding: utf-8 -*-
import itsdangerous
import mock
from nose.tools import *  # flake8: noqa
import unittest
import urlparse

from modularodm import Q

from tests.base import ApiTestCase
from tests.factories import AuthUserFactory, UserFactory, ProjectFactory, Auth

from api.base.settings.defaults import API_BASE

from framework.auth.cas import CasResponse
from framework.sessions.model import Session
from website.models import User
from website import settings
from website.oauth.models import ApiOAuth2PersonalToken
from website.util.permissions import CREATOR_PERMISSIONS


class TestUsers(ApiTestCase):

    def setUp(self):
        super(TestUsers, self).setUp()
        self.user_one = AuthUserFactory()
        self.user_two = AuthUserFactory()

    def tearDown(self):
        super(TestUsers, self).tearDown()

    def test_returns_200(self):
        res = self.app.get('/{}users/'.format(API_BASE))
        assert_equal(res.status_code, 200)
        assert_equal(res.content_type, 'application/vnd.api+json')

    def test_find_user_in_users(self):
        url = "/{}users/".format(API_BASE)

        res = self.app.get(url)
        user_son = res.json['data']

        ids = [each['id'] for each in user_son]
        assert_in(self.user_two._id, ids)

    def test_all_users_in_users(self):
        url = "/{}users/".format(API_BASE)

        res = self.app.get(url)
        user_son = res.json['data']

        ids = [each['id'] for each in user_son]
        assert_in(self.user_one._id, ids)
        assert_in(self.user_two._id, ids)

    def test_find_multiple_in_users(self):
        url = "/{}users/?filter[full_name]=fred".format(API_BASE)

        res = self.app.get(url)
        user_json = res.json['data']
        ids = [each['id'] for each in user_json]
        assert_in(self.user_one._id, ids)
        assert_in(self.user_two._id, ids)

    def test_find_single_user_in_users(self):
        url = "/{}users/?filter[full_name]=my".format(API_BASE)
        self.user_one.fullname = 'My Mom'
        self.user_one.save()
        res = self.app.get(url)
        user_json = res.json['data']
        ids = [each['id'] for each in user_json]
        assert_in(self.user_one._id, ids)
        assert_not_in(self.user_two._id, ids)

    def test_find_no_user_in_users(self):
        url = "/{}users/?filter[full_name]=NotMyMom".format(API_BASE)
        res = self.app.get(url)
        user_json = res.json['data']
        ids = [each['id'] for each in user_json]
        assert_not_in(self.user_one._id, ids)
        assert_not_in(self.user_two._id, ids)

    def test_more_than_one_projects_in_common(self):
        project1 = ProjectFactory(creator=self.user_one)
        project1.add_contributor(
            contributor=self.user_two,
            permissions=CREATOR_PERMISSIONS,
            auth=Auth(user=self.user_one)
        )
        project1.save()
        project2 = ProjectFactory(creator=self.user_one)
        project2.add_contributor(
            contributor=self.user_two,
            permissions=CREATOR_PERMISSIONS,
            auth=Auth(user=self.user_one)
        )
        project2.save()
        url = "/{}users/?show_projects_in_common=true".format(API_BASE)
        res = self.app.get(url, auth=self.user_two.auth)
        user_json = res.json['data']
        for user in user_json:
            if user['id'] == self.user_one._id or user['id'] == self.user_two._id:
                meta = user['relationships']['nodes']['links']['related']['meta']
                assert_in('projects_in_common', meta)
                assert_equal(meta['projects_in_common'], 2)

    def test_users_projects_in_common(self):
        self.user_one.fullname = 'hello'
        self.user_one.save()
        url = "/{}users/?show_projects_in_common=true".format(API_BASE)
        res = self.app.get(url, auth=self.user_two.auth)
        user_json = res.json['data']
        for user in user_json:
            meta = user['relationships']['nodes']['links']['related']['meta']
            assert_in('projects_in_common', meta)
            assert_equal(meta['projects_in_common'], 0)

    def test_users_projects_in_common_with_embed_and_right_query(self):
        project = ProjectFactory(creator=self.user_one)
        project.add_contributor(contributor=self.user_two, permissions=CREATOR_PERMISSIONS, auth=Auth(user=self.user_one))
        project.save()
        url = "/{}users/{}/nodes/?embed=contributors&show_projects_in_common=true".format(API_BASE, self.user_two._id)
        res = self.app.get(url, auth=self.user_two.auth)
        user_json = res.json['data'][0]['embeds']['contributors']['data']
        for user in user_json:
            meta = user['embeds']['users']['data']['relationships']['nodes']['links']['related']['meta']
            assert_in('projects_in_common', meta)
            assert_equal(meta['projects_in_common'], 1)

    def test_users_projects_in_common_with_embed_without_right_query(self):
        project = ProjectFactory(creator=self.user_one)
        project.add_contributor(contributor=self.user_two, permissions=CREATOR_PERMISSIONS, auth=Auth(user=self.user_one))
        project.save()<commit_msg>code clean up<commit_after># -*- coding: utf-8 -*-
import itsdangerous
import mock
from nose.tools import *  # flake8: noqa
import unittest
import urlparse

from modularodm import Q

from tests.base import ApiTestCase
from tests.factories import AuthUserFactory, UserFactory, ProjectFactory, Auth

from api.base.settings.defaults import API_BASE

from framework.auth.cas import CasResponse
from framework.sessions.model import Session
from website.models import User
from website import settings
from website.oauth.models import ApiOAuth2PersonalToken
from website.util.permissions import CREATOR_PERMISSIONS


class TestUsers(ApiTestCase):

    def setUp(self):
        super(TestUsers, self).setUp()
        self.user_one = AuthUserFactory()
        self.user_two = AuthUserFactory()

    def tearDown(self):
        super(TestUsers, self).tearDown()

    def test_returns_200(self):
        res = self.app.get('/{}users/'.format(API_BASE))
        assert_equal(res.status_code, 200)
        assert_equal(res.content_type, 'application/vnd.api+json')

    def test_find_user_in_users(self):
        url = "/{}users/".format(API_BASE)

        res = self.app.get(url)
        user_son = res.json['data']

        ids = [each['id'] for each in user_son]
        assert_in(self.user_two._id, ids)

    def test_all_users_in_users(self):
        url = "/{}users/".format(API_BASE)

        res = self.app.get(url)
        user_son = res.json['data']

        ids = [each['id'] for each in user_son]
        assert_in(self.user_one._id, ids)
        assert_in(self.user_two._id, ids)

    def test_find_multiple_in_users(self):
        url = "/{}users/?filter[full_name]=fred".format(API_BASE)

        res = self.app.get(url)
        user_json = res.json['data']
        ids = [each['id'] for each in user_json]
        assert_in(self.user_one._id, ids)
        assert_in(self.user_two._id, ids)

    def test_find_single_user_in_users(self):
        url = "/{}users/?filter[full_name]=my".format(API_BASE)
        self.user_one.fullname = 'My Mom'
        self.user_one.save()
        res = self.app.get(url)
        user_json = res.json['data']
        ids = [each['id'] for each in user_json]
        assert_in(self.user_one._id, ids)
        assert_not_in(self.user_two._id, ids)

    def test_find_no_user_in_users(self):
        url = "/{}users/?filter[full_name]=NotMyMom".format(API_BASE)
        res = self.app.get(url)
        user_json = res.json['data']
        ids = [each['id'] for each in user_json]
        assert_not_in(self.user_one._id, ids)
        assert_not_in(self.user_two._id, ids)

    def test_more_than_one_projects_in_common(self):
        project1 = ProjectFactory(creator=self.user_one)
        project1.add_contributor(
            contributor=self.user_two,
            permissions=CREATOR_PERMISSIONS,
            auth=Auth(user=self.user_one)
        )
        project1.save()
        project2 = ProjectFactory(creator=self.user_one)
        project2.add_contributor(
            contributor=self.user_two,
            permissions=CREATOR_PERMISSIONS,
            auth=Auth(user=self.user_one)
        )
        project2.save()
        url = "/{}users/?show_projects_in_common=true".format(API_BASE)
        res = self.app.get(url, auth=self.user_two.auth)
        user_json = res.json['data']
        for user in user_json:
            if user['id'] == self.user_one._id or user['id'] == self.user_two._id:
                meta = user['relationships']['nodes']['links']['related']['meta']
                assert_in('projects_in_common', meta)
                assert_equal(meta['projects_in_common'], 2)

    def test_users_projects_in_common(self):
        self.user_one.fullname = 'hello'
        self.user_one.save()
        url = "/{}users/?show_projects_in_common=true".format(API_BASE)
        res = self.app.get(url, auth=self.user_two.auth)
        user_json = res.json['data']
        for user in user_json:
            meta = user['relationships']['nodes']['links']['related']['meta']
            assert_in('projects_in_common', meta)
            assert_equal(meta['projects_in_common'], 0)

    def test_users_projects_in_common_with_embed_and_right_query(self):
        project = ProjectFactory(creator=self.user_one)
        project.add_contributor(
            contributor=self.user_two,
            permissions=CREATOR_PERMISSIONS,
            auth=Auth(user=self.user_one)
        )
        project.save()
        url = "/{}users/{}/nodes/?embed=contributors&show_projects_in_common=true".format(API_BASE, self.user_two._id)
        res = self.app.get(url, auth=self.user_two.auth)
        user_json = res.json['data'][0]['embeds']['contributors']['data']
        for user in user_json:
            meta = user['embeds']['users']['data']['relationships']['nodes']['links']['related']['meta']
            assert_in('projects_in_common', meta)
            assert_equal(meta['projects_in_common'], 1)

    def test_users_projects_in_common_with_embed_without_right_query(self):
        project = ProjectFactory(creator=self.user_one)
        project.add_contributor(
            contributor=self.user_two,
            permissions=CREATOR_PERMISSIONS,
            auth=Auth(user=self.user_one)
        )
        project.save()
{'fraction_non_alphanumeric': 0.12497607655502392, 'fraction_numerical': 0.0036363636363636364, 'mean_word_length': 2.9935040122277417, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5816916', 'n_tokens_mistral': 3389, 'n_tokens_neox': 3294, 'n_words': 539}