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

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

8
from activemembers.models import Committee, CommitteeMembership
9
10
11
12
13
from events.models import (Event, Registration,
                           RegistrationInformationField,
                           BooleanRegistrationInformation,
                           IntegerRegistrationInformation,
                           TextRegistrationInformation)
14
from mailinglists.models import MailingList
Tom van Bussel's avatar
Tom van Bussel committed
15
16
17
from members.models import Member


18
19
20
21
22
23
24
class AdminTest(TestCase):
    """Tests for admin views"""

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

    @classmethod
    def setUpTestData(cls):
Thom Wiggers's avatar
Thom Wiggers committed
25
        cls.committee = Committee.objects.get(pk=1)
26
27
        cls.event = Event.objects.create(
            pk=1,
Thom Wiggers's avatar
Thom Wiggers committed
28
            organiser=cls.committee,
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
            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')
45
46
47
        cls.permission_override_orga = Permission.objects.get(
            content_type__model='event',
            codename='override_organiser')
48
49
50
51
52
53
54
55
56
57
        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)

58
59
60
    def _add_override_organiser_permission(self):
        self.member.user.user_permissions.add(self.permission_override_orga)

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    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)

79
80
81
82
83
    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)

84
    def test_modeladmin_change_organiser_allowed(self):
Thom Wiggers's avatar
Thom Wiggers committed
85
        """Change event as an organiser
86
87
88
89
90
91
92
93

        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)
94
95

    def test_modeladmin_change_override_organiser_allowed(self):
Thom Wiggers's avatar
Thom Wiggers committed
96
        """Test the override organiser permission for changing events
97
98
99
100
101
102

        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)
103
104

    def test_modeladmin_change_organiser_no_permissions_denied(self):
Thom Wiggers's avatar
Thom Wiggers committed
105
        """Committee members without change permissions are banned
106
107
108
109
110
111
112
113
114
115
116
117

        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
118
        """Superuser should be allowed access always"""
119
120
121
122
123
124
        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
125
        """If I'm not an organiser I should not be allowed access"""
126
127
128
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(403, response.status_code)

129

Tom van Bussel's avatar
Tom van Bussel committed
130
131
132
class RegistrationTest(TestCase):
    """Tests for registration view"""

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

Thom Wiggers's avatar
Thom Wiggers committed
135
136
    @classmethod
    def setUpTestData(cls):
137
138
139
140
141
142
143
144
        cls.mailinglist = MailingList.objects.create(
            name="testmail"
        )
        cls.committee = Committee.objects.create(
            name_nl="commissie",
            name_en="committee",
            contact_mailinglist=cls.mailinglist
        )
Thom Wiggers's avatar
Thom Wiggers committed
145
        cls.event = Event.objects.create(
Tom van Bussel's avatar
Tom van Bussel committed
146
            pk=1,
147
            organiser=cls.committee,
Tom van Bussel's avatar
Tom van Bussel committed
148
149
150
151
152
153
154
155
156
157
158
159
            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
160
161
162
        cls.member = Member.objects.filter(user__last_name="Wiggers").first()

    def setUp(self):
Tom van Bussel's avatar
Tom van Bussel committed
163
164
165
166
167
168
169
        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)
170
        self.assertEqual(self.event.participants.count(), 0)
Tom van Bussel's avatar
Tom van Bussel committed
171
172
173
174
175
176
177
178
179
180
181
182

    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)
183
        self.assertEqual(self.event.participants.count(), 1)
Tom van Bussel's avatar
Tom van Bussel committed
184
        self.assertEqual(
185
            self.event.registration_set.first().member, self.member)
Tom van Bussel's avatar
Tom van Bussel committed
186
187
188
189
190
191
192
193
194
195
196
197

    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)
198
        response = self.client.post('/events/1/registration/register/',
Tom van Bussel's avatar
Tom van Bussel committed
199
200
                                    follow=True)
        self.assertEqual(response.status_code, 200)
201
        self.assertEqual(self.event.participants.count(), 1)
Tom van Bussel's avatar
Tom van Bussel committed
202
203
204
205
206
207
208
209
210
211
212
213

    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)
214
        self.assertEqual(self.event.participants.count(), 0)
Tom van Bussel's avatar
Tom van Bussel committed
215
216
217
218
219
220
221
222
223

    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
224
        Registration.objects.create(event=self.event, member=self.member)
Tom van Bussel's avatar
Tom van Bussel committed
225
226
227
        response = self.client.post('/events/1/registration/cancel/',
                                    follow=True)
        self.assertEqual(response.status_code, 200)
228
        self.assertEqual(self.event.participants.count(), 0)
Tom van Bussel's avatar
Tom van Bussel committed
229

230
    def test_registration_register_no_fields(self):
Tom van Bussel's avatar
Tom van Bussel committed
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
        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)

270
        self.assertEqual(self.event.participants.count(), 1)
Tom van Bussel's avatar
Tom van Bussel committed
271
        registration = self.event.registration_set.first()
272
273
274
        self.assertEqual(field1.get_value_for(registration), None)
        self.assertEqual(field2.get_value_for(registration), None)
        self.assertEqual(field3.get_value_for(registration), None)
Tom van Bussel's avatar
Tom van Bussel committed
275

276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    def test_registration_missing_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()

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

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

        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/',
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        template_names = [template.name for template in response.templates]
313
314
        self.assertIn('events/registration.html', template_names)
        self.assertEqual(self.event.participants.count(), 1)
315
316

    def test_registration_register_fields_required(self):
Tom van Bussel's avatar
Tom van Bussel committed
317
318
319
320
321
322
323
324
        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
325
        RegistrationInformationField.objects.create(
Tom van Bussel's avatar
Tom van Bussel committed
326
327
328
329
330
331
            event=self.event,
            type=RegistrationInformationField.TEXT_FIELD,
            name_en="test",
            name_nl="test",
            required=True)

332
333
        response = self.client.post('/events/1/registration/register/',
                                    follow=True)
Tom van Bussel's avatar
Tom van Bussel committed
334
335
        self.assertEqual(response.status_code, 200)
        template_names = [template.name for template in response.templates]
336
337
        self.assertIn('events/registration.html', template_names)
        self.assertEqual(self.event.participants.count(), 1)
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381

    def test_registration_update_form_load_not_changes_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)

        registration = Registration.objects.create(
            event=self.event, member=self.member)
        BooleanRegistrationInformation.objects.create(
            registration=registration, field=field1, value=True)
        IntegerRegistrationInformation.objects.create(
            registration=registration, field=field2, value=42)
        TextRegistrationInformation.objects.create(
            registration=registration, field=field3, value="text")

        # as if there is a csrf token
382
383
        response = self.client.get('/events/1/registration/',
                                   follow=True)
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
        self.assertEqual(response.status_code, 200)

        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_update_form_post_changes_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,
427
428
                                     'info_field_3': 'text',
                                     'csrf': 'random'},
429
430
431
                                    follow=True)
        self.assertEqual(response.status_code, 200)

432
        response = self.client.post('/events/1/registration/',
433
434
                                    {'info_field_1': False,
                                     'info_field_2': 1337,
435
436
                                     'info_field_3': 'no text',
                                     'csrf': 'random'},
437
438
439
                                    follow=True)
        self.assertEqual(response.status_code, 200)

440
        self.assertEqual(self.event.participants.count(), 1)
441
442
443
444
        registration = self.event.registration_set.first()
        self.assertEqual(field1.get_value_for(registration), False)
        self.assertEqual(field2.get_value_for(registration), 1337)
        self.assertEqual(field3.get_value_for(registration), 'no text')
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462

    def test_registration_cancel_after_deadline_notification(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.send_cancel_email = True
        self.event.save()
        Registration.objects.create(event=self.event, member=self.member)
        response = self.client.post('/events/1/registration/cancel/',
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(self.event.participants.count(), 0)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to, [self.event.organiser.
                         contact_mailinglist.name + "@thalia.nu"])