<commit_before>from __future__ import unicode_literals

import mock
import redis

from dash.orgs.models import Org

from temba_client.client import TembaClient

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase

from tracpro.polls.models import Question

from . import factories


class TracProTest(TestCase):
    """Base class for all test cases in TracPro."""

    def setUp(self):
        self.clear_cache()

        # Mock the RapidPro client for all tests at all times
        # so that the tests never reach out to the API server.
        self.mock_temba_client = mock.Mock(spec=TembaClient)
        self.patcher = mock.patch.object(Org, 'get_temba_client')
        self.mock_get_temba_client = self.patcher.start()
        self.mock_get_temba_client.return_value = self.mock_temba_client

        super(TracProTest, self).setUp()

    def tearDown(self):
        super(TracProTest, self).tearDown()
        self.patcher.stop()

    def clear_cache(self):
        # we are extra paranoid here and actually hardcode redis to 'localhost'
        # and '10' Redis 10 is our testing redis db
        r = redis.StrictRedis(host='localhost', db=10)
        r.flushdb()

    def create_admin(self, org, full_name, email):
        user = User.create(
            None, full_name, email, password=<PASSWORD>, change_password=False)
        user.org_admins.add(org)
        return user

    def create_user(self, org, full_name, email, regions):
        return User.create(
            org, full_name, email, password=<PASSWORD>, change_password=False,
            regions=regions)

    def login(self, user):
        result = self.client.login(username=user.username, password=<PASSWORD>)
        self.assertTrue(
            result, "Couldn't login as %(user)s / %(user)s" % dict(user=user.username))

    def switch_region(self, region):
        session = self.client.session
        key = '{org}:region_id'.format(org=region.org.pk)
        session[key] = region.pk
        session.save()

    def url_get(self, subdomain, url, params=None, **extra):
        if subdomain:
            extra.setdefault('HTTP_HOST', "{}.testserver".format(subdomain))
        return self.client.get(url, params, **extra)

    def url_post(self, subdomain, url, data=None, **extra):
        if subdomain:
            extra.setdefault('HTTP_HOST', "{}.testserver".format(subdomain))
        return self.client.post(url, data, **extra)

    def assertRedirects(self, response, url, subdomain=None, **kwargs):
        if subdomain:
            kwargs.setdefault('host', '{}.testserver'.format(subdomain))
        return super(TracProTest, self).assertRedirects(response, url, **kwargs)

    def assertLoginRedirect(self, response, subdomain, next_url):
        url = '{}?next={}'.format(reverse('users.user_login'), next_url)
        self.assertRedirects(response, url, subdomain)


class TracProDataTest(TracProTest):
    """Common data set-up."""

    def setUp(self):
        super(TracProDataTest, self).setUp()

        self.superuser = User.objects.create_superuser(
            username="root", email="<EMAIL>", password="<PASSWORD>")

        # some orgs
        self.unicef = factories.Org(
            name="UNICEF", timezone="Asia/Kabul", subdomain="unicef")
        self.nyaruka = factories.Org(
            name="Nyaruka", timezone="Africa/Kigali", subdomain="nyaruka")

        # some admins for those orgs
        self.admin = self.create_admin(self.unicef, "Richard", "<EMAIL>")
        self.eric = self.create_admin(self.nyaruka, "Eric", "<EMAIL>")
<commit_msg>Simplify override of test client get and post<commit_after>from __future__ import unicode_literals

import mock
import redis

from dash.orgs.models import Org

from temba_client.client import TembaClient

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase

from tracpro.polls.models import Question

from . import factories


class TracProTest(TestCase):
    """Base class for all test cases in TracPro."""

    def setUp(self):
        self.clear_cache()

        # Mock the RapidPro client for all tests at all times
        # so that the tests never reach out to the API server.
        self.mock_temba_client = mock.Mock(spec=TembaClient)
        self.patcher = mock.patch.object(Org, 'get_temba_client')
        self.mock_get_temba_client = self.patcher.start()
        self.mock_get_temba_client.return_value = self.mock_temba_client

        super(TracProTest, self).setUp()

    def tearDown(self):
        super(TracProTest, self).tearDown()
        self.patcher.stop()

    def clear_cache(self):
        # we are extra paranoid here and actually hardcode redis to 'localhost'
        # and '10' Redis 10 is our testing redis db
        r = redis.StrictRedis(host='localhost', db=10)
        r.flushdb()

    def create_admin(self, org, full_name, email):
        user = User.create(
            None, full_name, email, password=<PASSWORD>, change_password=False)
        user.org_admins.add(org)
        return user

    def create_user(self, org, full_name, email, regions):
        return User.create(
            org, full_name, email, password=<PASSWORD>, change_password=False,
            regions=regions)

    def login(self, user):
        result = self.client.login(username=user.username, password=<PASSWORD>)
        self.assertTrue(
            result, "Couldn't login as %(user)s / %(user)s" % dict(user=user.username))

    def switch_region(self, region):
        session = self.client.session
        key = '{org}:region_id'.format(org=region.org.pk)
        session[key] = region.pk
        session.save()

    def url_get(self, subdomain, *args, **kwargs):
        if subdomain:
            kwargs.setdefault('HTTP_HOST', "{}.testserver".format(subdomain))
        return self.client.get(*args, **kwargs)

    def url_post(self, subdomain, *args, **kwargs):
        if subdomain:
            kwargs.setdefault('HTTP_HOST', "{}.testserver".format(subdomain))
        return self.client.post(*args, **kwargs)

    def assertRedirects(self, response, url, subdomain=None, **kwargs):
        if subdomain:
            kwargs.setdefault('host', '{}.testserver'.format(subdomain))
        return super(TracProTest, self).assertRedirects(response, url, **kwargs)

    def assertLoginRedirect(self, response, subdomain, next_url):
        url = '{}?next={}'.format(reverse('users.user_login'), next_url)
        return self.assertRedirects(response, url, subdomain)


class TracProDataTest(TracProTest):
    """Common data set-up."""

    def setUp(self):
        super(TracProDataTest, self).setUp()

        self.superuser = User.objects.create_superuser(
            username="root", email="<EMAIL>", password="<PASSWORD>")

        # some orgs
        self.unicef = factories.Org(
            name="UNICEF", timezone="Asia/Kabul", subdomain="unicef")
        self.nyaruka = factories.Org(
            name="Nyaruka", timezone="Africa/Kigali", subdomain="nyaruka")

        # some admins for those orgs
        self.admin = self.create_admin(self.unicef, "Richard", "<EMAIL>")
        self.eric = self.create_admin(self.nyaruka, "Eric", "<EMAIL>")

{'fraction_non_alphanumeric': 0.1196534176867006, 'fraction_numerical': 0.001650391968092422, 'mean_word_length': 3.078519349411105, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '7422919', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2184, 'n_words': 518}