Commit 8e8c9a09 authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

test service long stopped

parent 86acc2b3
......@@ -12,5 +12,5 @@ Système d’Information (SI) du FAI associatif tetaneutral.net
## Local test
```
docker compose up --build --exit-code-from tests
docker compose up --exit-code-from tests
```
#!/bin/bash -eux
#!/bin/bash -eu
BASEDIR="$(dirname "$0")"
......@@ -10,6 +10,8 @@ then
pip install coverage
fi
set -x
coverage run --branch --source=djadhere,accounts,adhesions,banking,services,stocking,todo --omit=*/migrations/*.py "$BASEDIR"/manage.py test
coverage html
coverage report
from io import StringIO
from datetime import datetime, timedelta
from django.db.models import Q
from django.urls import reverse
from django.test import TestCase
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 Corporation, User
from banking.models import RecurringPayment, PaymentUpdate
from .models import Service, ServiceType, IPResource, ServiceAllocation, Route
......@@ -214,3 +218,84 @@ class ServiceTypesCommandTestCase(ServicesMixin, TestCase):
self.assertRaises(
ServiceType.DoesNotExist, ServiceType.objects.get, name="Abo Castre"
)
class ServiceLongStoppedTestCase(TestCase):
def test_service_long_stopped(self):
one_year_ago = datetime.now() - timedelta(days=365)
two_years_ago = datetime.now() - timedelta(days=365 * 2)
vm = ServiceType.objects.create(name="VM")
User.objects.create_user(
"admin", password="admin", is_staff=True, is_superuser=True
)
old_user = User.objects.create(username="old user", password="old user")
new_user = User.objects.create(username="new user", password="new user")
old_contrib = RecurringPayment.objects.create()
new_contrib = RecurringPayment.objects.create()
PaymentUpdate.objects.create(
payment=old_contrib,
amount=0,
period=1,
payment_method=PaymentUpdate.STOP,
start=two_years_ago,
validated=True,
)
PaymentUpdate.objects.create(
payment=new_contrib,
amount=4.2,
period=1,
start=datetime.now(),
validated=True,
)
old_service = Service.objects.create(
adhesion=old_user.adhesion,
service_type=vm,
contribution=old_contrib,
label="old service",
)
Service.objects.create(
adhesion=new_user.adhesion,
service_type=vm,
contribution=new_contrib,
label="new service",
)
# checks old service should trigger the "long_stopped" annotation
self.assertIsNotNone(old_service.contribution)
self.assertTrue(
RecurringPayment.objects.filter(
service=old_service, long_stopped=True
).exists()
)
self.assertFalse(
ServiceAllocation.objects.filter(
Q(service=old_service)
& (Q(end__isnull=True) | Q(end__gte=one_year_ago))
).exists()
)
# everything else should be trivial now
self.assertEqual(Service.objects.filter(long_stopped=True).count(), 1)
self.assertEqual(Service.objects.filter(long_stopped=False).count(), 1)
self.assertEqual(Service.objects.get(long_stopped=True).label, "old service")
self.assertEqual(Service.objects.get(long_stopped=False).label, "new service")
self.client.login(username="admin", password="admin")
all_response = self.client.get("/admin/services/service/")
old_response = self.client.get("/admin/services/service/", {"deletable": "1"})
new_response = self.client.get("/admin/services/service/", {"deletable": "0"})
self.assertEqual(all_response.status_code, 200)
self.assertEqual(old_response.status_code, 200)
self.assertEqual(new_response.status_code, 200)
self.assertIn("old user", all_response.content.decode())
self.assertIn("new user", all_response.content.decode())
self.assertIn("old user", old_response.content.decode())
self.assertIn("new user", new_response.content.decode())
self.assertNotIn("new user", old_response.content.decode())
self.assertNotIn("old user", new_response.content.decode())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment