gestioCOF/gestioncof/tests/test_views.py
2018-01-21 18:19:43 +01:00

151 lines
4.8 KiB
Python

import uuid
from datetime import timedelta
from django.contrib import messages
from django.contrib.messages.api import get_messages
from django.contrib.messages.storage.base import Message
from django.test import TestCase
from bda.models import Salle, Tirage
from gestioncof.models import CalendarSubscription
from gestioncof.tests.testcases import ViewTestCaseMixin
class CalendarViewTests(ViewTestCaseMixin, TestCase):
url_name = 'calendar'
url_expected = '/calendar/subscription'
auth_user = 'member'
auth_forbidden = [None, 'user']
post_expected_message = Message(
messages.SUCCESS, "Calendrier mis à jour avec succès.")
def test_get(self):
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
def test_post_new(self):
r = self.client.post(self.url, {
'subscribe_to_events': True,
'subscribe_to_my_shows': True,
'other_shows': [],
})
self.assertEqual(r.status_code, 200)
self.assertIn(self.post_expected_message, get_messages(r.wsgi_request))
cs = self.users['member'].calendarsubscription
self.assertTrue(cs.subscribe_to_events)
self.assertTrue(cs.subscribe_to_my_shows)
def test_post_edit(self):
u = self.users['member']
token = uuid.uuid4()
cs = CalendarSubscription.objects.create(token=token, user=u)
r = self.client.post(self.url, {
'other_shows': [],
})
self.assertEqual(r.status_code, 200)
self.assertIn(self.post_expected_message, get_messages(r.wsgi_request))
cs.refresh_from_db()
self.assertEqual(cs.token, token)
self.assertFalse(cs.subscribe_to_events)
self.assertFalse(cs.subscribe_to_my_shows)
def test_post_other_shows(self):
t = Tirage.objects.create(
ouverture=self.now,
fermeture=self.now,
active=True,
)
l = Salle.objects.create()
s = t.spectacle_set.create(
date=self.now, price=3.5, slots=20, location=l, listing=True)
r = self.client.post(self.url, {'other_shows': [str(s.pk)]})
self.assertEqual(r.status_code, 200)
class CalendarICSViewTests(ViewTestCaseMixin, TestCase):
url_name = 'calendar.ics'
auth_user = None
auth_forbidden = []
@property
def url_kwargs(self):
return {'token': self.token}
@property
def url_expected(self):
return '/calendar/{}/calendar.ics'.format(self.token)
def setUp(self):
super().setUp()
self.token = uuid.uuid4()
self.t = Tirage.objects.create(
ouverture=self.now,
fermeture=self.now,
active=True,
)
l = Salle.objects.create(name='Location')
self.s1 = self.t.spectacle_set.create(
price=1, slots=10, location=l, listing=True,
title='Spectacle 1', date=self.now + timedelta(days=1),
)
self.s2 = self.t.spectacle_set.create(
price=2, slots=20, location=l, listing=True,
title='Spectacle 2', date=self.now + timedelta(days=2),
)
self.s3 = self.t.spectacle_set.create(
price=3, slots=30, location=l, listing=True,
title='Spectacle 3', date=self.now + timedelta(days=3),
)
def test(self):
u = self.users['user']
p = u.participant_set.create(tirage=self.t)
p.attribution_set.create(spectacle=self.s1)
self.cs = CalendarSubscription.objects.create(
user=u, token=self.token,
subscribe_to_my_shows=True, subscribe_to_events=True,
)
self.cs.other_shows.add(self.s2)
r = self.client.get(self.url)
def get_dt_from_ical(v):
return v.dt
self.assertCalEqual(r.content.decode('utf-8'), [
{
'summary': 'Spectacle 1',
'dtstart': (get_dt_from_ical, (
(self.now + timedelta(days=1)).replace(microsecond=0)
)),
'dtend': (get_dt_from_ical, (
(self.now + timedelta(days=1, hours=2)).replace(
microsecond=0)
)),
'location': 'Location',
'uid': 'show-{}-{}@example.com'.format(self.s1.pk, self.t.pk),
},
{
'summary': 'Spectacle 2',
'dtstart': (get_dt_from_ical, (
(self.now + timedelta(days=2)).replace(microsecond=0)
)),
'dtend': (get_dt_from_ical, (
(self.now + timedelta(days=2, hours=2)).replace(
microsecond=0)
)),
'location': 'Location',
'uid': 'show-{}-{}@example.com'.format(self.s2.pk, self.t.pk),
},
])