<commit_before>from django.contrib.sites.shortcuts import get_current_site
from django.test import TestCase, Client
from mock import Mock, patch
from social_core.exceptions import AuthForbidden

import factories
from tola import auth_pipeline
from workflow.models import TolaUser

# TODO Extend OAuth tests


class OAuthTest(TestCase):
    """
    Test cases for OAuth Provider interface
    """

    def setUp(self):
        self.tola_user = factories.TolaUser()
        self.org = factories.Organization()
        self.country = factories.Country()
        self.site = factories.TolaSites(site=get_current_site(None),
                                        whitelisted_domains='testenv.com')
        self.app = factories.Application(user=self.tola_user.user)
        self.grant = factories.Grant(application=self.app,
                                     user=self.tola_user.user)

    def test_authorization(self):
        """
        Tests if the simple search responds
        :return:
        """
        self.tola_user.user.set_password('<PASSWORD>')
        self.tola_user.user.save()

        c = Client(HTTP_USER_AGENT='Test/1.0')
        c.login(username=self.tola_user.user.username, password='<PASSWORD>')

        # Get Authorization token
        authorize_url = '/oauth/authorize?state=random_state_string' \
                        '&client_id={}&response_type=code'.format(
                            self.app.client_id)

        response = c.get(authorize_url, follow=True)
        self.assertContains(
            response, "value=\"CXGVOGFnTAt5cQW6m5AxbGrRq1lzKNSrou31dWm9\"")
        self.assertEqual(response.status_code, 200)

    @patch('tola.auth_pipeline.register_user')
    def test_user_to_tola(self, mock_register_user):
        mock_register_user.return_value = Mock()

        # TolaUser will be created with default Org
        response = {
            'displayName': 'Foo Bar',
            'emails': [{
                'type': 'account',
                'value': 'foo@<EMAIL>'
            }]
        }
        user = factories.User(first_name='John', last_name='Lennon',
                              is_superuser=True, is_staff=True)

        auth_pipeline.user_to_tola(None, user, response)
        tola_user = TolaUser.objects.get(user=user)

        self.assertEqual(tola_user.name, response.get('displayName'))
        self.assertEqual(tola_user.organization.name, self.org.name)
        self.assertEqual(tola_user.country.country, self.country.country)

        # TolaUser will be retrieved and Org won't be the default anymore
        new_org = factories.Organization(name='New Organization')
        tola_user.organization = new_org
        tola_user.save()
        auth_pipeline.user_to_tola(None, user, response)
        tola_user = TolaUser.objects.get(user=user)

        self.assertEqual(tola_user.organization.name, new_org.name)

    def test_auth_allowed_in_whitelist(self):
        # Fake backend class for the test
        class BackendTest(object):
            def __init__(self):
                self.WHITELISTED_EMAILS = []
                self.WHITELISTED_DOMAINS = []

            def setting(self, name, default=None):
                return self.__dict__.get(name, default)

        backend = BackendTest()
        details = {'email': self.tola_user.user.email}
        auth_pipeline.auth_allowed(backend, details, None)

    def test_auth_allowed_not_in_whitelist(self):
        # Fake backend class for the test
        class BackendTest(object):
            def __init__(self):
                self.WHITELISTED_EMAILS = []
                self.WHITELISTED_DOMAINS = []

            def setting(self, name, default=None):
                return self.__dict__.get(name, default)

        backend = BackendTest()
        details = {'email': self.tola_user.user.email}
        self.site.whitelisted_domains = 'anotherdomain.com'
        self.site.save()
        response = auth_pipeline.auth_allowed(backend, details, None)
        template_content = response.content
        self.assertIn("Your organization doesn't appear to have permissions "
                      "to access the system.", template_content)
        self.assertIn("Please check with your organization to have access.",
                      template_content)

    def test_auth_allowed_no_whitelist(self):
        # Fake backend class for the test
        class BackendTest(object):
            def __init__(self):
                self.WHITELISTED_EMAILS = []
                self.WHITELISTED_DOMAINS = []

            def setting(self, name, default=None):
                return self.__dict__.get(name, default)

        self.site.whitelisted_domains = None
        self.site.save()

        backend = BackendTest()
        details = {'email': self.tola_user.user.email}
        response = auth_pipeline.auth_allowed(backend, details, None)
        template_content = response.content
        self.assertIn("Your organization doesn't appear to have permissions "
                      "to access the system.", template_content)
        self.assertIn("Please check with your organization to have access.",
                      template_content)
<commit_msg>Remove unused import<commit_after>from django.contrib.sites.shortcuts import get_current_site
from django.test import TestCase, Client
from mock import Mock, patch

import factories
from tola import auth_pipeline
from workflow.models import TolaUser

# TODO Extend OAuth tests


class OAuthTest(TestCase):
    """
    Test cases for OAuth Provider interface
    """

    def setUp(self):
        self.tola_user = factories.TolaUser()
        self.org = factories.Organization()
        self.country = factories.Country()
        self.site = factories.TolaSites(site=get_current_site(None),
                                        whitelisted_domains='testenv.com')
        self.app = factories.Application(user=self.tola_user.user)
        self.grant = factories.Grant(application=self.app,
                                     user=self.tola_user.user)

    def test_authorization(self):
        """
        Tests if the simple search responds
        :return:
        """
        self.tola_user.user.set_password('<PASSWORD>')
        self.tola_user.user.save()

        c = Client(HTTP_USER_AGENT='Test/1.0')
        c.login(username=self.tola_user.user.username, password='<PASSWORD>')

        # Get Authorization token
        authorize_url = '/oauth/authorize?state=random_state_string' \
                        '&client_id={}&response_type=code'.format(
                            self.app.client_id)

        response = c.get(authorize_url, follow=True)
        self.assertContains(
            response, "value=\"CXGVOGFnTAt5cQW6m5AxbGrRq1lzKNSrou31dWm9\"")
        self.assertEqual(response.status_code, 200)

    @patch('tola.auth_pipeline.register_user')
    def test_user_to_tola(self, mock_register_user):
        mock_register_user.return_value = Mock()

        # TolaUser will be created with default Org
        response = {
            'displayName': 'Foo Bar',
            'emails': [{
                'type': 'account',
                'value': '<EMAIL>'
            }]
        }
        user = factories.User(first_name='John', last_name='Lennon',
                              is_superuser=True, is_staff=True)

        auth_pipeline.user_to_tola(None, user, response)
        tola_user = TolaUser.objects.get(user=user)

        self.assertEqual(tola_user.name, response.get('displayName'))
        self.assertEqual(tola_user.organization.name, self.org.name)
        self.assertEqual(tola_user.country.country, self.country.country)

        # TolaUser will be retrieved and Org won't be the default anymore
        new_org = factories.Organization(name='New Organization')
        tola_user.organization = new_org
        tola_user.save()
        auth_pipeline.user_to_tola(None, user, response)
        tola_user = TolaUser.objects.get(user=user)

        self.assertEqual(tola_user.organization.name, new_org.name)

    def test_auth_allowed_in_whitelist(self):
        # Fake backend class for the test
        class BackendTest(object):
            def __init__(self):
                self.WHITELISTED_EMAILS = []
                self.WHITELISTED_DOMAINS = []

            def setting(self, name, default=None):
                return self.__dict__.get(name, default)

        backend = BackendTest()
        details = {'email': self.tola_user.user.email}
        auth_pipeline.auth_allowed(backend, details, None)

    def test_auth_allowed_not_in_whitelist(self):
        # Fake backend class for the test
        class BackendTest(object):
            def __init__(self):
                self.WHITELISTED_EMAILS = []
                self.WHITELISTED_DOMAINS = []

            def setting(self, name, default=None):
                return self.__dict__.get(name, default)

        backend = BackendTest()
        details = {'email': self.tola_user.user.email}
        self.site.whitelisted_domains = 'anotherdomain.com'
        self.site.save()
        response = auth_pipeline.auth_allowed(backend, details, None)
        template_content = response.content
        self.assertIn("Your organization doesn't appear to have permissions "
                      "to access the system.", template_content)
        self.assertIn("Please check with your organization to have access.",
                      template_content)

    def test_auth_allowed_no_whitelist(self):
        # Fake backend class for the test
        class BackendTest(object):
            def __init__(self):
                self.WHITELISTED_EMAILS = []
                self.WHITELISTED_DOMAINS = []

            def setting(self, name, default=None):
                return self.__dict__.get(name, default)

        self.site.whitelisted_domains = None
        self.site.save()

        backend = BackendTest()
        details = {'email': self.tola_user.user.email}
        response = auth_pipeline.auth_allowed(backend, details, None)
        template_content = response.content
        self.assertIn("Your organization doesn't appear to have permissions "
                      "to access the system.", template_content)
        self.assertIn("Please check with your organization to have access.",
                      template_content)

{'fraction_non_alphanumeric': 0.09390125847047434, 'fraction_numerical': 0.002323330106485963, 'mean_word_length': 2.3048624440179144, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1172824', 'n_tokens_mistral': 3010, 'n_tokens_neox': 2878, 'n_words': 576}