tests.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from django.test import TestCase
  2. from django.contrib.auth.models import User
  3. from django.contrib.contenttypes.models import ContentType
  4. from django.core.urlresolvers import reverse
  5. from django.core.management import call_command
  6. from django.core.management.base import CommandError
  7. from io import StringIO
  8. from os import devnull
  9. from .models import Corporation, Adhesion
  10. class AdhesionsMixin:
  11. def setUp(self):
  12. admin = User.objects.create_user('admin', email='admin@example.net', password='admin', is_superuser=True)
  13. user = User.objects.create_user('user', first_name='first', last_name='last', email='user@example.net', password='user')
  14. adh1 = Adhesion.objects.create(adherent_type=ContentType.objects.get_for_model(User), adherent_id=user.pk)
  15. corp1 = Corporation.objects.create(social_reason='GoodCorp')
  16. corp1.members.add(user)
  17. corp2 = Corporation.objects.create(social_reason='EvilCorp')
  18. adh2 = Adhesion.objects.create(adherent_type=ContentType.objects.get_for_model(Corporation), adherent_id=corp1.pk)
  19. class ViewsTestCase(AdhesionsMixin, TestCase):
  20. def test_adhesion_backend(self):
  21. user = User.objects.get(username='user')
  22. adhesion = user.profile.adhesion
  23. self.assertFalse(self.client.login(username='%d' % adhesion.pk, password='wrong'))
  24. self.assertFalse(self.client.login(username='ADT%d' % adhesion.pk, password='wrong'))
  25. self.assertFalse(self.client.login(username='9999', password='user'))
  26. self.assertFalse(self.client.login(username='ADT9999', password='user'))
  27. self.assertTrue(self.client.login(username='%d' % adhesion.pk, password='user'))
  28. self.assertTrue(self.client.login(username='ADT%d' % adhesion.pk, password='user'))
  29. def test_user(self):
  30. response = self.client.get(reverse('adhesion-detail-user'))
  31. self.assertRedirects(response, reverse('login') + '?next=' + reverse('adhesion-detail-user'))
  32. self.client.login(username='user', password='user')
  33. response = self.client.get(reverse('adhesion-detail-user'))
  34. self.assertEqual(response.status_code, 200)
  35. def test_corporation(self):
  36. user = User.objects.get(username='user')
  37. corp = Corporation.objects.get(social_reason='EvilCorp')
  38. url = reverse('corporation-detail-user', kwargs={'pk': corp.pk})
  39. response = self.client.get(url)
  40. self.assertRedirects(response, reverse('login') + '?next=' + url)
  41. self.client.login(username='user', password='user')
  42. response = self.client.get(url)
  43. self.assertEqual(response.status_code, 403)
  44. corp.members.add(user)
  45. response = self.client.get(url)
  46. self.assertEqual(response.status_code, 200)
  47. self.assertContains(response, '')
  48. def test_corporation_menu(self):
  49. self.client.login(username='user', password='user')
  50. response = self.client.get('/')
  51. self.assertContains(response, 'GoodCorp')
  52. self.assertNotContains(response, 'EvilCorp')
  53. def test_users_admin(self):
  54. self.client.login(username='admin', password='admin')
  55. response = self.client.get(reverse('user-list'))
  56. self.assertEqual(response.status_code, 200)
  57. self.assertContains(response, 'admin')
  58. self.assertContains(response, 'user')
  59. response = self.client.get(reverse('user-add'))
  60. self.assertEqual(response.status_code, 200)
  61. response = self.client.get(reverse('user-detail', kwargs={'pk': 1}))
  62. self.assertEqual(response.status_code, 200)
  63. response = self.client.get(reverse('user-edit', kwargs={'pk': 1}))
  64. self.assertEqual(response.status_code, 200)
  65. def test_corporations_admin(self):
  66. self.client.login(username='admin', password='admin')
  67. response = self.client.get(reverse('corporation-list'))
  68. self.assertEqual(response.status_code, 200)
  69. self.assertContains(response, 'GoodCorp')
  70. self.assertContains(response, 'EvilCorp')
  71. response = self.client.get(reverse('corporation-add'))
  72. self.assertEqual(response.status_code, 200)
  73. response = self.client.get(reverse('corporation-detail', kwargs={'pk': 1}))
  74. self.assertEqual(response.status_code, 200)
  75. response = self.client.get(reverse('corporation-edit', kwargs={'pk': 1}))
  76. self.assertEqual(response.status_code, 200)
  77. def test_adhesions_admin(self):
  78. self.client.login(username='admin', password='admin')
  79. response = self.client.get(reverse('adhesion-list'))
  80. self.assertEqual(response.status_code, 200)
  81. self.assertContains(response, 'user')
  82. response = self.client.get(reverse('adhesion-detail', kwargs={'pk': 1}))
  83. self.assertEqual(response.status_code, 200)
  84. def test_adhesion_model(self):
  85. user = User.objects.get(username='user')
  86. adh1 = Adhesion.objects.get(adherent_type=ContentType.objects.get_for_model(User), adherent_id=user.pk)
  87. self.assertEquals(user.profile.adhesion, adh1)
  88. self.assertTrue(adh1.is_physical())
  89. self.assertFalse(adh1.is_moral())
  90. self.assertEquals(adh1.get_adherent_name(), 'first last')
  91. corp = Corporation.objects.get(social_reason='GoodCorp')
  92. adh2 = Adhesion.objects.get(adherent_type=ContentType.objects.get_for_model(Corporation), adherent_id=corp.pk)
  93. self.assertEquals(corp.adhesion, adh2)
  94. self.assertFalse(adh2.is_physical())
  95. self.assertTrue(adh2.is_moral())
  96. self.assertEquals(adh2.get_adherent_name(), 'GoodCorp')
  97. def test_user_creation(self):
  98. url = reverse('user-add')
  99. self.client.login(username='user', password='user')
  100. response = self.client.get(url)
  101. self.assertRedirects(response, reverse('login') + '?next=' + url)
  102. self.client.login(username='admin', password='admin')
  103. response = self.client.get(url)
  104. self.assertEqual(response.status_code, 200)
  105. def test_user_update(self):
  106. user = User.objects.get(username='user')
  107. url = reverse('user-edit', kwargs={'pk': user.pk})
  108. self.client.login(username='user', password='user')
  109. response = self.client.get(url)
  110. self.assertRedirects(response, reverse('login') + '?next=' + url)
  111. self.client.login(username='admin', password='admin')
  112. response = self.client.get(url)
  113. self.assertEqual(response.status_code, 200)
  114. def test_corp_creation(self):
  115. url = reverse('corporation-add')
  116. self.client.login(username='user', password='user')
  117. response = self.client.get(url)
  118. self.assertRedirects(response, reverse('login') + '?next=' + url)
  119. self.client.login(username='admin', password='admin')
  120. response = self.client.get(url)
  121. self.assertEqual(response.status_code, 200)
  122. def test_corp_update(self):
  123. corp = Corporation.objects.get(social_reason='EvilCorp')
  124. url = reverse('corporation-edit', kwargs={'pk': corp.pk})
  125. self.client.login(username='user', password='user')
  126. response = self.client.get(url)
  127. self.assertRedirects(response, reverse('login') + '?next=' + url)
  128. self.client.login(username='admin', password='admin')
  129. response = self.client.get(url)
  130. self.assertEqual(response.status_code, 200)
  131. class CommandsTestCase(AdhesionsMixin, TestCase):
  132. def test_list(self):
  133. out = StringIO()
  134. call_command("adherents", "list", stdout=out)
  135. result = out.getvalue()
  136. self.assertNotRegex(result, "admin") # non adhérent
  137. self.assertRegex(result, "first last")
  138. self.assertRegex(result, "GoodCorp")
  139. self.assertNotRegex(result, "EvilCorp") # non adhérent
  140. out = StringIO()
  141. call_command("adherents", "list", "--physique", stdout=out)
  142. result = out.getvalue()
  143. self.assertRegex(result, "first last")
  144. self.assertNotRegex(result, "GoodCorp")
  145. out = StringIO()
  146. call_command("adherents", "list", "--morale", stdout=out)
  147. result = out.getvalue()
  148. self.assertNotRegex(result, "first last")
  149. self.assertRegex(result, "GoodCorp")
  150. def test_create_personne_physique(self):
  151. user = User.objects.first()
  152. adh = Adhesion.objects.first()
  153. args = ["adherents", "create", "--id", "%d" % adh.pk, "physique", "--login", "user2", "--firstname", "first2", "--lastname", "last2"]
  154. kwargs = {'stdout': open(devnull, 'w')}
  155. self.assertRaisesRegex(CommandError, "numéro .* déjà attribué", call_command, *args, **kwargs)
  156. args[args.index("--id") + 1] = "1000"
  157. args[args.index("--login") + 1] = user.username
  158. self.assertRaisesRegex(CommandError, "nom d’utilisateur .* déjà utilisé", call_command, *args, **kwargs)
  159. args[args.index("--login") + 1] = "user2"
  160. call_command(*args, **kwargs)
  161. user = User.objects.get(username='user2')
  162. adh = Adhesion.objects.get(adherent_type=ContentType.objects.get_for_model(User), adherent_id=user.pk)
  163. self.assertEquals(adh.id, 1000)
  164. def test_create_personne_morale(self):
  165. corp = Corporation.objects.first()
  166. adh = Adhesion.objects.first()
  167. args = ["adherents", "create", "--id", "%d" % adh.pk, "morale", "--name", "NewCorp"]
  168. kwargs = {'stdout': open(devnull, 'w')}
  169. self.assertRaisesRegex(CommandError, "numéro .* déjà attribué", call_command, *args, **kwargs)
  170. args[args.index("--id") + 1] = "1000"
  171. args[args.index("--name") + 1] = corp.social_reason
  172. self.assertRaisesRegex(CommandError, "raison sociale .* déjà utilisé", call_command, *args, **kwargs)
  173. args[args.index("--name") + 1] = "NewCorp"
  174. call_command(*args, **kwargs)
  175. corp = Corporation.objects.get(social_reason='NewCorp')
  176. adh = Adhesion.objects.get(adherent_type=ContentType.objects.get_for_model(Corporation), adherent_id=corp.pk)
  177. self.assertEquals(adh.id, 1000)