<commit_before># -*- coding: utf-8 -*-
import copy
from datetime import datetime, timedelta
import json
from mock import patch, Mock
from urlparse import urlparse

from django.conf import settings
from django.contrib.auth.models import User, AnonymousUser
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import resolve, reverse
from django.template.loader import render_to_string
from django.test import TestCase, Client, RequestFactory
from django.utils.translation import get_language

from TWLight.applications.factories import ApplicationFactory
from TWLight.applications.models import Application

from . import views
from .authorization import OAuthBackend
from .helpers.wiki_list import WIKIS, LANGUAGE_CODES
from .factories import EditorFactory, UserFactory
from .groups import get_coordinators
from .models import UserProfile, Editor


FAKE_IDENTITY_DATA = {'query': {
    'userinfo': {
        'options': {
            'disablemail': 0
            }
        }
    }
}

FAKE_IDENTITY = {
    'editcount': 5000,
    'registered': '20151106154629', # Well before first commit.
    'blocked': False,
    'iss': urlparse(settings.TWLIGHT_OAUTH_PROVIDER_URL).scheme + urlparse(settings.TWLIGHT_OAUTH_PROVIDER_URL).netloc,
    'sub': 567823,
    'rights': ['deletion', 'spaceflight', 'autoconfirmed'],
    'groups': ['charismatic megafauna'],
    'email': '<EMAIL>',
    'username': 'alice',
}

FAKE_GLOBAL_USERINFO = {
    'home': 'enwiki',
    'id': 567823,
    'registration': '2015-11-06T15:46:29Z', # Well before first commit.
    'name': 'alice',
    'editcount': 5000,
}

class ViewsTestCase(TestCase):

    def setUp(self):
        super(ViewsTestCase, self).setUp()
        self.client = Client()

        # User 1: regular Editor
        self.username1 = 'alice'
        self.user_editor = UserFactory(username=self.username1)
        self.editor1 = EditorFactory(user=self.user_editor)
        self.url1 = reverse('users:editor_detail',
            kwargs={'pk': self.editor1.pk})


        # User 2: regular Editor
        self.username2 = 'bob'
        self.user_editor2 = UserFactory(username=self.username2)
        self.editor2 = EditorFactory(user=self.user_editor2)
        self.url2 = reverse('users:editor_detail',
            kwargs={'pk': self.editor2.pk})


        # User 3: Site administrator
        self.username3 = 'carol'
        self.user_superuser = UserFactory(username=self.username3)
        self.user_superuser.is_superuser = True
        self.user_superuser.save()
        self.editor3 = EditorFactory(user=self.user_superuser)


        # User 4: Coordinator
        self.username4 = 'eve'
        self.user_coordinator = UserFactory(username=self.username4)
        self.editor4 = EditorFactory(user=self.user_coordinator)
        get_coordinators().user_set.add(self.user_coordinator)


    def tearDown(self):
        super(ViewsTestCase, self).tearDown()
        self.user_editor.delete()
        self.editor1.delete()
        self.user_editor2.delete()
        self.editor2.delete()
        self.user_superuser.delete()
        self.editor3.delete()
        self.user_coordinator.delete()
        self.editor4.delete()


    def test_editor_detail_url_resolves(self):
        """
        The EditorDetailView resolves.
        """
        _ = resolve(self.url1)


    def test_anon_user_cannot_see_editor_details(self):
        """
        If an AnonymousUser hits an editor page, they are redirected to login.
        """<commit_msg>Add message patcher to users test suite.<commit_after># -*- coding: utf-8 -*-
import copy
from datetime import datetime, timedelta
import json
from mock import patch, Mock
from urlparse import urlparse

from django.conf import settings
from django.contrib.auth.models import User, AnonymousUser
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import resolve, reverse
from django.template.loader import render_to_string
from django.test import TestCase, Client, RequestFactory
from django.utils.translation import get_language

from TWLight.applications.factories import ApplicationFactory
from TWLight.applications.models import Application

from . import views
from .authorization import OAuthBackend
from .helpers.wiki_list import WIKIS, LANGUAGE_CODES
from .factories import EditorFactory, UserFactory
from .groups import get_coordinators
from .models import UserProfile, Editor


FAKE_IDENTITY_DATA = {'query': {
    'userinfo': {
        'options': {
            'disablemail': 0
            }
        }
    }
}

FAKE_IDENTITY = {
    'editcount': 5000,
    'registered': '20151106154629', # Well before first commit.
    'blocked': False,
    'iss': urlparse(settings.TWLIGHT_OAUTH_PROVIDER_URL).scheme + urlparse(settings.TWLIGHT_OAUTH_PROVIDER_URL).netloc,
    'sub': 567823,
    'rights': ['deletion', 'spaceflight', 'autoconfirmed'],
    'groups': ['charismatic megafauna'],
    'email': '<EMAIL>',
    'username': 'alice',
}

FAKE_GLOBAL_USERINFO = {
    'home': 'enwiki',
    'id': 567823,
    'registration': '2015-11-06T15:46:29Z', # Well before first commit.
    'name': 'alice',
    'editcount': 5000,
}

class ViewsTestCase(TestCase):

    def setUp(self):
        super(ViewsTestCase, self).setUp()
        self.client = Client()

        # User 1: regular Editor
        self.username1 = 'alice'
        self.user_editor = UserFactory(username=self.username1)
        self.editor1 = EditorFactory(user=self.user_editor)
        self.url1 = reverse('users:editor_detail',
            kwargs={'pk': self.editor1.pk})


        # User 2: regular Editor
        self.username2 = 'bob'
        self.user_editor2 = UserFactory(username=self.username2)
        self.editor2 = EditorFactory(user=self.user_editor2)
        self.url2 = reverse('users:editor_detail',
            kwargs={'pk': self.editor2.pk})


        # User 3: Site administrator
        self.username3 = 'carol'
        self.user_superuser = UserFactory(username=self.username3)
        self.user_superuser.is_superuser = True
        self.user_superuser.save()
        self.editor3 = EditorFactory(user=self.user_superuser)


        # User 4: Coordinator
        self.username4 = 'eve'
        self.user_coordinator = UserFactory(username=self.username4)
        self.editor4 = EditorFactory(user=self.user_coordinator)
        get_coordinators().user_set.add(self.user_coordinator)

        # We should mock out any call to messages call in the view, since
        # RequestFactory (unlike Client) doesn't run middleware. If you
        # actually want to test that messages are displayed, use Client(),
        # and stop/restart the patcher.
        self.message_patcher = patch('TWLight.applications.views.messages.add_message')
        self.message_patcher.start()



    def tearDown(self):
        super(ViewsTestCase, self).tearDown()
        self.user_editor.delete()
        self.editor1.delete()
        self.user_editor2.delete()
        self.editor2.delete()
        self.user_superuser.delete()
        self.editor3.delete()
        self.user_coordinator.delete()
        self.editor4.delete()
        self.message_patcher.stop()


    def test_editor_detail_url_resolves(self):
        """
        The EditorDetailView resolves.
        """
        _ = resolve(self.url1)


    def test_anon_user_cannot_see_editor_details(self):
        """
        If an AnonymousUser hits an editor page, they are redirected to login.
        """
{'fraction_non_alphanumeric': 0.10431991440417279, 'fraction_numerical': 0.020863982880834558, 'mean_word_length': 3.145232815964523, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '732233', 'n_tokens_mistral': 2331, 'n_tokens_neox': 2203, 'n_words': 497}