from django.core.urlresolvers import reverse, reverse_lazy
from test_plus.test import TestCase

from company.factories import CompanyFactory
from company.models import Company
from pola.users.factories import StaffFactory, UserFactory


class PermissionMixin(object):
    def setUp(self):
        super(PermissionMixin, self).setUp()
        self.user = StaffFactory()

    def login(self, username=None):
        self.client.login(username=username or self.user.username, password='pass')

    def test_anonymous_denied(self):
        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 302)


class TemplateUsedMixin(object):
    def test_template_used(self):
        self.login()
        resp = self.client.get(self.url)
        self.assertTemplateUsed(resp, self.template_name)


class InstanceMixin(object):
    def setUp(self):
        super(InstanceMixin, self).setUp()
        self.instance = CompanyFactory()

    def test_contains_official_name(self):
        self.login()
        resp = self.client.get(self.url)
        self.assertContains(resp, self.instance.official_name)


class CompanyCreatelViewTestCase(PermissionMixin, TemplateUsedMixin, TestCase):
    url = reverse_lazy('company:create')
    template_name = 'company/company_form.html'


class CompanyCreateFromKRSViewTestCase(PermissionMixin, TemplateUsedMixin, TestCase):
    url = reverse_lazy('company:create_from_krs')
    template_name = 'company/company_from_krs.html'


class CompanyUpdateTestCase(InstanceMixin, PermissionMixin, TemplateUsedMixin, TestCase):
    url = reverse_lazy('company:edit')
    template_name = 'company/company_form.html'

    def setUp(self):
        super(CompanyUpdateTestCase, self).setUp()
        self.url = reverse('company:edit', kwargs={'pk': self.instance.pk})


class CompanyDeleteViewTestCase(InstanceMixin, PermissionMixin, TemplateUsedMixin, TestCase):
    template_name = 'company/company_confirm_delete.html'

    def setUp(self):
        super(CompanyDeleteViewTestCase, self).setUp()
        self.url = reverse('company:delete', kwargs={'pk': self.instance.pk})

    def test_object_delete(self):
        self.login()
        self.client.post(self.url)
        self.assertFalse(Company.objects.filter(pk=self.instance.pk).exists())


class CompanyDetailViewTestCase(InstanceMixin, PermissionMixin, TemplateUsedMixin, TestCase):
    template_name = 'company/company_detail.html'

    def setUp(self):
        super(CompanyDetailViewTestCase, self).setUp()
        self.url = reverse('company:detail', kwargs={'pk': self.instance.pk})


class CompanyListViewTestCase(PermissionMixin, TemplateUsedMixin, TestCase):
    url = reverse_lazy('company:list')
    template_name = 'company/company_filter.html'


@@ -1787,32 +1787,673 @@
 instance.pk%7D)%0A%0A%0A
+class ConcurencyComapnyUpdateTestCase(TestCase):%0A    user_factory = UserFactory%0A%0A    def setUp(self):%0A        super(ConcurencyComapnyUpdateTestCase, self).setUp()%0A        self.instance = CompanyFactory()%0A%0A    def test_restrictions(self):%0A        user1 = self.make_user('u1')%0A        user2 = self.make_user('u2')%0A        url = reverse('company:edit', kwargs=%7B'pk': self.instance.pk%7D)%0A%0A        with self.login(username=user1.username):%0A            response = self.get(url)%0A            self.response_200(response)%0A%0A        with self.login(username=user2.username):%0A            response = self.get(url)%0A            self.response_302(response)%0A%0A%0A
 class CompanyDel

Add concurency lock test