Sith/counter/tests/test_account_dump.py

169 lines
6.6 KiB
Python
Raw Permalink Normal View History

2024-11-10 01:55:18 +00:00
from collections.abc import Iterable
2024-10-09 11:12:09 +00:00
from datetime import timedelta
2024-10-06 20:24:20 +00:00
2024-11-10 01:55:18 +00:00
import freezegun
import pytest
2024-10-09 11:12:09 +00:00
from django.conf import settings
from django.core import mail
from django.core.management import call_command
from django.test import TestCase
from django.utils.timezone import now
from model_bakery import baker
from model_bakery.recipe import Recipe
2024-10-06 20:24:20 +00:00
2024-10-09 11:12:09 +00:00
from core.baker_recipes import subscriber_user, very_old_subscriber_user
2024-11-10 01:55:18 +00:00
from counter.management.commands.dump_accounts import Command as DumpCommand
from counter.management.commands.dump_warning_mail import Command as WarningCommand
from counter.models import AccountDump, Customer, Refilling, Selling
from subscription.models import Subscription
2024-10-09 11:12:09 +00:00
2024-11-10 01:55:18 +00:00
class TestAccountDump(TestCase):
2024-10-09 11:12:09 +00:00
@classmethod
2024-11-10 01:55:18 +00:00
def set_up_notified_users(cls):
"""Create the users which should be considered as dumpable"""
2024-10-09 11:12:09 +00:00
cls.notified_users = very_old_subscriber_user.make(_quantity=3)
2024-11-10 01:55:18 +00:00
baker.make(
Refilling,
amount=10,
2024-10-09 11:12:09 +00:00
customer=(u.customer for u in cls.notified_users),
2024-11-10 01:55:18 +00:00
date=now() - settings.SITH_ACCOUNT_INACTIVITY_DELTA - timedelta(days=1),
2024-10-09 11:12:09 +00:00
_quantity=len(cls.notified_users),
)
2024-11-10 01:55:18 +00:00
@classmethod
def set_up_not_notified_users(cls):
"""Create the users which should not be considered as dumpable"""
refill_recipe = Recipe(Refilling, amount=10)
2024-10-09 11:12:09 +00:00
cls.not_notified_users = [
subscriber_user.make(),
very_old_subscriber_user.make(), # inactive, but account already empty
very_old_subscriber_user.make(), # inactive, but with a recent transaction
very_old_subscriber_user.make(), # inactive, but already warned
]
refill_recipe.make(
customer=cls.not_notified_users[2].customer, date=now() - timedelta(days=1)
)
refill_recipe.make(
2024-11-10 01:55:18 +00:00
customer=cls.not_notified_users[3].customer,
date=now() - settings.SITH_ACCOUNT_INACTIVITY_DELTA - timedelta(days=1),
2024-10-09 11:12:09 +00:00
)
baker.make(
AccountDump,
customer=cls.not_notified_users[3].customer,
dump_operation=None,
)
2024-11-10 01:55:18 +00:00
class TestAccountDumpWarningMailCommand(TestAccountDump):
@classmethod
def setUpTestData(cls):
# delete existing accounts to avoid side effect
Customer.objects.all().delete()
cls.set_up_notified_users()
cls.set_up_not_notified_users()
2024-10-09 11:12:09 +00:00
def test_user_selection(self):
"""Test that the user to warn are well selected."""
2024-11-10 01:55:18 +00:00
users = list(WarningCommand._get_users())
assert len(users) == len(self.notified_users)
2024-10-09 11:12:09 +00:00
assert set(users) == set(self.notified_users)
def test_command(self):
"""The actual command test."""
call_command("dump_warning_mail")
# 1 already existing + 3 new account dump objects
assert AccountDump.objects.count() == 4
sent_mails = list(mail.outbox)
assert len(sent_mails) == 3
target_emails = {u.email for u in self.notified_users}
for sent in sent_mails:
assert len(sent.to) == 1
assert sent.to[0] in target_emails
2024-11-10 01:55:18 +00:00
class TestAccountDumpCommand(TestAccountDump):
@classmethod
def setUpTestData(cls):
with freezegun.freeze_time(
now() - settings.SITH_ACCOUNT_DUMP_DELTA - timedelta(hours=1)
):
# pretend the notifications happened enough time ago
# to make sure the accounts are dumpable right now
cls.set_up_notified_users()
AccountDump.objects.bulk_create(
[
AccountDump(customer=u.customer, warning_mail_sent_at=now())
for u in cls.notified_users
]
)
# One of the users reactivated its account
baker.make(
Subscription,
member=cls.notified_users[0],
subscription_start=now() - timedelta(days=1),
)
def assert_accounts_dumped(self, accounts: Iterable[Customer]):
"""Assert that the given accounts have been dumped"""
assert not (
AccountDump.objects.ongoing().filter(customer__in=accounts).exists()
)
for customer in accounts:
initial_amount = customer.amount
customer.refresh_from_db()
assert customer.amount == 0
operation: Selling = customer.buyings.order_by("date").last()
assert operation.unit_price == initial_amount
assert operation.counter_id == settings.SITH_COUNTER_ACCOUNT_DUMP_ID
assert operation.is_validated is True
dump = customer.dumps.last()
assert dump.dump_operation == operation
def test_user_selection(self):
"""Test that users to dump are well selected"""
# even reactivated users should be selected,
# because their pending AccountDump must be dealt with
users = list(DumpCommand._get_users())
assert len(users) == len(self.notified_users)
assert set(users) == set(self.notified_users)
def test_dump_accounts(self):
"""Test the _dump_accounts method"""
# the first user reactivated its account, thus should not be dumped
to_dump: set[Customer] = {u.customer for u in self.notified_users[1:]}
DumpCommand._dump_accounts(to_dump)
self.assert_accounts_dumped(to_dump)
def test_dump_account_with_active_users(self):
"""Test that the dump account method failed if given active users."""
active_user = subscriber_user.make()
active_user.customer.amount = 10
active_user.customer.save()
customers = {u.customer for u in self.notified_users}
customers.add(active_user.customer)
with pytest.raises(ValueError):
DumpCommand._dump_accounts(customers)
for customer in customers:
# all users should have kept their money
initial_amount = customer.amount
customer.refresh_from_db()
assert customer.amount == initial_amount
def test_command(self):
"""test the actual command"""
call_command("dump_accounts")
reactivated_user = self.notified_users[0]
# the pending operation should be deleted for reactivated users
assert not reactivated_user.customer.dumps.exists()
assert reactivated_user.customer.amount == 10
dumped_users = self.notified_users[1:]
self.assert_accounts_dumped([u.customer for u in dumped_users])
sent_mails = list(mail.outbox)
assert len(sent_mails) == 2
target_emails = {u.email for u in dumped_users}
for sent in sent_mails:
assert len(sent.to) == 1
assert sent.to[0] in target_emails