test_views.py 13.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
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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class AdminTest(TestCase):
    """Tests for admin views"""

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

    @classmethod
    def setUpTestData(cls):
        cls.event = Event.objects.create(
            pk=1,
            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')
37
38
39
        cls.permission_override_orga = Permission.objects.get(
            content_type__model='event',
            codename='override_organiser')
40
41
42
43
44
45
46
47
48
49
50
        cls.member.user.user_permissions.add(cls.permission_change_event)
        cls.member.user.is_superuser = False
        cls.member.user.save()
        cls.committee = Committee.objects.get(pk=1)

    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)

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

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    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_no_organiser_allowed_access(self):
        """If an event has no organiser, then I should be allowed access"""
        response = self.client.get('/events/admin/1/')
        self.assertEqual(200, response.status_code)

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

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

81
82
83
84
85
86
87
    def test_admin_details_override_organiser_allowed(self):
        self._add_override_organiser_permission()
        self.event.organiser = self.committee
        self.event.save()
        response = self.client.get('/events/admin/1/')
        self.assertEqual(200, response.status_code)

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    def test_modeladmin_change_no_organiser_allowed(self):
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(200, response.status_code)

    def test_modeladmin_change_organiser_allowed(self):
        """Test the ModelAdmin change page

        If I'm an organiser I should be allowed access
        """
        self.event.organiser = self.committee
        CommitteeMembership.objects.create(
            member=self.member,
            committee=self.committee)
        self.event.save()
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(200, response.status_code)
104
105
106
107
108
109
110
111
112
113
114

    def test_modeladmin_change_override_organiser_allowed(self):
        """Test the ModelAdmin change page

        If I'm allowed to override organiser restrictions..
        """
        self._add_override_organiser_permission()
        self.event.organiser = self.committee
        self.event.save()
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(200, response.status_code)
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

    def test_modeladmin_change_organiser_no_permissions_denied(self):
        """Test the ModelAdmin change page

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

    def test_modeladmin_change_superuser_allowed(self):
        """Test the ModelAdmin change page

        If I'm an organiser I should be allowed access
        """
        self.event.organiser = self.committee
        self.event.save()
        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):
        """Test the ModelAdmin change page

        If I'm not an organiser I should not be allowed access
        """
        self.event.organiser = self.committee
        self.event.save()
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(403, response.status_code)

153

Tom van Bussel's avatar
Tom van Bussel committed
154
155
156
157
158
class RegistrationTest(TestCase):
    """Tests for registration view"""

    fixtures = ['members.json']

Thom Wiggers's avatar
Thom Wiggers committed
159
160
161
    @classmethod
    def setUpTestData(cls):
        cls.event = Event.objects.create(
Tom van Bussel's avatar
Tom van Bussel committed
162
163
164
165
166
167
168
169
170
171
172
173
174
            pk=1,
            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
175
176
177
        cls.member = Member.objects.filter(user__last_name="Wiggers").first()

    def setUp(self):
Tom van Bussel's avatar
Tom van Bussel committed
178
179
180
181
182
183
184
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
210
211
212
        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)
213
        response = self.client.post('/events/1/registration/register/',
Tom van Bussel's avatar
Tom van Bussel committed
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
                                    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
239
        Registration.objects.create(event=self.event, member=self.member)
Tom van Bussel's avatar
Tom van Bussel committed
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
271
272
273
274
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
        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
300
        RegistrationInformationField.objects.create(
Tom van Bussel's avatar
Tom van Bussel committed
301
302
303
304
305
306
307
308
            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)
309

Tom van Bussel's avatar
Tom van Bussel committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
        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)