gestioCOF/events/tests/test_views.py

162 lines
5.2 KiB
Python
Raw Permalink Normal View History

2019-10-05 16:55:05 +02:00
from unittest import mock
from django.conf import settings
2019-10-05 16:55:05 +02:00
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.test import Client, TestCase
from django.urls import reverse
2019-10-08 23:33:46 +02:00
from events.models import (
Event,
ExtraField,
ExtraFieldContent,
Option,
OptionChoice,
Registration,
)
2020-05-11 01:16:58 +02:00
from shared.tests.mixins import CSVResponseMixin
2019-10-05 16:55:05 +02:00
User = get_user_model()
def make_user(name):
return User.objects.create_user(username=name, password=name)
def make_staff_user(name):
2019-12-24 17:13:27 +01:00
view_event_perm = Permission.objects.get(
2020-08-28 18:00:54 +02:00
codename="view_event",
content_type__app_label="events",
2019-10-05 16:55:05 +02:00
)
user = make_user(name)
user.user_permissions.add(view_event_perm)
return user
class MessagePatch:
2019-10-05 16:55:05 +02:00
def setUp(self):
# Signals handlers on login/logout send messages.
# Due to the way the Django' test Client performs login, this raise an
# error. As workaround, we mock the Django' messages module.
patcher_messages = mock.patch("gestioncof.signals.messages")
patcher_messages.start()
self.addCleanup(patcher_messages.stop)
class CSVExportAccessTest(MessagePatch, TestCase):
def setUp(self):
super().setUp()
2019-10-05 16:55:05 +02:00
self.staff = make_staff_user("staff")
self.u1 = make_user("toto")
self.event = Event.objects.create(title="test_event", location="somewhere")
self.url = reverse("events:csv-participants", args=[self.event.id])
def test_get(self):
client = Client()
2021-06-15 16:52:56 +02:00
client.force_login(
self.staff, backend="django.contrib.auth.backends.ModelBackend"
)
2019-10-05 16:55:05 +02:00
r = client.get(self.url)
self.assertEqual(r.status_code, 200)
def test_anonymous(self):
client = Client()
r = client.get(self.url)
login_url = "{}?next={}".format(reverse(settings.LOGIN_URL), self.url)
self.assertRedirects(r, login_url, fetch_redirect_response=False)
2019-10-05 16:55:05 +02:00
def test_unauthorised(self):
client = Client()
2021-06-15 16:52:56 +02:00
client.force_login(self.u1, backend="django.contrib.auth.backends.ModelBackend")
2019-10-05 16:55:05 +02:00
r = client.get(self.url)
self.assertEqual(r.status_code, 403)
2020-05-11 01:16:58 +02:00
class CSVExportContentTest(MessagePatch, CSVResponseMixin, TestCase):
def setUp(self):
super().setUp()
self.event = Event.objects.create(title="test_event", location="somewhere")
self.url = reverse("events:csv-participants", args=[self.event.id])
self.u1 = User.objects.create_user(
username="toto_foo", first_name="toto", last_name="foo", email="toto@a.b"
)
self.u2 = User.objects.create_user(
username="titi_bar", first_name="titi", last_name="bar", email="titi@a.b"
)
self.staff = make_staff_user("staff")
self.client = Client()
2021-06-15 16:52:56 +02:00
self.client.force_login(
self.staff, backend="django.contrib.auth.backends.ModelBackend"
)
def test_simple_event(self):
self.event.subscribers.set([self.u1, self.u2])
2020-05-11 01:16:58 +02:00
response = self.client.get(self.url)
2020-05-12 01:11:59 +02:00
self.assertCSVEqual(
response,
2020-05-11 01:16:58 +02:00
[
{
"username": "toto_foo",
"prénom": "toto",
"nom de famille": "foo",
"email": "toto@a.b",
},
{
"username": "titi_bar",
"prénom": "titi",
"nom de famille": "bar",
"email": "titi@a.b",
},
],
)
def test_complex_event(self):
registration = Registration.objects.create(event=self.event, user=self.u1)
# Set up some options
option1 = Option.objects.create(
event=self.event, name="abc", multi_choices=False
)
option2 = Option.objects.create(
event=self.event, name="def", multi_choices=True
)
OptionChoice.objects.bulk_create(
[
OptionChoice(option=option1, choice="a"),
OptionChoice(option=option1, choice="b"),
OptionChoice(option=option1, choice="c"),
OptionChoice(option=option2, choice="d"),
OptionChoice(option=option2, choice="e"),
OptionChoice(option=option2, choice="f"),
]
)
registration.options_choices.set(
OptionChoice.objects.filter(choice__in=["d", "f"])
)
registration.options_choices.add(OptionChoice.objects.get(choice="a"))
# And an extra field
field = ExtraField.objects.create(event=self.event, name="remarks")
ExtraFieldContent.objects.create(
field=field, registration=registration, content="hello"
)
2020-05-11 01:16:58 +02:00
response = self.client.get(self.url)
2020-05-14 21:23:25 +02:00
self.assertCSVEqual(
response,
[
{
"username": "toto_foo",
"prénom": "toto",
"nom de famille": "foo",
"email": "toto@a.b",
"abc": "a",
"def": "d & f",
"remarks": "hello",
}
],
)