tests.py 2.59 KB
Newer Older
Sli's avatar
Sli committed
1
from django.test import TestCase
Sli's avatar
Sli committed
2 3 4
from django.core.urlresolvers import reverse
from django.core.management import call_command
from django.conf import settings
Sli's avatar
Sli committed
5

6
from core.models import User, Group
Sli's avatar
Sli committed
7
from election.models import Election
Sli's avatar
Sli committed
8 9


Sli's avatar
Sli committed
10
class MainElection(TestCase):
Sli's avatar
Sli committed
11 12 13
    def setUp(self):
        call_command("populate")

Sli's avatar
Sli committed
14 15
        self.election = Election.objects.all().first()
        self.public_group = Group.objects.get(id=settings.SITH_GROUP_PUBLIC_ID)
Sli's avatar
Sli committed
16 17 18 19 20
        self.subscriber_group = Group.objects.get(name=settings.SITH_MAIN_MEMBERS_GROUP)
        self.ae_board_group = Group.objects.get(name=settings.SITH_MAIN_BOARD_GROUP)
        self.sli = User.objects.get(username="sli")
        self.subscriber = User.objects.get(username="subscriber")
        self.public = User.objects.get(username="public")
Sli's avatar
Sli committed
21 22 23


class ElectionDetailTest(MainElection):
Sli's avatar
Sli committed
24 25 26 27
    def test_permission_denied(self):
        self.election.view_groups.remove(self.public_group)
        self.election.view_groups.add(self.subscriber_group)
        self.election.save()
Sli's avatar
Sli committed
28 29 30 31 32 33 34
        self.client.login(username=self.public.username, password="plop")
        response_get = self.client.get(
            reverse("election:detail", args=str(self.election.id))
        )
        response_post = self.client.get(
            reverse("election:detail", args=str(self.election.id))
        )
Sli's avatar
Sli committed
35 36 37 38 39 40 41
        self.assertTrue(response_get.status_code == 403)
        self.assertTrue(response_post.status_code == 403)
        self.election.view_groups.remove(self.subscriber_group)
        self.election.view_groups.add(self.public_group)
        self.election.save()

    def test_permisson_granted(self):
Sli's avatar
Sli committed
42 43 44 45 46 47 48
        self.client.login(username=self.public.username, password="plop")
        response_get = self.client.get(
            reverse("election:detail", args=str(self.election.id))
        )
        response_post = self.client.post(
            reverse("election:detail", args=str(self.election.id))
        )
Sli's avatar
Sli committed
49 50
        self.assertFalse(response_get.status_code == 403)
        self.assertFalse(response_post.status_code == 403)
Sli's avatar
Sli committed
51
        self.assertTrue("La roue tourne" in str(response_get.content))
Sli's avatar
Sli committed
52 53 54 55


class ElectionUpdateView(MainElection):
    def test_permission_denied(self):
Sli's avatar
Sli committed
56 57 58 59 60 61 62
        self.client.login(username=self.subscriber.username, password="plop")
        response_get = self.client.get(
            reverse("election:update", args=str(self.election.id))
        )
        response_post = self.client.post(
            reverse("election:update", args=str(self.election.id))
        )
Sli's avatar
Sli committed
63 64
        self.assertTrue(response_get.status_code == 403)
        self.assertTrue(response_post.status_code == 403)