From 0b1641a002162152897dbe1521af95b3d07b96bf Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Thu, 17 Aug 2017 15:10:46 +0200 Subject: [PATCH] Tests and group uniqueness --- communication/tests.py | 8 +++--- event/signals.py | 4 +-- event/tests.py | 61 +++++++++++++++++++++++++++++++++++++----- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/communication/tests.py b/communication/tests.py index 800eac6..0fce8fd 100644 --- a/communication/tests.py +++ b/communication/tests.py @@ -28,10 +28,8 @@ class SubscriptionTest(TestCase): created_by=cls.root, created_at=timezone.now(), description="Ceci est un test", - beginning_date=timezone.now() - + timedelta(days=30), - ending_date=timezone.now() - + timedelta(days=31), + beginning_date=timezone.now() + timedelta(days=30), + ending_date=timezone.now() + timedelta(days=31), ) cls.groupsub = GroupSubscription.objects.create( content_object=cls.event, @@ -55,4 +53,4 @@ class SubscriptionTest(TestCase): def test_all_subs(self): self.assertSetEqual(set(self.event.get_all_subscribers()), - {self.user_true, self.user_group_true}) + {self.user_true, self.user_group_true}) diff --git a/event/signals.py b/event/signals.py index f711f39..edac92b 100644 --- a/event/signals.py +++ b/event/signals.py @@ -12,7 +12,7 @@ def create_groups_for_event(sender, **kwargs): event, created = kwargs["instance"], kwargs["created"] if created: orgas = EventGroup.objects.create( - name="orga", + name="{}_orgas".format(event.slug), event=event ) @@ -22,7 +22,7 @@ def create_groups_for_event(sender, **kwargs): assign_perm(perm.codename, orgas, event) EventGroup.objects.create( - name="participants", + name="{}_participants".format(event.slug), event=event, ) diff --git a/event/tests.py b/event/tests.py index 29c840f..cac64c8 100644 --- a/event/tests.py +++ b/event/tests.py @@ -1,10 +1,13 @@ from django.contrib.auth import get_user_model +from django.contrib.contenttypes.models import ContentType +from django.contrib.auth.models import Permission from django.core.exceptions import ValidationError from django.test import TestCase from datetime import timedelta from django.utils import timezone -from .models import Event, ActivityTemplate, Activity, Place, \ +from .models import Event, EventGroup, ActivityTemplate, Activity, Place, \ ActivityTag +from guardian.shortcuts import assign_perm User = get_user_model() @@ -24,10 +27,8 @@ class ActivityInheritanceTest(TestCase): created_by=cls.erkan, created_at=timezone.now(), description="La nuit c'est lol", - beginning_date=timezone.now() - + timedelta(days=30), - ending_date=timezone.now() - + timedelta(days=31), + beginning_date=timezone.now() + timedelta(days=30), + ending_date=timezone.now() + timedelta(days=31), ) cls.loge = Place.objects.create(name="Loge 45") cls.aqua = Place.objects.create(name="Aquarium") @@ -47,8 +48,7 @@ class ActivityInheritanceTest(TestCase): self.real_act = Activity.objects.create( parent=self.template_act, event=self.event, - beginning=timezone.now() - + timedelta(days=30), + beginning=timezone.now() + timedelta(days=30), end=timezone.now() + timedelta(days=30) + timedelta(hours=2), @@ -155,3 +155,50 @@ class ActivityTagColorTest(TestCase): ) with self.assertRaises(ValidationError): self.tag.full_clean() + + +class EventPermissionTest(TestCase): + @classmethod + def setUpTestData(cls): + cls.user_perm = User.objects.create(username="userperm") + cls.user_noperm = User.objects.create(username="usernoperm") + cls.user_groupperm = User.objects.create(username="usergroupperm") + cls.user_groupnoperm = User.objects.create(username="usergroupnoperm") + cls.root = User.objects.create_superuser( + username="root", + email="toto@toto.io", + password="toto" + ) + cls.event = Event.objects.create( + title="Hackathon", + slug="django", + created_by=cls.root, + description="Le code c'est cool", + beginning_date=timezone.now(), + ending_date=timezone.now() + timedelta(days=1), + ) + + def test_event_groups(self): + groups = EventGroup.objects.filter( + event=self.event + ) + self.assertEqual(groups.count(), 2) + + def test_individual_perms(self): + assign_perm("event_add_place", self.user_perm, self.event) + self.assertTrue(self.user_perm.has_perm("event_add_place", self.event)) + self.assertFalse(self.user_noperm.has_perm("event_add_place", + self.event)) + + def test_group_perms(self): + orgas = EventGroup.objects.get( + name="{}_orgas".format(self.event.slug), + ) + self.user_groupperm.groups.add(orgas) + for perm in Permission.objects.filter( + content_type=ContentType.objects.get_for_model(Event), + codename__contains="event_"): + self.assertTrue(self.user_groupperm.has_perm(perm.codename, + self.event)) + self.assertFalse(self.user_groupnoperm.has_perm(perm.codename, + self.event))