# -*- encoding: utf-8 -*-

from django.contrib.auth.models import Permission
from django.utils import translation
from django.utils.translation import ugettext as _

# Workaround https://code.djangoproject.com/ticket/22865
from geotrek.common.models import FileType  # NOQA

from mapentity.tests import MapEntityTest

from geotrek.authent.factories import StructureFactory
from geotrek.authent.tests import AuthentFixturesTest


class TranslationResetMixin(object):
    def setUp(self):
        translation.deactivate()
        super(TranslationResetMixin, self).setUp()


class CommonTest(AuthentFixturesTest, TranslationResetMixin, MapEntityTest):
    api_prefix = '/api/en/'

    def get_bad_data(self):
        return {'topology': 'doh!'}, _(u'Topology is not valid.')

    def test_structure_is_set(self):
        if not hasattr(self.model, 'structure'):
            return
        self.login()
        response = self.client.post(self._get_add_url(), self.get_good_data())
        self.assertEqual(response.status_code, 302)
        obj = self.model.objects.last()
        self.assertEqual(obj.structure, self.user.profile.structure)

    def test_structure_is_not_changed_without_permission(self):
        if not hasattr(self.model, 'structure'):
            return
        self.login()
        structure = StructureFactory()
        self.assertNotEqual(structure, self.user.profile.structure)
        self.assertFalse(self.user.has_perm('authent.can_bypass_structure'))
        obj = self.modelfactory.create(structure=structure)
        data = self.get_good_data()
        data['structure'] = self.user.profile.structure.pk
        result = self.client.post(obj.get_update_url(), data)
        self.assertEqual(result.status_code, 302)
        self.assertEqual(self.model.objects.first().structure, structure)
        self.logout()

    def test_structure_is_changed_with_permission(self):
        if not hasattr(self.model, 'structure'):
            return
        self.login()
        perm = Permission.objects.get(codename='can_bypass_structure')
        self.user.user_permissions.add(perm)
        structure = StructureFactory()
        self.assertNotEqual(structure, self.user.profile.structure)
        obj = self.modelfactory.create(structure=structure)
        data = self.get_good_data()
        data['structure'] = self.user.profile.structure.pk
        result = self.client.post(obj.get_update_url(), data)
        self.assertEqual(result.status_code, 302)
        self.assertEqual(self.model.objects.first().structure, self.user.profile.structure)
        self.logout()

@@ -2565,28 +2565,732 @@
 ture)%0A        self.logout()%0A
+%0A    def test_set_structure_with_permission(self):%0A        if not hasattr(self.model, 'structure'):%0A            return%0A        self.login()%0A        perm = Permission.objects.get(codename='can_bypass_structure')%0A        self.user.user_permissions.add(perm)%0A        structure = StructureFactory()%0A        self.assertNotEqual(structure, self.user.profile.structure)%0A        data = self.get_good_data()%0A        data%5B'structure'%5D = self.user.profile.structure.pk%0A        response = self.client.post(self._get_add_url(), data)%0A        self.assertEqual(response.status_code, 302)%0A        obj = self.model.objects.last()%0A        self.assertEqual(obj.structure, self.user.profile.structure)%0A        self.logout()%0A

Add test set structure with permission