test_views.py 12.4 KB
Newer Older
Tom van Bussel's avatar
Tom van Bussel committed
1
2
import datetime

3
from django.contrib.auth.models import Permission
Tom van Bussel's avatar
Tom van Bussel committed
4
5
6
from django.test import Client, TestCase
from django.utils import timezone

7
from activemembers.models import Committee, CommitteeMembership
Tom van Bussel's avatar
Tom van Bussel committed
8
9
10
11
from events.models import Event, Registration, RegistrationInformationField
from members.models import Member


12
13
14
15
16
17
18
class AdminTest(TestCase):
    """Tests for admin views"""

    fixtures = ['members.json', 'committees.json']

    @classmethod
    def setUpTestData(cls):
Thom Wiggers's avatar
Thom Wiggers committed
19
        cls.committee = Committee.objects.get(pk=1)
20
21
        cls.event = Event.objects.create(
            pk=1,
Thom Wiggers's avatar
Thom Wiggers committed
22
            organiser=cls.committee,
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
            title_nl='testevenement',
            title_en='testevent',
            description_en='desc',
            description_nl='besch',
            published=True,
            start=(timezone.now() + datetime.timedelta(hours=1)),
            end=(timezone.now() + datetime.timedelta(hours=2)),
            location_en='test location',
            location_nl='test locatie',
            map_location='test map location',
            price=0.00,
            fine=0.00)
        cls.member = Member.objects.filter(user__last_name="Wiggers").first()
        cls.permission_change_event = Permission.objects.get(
            content_type__model='event',
            codename='change_event')
39
40
41
        cls.permission_override_orga = Permission.objects.get(
            content_type__model='event',
            codename='override_organiser')
42
43
44
45
46
47
48
49
50
51
        cls.member.user.user_permissions.add(cls.permission_change_event)
        cls.member.user.is_superuser = False
        cls.member.user.save()

    def setUp(self):
        self.client.force_login(self.member.user)

    def _remove_event_permission(self):
        self.member.user.user_permissions.remove(self.permission_change_event)

52
53
54
    def _add_override_organiser_permission(self):
        self.member.user.user_permissions.add(self.permission_override_orga)

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    def test_admin_details_need_change_event_access(self):
        """I need the event.change_event permission to do stuff"""
        self._remove_event_permission()
        response = self.client.get('/events/admin/1/')
        self.assertEqual(302, response.status_code)
        self.assertTrue(response.url.startswith('/login/'))

    def test_admin_details_organiser_denied(self):
        response = self.client.get('/events/admin/1/')
        self.assertEqual(403, response.status_code)

    def test_admin_details_organiser_allowed(self):
        CommitteeMembership.objects.create(
            member=self.member,
            committee=self.committee)
        response = self.client.get('/events/admin/1/')
        self.assertEqual(200, response.status_code)

73
74
75
76
77
    def test_admin_details_override_organiser_allowed(self):
        self._add_override_organiser_permission()
        response = self.client.get('/events/admin/1/')
        self.assertEqual(200, response.status_code)

78
    def test_modeladmin_change_organiser_allowed(self):
Thom Wiggers's avatar
Thom Wiggers committed
79
        """Change event as an organiser
80
81
82
83
84
85
86
87

        If I'm an organiser I should be allowed access
        """
        CommitteeMembership.objects.create(
            member=self.member,
            committee=self.committee)
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(200, response.status_code)
88
89

    def test_modeladmin_change_override_organiser_allowed(self):
Thom Wiggers's avatar
Thom Wiggers committed
90
        """Test the override organiser permission for changing events
91
92
93
94
95
96

        If I'm allowed to override organiser restrictions..
        """
        self._add_override_organiser_permission()
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(200, response.status_code)
97
98

    def test_modeladmin_change_organiser_no_permissions_denied(self):
Thom Wiggers's avatar
Thom Wiggers committed
99
        """Committee members without change permissions are banned
100
101
102
103
104
105
106
107
108
109
110
111

        If I'm an organiser, but don't have perms I should not
        be allowed access
        """
        self._remove_event_permission()
        CommitteeMembership.objects.create(
            member=self.member,
            committee=self.committee)
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(403, response.status_code)

    def test_modeladmin_change_superuser_allowed(self):
Thom Wiggers's avatar
Thom Wiggers committed
112
        """Superuser should be allowed access always"""
113
114
115
116
117
118
        self.member.user.is_superuser = True
        self.member.user.save()
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(200, response.status_code)

    def test_modeladmin_change_organiser_denied(self):
Thom Wiggers's avatar
Thom Wiggers committed
119
        """If I'm not an organiser I should not be allowed access"""
120
121
122
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(403, response.status_code)

123

Tom van Bussel's avatar
Tom van Bussel committed
124
125
126
class RegistrationTest(TestCase):
    """Tests for registration view"""

Thom Wiggers's avatar
Thom Wiggers committed
127
    fixtures = ['members.json', 'committees.json']
Tom van Bussel's avatar
Tom van Bussel committed
128

Thom Wiggers's avatar
Thom Wiggers committed
129
130
131
    @classmethod
    def setUpTestData(cls):
        cls.event = Event.objects.create(
Tom van Bussel's avatar
Tom van Bussel committed
132
            pk=1,
Thom Wiggers's avatar
Thom Wiggers committed
133
            organiser=Committee.objects.get(pk=1),
Tom van Bussel's avatar
Tom van Bussel committed
134
135
136
137
138
139
140
141
142
143
144
145
            title_nl='testevene',
            title_en='testevent',
            description_en='desc',
            description_nl='besch',
            published=True,
            start=(timezone.now() + datetime.timedelta(hours=1)),
            end=(timezone.now() + datetime.timedelta(hours=2)),
            location_en='test location',
            location_nl='test locatie',
            map_location='test map location',
            price=0.00,
            fine=0.00)
Thom Wiggers's avatar
Thom Wiggers committed
146
147
148
        cls.member = Member.objects.filter(user__last_name="Wiggers").first()

    def setUp(self):
Tom van Bussel's avatar
Tom van Bussel committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        self.client = Client()
        self.client.force_login(self.member.user)

    def test_registration_register_not_required(self):
        response = self.client.post('/events/1/registration/register/',
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(self.event.num_participants(), 0)

    def test_registration_register(self):
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=1))
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
        self.event.save()
        response = self.client.post('/events/1/registration/register/',
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(self.event.num_participants(), 1)
        self.assertEqual(
                self.event.registration_set.first().member, self.member)

    def test_registration_register_twice(self):
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=1))
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
        self.event.save()
        response = self.client.post('/events/1/registration/register/',
                                    follow=True)
        self.assertEqual(response.status_code, 200)
184
        response = self.client.post('/events/1/registration/register/',
Tom van Bussel's avatar
Tom van Bussel committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(self.event.num_participants(), 1)

    def test_registration_register_closed(self):
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=2))
        self.event.registration_end = (timezone.now() -
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
        self.event.save()
        response = self.client.post('/events/1/registration/register/',
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(self.event.num_participants(), 0)

    def test_registration_cancel(self):
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=1))
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
        self.event.save()
Thom Wiggers's avatar
Thom Wiggers committed
210
        Registration.objects.create(event=self.event, member=self.member)
Tom van Bussel's avatar
Tom van Bussel committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
        response = self.client.post('/events/1/registration/cancel/',
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(self.event.num_participants(), 0)

    def test_registration_register_fields(self):
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=1))
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
        self.event.save()

        field1 = RegistrationInformationField.objects.create(
            pk=1,
            event=self.event,
            type=RegistrationInformationField.BOOLEAN_FIELD,
            name_en="test bool",
            name_nl="test bool",
            required=False)

        field2 = RegistrationInformationField.objects.create(
            pk=2,
            event=self.event,
            type=RegistrationInformationField.INTEGER_FIELD,
            name_en="test int",
            name_nl="test int",
            required=False)

        field3 = RegistrationInformationField.objects.create(
            pk=3,
            event=self.event,
            type=RegistrationInformationField.TEXT_FIELD,
            name_en="test text",
            name_nl="test text",
            required=False)

        response = self.client.post('/events/1/registration/register/',
                                    {'info_field_1': True,
                                     'info_field_2': 42,
                                     'info_field_3': "text"},
                                    follow=True)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(self.event.num_participants(), 1)
        registration = self.event.registration_set.first()
        self.assertEqual(field1.get_value_for(registration), True)
        self.assertEqual(field2.get_value_for(registration), 42)
        self.assertEqual(field3.get_value_for(registration), 'text')

    def test_registration_register_field_required(self):
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=1))
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
        self.event.save()

Thom Wiggers's avatar
Thom Wiggers committed
271
        RegistrationInformationField.objects.create(
Tom van Bussel's avatar
Tom van Bussel committed
272
273
274
275
276
277
278
279
            event=self.event,
            type=RegistrationInformationField.TEXT_FIELD,
            name_en="test",
            name_nl="test",
            required=True)

        response = self.client.post('/events/1/registration/register/',
                                    follow=True)
280

Tom van Bussel's avatar
Tom van Bussel committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
        self.assertEqual(response.status_code, 200)
        template_names = [template.name for template in response.templates]
        self.assertIn('events/event_fields.html', template_names)
        self.assertEqual(self.event.num_participants(), 0)

        # This is wrong
        response = self.client.post('/events/1/registration/register/',
                                    {'test': 'test'},
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(self.event.num_participants(), 0)

        # This is correct
        response = self.client.post('/events/1/registration/register/',
                                    {'info_field_1': 'test'},
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(self.event.num_participants(), 1)