tests.py 6.01 KB
Newer Older
Thom Wiggers's avatar
Thom Wiggers committed
1
from django.contrib.auth import get_user_model
2
from django.core.exceptions import ValidationError
Thom Wiggers's avatar
Thom Wiggers committed
3
4
5
6
from django.db.utils import IntegrityError
from django.test import TestCase
from django.utils import timezone

7
from activemembers.models import Committee, CommitteeMembership, Board
Thom Wiggers's avatar
Thom Wiggers committed
8
9
10
11
12
13
from members.models import Member


class CommitteeMembersTest(TestCase):
    fixtures = ['members.json', 'committees.json']

Thom Wiggers's avatar
Thom Wiggers committed
14
15
16
17
18
19
20
21
22
    @classmethod
    def setUpTestData(cls):
        cls.testcie = Committee.objects.get(pk=1)
        cls.testuser = Member.objects.get(pk=1)
        cls.m = CommitteeMembership.objects.create(
            committee=cls.testcie,
            member=cls.testuser,
            chair=False)

23
    def setUp(self):
Thom Wiggers's avatar
Thom Wiggers committed
24
25
26
        self.testcie.refresh_from_db()
        self.testuser.refresh_from_db()
        self.m.refresh_from_db()
Thom Wiggers's avatar
Thom Wiggers committed
27
28
29

    def test_unique(self):
        with self.assertRaises(IntegrityError):
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
30
31
32
33
            Committee.objects.create(name_nl="testcie1",
                                     name_en="testcie1",
                                     description_nl="desc3",
                                     description_en="desc3",
Thom Wiggers's avatar
Thom Wiggers committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
                                     photo="")

    def test_join(self):
        testuser2 = Member.objects.get(pk=2)
        m = CommitteeMembership(committee=self.testcie,
                                member=testuser2)
        m.full_clean()
        m.save()

    def test_join_unique(self):
        m = CommitteeMembership(committee=self.testcie,
                                member=self.testuser)
        with self.assertRaises(ValidationError):
            m.full_clean()

49
50
51
52
53
54
55
56
57
58
59
60
    def test_join_unique2(self):
        m = CommitteeMembership(committee=self.testcie,
                                member=self.testuser,
                                since=timezone.now().date().replace(
                                     year=2014, month=1))
        with self.assertRaises(ValidationError):
            m.full_clean()

    def test_join_unique_period(self):
        m1 = CommitteeMembership(committee=self.testcie,
                                 member=self.testuser,
                                 since=timezone.now().date().replace(
61
                                     year=2014, month=1, day=1),
62
                                 until=timezone.now().date().replace(
63
                                     year=2014, month=3, day=1))
64
65
66
67
68
        m1.save()

        m2 = CommitteeMembership(committee=self.testcie,
                                 member=self.testuser,
                                 since=timezone.now().date().replace(
69
                                     year=2014, month=1, day=1),
70
                                 until=timezone.now().date().replace(
71
                                     year=2014, month=2, day=1))
72
73
74
        with self.assertRaises(ValidationError):
            m2.full_clean()

Thom Wiggers's avatar
Thom Wiggers committed
75
76
77
    def test_until_date(self):
        m = CommitteeMembership(committee=self.testcie,
                                member=self.testuser,
78
                                until=timezone.now().date().replace(year=2000),
Thom Wiggers's avatar
Thom Wiggers committed
79
80
81
                                chair=False)
        with self.assertRaises(ValidationError):
            m.clean()
82
        m.since = timezone.now().date().replace(year=1900)
Thom Wiggers's avatar
Thom Wiggers committed
83
84
85
86
        m.clean()

    def test_inactive(self):
        self.assertTrue(self.m.is_active)
87
        self.m.until = timezone.now().date().replace(year=1900)
Thom Wiggers's avatar
Thom Wiggers committed
88
89
90
91
92
93
        self.assertFalse(self.m.is_active)


class CommitteeMembersChairTest(TestCase):
    fixtures = ['members.json', 'committees.json']

Thom Wiggers's avatar
Thom Wiggers committed
94
95
96
97
98
99
    @classmethod
    def setUpTestData(cls):
        cls.testcie = Committee.objects.get(pk=1)
        cls.testuser = Member.objects.get(pk=1)
        cls.testuser2 = Member.objects.get(pk=2)

Thom Wiggers's avatar
Thom Wiggers committed
100
    def setUp(self):
101
        self.m1 = CommitteeMembership(committee=self.testcie,
102
                                      since=timezone.now().date().replace(
Thom Wiggers's avatar
Thom Wiggers committed
103
                                          day=1,
104
                                          year=1900),
105
106
107
108
                                      member=self.testuser,
                                      chair=True)
        self.m1.full_clean()
        self.m1.save()
Thom Wiggers's avatar
Thom Wiggers committed
109
110
111

    def test_second_chair_fails(self):
        m = CommitteeMembership(committee=self.testcie,
112
                                member=self.testuser2,
Thom Wiggers's avatar
Thom Wiggers committed
113
114
115
116
117
                                chair=True)
        with self.assertRaises(ValidationError):
            m.full_clean()

    def test_inactive_chair(self):
Thom Wiggers's avatar
Thom Wiggers committed
118
        self.m1.until = timezone.now().date().replace(day=1, year=1900)
Thom Wiggers's avatar
Thom Wiggers committed
119
        self.m1.save()
120

Thom Wiggers's avatar
Thom Wiggers committed
121
        m = CommitteeMembership(committee=self.testcie,
122
                                member=self.testuser2,
Thom Wiggers's avatar
Thom Wiggers committed
123
124
125
                                chair=True)
        m.full_clean()

126
127
128
129
    def test_clean_self_chair(self):
        self.m1.chair = True
        self.m1.full_clean()

Thom Wiggers's avatar
Thom Wiggers committed
130

131
class PermissionsBackendTest(TestCase):
Thom Wiggers's avatar
Thom Wiggers committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    fixtures = ['members.json', 'committees.json']

    @classmethod
    def setUpTestData(cls):
        cls.u1 = Member.objects.get(pk=1)
        cls.u1.user.is_superuser = False
        cls.u1.save()
        cls.u2 = Member.objects.get(pk=2)
        cls.u3 = Member.objects.get(pk=3)
        cls.c1 = Committee.objects.get(pk=1)
        cls.c2 = Committee.objects.get(pk=2)
        cls.m1 = CommitteeMembership.objects.create(committee=cls.c1,
                                                    member=cls.u1)
        cls.m2 = CommitteeMembership.objects.create(committee=cls.c2,
                                                    member=cls.u2)

    def test_permissions(self):
        self.assertEqual(3, len(self.u1.user.get_all_permissions()))
        self.assertEqual(set(), self.u2.user.get_all_permissions())
        self.assertEqual(set(), self.u3.user.get_all_permissions())

    def test_nonmember_user(self):
        u = get_user_model().objects.create(username='foo')
        self.assertEqual(set(), u.get_all_permissions())
156
157
158
159
160
161
162
163
164
165


class BoardTest(TestCase):
    fixtures = ['committees.json']

    def setUp(self):
        self.testboard = Board.objects.get(pk=3)

    def test_get_absolute_url(self):
        self.testboard.get_absolute_url()