test_views.py 16.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
8
9
10
from events.models import Event, Registration, RegistrationInformationField, \
    BooleanRegistrationInformation, IntegerRegistrationInformation, \
    TextRegistrationInformation
Tom van Bussel's avatar
Tom van Bussel committed
11
12
13
from members.models import Member


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

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

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

54
55
56
    def _add_override_organiser_permission(self):
        self.member.user.user_permissions.add(self.permission_override_orga)

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    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)

75
76
77
78
79
    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)

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

        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)
90
91

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

        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)
99
100

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

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

125

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

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

Thom Wiggers's avatar
Thom Wiggers committed
131
132
133
    @classmethod
    def setUpTestData(cls):
        cls.event = Event.objects.create(
Tom van Bussel's avatar
Tom van Bussel committed
134
            pk=1,
Thom Wiggers's avatar
Thom Wiggers committed
135
            organiser=Committee.objects.get(pk=1),
Tom van Bussel's avatar
Tom van Bussel committed
136
137
138
139
140
141
142
143
144
145
146
147
            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
148
149
150
        cls.member = Member.objects.filter(user__last_name="Wiggers").first()

    def setUp(self):
Tom van Bussel's avatar
Tom van Bussel committed
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
184
185
        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)
186
        response = self.client.post('/events/1/registration/register/',
Tom van Bussel's avatar
Tom van Bussel committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
                                    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
212
        Registration.objects.create(event=self.event, member=self.member)
Tom van Bussel's avatar
Tom van Bussel committed
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
        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')

264
    def test_registration_register_fields_not_required(self):
Tom van Bussel's avatar
Tom van Bussel committed
265
266
267
268
269
270
271
272
        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
273
        RegistrationInformationField.objects.create(
Tom van Bussel's avatar
Tom van Bussel committed
274
275
276
277
278
279
280
281
            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)
282

Tom van Bussel's avatar
Tom van Bussel committed
283
284
285
286
        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(), 1)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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

    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
        response = self.client.post('/events/1/registration/update/',
                                    {'csrf': 'random'},
                                    follow=True)
        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,
377
378
                                     'info_field_3': 'text',
                                     'csrf': 'random'},
379
380
381
382
383
384
                                    follow=True)
        self.assertEqual(response.status_code, 200)

        response = self.client.post('/events/1/registration/update/',
                                    {'info_field_1': False,
                                     'info_field_2': 1337,
385
386
                                     'info_field_3': 'no text',
                                     'csrf': 'random'},
387
388
389
390
391
392
393
394
                                    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), False)
        self.assertEqual(field2.get_value_for(registration), 1337)
        self.assertEqual(field3.get_value_for(registration), 'no text')