from django.test import Client, TestCase from django.urls import reverse from gestioncof.models import Club 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())