123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- from django.contrib.auth.models import User
- from django.core.urlresolvers import reverse
- from django.test import TestCase
- from django.contrib.contenttypes.models import ContentType
- from django.utils import timezone
- from django.core.management import call_command
- from django.core.management.base import CommandError
- from io import StringIO
- from adhesions.models import Adhesion, Corporation
- from .models import Service, ServiceType, IPResource, ResourceAllocation
- class ServicesMixin:
- def setUp(self):
- user_ct = ContentType.objects.get_for_model(User)
- corp_ct = ContentType.objects.get_for_model(Corporation)
- user1 = User.objects.create_user('user1', email='user1@example.net', password='user1')
- adh_u1 = Adhesion.objects.create(adherent_type=user_ct, adherent_id=user1.pk)
- corp1 = Corporation.objects.create(social_reason='Corp 1')
- corp1.members.add(user1)
- adh_c1 = Adhesion.objects.create(adherent_type=corp_ct, adherent_id=corp1.pk)
- user2 = User.objects.create_user('user2', email='user2@example.net', password='user2', is_superuser=True)
- adh_u2 = Adhesion.objects.create(adherent_type=user_ct, adherent_id=user2.pk)
- corp2 = Corporation.objects.create(social_reason='Corp 2')
- corp2.members.add(user2)
- adh_c2 = Adhesion.objects.create(adherent_type=corp_ct, adherent_id=corp2.pk)
- stype1 = ServiceType.objects.create(name='VM')
- stype2 = ServiceType.objects.create(name='Abo Toulouse')
- stype3 = ServiceType.objects.create(name='Abo Castre')
- s1 = Service.objects.create(adhesion=adh_u1, service_type=stype1, label='Service 1')
- s2 = Service.objects.create(adhesion=adh_u2, service_type=stype1, label='Service 2')
- s3 = Service.objects.create(adhesion=adh_c1, service_type=stype2, label='Service 3')
- s4 = Service.objects.create(adhesion=adh_c2, service_type=stype1, label='Service 4', active=False)
- ip1 = IPResource.objects.create(ip='91.224.148.1')
- ResourceAllocation.objects.create(resource=ip1, service=s1, start=timezone.now())
- ip2 = IPResource.objects.create(ip='91.224.148.2')
- ip3 = IPResource.objects.create(ip='91.224.148.3', mask=32)
- class ViewsTestCase(ServicesMixin, TestCase):
- def test_home_service_list(self):
- self.client.login(username='user1', password='user1')
- response = self.client.get(reverse('adhesion-detail-user'))
- self.assertContains(response, 'Service 1')
- self.assertNotContains(response, 'Service 2')
- self.assertNotContains(response, 'Service 3')
- self.assertNotContains(response, 'Service 4')
- def test_service_list(self):
- self.client.login(username='user1', password='user1')
- response = self.client.get(reverse('service-list'))
- self.assertRedirects(response, reverse('login') + '?next=' + reverse('service-list'))
- self.client.login(username='user2', password='user2')
- response = self.client.get(reverse('service-list'))
- self.assertEqual(response.status_code, 200)
- self.assertContains(response, 'Service 1')
- self.assertContains(response, 'Service 2')
- self.assertContains(response, 'Service 3')
- self.assertContains(response, 'Service 4')
- def test_service_detail(self):
- s1 = Service.objects.get(label='Service 1')
- s2 = Service.objects.get(label='Service 2')
- s3 = Service.objects.get(label='Service 3')
- s4 = Service.objects.get(label='Service 4')
- # L’utilisateur lambda n’a accès qu’aux services 1 et 3
- self.client.login(username='user1', password='user1')
- response = self.client.get(reverse('service-detail', kwargs={'pk': s1.pk}))
- self.assertEqual(response.status_code, 200)
- response = self.client.get(reverse('service-detail', kwargs={'pk': s2.pk}))
- self.assertEqual(response.status_code, 404)
- response = self.client.get(reverse('service-detail', kwargs={'pk': s3.pk}))
- self.assertEqual(response.status_code, 200)
- response = self.client.get(reverse('service-detail', kwargs={'pk': s4.pk}))
- self.assertEqual(response.status_code, 404)
- # L’admin a accès à tous les services
- self.client.login(username='user2', password='user2')
- response = self.client.get(reverse('service-detail', kwargs={'pk': s1.pk}))
- self.assertEqual(response.status_code, 200)
- response = self.client.get(reverse('service-detail', kwargs={'pk': s2.pk}))
- self.assertEqual(response.status_code, 200)
- response = self.client.get(reverse('service-detail', kwargs={'pk': s3.pk}))
- self.assertEqual(response.status_code, 200)
- response = self.client.get(reverse('service-detail', kwargs={'pk': s4.pk}))
- self.assertEqual(response.status_code, 200)
- def test_ipresource_allocate(self):
- ip1 = IPResource.objects.get(ip='91.224.148.1')
- url1 = reverse('ip-allocate', kwargs={'pk': ip1.pk})
- ip2 = IPResource.objects.get(ip='91.224.148.2')
- url2 = reverse('ip-allocate', kwargs={'pk': ip2.pk})
- self.client.login(username='user1', password='user1')
- response = self.client.get(url1)
- self.assertRedirects(response, reverse('login') + '?next=' + url1)
- self.client.login(username='user2', password='user2')
- response = self.client.get(url1)
- self.assertEqual(response.status_code, 410)
- response = self.client.get(url2)
- self.assertEqual(response.status_code, 200)
- def test_service_allocate(self):
- s1 = Service.objects.get(label='Service 1')
- url = reverse('service-allocate', kwargs={'pk': s1.pk})
- self.client.login(username='user1', password='user1')
- response = self.client.get(url)
- self.assertRedirects(response, reverse('login') + '?next=' + url)
- self.client.login(username='user2', password='user2')
- response = self.client.get(url)
- self.assertEqual(response.status_code, 200)
- self.assertNotContains(response, '91.224.148.1') # in use
- self.assertContains(response, '91.224.148.2')
- self.assertContains(response, '91.224.148.3')
- def test_deallocate(self):
- ip1 = IPResource.objects.get(ip='91.224.148.1')
- ip2 = IPResource.objects.get(ip='91.224.148.2')
- allocation = ResourceAllocation.objects.get(resource=ip1)
- self.assertTrue(ip1.in_use)
- self.assertFalse(ip2.in_use)
- self.assertTrue(allocation.active)
- url = reverse('deallocate', kwargs={'pk': allocation.pk})
- self.client.login(username='user1', password='user1')
- response = self.client.get(url)
- self.assertRedirects(response, reverse('login') + '?next=' + url)
- self.client.login(username='user2', password='user2')
- response = self.client.get(url)
- self.assertEqual(response.status_code, 405)
- response = self.client.post(url)
- self.assertRedirects(response, reverse('ip-detail', kwargs={'pk': ip1.pk}))
- self.assertFalse(ip1.in_use)
- self.assertFalse(allocation.active)
- response = self.client.post(url)
- self.assertEqual(response.status_code, 403)
- class ServicesCommandTestCase(ServicesMixin, TestCase):
- def test_stats(self):
- out = StringIO()
- call_command("services", "stats", stdout=out)
- result = out.getvalue()
- self.assertRegex(result, "VM")
- self.assertRegex(result, "Abo Toulouse")
- def test_list(self):
- out = StringIO()
- call_command("services", "list", stdout=out)
- result = out.getvalue()
- self.assertRegex(result, "Service 1")
- self.assertRegex(result, "Service 4")
- out = StringIO()
- call_command("services", "list", "--active", stdout=out)
- result = out.getvalue()
- self.assertRegex(result, "Service 1")
- self.assertNotRegex(result, "Service 4")
- out = StringIO()
- call_command("services", "list", "--inactive", stdout=out)
- result = out.getvalue()
- self.assertNotRegex(result, "Service 1")
- self.assertRegex(result, "Service 4")
- out = StringIO()
- call_command("services", "list", "--type", "VM", stdout=out)
- result = out.getvalue()
- self.assertRegex(result, "Service 1")
- self.assertNotRegex(result, "Service 3")
- def test_show(self):
- pass
- def test_add(self):
- pass
- def test_delete(self):
- pass
- class ServiceTypesCommandTestCase(ServicesMixin, TestCase):
- def test_list(self):
- out = StringIO()
- call_command("servicetypes", "list", stdout=out)
- result = out.getvalue()
- self.assertRegex(result, "VM")
- def test_add(self):
- self.assertRaisesRegex(CommandError, "type de service .* existe déjà", call_command, "servicetypes", "add", "VM", stdout=None)
- out = StringIO()
- call_command("servicetypes", "add", "Abo Muret", stdout=out)
- result = out.getvalue()
- self.assertRegex(result, "succès")
- st = ServiceType.objects.get(name="Abo Muret")
- def test_change(self):
- st_old = ServiceType.objects.get(name="Abo Toulouse")
- self.assertRaisesRegex(CommandError, "type de service .* existe déjà", call_command, "servicetypes", "change", "Abo Toulouse", "VM", stdout=None)
- self.assertRaisesRegex(CommandError, "type de service .* n’existe pas", call_command, "servicetypes", "change", "Abo Muret", "Abo Auch", stdout=None)
- out = StringIO()
- call_command("servicetypes", "change", "Abo Toulouse", "Abo Muret", stdout=out)
- result = out.getvalue()
- self.assertRegex(result, "renommé")
- st_new = ServiceType.objects.get(name="Abo Muret")
- self.assertEquals(st_old.pk, st_new.pk)
- def test_delete(self):
- self.assertRaisesRegex(CommandError, "suppression refusé", call_command, "servicetypes", "delete", "VM", stdout=None)
- self.assertRaisesRegex(CommandError, "type de service .* n’existe pas", call_command, "servicetypes", "delete", "Abo Paris", stdout=None)
- out = StringIO()
- call_command("servicetypes", "delete", "Abo Castre", stdout=out)
- result = out.getvalue()
- self.assertRegex(result, "supprimé")
- ServiceType.objects.get(name="VM")
- self.assertRaises(ServiceType.DoesNotExist, ServiceType.objects.get, name="Abo Castre")
|