tests.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from django.contrib.auth.models import User
  2. from django.core.urlresolvers import reverse
  3. from django.test import TestCase
  4. from django.contrib.contenttypes.models import ContentType
  5. from django.utils import timezone
  6. from django.core.management import call_command
  7. from django.core.management.base import CommandError
  8. from io import StringIO
  9. from adhesions.models import Adhesion, Corporation
  10. from .models import Service, ServiceType, IPResource, ResourceAllocation
  11. class ServicesMixin:
  12. def setUp(self):
  13. user_ct = ContentType.objects.get_for_model(User)
  14. corp_ct = ContentType.objects.get_for_model(Corporation)
  15. user1 = User.objects.create_user('user1', email='user1@example.net', password='user1')
  16. adh_u1 = Adhesion.objects.create(adherent_type=user_ct, adherent_id=user1.pk)
  17. corp1 = Corporation.objects.create(social_reason='Corp 1')
  18. corp1.members.add(user1)
  19. adh_c1 = Adhesion.objects.create(adherent_type=corp_ct, adherent_id=corp1.pk)
  20. user2 = User.objects.create_user('user2', email='user2@example.net', password='user2', is_superuser=True)
  21. adh_u2 = Adhesion.objects.create(adherent_type=user_ct, adherent_id=user2.pk)
  22. corp2 = Corporation.objects.create(social_reason='Corp 2')
  23. corp2.members.add(user2)
  24. adh_c2 = Adhesion.objects.create(adherent_type=corp_ct, adherent_id=corp2.pk)
  25. stype1 = ServiceType.objects.create(name='VM')
  26. stype2 = ServiceType.objects.create(name='Abo Toulouse')
  27. stype3 = ServiceType.objects.create(name='Abo Castre')
  28. s1 = Service.objects.create(adhesion=adh_u1, service_type=stype1, label='Service 1')
  29. s2 = Service.objects.create(adhesion=adh_u2, service_type=stype1, label='Service 2')
  30. s3 = Service.objects.create(adhesion=adh_c1, service_type=stype2, label='Service 3')
  31. s4 = Service.objects.create(adhesion=adh_c2, service_type=stype1, label='Service 4', active=False)
  32. ip1 = IPResource.objects.create(ip='91.224.148.1')
  33. ResourceAllocation.objects.create(resource=ip1, service=s1, start=timezone.now())
  34. ip2 = IPResource.objects.create(ip='91.224.148.2')
  35. ip3 = IPResource.objects.create(ip='91.224.148.3', mask=32)
  36. class ViewsTestCase(ServicesMixin, TestCase):
  37. def test_home_service_list(self):
  38. self.client.login(username='user1', password='user1')
  39. response = self.client.get(reverse('adhesion-detail-user'))
  40. self.assertContains(response, 'Service 1')
  41. self.assertNotContains(response, 'Service 2')
  42. self.assertNotContains(response, 'Service 3')
  43. self.assertNotContains(response, 'Service 4')
  44. def test_service_list(self):
  45. self.client.login(username='user1', password='user1')
  46. response = self.client.get(reverse('service-list'))
  47. self.assertRedirects(response, reverse('login') + '?next=' + reverse('service-list'))
  48. self.client.login(username='user2', password='user2')
  49. response = self.client.get(reverse('service-list'))
  50. self.assertEqual(response.status_code, 200)
  51. self.assertContains(response, 'Service 1')
  52. self.assertContains(response, 'Service 2')
  53. self.assertContains(response, 'Service 3')
  54. self.assertContains(response, 'Service 4')
  55. def test_service_detail(self):
  56. s1 = Service.objects.get(label='Service 1')
  57. s2 = Service.objects.get(label='Service 2')
  58. s3 = Service.objects.get(label='Service 3')
  59. s4 = Service.objects.get(label='Service 4')
  60. # L’utilisateur lambda n’a accès qu’aux services 1 et 3
  61. self.client.login(username='user1', password='user1')
  62. response = self.client.get(reverse('service-detail', kwargs={'pk': s1.pk}))
  63. self.assertEqual(response.status_code, 200)
  64. response = self.client.get(reverse('service-detail', kwargs={'pk': s2.pk}))
  65. self.assertEqual(response.status_code, 404)
  66. response = self.client.get(reverse('service-detail', kwargs={'pk': s3.pk}))
  67. self.assertEqual(response.status_code, 200)
  68. response = self.client.get(reverse('service-detail', kwargs={'pk': s4.pk}))
  69. self.assertEqual(response.status_code, 404)
  70. # L’admin a accès à tous les services
  71. self.client.login(username='user2', password='user2')
  72. response = self.client.get(reverse('service-detail', kwargs={'pk': s1.pk}))
  73. self.assertEqual(response.status_code, 200)
  74. response = self.client.get(reverse('service-detail', kwargs={'pk': s2.pk}))
  75. self.assertEqual(response.status_code, 200)
  76. response = self.client.get(reverse('service-detail', kwargs={'pk': s3.pk}))
  77. self.assertEqual(response.status_code, 200)
  78. response = self.client.get(reverse('service-detail', kwargs={'pk': s4.pk}))
  79. self.assertEqual(response.status_code, 200)
  80. def test_ipresource_allocate(self):
  81. ip1 = IPResource.objects.get(ip='91.224.148.1')
  82. url1 = reverse('ip-allocate', kwargs={'pk': ip1.pk})
  83. ip2 = IPResource.objects.get(ip='91.224.148.2')
  84. url2 = reverse('ip-allocate', kwargs={'pk': ip2.pk})
  85. self.client.login(username='user1', password='user1')
  86. response = self.client.get(url1)
  87. self.assertRedirects(response, reverse('login') + '?next=' + url1)
  88. self.client.login(username='user2', password='user2')
  89. response = self.client.get(url1)
  90. self.assertEqual(response.status_code, 410)
  91. response = self.client.get(url2)
  92. self.assertEqual(response.status_code, 200)
  93. def test_service_allocate(self):
  94. s1 = Service.objects.get(label='Service 1')
  95. url = reverse('service-allocate', kwargs={'pk': s1.pk})
  96. self.client.login(username='user1', password='user1')
  97. response = self.client.get(url)
  98. self.assertRedirects(response, reverse('login') + '?next=' + url)
  99. self.client.login(username='user2', password='user2')
  100. response = self.client.get(url)
  101. self.assertEqual(response.status_code, 200)
  102. self.assertNotContains(response, '91.224.148.1') # in use
  103. self.assertContains(response, '91.224.148.2')
  104. self.assertContains(response, '91.224.148.3')
  105. def test_deallocate(self):
  106. ip1 = IPResource.objects.get(ip='91.224.148.1')
  107. ip2 = IPResource.objects.get(ip='91.224.148.2')
  108. allocation = ResourceAllocation.objects.get(resource=ip1)
  109. self.assertTrue(ip1.in_use)
  110. self.assertFalse(ip2.in_use)
  111. self.assertTrue(allocation.active)
  112. url = reverse('deallocate', kwargs={'pk': allocation.pk})
  113. self.client.login(username='user1', password='user1')
  114. response = self.client.get(url)
  115. self.assertRedirects(response, reverse('login') + '?next=' + url)
  116. self.client.login(username='user2', password='user2')
  117. response = self.client.get(url)
  118. self.assertEqual(response.status_code, 405)
  119. response = self.client.post(url)
  120. self.assertRedirects(response, reverse('ip-detail', kwargs={'pk': ip1.pk}))
  121. self.assertFalse(ip1.in_use)
  122. self.assertFalse(allocation.active)
  123. response = self.client.post(url)
  124. self.assertEqual(response.status_code, 403)
  125. class ServicesCommandTestCase(ServicesMixin, TestCase):
  126. def test_stats(self):
  127. out = StringIO()
  128. call_command("services", "stats", stdout=out)
  129. result = out.getvalue()
  130. self.assertRegex(result, "VM")
  131. self.assertRegex(result, "Abo Toulouse")
  132. def test_list(self):
  133. out = StringIO()
  134. call_command("services", "list", stdout=out)
  135. result = out.getvalue()
  136. self.assertRegex(result, "Service 1")
  137. self.assertRegex(result, "Service 4")
  138. out = StringIO()
  139. call_command("services", "list", "--active", stdout=out)
  140. result = out.getvalue()
  141. self.assertRegex(result, "Service 1")
  142. self.assertNotRegex(result, "Service 4")
  143. out = StringIO()
  144. call_command("services", "list", "--inactive", stdout=out)
  145. result = out.getvalue()
  146. self.assertNotRegex(result, "Service 1")
  147. self.assertRegex(result, "Service 4")
  148. out = StringIO()
  149. call_command("services", "list", "--type", "VM", stdout=out)
  150. result = out.getvalue()
  151. self.assertRegex(result, "Service 1")
  152. self.assertNotRegex(result, "Service 3")
  153. def test_show(self):
  154. pass
  155. def test_add(self):
  156. pass
  157. def test_delete(self):
  158. pass
  159. class ServiceTypesCommandTestCase(ServicesMixin, TestCase):
  160. def test_list(self):
  161. out = StringIO()
  162. call_command("servicetypes", "list", stdout=out)
  163. result = out.getvalue()
  164. self.assertRegex(result, "VM")
  165. def test_add(self):
  166. self.assertRaisesRegex(CommandError, "type de service .* existe déjà", call_command, "servicetypes", "add", "VM", stdout=None)
  167. out = StringIO()
  168. call_command("servicetypes", "add", "Abo Muret", stdout=out)
  169. result = out.getvalue()
  170. self.assertRegex(result, "succès")
  171. st = ServiceType.objects.get(name="Abo Muret")
  172. def test_change(self):
  173. st_old = ServiceType.objects.get(name="Abo Toulouse")
  174. self.assertRaisesRegex(CommandError, "type de service .* existe déjà", call_command, "servicetypes", "change", "Abo Toulouse", "VM", stdout=None)
  175. self.assertRaisesRegex(CommandError, "type de service .* n’existe pas", call_command, "servicetypes", "change", "Abo Muret", "Abo Auch", stdout=None)
  176. out = StringIO()
  177. call_command("servicetypes", "change", "Abo Toulouse", "Abo Muret", stdout=out)
  178. result = out.getvalue()
  179. self.assertRegex(result, "renommé")
  180. st_new = ServiceType.objects.get(name="Abo Muret")
  181. self.assertEquals(st_old.pk, st_new.pk)
  182. def test_delete(self):
  183. self.assertRaisesRegex(CommandError, "suppression refusé", call_command, "servicetypes", "delete", "VM", stdout=None)
  184. self.assertRaisesRegex(CommandError, "type de service .* n’existe pas", call_command, "servicetypes", "delete", "Abo Paris", stdout=None)
  185. out = StringIO()
  186. call_command("servicetypes", "delete", "Abo Castre", stdout=out)
  187. result = out.getvalue()
  188. self.assertRegex(result, "supprimé")
  189. ServiceType.objects.get(name="VM")
  190. self.assertRaises(ServiceType.DoesNotExist, ServiceType.objects.get, name="Abo Castre")