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

DeletableTestCase

parent 8e8c9a09
......@@ -12,5 +12,7 @@ Système d’Information (SI) du FAI associatif tetaneutral.net
## Local test
```
export UID=$(id -u)
export GID=$(id -g)
docker compose up --exit-code-from tests
```
from datetime import datetime, timedelta
from io import StringIO
from django.test import TestCase
from django.urls import reverse
from django.core.management import call_command
from io import StringIO
from banking.models import RecurringPayment, PaymentUpdate
from services.models import ServiceType, Service, IPResource, ServiceAllocation, Route
from .models import Corporation, Adhesion, User
......@@ -105,3 +109,91 @@ class CommandsTestCase(AdhesionsMixin, TestCase):
result = out.getvalue()
self.assertNotRegex(result, "first last")
self.assertRegex(result, "GoodCorp")
class DeletableTestCase(TestCase):
def test_deletable(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
)
user = User.objects.create(username="user", password="user")
# user just created. it should already have an adhesion. Admin too.
self.assertEqual(Adhesion.objects.count(), 2)
# this adhesion has no membership yet. it should not be "long stopped"
self.assertFalse(Adhesion.objects.filter(long_stopped=True).exists())
# this user started a membership 2y ago, they are still not "long stopped"
membership = RecurringPayment.objects.create()
PaymentUpdate.objects.create(
payment=membership,
amount=20,
period=12,
start=two_years_ago,
validated=True,
)
user.adhesion.membership = membership
user.adhesion.save()
self.assertFalse(Adhesion.objects.filter(long_stopped=True).exists())
# unless they stopped paying more than a year ago
PaymentUpdate.objects.create(
payment=membership,
amount=0,
period=1,
payment_method=PaymentUpdate.STOP,
start=one_year_ago - timedelta(days=10),
validated=True,
)
self.assertTrue(Adhesion.objects.filter(long_stopped=True).exists())
# unless they had a service 2y ago
service = Service.objects.create(
adhesion=user.adhesion,
service_type=vm,
label="service",
)
self.assertFalse(Adhesion.objects.filter(long_stopped=True).exists())
# especially if they were paying for it
contribution = RecurringPayment.objects.create()
PaymentUpdate.objects.create(
payment=contribution,
amount=5,
period=1,
start=two_years_ago,
validated=True,
)
service.contribution = contribution
service.save()
self.assertFalse(Adhesion.objects.filter(long_stopped=True).exists())
# except if they stopped paying mor than a year ago
PaymentUpdate.objects.create(
payment=contribution,
amount=0,
period=1,
payment_method=PaymentUpdate.STOP,
start=one_year_ago - timedelta(days=10),
validated=True,
)
self.assertTrue(Adhesion.objects.filter(long_stopped=True).exists())
# except if that service had ressources
ip = IPResource.objects.create(
ip="91.224.148.168", category=IPResource.CATEGORY_PUBLIC
)
route = Route.objects.create(name="openstack")
allocation = ServiceAllocation.objects.create(
start=two_years_ago, resource=ip, service=service, route=route
)
self.assertFalse(Adhesion.objects.filter(long_stopped=True).exists())
# except if those ressources were freed
allocation.end = one_year_ago - timedelta(days=10)
allocation.save()
self.assertTrue(Adhesion.objects.filter(long_stopped=True).exists())
......@@ -7,6 +7,7 @@ services:
- .env
tests:
build: .
user: "${UID}:${GID}"
env_file:
- .env
volumes:
......
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