Skip to content

Commit

Permalink
add some sigange tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juggler31 committed Nov 17, 2023
1 parent 7be4121 commit fdf6de1
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 10 deletions.
4 changes: 2 additions & 2 deletions geotrek/signage/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def get_list_filter(self, request):
return ('structure', )


class SignageSimpleFieldAdmin(MergeActionMixin, admin.ModelAdmin):
class SignageConditionAdmin(MergeActionMixin, admin.ModelAdmin):
search_fields = ('label', 'structure__name')
merge_field = "label"

Expand Down Expand Up @@ -171,4 +171,4 @@ def get_list_filter(self, request):
admin.site.register(Direction, DirectionBladeAdmin)
admin.site.register(BladeType, BladeTypeAdmin)
admin.site.register(LinePictogram, LinePictogramAdmin)
admin.site.register(SignageCondition, SignageSimpleFieldAdmin)
admin.site.register(SignageCondition, SignageConditionAdmin)
4 changes: 2 additions & 2 deletions geotrek/signage/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class SignageConditionFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.SignageCondition

label = factory.Sequence(lambda n: "Conditions %s" % n)
label = factory.Sequence(lambda n: "Condition %s" % n)


class SignageFactory(PointTopologyFactory):
Expand Down Expand Up @@ -95,7 +95,7 @@ class BladeConditionFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.BladeCondition

label = factory.Sequence(lambda n: "Conditions %s" % n)
label = factory.Sequence(lambda n: "Condition %s" % n)


class BladeFactory(factory.django.DjangoModelFactory):
Expand Down
105 changes: 103 additions & 2 deletions geotrek/signage/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

from geotrek.authent.tests.base import AuthentFixturesTest
from geotrek.authent.tests.factories import StructureFactory
from geotrek.signage.models import SignageType, Sealing, Color, Direction, BladeType
from geotrek.signage.models import SignageType, Sealing, Color, Direction, BladeType, SignageCondition
from geotrek.signage.tests.factories import (SealingFactory, SignageTypeFactory, BladeColorFactory,
BladeDirectionFactory, BladeTypeFactory)
BladeDirectionFactory, BladeTypeFactory, SignageConditionFactory)
from mapentity.tests.factories import SuperUserFactory, UserFactory


Expand Down Expand Up @@ -220,3 +220,104 @@ def test_signage_type_change_not_same_structure(self):
response = self.client.get(change_url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, '<select name="structure" id="id_structure">')


class SignageConditionAdminTest(TestCase):
@classmethod
def setUpTestData(cls):
structure = StructureFactory(name="First structure")
cls.second_structure = StructureFactory(name="Second structure")

# User without "can_bypass_structure" permission

cls.user_withoutperm = UserFactory.create(is_staff=True)
for perm in Permission.objects.exclude(codename='can_bypass_structure'):
cls.user_withoutperm.user_permissions.add(perm)

p = cls.user_withoutperm.profile
p.structure = structure
p.save()

# User with "can_bypass_structure" permission
cls.user_perm = UserFactory.create(is_staff=True)
for perm in Permission.objects.all():
cls.user_perm.user_permissions.add(perm)
p = cls.user_perm.profile
p.structure = structure
p.save()
cls.signage_condition = SignageConditionFactory.create(structure=structure)
SignageConditionFactory.create(structure=cls.second_structure)

def test_get_queryset_signage_conditions_withpermission(self):
self.client.force_login(self.user_perm)
change_url = reverse('admin:signage_signagecondition_change', args=[self.signage_condition.pk])
response = self.client.get(change_url)

self.assertEqual(response.status_code, 200)
self.assertEqual(SignageCondition.objects.count(), 2)

def test_get_queryset_signage_conditions_withoutpermission(self):
self.client.force_login(self.user_withoutperm)
change_url = reverse('admin:signage_signagecondition_change', args=[self.signage_condition.pk])
response = self.client.get(change_url)

self.assertEqual(response.status_code, 200)
self.assertEqual(SignageCondition.objects.filter(structure=self.second_structure.pk).count(), 1)

def test_formfield_for_foreignkey_signage_conditions_withpermission(self):
self.client.force_login(self.user_perm)
structure = StructureFactory(name="Other structure")
signagecondition = SignageConditionFactory.create(structure=structure)
change_url = reverse('admin:signage_signagecondition_change', args=[signagecondition.pk])
response = self.client.get(change_url)

self.assertEqual(response.status_code, 200)
self.assertContains(response, '<select name="structure" id="id_structure">')
self.assertContains(response, '<input type="text" name="label" value="{}" class="vTextField" maxlength="250" required id="id_label">'.format(signagecondition.label))

def test_formfield_for_foreignkey_signage_conditions_withoutpermission(self):
self.client.force_login(self.user_withoutperm)
structure = StructureFactory(name="Other structure")
signagecondition = SignageConditionFactory.create(structure=structure)
change_url = reverse('admin:signage_signagecondition_change', args=[signagecondition.pk])
response = self.client.get(change_url)

self.assertEqual(response.status_code, 302)

def test_save_model_signage_conditions_withpermission(self):
self.client.force_login(self.user_perm)
change_url = reverse('admin:signage_signagecondition_add')
response = self.client.post(change_url, data={
"structure": StructureFactory(name="Other structure").pk,
"label": "New condition"
})

self.assertEqual(response.status_code, 302)
self.assertEqual(SignageCondition.objects.count(), 3)

def test_save_model_signage_conditions_withoutpermission(self):
self.client.force_login(self.user_withoutperm)
change_url = reverse('admin:signage_signagecondition_add')
response = self.client.post(change_url, data={
"structure": StructureFactory(name="Other structure").pk,
"label": "New condition"
})

self.assertEqual(response.status_code, 302)
self.assertEqual(SignageCondition.objects.count(), 3)

def test_get_list_display_signage_conditions_withpermission(self):
self.client.force_login(self.user_perm)
change_url = reverse('admin:signage_signagecondition_changelist')
response = self.client.get(change_url)

self.assertEqual(response.status_code, 200)
self.assertContains(response, '<a href="/admin/signage/signagecondition/{}/change/">{}</a>'.format(self.signage_condition.pk, self.signage_condition.label))

def test_get_list_display_signage_conditions_withoutpermission(self):
self.client.force_login(self.user_withoutperm)
change_url = reverse('admin:signage_signagecondition_changelist')
response = self.client.get(change_url)

self.assertEqual(response.status_code, 200)
self.assertContains(response, '<a href="/admin/signage/signagecondition/{}/change/">{}</a>'.format(self.signage_condition.pk, self.signage_condition.label))
8 changes: 4 additions & 4 deletions geotrek/signage/tests/test_command_signage.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_load_signage(self):
structure = StructureFactory.create(name='structure')
filename = os.path.join(os.path.dirname(__file__), 'data', 'signage.shp')
call_command('loadsignage', filename, type_default='label', name_default='name',
condition_default='condition', structure_default='structure',
condition_default='conditions', structure_default='structure',
description_default='description', year_default=2010, verbosity=2, stdout=output)
self.assertIn('Signages will be linked to %s' % structure, output.getvalue())
self.assertIn('2 objects created.', output.getvalue())
Expand All @@ -44,7 +44,7 @@ def test_load_signage_multipoints(self):
structure = StructureFactory.create(name='structure')
filename = os.path.join(os.path.dirname(__file__), 'data', 'signage_good_multipoint.geojson')
call_command('loadsignage', filename, type_default='label', name_default='name',
condition_default='condition', structure_default='structure',
condition_default='conditions', structure_default='structure',
description_default='description', year_default=2010, verbosity=2, stdout=output)
self.assertIn('Signages will be linked to %s' % structure, output.getvalue())
self.assertIn('1 objects created.', output.getvalue())
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_load_signage_bad_year(self):
output_file_path,
type_default='label',
name_default='name',
condition_default='condition',
condition_default='conditions',
structure_default='structure',
description_default='description',
year_field='year',
Expand All @@ -110,7 +110,7 @@ def test_load_signage_bad_multipoints_error(self):
filename = os.path.join(os.path.dirname(__file__), 'data', 'signage_bad_multipoint.geojson')
with self.assertRaisesRegex(CommandError, 'One of your geometry is a MultiPoint object with multiple points'):
call_command('loadsignage', filename, type_default='label', name_default='name',
condition_default='condition', structure_default='structure',
condition_default='conditions', structure_default='structure',
description_default='description', year_default=2010, verbosity=2, stdout=output)

def test_load_signage_with_fields(self):
Expand Down

0 comments on commit fdf6de1

Please sign in to comment.