forked from DGNum/gestioCOF
Merge branch 'aureplop/cof-tests_club' into 'master'
cof -- Add tests for club views See merge request cof-geek/gestioCOF!289
This commit is contained in:
commit
2d14296c47
2 changed files with 123 additions and 3 deletions
|
@ -4,12 +4,132 @@ 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 django.test import Client, TestCase
|
||||
from django.urls import reverse
|
||||
|
||||
from bda.models import Salle, Tirage
|
||||
from gestioncof.models import CalendarSubscription, Event, Survey, SurveyAnswer
|
||||
from gestioncof.models import (
|
||||
CalendarSubscription, Club, Event, Survey, SurveyAnswer
|
||||
)
|
||||
from gestioncof.tests.testcases import ViewTestCaseMixin
|
||||
|
||||
from .utils import create_user
|
||||
|
||||
|
||||
class ClubListViewTests(ViewTestCaseMixin, TestCase):
|
||||
url_name = 'liste-clubs'
|
||||
url_expected = '/clubs/liste'
|
||||
|
||||
auth_user = 'member'
|
||||
auth_forbidden = [None, 'user']
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
||||
self.c1 = Club.objects.create(name='Club1')
|
||||
self.c2 = Club.objects.create(name='Club2')
|
||||
|
||||
m = self.users['member']
|
||||
self.c1.membres.add(m)
|
||||
self.c1.respos.add(m)
|
||||
|
||||
def test_as_member(self):
|
||||
r = self.client.get(self.url)
|
||||
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(r.context['owned_clubs'].get(), self.c1)
|
||||
self.assertEqual(r.context['other_clubs'].get(), self.c2)
|
||||
|
||||
def test_as_staff(self):
|
||||
u = self.users['staff']
|
||||
c = Client()
|
||||
c.force_login(u)
|
||||
|
||||
r = c.get(self.url)
|
||||
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertQuerysetEqual(
|
||||
r.context['owned_clubs'], map(repr, [self.c1, self.c2]),
|
||||
ordered=False,
|
||||
)
|
||||
|
||||
|
||||
class ClubMembersViewTests(ViewTestCaseMixin, TestCase):
|
||||
url_name = 'membres-club'
|
||||
|
||||
auth_user = 'staff'
|
||||
auth_forbidden = [None, 'user', 'member']
|
||||
|
||||
@property
|
||||
def url_kwargs(self):
|
||||
return {'name': self.c.name}
|
||||
|
||||
@property
|
||||
def url_expected(self):
|
||||
return '/clubs/membres/{}'.format(self.c.name)
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
||||
self.u1 = create_user('u1')
|
||||
self.u2 = create_user('u2')
|
||||
|
||||
self.c = Club.objects.create(name='Club')
|
||||
self.c.membres.add(self.u1, self.u2)
|
||||
self.c.respos.add(self.u1)
|
||||
|
||||
def test_as_staff(self):
|
||||
r = self.client.get(self.url)
|
||||
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(r.context['members_no_respo'].get(), self.u2)
|
||||
|
||||
def test_as_respo(self):
|
||||
u = self.users['user']
|
||||
self.c.respos.add(u)
|
||||
|
||||
c = Client()
|
||||
c.force_login(u)
|
||||
r = c.get(self.url)
|
||||
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
|
||||
class ClubChangeRespoViewTests(ViewTestCaseMixin, TestCase):
|
||||
url_name = 'change-respo'
|
||||
|
||||
auth_user = 'staff'
|
||||
auth_forbidden = [None, 'user', 'member']
|
||||
|
||||
@property
|
||||
def url_kwargs(self):
|
||||
return {'club_name': self.c.name, 'user_id': self.users['user'].pk}
|
||||
|
||||
@property
|
||||
def url_expected(self):
|
||||
return '/clubs/change_respo/{}/{}'.format(
|
||||
self.c.name, self.users['user'].pk,
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
||||
self.c = Club.objects.create(name='Club')
|
||||
|
||||
def test(self):
|
||||
u = self.users['user']
|
||||
expected_redirect = reverse('membres-club', kwargs={
|
||||
'name': self.c.name,
|
||||
})
|
||||
self.c.membres.add(u)
|
||||
|
||||
r = self.client.get(self.url)
|
||||
self.assertRedirects(r, expected_redirect)
|
||||
self.assertIn(u, self.c.respos.all())
|
||||
|
||||
self.client.get(self.url)
|
||||
self.assertNotIn(u, self.c.respos.all())
|
||||
|
||||
|
||||
class CalendarViewTests(ViewTestCaseMixin, TestCase):
|
||||
url_name = 'calendar'
|
||||
|
|
|
@ -566,7 +566,7 @@ def liste_clubs(request):
|
|||
if request.user.profile.is_buro:
|
||||
data = {'owned_clubs': clubs.all()}
|
||||
else:
|
||||
data = {'owned_clubs': request.user.clubs_geres,
|
||||
data = {'owned_clubs': request.user.clubs_geres.all(),
|
||||
'other_clubs': clubs.exclude(respos=request.user)}
|
||||
return render(request, 'liste_clubs.html', data)
|
||||
|
||||
|
|
Loading…
Reference in a new issue