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

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
3
import factory
4
from django.contrib.auth.models import Permission
5
from django.core import mail
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
6
from django.db.models import signals
Tom van Bussel's avatar
Tom van Bussel committed
7 8 9
from django.test import Client, TestCase
from django.utils import timezone

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


20 21 22
class AdminTest(TestCase):
    """Tests for admin views"""

23
    fixtures = ['members.json', 'member_groups.json']
24 25 26

    @classmethod
    def setUpTestData(cls):
Thom Wiggers's avatar
Thom Wiggers committed
27
        cls.committee = Committee.objects.get(pk=1)
28 29
        cls.event = Event.objects.create(
            pk=1,
Thom Wiggers's avatar
Thom Wiggers committed
30
            organiser=cls.committee,
31 32 33 34 35 36 37 38 39 40 41 42
            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)
43
        cls.member = Member.objects.filter(last_name="Wiggers").first()
44 45 46
        cls.permission_change_event = Permission.objects.get(
            content_type__model='event',
            codename='change_event')
47 48 49
        cls.permission_override_orga = Permission.objects.get(
            content_type__model='event',
            codename='override_organiser')
50 51 52
        cls.member.user_permissions.add(cls.permission_change_event)
        cls.member.is_superuser = False
        cls.member.save()
53 54

    def setUp(self):
55
        self.client.force_login(self.member)
56 57

    def _remove_event_permission(self):
58
        self.member.user_permissions.remove(self.permission_change_event)
59

60
    def _add_override_organiser_permission(self):
61
        self.member.user_permissions.add(self.permission_override_orga)
62

63 64 65
    def test_admin_details_need_change_event_access(self):
        """I need the event.change_event permission to do stuff"""
        self._remove_event_permission()
66 67
        response = self.client.get('/admin/events/event/1/details/')
        self.assertEqual(403, response.status_code)
68 69

    def test_admin_details_organiser_denied(self):
70
        response = self.client.get('/admin/events/event/1/details/')
71 72 73
        self.assertEqual(403, response.status_code)

    def test_admin_details_organiser_allowed(self):
74
        MemberGroupMembership.objects.create(
75
            member=self.member,
76
            group=self.committee)
77
        response = self.client.get('/admin/events/event/1/details/')
78 79
        self.assertEqual(200, response.status_code)

80 81
    def test_admin_details_override_organiser_allowed(self):
        self._add_override_organiser_permission()
82
        response = self.client.get('/admin/events/event/1/details/')
83 84
        self.assertEqual(200, response.status_code)

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

        If I'm an organiser I should be allowed access
        """
90
        MemberGroupMembership.objects.create(
91
            member=self.member,
92
            group=self.committee)
93 94
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(200, response.status_code)
95 96

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

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

    def test_modeladmin_change_organiser_no_permissions_denied(self):
Thom Wiggers's avatar
Thom Wiggers committed
106
        """Committee members without change permissions are banned
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()
112
        MemberGroupMembership.objects.create(
113
            member=self.member,
114
            group=self.committee)
115 116 117 118
        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
119
        """Superuser should be allowed access always"""
120 121
        self.member.is_superuser = True
        self.member.save()
122 123
        response = self.client.get('/admin/events/event/1/change/')
        self.assertEqual(200, response.status_code)
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
124
        self.assertIn('Change event', str(response.content))
125 126

    def test_modeladmin_change_organiser_denied(self):
Thom Wiggers's avatar
Thom Wiggers committed
127
        """If I'm not an organiser I should not be allowed access"""
128
        response = self.client.get('/admin/events/event/1/change/')
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
129 130
        self.assertEqual(200, response.status_code)
        self.assertIn('View event', str(response.content))
131

132

Tom van Bussel's avatar
Tom van Bussel committed
133 134 135
class RegistrationTest(TestCase):
    """Tests for registration view"""

136
    fixtures = ['members.json', 'member_groups.json']
Tom van Bussel's avatar
Tom van Bussel committed
137

Thom Wiggers's avatar
Thom Wiggers committed
138
    @classmethod
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
139
    @factory.django.mute_signals(signals.pre_save)
Thom Wiggers's avatar
Thom Wiggers committed
140
    def setUpTestData(cls):
141 142 143 144 145 146 147 148
        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
149
        cls.event = Event.objects.create(
Tom van Bussel's avatar
Tom van Bussel committed
150
            pk=1,
151
            organiser=cls.committee,
Tom van Bussel's avatar
Tom van Bussel committed
152 153 154 155 156 157 158 159 160 161 162 163
            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)
164
        cls.member = Member.objects.filter(last_name="Wiggers").first()
Thom Wiggers's avatar
Thom Wiggers committed
165 166

    def setUp(self):
Tom van Bussel's avatar
Tom van Bussel committed
167
        self.client = Client()
168
        self.client.force_login(self.member)
Tom van Bussel's avatar
Tom van Bussel committed
169 170 171 172 173

    def test_registration_register_not_required(self):
        response = self.client.post('/events/1/registration/register/',
                                    follow=True)
        self.assertEqual(response.status_code, 200)
174
        self.assertEqual(self.event.participants.count(), 0)
Tom van Bussel's avatar
Tom van Bussel committed
175 176 177 178 179 180 181 182 183 184 185 186

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

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

    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)
218
        self.assertEqual(self.event.participants.count(), 0)
Tom van Bussel's avatar
Tom van Bussel committed
219 220 221 222 223 224 225 226 227

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

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

274
        self.assertEqual(self.event.participants.count(), 1)
Tom van Bussel's avatar
Tom van Bussel committed
275
        registration = self.event.registration_set.first()
276 277 278
        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
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 313 314 315 316
    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]
317 318
        self.assertIn('events/registration.html', template_names)
        self.assertEqual(self.event.participants.count(), 1)
319 320

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

336 337
        response = self.client.post('/events/1/registration/register/',
                                    follow=True)
Tom van Bussel's avatar
Tom van Bussel committed
338 339
        self.assertEqual(response.status_code, 200)
        template_names = [template.name for template in response.templates]
340 341
        self.assertIn('events/registration.html', template_names)
        self.assertEqual(self.event.participants.count(), 1)
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 382 383 384 385

    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
386 387
        response = self.client.get('/events/1/registration/',
                                   follow=True)
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 427 428 429 430
        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,
431 432
                                     'info_field_3': 'text',
                                     'csrf': 'random'},
433 434 435
                                    follow=True)
        self.assertEqual(response.status_code, 200)

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

444
        self.assertEqual(self.event.participants.count(), 1)
445 446 447 448
        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')
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

    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"])