<commit_before>import datetime
from datetime import timedelta
from django.contrib.admin.sites import AdminSite
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.test import RequestFactory, TestCase
from django.test.utils import override_settings
from django.utils.timezone import utc

from guardian.shortcuts import assign_perm

from cases.admin import CaseAdmin
from cases.factories import CaseFactory
from cases.filters import StaffCaseFilter
from cases.models import Case
from letters.factories import LetterFactory
from letters.models import Letter
from users.factories import UserFactory


class CaseQuerySetTestCase(TestCase):
    def setUp(self):
        self.user = UserFactory()

    def test_for_user_cant(self):
        self.assertFalse(Case.objects.for_user(self.user).filter(pk=CaseFactory().pk).exists())

    def test_for_user_can_view_all(self):
        assign_perm('cases.can_view_all', self.user)
        self.assertTrue(Case.objects.for_user(self.user).filter(pk=CaseFactory().pk).exists())

    def test_for_user_can_view_client(self): # perm set by signal
        self.assertTrue(Case.objects.for_user(self.user).filter(pk=CaseFactory(created_by=self.user).pk).exists())

    def test_for_user_can_view_created(self): # perm set by signal
        self.assertTrue(Case.objects.for_user(self.user).filter(pk=CaseFactory(client=self.user).pk).exists())

    def test_with_perm(self):
        CaseFactory.create_batch(size=25)
        with self.assertNumQueries(2):
            qs = Case.objects.with_perm().all()
            for obj in qs:
                list(obj.caseuserobjectpermission_set.all())

    def test_with_record_count(self):
        obj = CaseFactory()
        LetterFactory.create_batch(size=25, case=obj)
        self.assertEqual(Case.objects.filter(pk=obj.pk).with_record_count().get().record_count, 25)

    def test_by_involved_in(self):
        # TODO
        self.assertTrue(True)

    def test_by_msg(self):
        # TODO
        self.assertTrue(True)


class CaseTestCase(TestCase):
    def setUp(self):
        self.object = CaseFactory()

    def test_get_edit_url(self):
        self.assertEqual(CaseFactory(pk=50).get_edit_url(), '/sprawy/sprawa-50/edytuj/')

    @override_settings(PORADNIA_EMAIL_INPUT='<EMAIL>-(?P<pk>\<EMAIL>')
    def test_get_by_email(self):
        with self.assertRaises(Case.DoesNotExist):
            Case.get_by_email('<EMAIL>')
            Case.get_by_email('<EMAIL>')
        obj = CaseFactory(pk=22)
        obj2 = Case.get_by_email('<EMAIL>')
        self.assertEqual(obj, obj2)

    def test_perm_check(self):
        u1 = UserFactory()
        assign_perm('cases.can_view', u1)

        u2 = UserFactory()
        assign_perm('cases.can_view', u2, self.object)

        self.assertTrue(self.object.perm_check(u1, 'can_view'))
        self.assertTrue(self.object.perm_check(u2, 'can_view'))
        with self.assertRaises(PermissionDenied):
            self.object.perm_check(UserFactory(), 'can_view')

    def test_status_update_initial(self):
        self.assertEqual(self.object.status, Case.STATUS.free)

    def test_status_update_still_open(self):
        assign_perm('cases.can_send_to_client', UserFactory(is_staff=False), self.object)
        self.object.status_update()
        self.assertEqual(self.object.status, Case.STATUS.free)

    def test_status_update_assigned(self):
        assign_perm('cases.can_send_to_client', UserFactory(is_staff=True), self.object)
        self.object.status_update()
        self.assertEqual(self.object.status, Case.STATUS.assigned)

    def test_status_update_closed(self):
        self.object.status = Case.STATUS.closed
        self.object.status_update()
        self.assertEqual(self.object.status, Case.STATUS.closed)
        self.object.status_update(reopen=True)
        self.assertEqual(self.object.status, Case.STATUS.free)

    def test_update_counters_last_received_default(self):
        self.object.update_counters()
        self.assertEqual(self.object.last_received, None)

    def _make_letter(self):  # Hack for Travis
        o = LetterFactory(case=self.object, created_by__is_staff=False)
        return Letter.objects.get(pk=o.pk)

    def test_update_counters_last_received_setup(self):
        l = self._make_letter()
        self.object.update_counters()
        self.assertEqual(l.created_on, self.object.last_received)

    def test_update_counters_last_received_update(self):
        self._make_letter()
        self.object.update_counters()
        new = self._make_letter()
        self.object.update_counters()
        self.assertEqual(new.created_on, self.object.last_received)


class CaseDetailViewTestCase(TestCase):
    def setUp(self):
        self.object = CaseFactory()
        self.client.login(username=self.object.created_by.username, password='<PASSWORD>')

    def test_can_view(self):
        self.client.get(self.object.get_absolute_url())

    def test_display_letter(self):
        obj = LetterFactory(case=self.object)
        resp = self.client.get(self.object.get_absolute_url())
        self.assertContains(resp, obj.name)

    def test_hide_internal_letter(self):
        obj = LetterFactory(case=self.object, status='staff')
        obj2 = LetterFactory(case=self.object, status='done')
        self.object.created_by.is_staff = False
        self.object.created_by.save()
        resp = self.client.get(self.object.get_absolute_url())
        self.assertNotContains(resp, obj.name)
        self.assertContains(resp, obj2.name)


class StaffCaseFilterTestCase(TestCase):
    def test_hide_permission(self):
        def get_fields(user):
            return StaffCaseFilter(user=user).form.fields
        self.assertNotIn('permission', get_fields(UserFactory(is_staff=True)))
        self.assertNotIn('permission', get_fields(UserFactory(is_staff=False)))
        user = UserFactory(is_staff=True)
        assign_perm('cases.can_assign', user)
        self.assertIn('permission', get_fields(user))

    def get_permission_filter_qs(self, user, **kwargs):
        admin = UserFactory(is_staff=True, is_superuser=True)
        return StaffCaseFilter(user=admin, data={'permission': user.pk}).qs.filter(**kwargs)

    def test_permission_filter(self):
        obj = CaseFactory()
        self.assertFalse(self.get_permission_filter_qs(user=UserFactory(), pk=obj.pk).exists())
        user = UserFactory(is_staff=True)
        assign_perm('cases.can_view', user, obj)
        self.assertTrue(self.get_permission_filter_qs(user=user, pk=obj.pk).exists())

    def _get_filter(self, user=None, choice='default'):
        return StaffCaseFilter(user=user or UserFactory()).get_order_by(choice)

    def test_order_by(self):
        self.assertEqual(self._get_filter(), ['-deadline', 'status', '-last_received', '-last_send', '-last_action'])
        self.assertEqual(self._get_filter(choice='status'), ['status'])
<commit_msg>Fix tests due switch order rules<commit_after>import datetime
from datetime import timedelta
from django.contrib.admin.sites import AdminSite
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.test import RequestFactory, TestCase
from django.test.utils import override_settings
from django.utils.timezone import utc

from guardian.shortcuts import assign_perm

from cases.admin import CaseAdmin
from cases.factories import CaseFactory
from cases.filters import StaffCaseFilter
from cases.models import Case
from letters.factories import LetterFactory
from letters.models import Letter
from users.factories import UserFactory


class CaseQuerySetTestCase(TestCase):
    def setUp(self):
        self.user = UserFactory()

    def test_for_user_cant(self):
        self.assertFalse(Case.objects.for_user(self.user).filter(pk=CaseFactory().pk).exists())

    def test_for_user_can_view_all(self):
        assign_perm('cases.can_view_all', self.user)
        self.assertTrue(Case.objects.for_user(self.user).filter(pk=CaseFactory().pk).exists())

    def test_for_user_can_view_client(self): # perm set by signal
        self.assertTrue(Case.objects.for_user(self.user).filter(pk=CaseFactory(created_by=self.user).pk).exists())

    def test_for_user_can_view_created(self): # perm set by signal
        self.assertTrue(Case.objects.for_user(self.user).filter(pk=CaseFactory(client=self.user).pk).exists())

    def test_with_perm(self):
        CaseFactory.create_batch(size=25)
        with self.assertNumQueries(2):
            qs = Case.objects.with_perm().all()
            for obj in qs:
                list(obj.caseuserobjectpermission_set.all())

    def test_with_record_count(self):
        obj = CaseFactory()
        LetterFactory.create_batch(size=25, case=obj)
        self.assertEqual(Case.objects.filter(pk=obj.pk).with_record_count().get().record_count, 25)

    def test_by_involved_in(self):
        # TODO
        self.assertTrue(True)

    def test_by_msg(self):
        # TODO
        self.assertTrue(True)


class CaseTestCase(TestCase):
    def setUp(self):
        self.object = CaseFactory()

    def test_get_edit_url(self):
        self.assertEqual(CaseFactory(pk=50).get_edit_url(), '/sprawy/sprawa-50/edytuj/')

    @override_settings(PORADNIA_EMAIL_INPUT='<EMAIL>-(?P<pk>\d+)<EMAIL>')
    def test_get_by_email(self):
        with self.assertRaises(Case.DoesNotExist):
            Case.get_by_email('<EMAIL>')
            Case.get_by_email('<EMAIL>')
        obj = CaseFactory(pk=22)
        obj2 = Case.get_by_email('<EMAIL>')
        self.assertEqual(obj, obj2)

    def test_perm_check(self):
        u1 = UserFactory()
        assign_perm('cases.can_view', u1)

        u2 = UserFactory()
        assign_perm('cases.can_view', u2, self.object)

        self.assertTrue(self.object.perm_check(u1, 'can_view'))
        self.assertTrue(self.object.perm_check(u2, 'can_view'))
        with self.assertRaises(PermissionDenied):
            self.object.perm_check(UserFactory(), 'can_view')

    def test_status_update_initial(self):
        self.assertEqual(self.object.status, Case.STATUS.free)

    def test_status_update_still_open(self):
        assign_perm('cases.can_send_to_client', UserFactory(is_staff=False), self.object)
        self.object.status_update()
        self.assertEqual(self.object.status, Case.STATUS.free)

    def test_status_update_assigned(self):
        assign_perm('cases.can_send_to_client', UserFactory(is_staff=True), self.object)
        self.object.status_update()
        self.assertEqual(self.object.status, Case.STATUS.assigned)

    def test_status_update_closed(self):
        self.object.status = Case.STATUS.closed
        self.object.status_update()
        self.assertEqual(self.object.status, Case.STATUS.closed)
        self.object.status_update(reopen=True)
        self.assertEqual(self.object.status, Case.STATUS.free)

    def test_update_counters_last_received_default(self):
        self.object.update_counters()
        self.assertEqual(self.object.last_received, None)

    def _make_letter(self):  # Hack for Travis
        o = LetterFactory(case=self.object, created_by__is_staff=False)
        return Letter.objects.get(pk=o.pk)

    def test_update_counters_last_received_setup(self):
        l = self._make_letter()
        self.object.update_counters()
        self.assertEqual(l.created_on, self.object.last_received)

    def test_update_counters_last_received_update(self):
        self._make_letter()
        self.object.update_counters()
        new = self._make_letter()
        self.object.update_counters()
        self.assertEqual(new.created_on, self.object.last_received)


class CaseDetailViewTestCase(TestCase):
    def setUp(self):
        self.object = CaseFactory()
        self.client.login(username=self.object.created_by.username, password='<PASSWORD>')

    def test_can_view(self):
        self.client.get(self.object.get_absolute_url())

    def test_display_letter(self):
        obj = LetterFactory(case=self.object)
        resp = self.client.get(self.object.get_absolute_url())
        self.assertContains(resp, obj.name)

    def test_hide_internal_letter(self):
        obj = LetterFactory(case=self.object, status='staff')
        obj2 = LetterFactory(case=self.object, status='done')
        self.object.created_by.is_staff = False
        self.object.created_by.save()
        resp = self.client.get(self.object.get_absolute_url())
        self.assertNotContains(resp, obj.name)
        self.assertContains(resp, obj2.name)


class StaffCaseFilterTestCase(TestCase):
    def test_hide_permission(self):
        def get_fields(user):
            return StaffCaseFilter(user=user).form.fields
        self.assertNotIn('permission', get_fields(UserFactory(is_staff=True)))
        self.assertNotIn('permission', get_fields(UserFactory(is_staff=False)))
        user = UserFactory(is_staff=True)
        assign_perm('cases.can_assign', user)
        self.assertIn('permission', get_fields(user))

    def get_permission_filter_qs(self, user, **kwargs):
        admin = UserFactory(is_staff=True, is_superuser=True)
        return StaffCaseFilter(user=admin, data={'permission': user.pk}).qs.filter(**kwargs)

    def test_permission_filter(self):
        obj = CaseFactory()
        self.assertFalse(self.get_permission_filter_qs(user=UserFactory(), pk=obj.pk).exists())
        user = UserFactory(is_staff=True)
        assign_perm('cases.can_view', user, obj)
        self.assertTrue(self.get_permission_filter_qs(user=user, pk=obj.pk).exists())

    def _get_filter(self, user=None, choice='default'):
        return StaffCaseFilter(user=user or UserFactory()).get_order_by(choice)

    def test_order_by(self):
        self.assertEqual(self._get_filter(), ['-last_action', ])
        self.assertEqual(self._get_filter(choice='status'), ['status'])

{'fraction_non_alphanumeric': 0.11505507955936352, 'fraction_numerical': 0.003311973504211966, 'mean_word_length': 3.7244897959183674, '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': '3184057', 'n_tokens_mistral': 4321, 'n_tokens_neox': 4220, 'n_words': 637}