<commit_before>from mock import patch
from django.test import TestCase, Client, override_settings
from django.contrib.auth.models import User
from django.conf.urls import patterns, url, include
from django.contrib.contenttypes.models import ContentType

from molo.core.tests.base import MoloTestCaseMixin
from molo.core.models import Main
from molo.core.urls import urlpatterns
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtailcore import urls as wagtail_urls
from django.contrib.auth.models import Group, Permission
from wagtail.wagtailcore.models import GroupPagePermission

urlpatterns += patterns(
    '',
    url(
        r'^profiles/login/$',
        'django.contrib.auth.views.login',
        name='auth_login'),
    url(
        r'^profiles/logout/$',
        'django.contrib.auth.views.logout',
        name='auth_logout'),
    url('^', include('django.contrib.auth.urls')),
    url(r'^admin/', include(wagtailadmin_urls)),
    url(r'^profiles/',
        include('molo.profiles.urls',
                namespace='molo.profiles',
                app_name='molo.profiles')),
    url(r'', include(wagtail_urls)),
)


class CASTestCase(TestCase, MoloTestCaseMixin):

    def setUp(self):
        self.mk_main()
        self.mk_main2()
        moderators_group = Group.objects.create(name='Moderators')
        Group.objects.create(name='Wagtail Login Only')
        # Create group permissions
        GroupPagePermission.objects.create(
            group=moderators_group,
            page=self.main,
            permission_type='add',
        )
        GroupPagePermission.objects.create(
            group=moderators_group,
            page=self.main,
            permission_type='edit',
        )
        GroupPagePermission.objects.create(
            group=moderators_group,
            page=self.main,
            permission_type='publish',
        )

        wagtailadmin_content_type, created = ContentType.objects.get_or_create(
            app_label='wagtailadmin',
            model='admin'
        )

        # Create admin permission
        admin_permission, created = Permission.objects.get_or_create(
            content_type=wagtailadmin_content_type,
            codename='access_admin',
            name='Can access Wagtail admin'
        )

        # Assign it to Editors and Moderators groups
        for group in Group.objects.filter(name__in=['Editors', 'Moderators',
                                          'Wagtail Login Only']):
            group.permissions.add(admin_permission)

    def test_login_redirect(self):
        response = self.client.get('/admin/', follow=True)
        self.assertEquals(
            response.request.get('QUERY_STRING'),
            'service=http%3A%2F%2Ftestserver'
            '%2Fadmin%2Flogin%2F%3Fnext%3D%252Fadmin%252F')

    @patch('cas.CASClientV2.verify_ticket')
    def test_succesful_login(self, mock_verify):
        service = ('http%3A%2F%2Ftestserver'
                   '%2Fadmin%2Flogin%2F%3Fnext%3D%252Fadmin%252F')

        mock_verify.return_value = (
            '<EMAIL>',
            {'ticket': 'fake-ticket', 'service': service, 'has_perm': 'True',
             'is_admin': 'True', 'email': '<EMAIL>'},
            None)

        response = self.client.get(
            '/admin/login/',
            {'ticket': 'fake-ticket', 'next': '/admin/'},
            follow=True)
        user = User.objects.get(username='<EMAIL>')
        self.assertContains(response, 'Welcome to the testapp Wagtail CMS')
        self.assertEqual(user.email, '<EMAIL>')

    @patch('cas.CASClientV2.verify_ticket')
    def test_succesful_login_if_user_is_not_admin(self, mock_verify):
        service = ('http%3A%2F%2Ftestserver'
                   '%2Fadmin%2Flogin%2F%3Fnext%3D%252Fadmin%252F')

        mock_verify.return_value = (
            '<EMAIL>',
            {'ticket': 'fake-ticket', 'service': service, 'has_perm': 'True',
             'is_admin': 'False'},
            None)

        response = self.client.get(
            '/admin/login/',
            {'ticket': 'fake-ticket', 'next': '/admin/'},
            follow=True)

        self.assertContains(response, 'Welcome to the testapp Wagtail CMS')

    @patch('cas.CASClientV2.verify_ticket')
    def test_user_only_able_to_login_to_sites_where_it_has_permissions_to(
            self, mock_verify):
        service = ('http%3A%2F%2Ftestserver'
                   '%2Fadmin%2Flogin%2F%3Fnext%3D%252Fadmin%252F')

        mock_verify.return_value = (
            '<EMAIL>',
            {'ticket': 'fake-ticket', 'service': service, 'has_perm': 'True',
             'is_admin': 'False'},
            None)

        response = self.client.get(<commit_msg>move mk_main2 out from setup<commit_after>from mock import patch
from django.test import TestCase, Client, override_settings
from django.contrib.auth.models import User
from django.conf.urls import patterns, url, include
from django.contrib.contenttypes.models import ContentType

from molo.core.tests.base import MoloTestCaseMixin
from molo.core.models import Main
from molo.core.urls import urlpatterns
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtailcore import urls as wagtail_urls
from django.contrib.auth.models import Group, Permission
from wagtail.wagtailcore.models import GroupPagePermission

urlpatterns += patterns(
    '',
    url(
        r'^profiles/login/$',
        'django.contrib.auth.views.login',
        name='auth_login'),
    url(
        r'^profiles/logout/$',
        'django.contrib.auth.views.logout',
        name='auth_logout'),
    url('^', include('django.contrib.auth.urls')),
    url(r'^admin/', include(wagtailadmin_urls)),
    url(r'^profiles/',
        include('molo.profiles.urls',
                namespace='molo.profiles',
                app_name='molo.profiles')),
    url(r'', include(wagtail_urls)),
)


class CASTestCase(TestCase, MoloTestCaseMixin):

    def setUp(self):
        self.mk_main()

        moderators_group = Group.objects.create(name='Moderators')
        Group.objects.create(name='Wagtail Login Only')
        # Create group permissions
        GroupPagePermission.objects.create(
            group=moderators_group,
            page=self.main,
            permission_type='add',
        )
        GroupPagePermission.objects.create(
            group=moderators_group,
            page=self.main,
            permission_type='edit',
        )
        GroupPagePermission.objects.create(
            group=moderators_group,
            page=self.main,
            permission_type='publish',
        )

        wagtailadmin_content_type, created = ContentType.objects.get_or_create(
            app_label='wagtailadmin',
            model='admin'
        )

        # Create admin permission
        admin_permission, created = Permission.objects.get_or_create(
            content_type=wagtailadmin_content_type,
            codename='access_admin',
            name='Can access Wagtail admin'
        )

        # Assign it to Editors and Moderators groups
        for group in Group.objects.filter(name__in=['Editors', 'Moderators',
                                          'Wagtail Login Only']):
            group.permissions.add(admin_permission)

    def test_login_redirect(self):
        response = self.client.get('/admin/', follow=True)
        self.assertEquals(
            response.request.get('QUERY_STRING'),
            'service=http%3A%2F%2Ftestserver'
            '%2Fadmin%2Flogin%2F%3Fnext%3D%252Fadmin%252F')

    @patch('cas.CASClientV2.verify_ticket')
    def test_succesful_login(self, mock_verify):
        service = ('http%3A%2F%2Ftestserver'
                   '%2Fadmin%2Flogin%2F%3Fnext%3D%252Fadmin%252F')

        mock_verify.return_value = (
            '<EMAIL>',
            {'ticket': 'fake-ticket', 'service': service, 'has_perm': 'True',
             'is_admin': 'True', 'email': '<EMAIL>'},
            None)
        response = self.client.get(
            '/admin/login/',
            {'ticket': 'fake-ticket', 'next': '/admin/'},
            follow=True)
        user = User.objects.get(username='<EMAIL>')
        self.assertContains(response, 'Welcome to the testapp Wagtail CMS')
        self.assertEqual(user.email, '<EMAIL>')

    @patch('cas.CASClientV2.verify_ticket')
    def test_succesful_login_if_user_is_not_admin(self, mock_verify):
        service = ('http%3A%2F%2Ftestserver'
                   '%2Fadmin%2Flogin%2F%3Fnext%3D%252Fadmin%252F')

        mock_verify.return_value = (
            '<EMAIL>',
            {'ticket': 'fake-ticket', 'service': service, 'has_perm': 'True',
             'is_admin': 'False'},
            None)

        response = self.client.get(
            '/admin/login/',
            {'ticket': 'fake-ticket', 'next': '/admin/'},
            follow=True)

        self.assertContains(response, 'Welcome to the testapp Wagtail CMS')

    @patch('cas.CASClientV2.verify_ticket')
    def test_user_only_able_to_login_to_sites_where_it_has_permissions_to(
            self, mock_verify):
        self.mk_main2()
        service = ('http%3A%2F%2Ftestserver'
                   '%2Fadmin%2Flogin%2F%3Fnext%3D%252Fadmin%252F')

        mock_verify.return_value = (
            '<EMAIL>',
            {'ticket': 'fake-ticket', 'service': service, 'has_perm': 'True',
             'is_admin': 'False'},
            None)

        response = self.client.get(
{'fraction_non_alphanumeric': 0.1224706006992266, 'fraction_numerical': 0.012819154571458841, 'mean_word_length': 2.4821099225378087, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '1752985', 'n_tokens_mistral': 2962, 'n_tokens_neox': 2868, 'n_words': 481}