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, EventGroup, ActivityTemplate, Activity, Place, \ ActivityTag from guardian.shortcuts import assign_perm User = get_user_model() class ActivityInheritanceTest(TestCase): @classmethod def setUpTestData(cls): cls.erkan = User.objects.create( username='enarmanli', email='erkan.narmanli@ens.fr', first_name='Erkan', last_name='Narmanli', ) cls.event = Event.objects.create( title='La Nuit 2042', slug='nuit42', 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), ) cls.loge = Place.objects.create(name="Loge 45") cls.aqua = Place.objects.create(name="Aquarium") cls.tag_foo = ActivityTag.objects.create( name="foo", is_public=True, color="#0F0F0F", ) cls.tag_bar = ActivityTag.objects.create( name="bar", is_public=True, color="#0F0F0F", ) def setUp(self): self.template_act = ActivityTemplate.objects.create(event=self.event) self.real_act = Activity.objects.create( parent=self.template_act, event=self.event, beginning=timezone.now() + timedelta(days=30), end=timezone.now() + timedelta(days=30) + timedelta(hours=2), ) def test_inherits_title(self): self.template_act.title = "parent" self.assertEqual(self.real_act.get_herited('title'), "parent") self.real_act.title = "enfant" self.assertEqual(self.real_act.get_herited('title'), "enfant") def test_inherits_description(self): self.template_act.description = "parent" self.assertEqual(self.real_act.get_herited('description'), "parent") self.real_act.description = "enfant" self.assertEqual(self.real_act.get_herited('description'), "enfant") def test_inherits_remarks(self): self.template_act.remarks = "parent" self.assertEqual(self.real_act.get_herited('remarks'), "parent") self.real_act.remarks = "enfant" self.assertEqual(self.real_act.get_herited('remarks'), "enfant") def test_inherits_is_public(self): self.template_act.is_public = True self.assertEqual(self.real_act.get_herited('is_public'), True) self.real_act.is_public = False self.assertEqual(self.real_act.get_herited('is_public'), False) def test_inherits_has_perm(self): self.template_act.has_perm = True self.assertEqual(self.real_act.get_herited('has_perm'), True) self.real_act.has_perm = False self.assertEqual(self.real_act.get_herited('has_perm'), False) def test_inherits_min_perm(self): self.template_act.min_perm = 42 self.assertEqual(self.real_act.get_herited('min_perm'), 42) self.real_act.min_perm = 1 self.assertEqual(self.real_act.get_herited('min_perm'), 1) def test_inherits_max_perm(self): self.template_act.max_perm = 42 self.assertEqual(self.real_act.get_herited('max_perm'), 42) self.real_act.max_perm = 1 self.assertEqual(self.real_act.get_herited('max_perm'), 1) def test_inherits_place(self): self.template_act.places.add(self.loge) self.assertEqual( self.real_act.get_herited('places').get(), self.loge ) self.real_act.places.add(self.aqua) self.assertEqual( self.real_act.get_herited('places').get(), self.aqua ) def test_inherits_tags(self): self.template_act.tags.add(self.tag_foo) self.assertEqual( self.real_act.get_herited('tags').get(), self.tag_foo ) self.real_act.tags.add(self.tag_bar) self.assertEqual( self.real_act.get_herited('tags').get(), self.tag_bar ) class ActivityTagColorTest(TestCase): def test_positive_color_long(self): self.tag = ActivityTag.objects.create( name="bar", is_public=True, color="#0F0F0F", ) self.tag.full_clean() def test_positive_color_small(self): self.tag = ActivityTag.objects.create( name="bar", is_public=True, color="#0F0", ) self.tag.full_clean() def test_negative_color_1(self): self.tag = ActivityTag.objects.create( name="bar", is_public=True, color="#ABCDEG", ) with self.assertRaises(ValidationError): self.tag.full_clean() def test_negative_color_2(self): self.tag = ActivityTag.objects.create( name="bar", is_public=True, color="#ACDE-1", ) 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))