diff --git a/tests/factories/finance.py b/tests/factories/finance.py index 0df53d555..5db69b67d 100644 --- a/tests/factories/finance.py +++ b/tests/factories/finance.py @@ -36,6 +36,7 @@ class Meta: name = Faker('word') bank = Faker('word') + owner = Faker('word') account_number = Faker('random_number', digits=10) routing_number = Faker('random_number', digits=8) iban = Faker('iban') @@ -51,8 +52,8 @@ class Meta: bank_account = SubFactory(BankAccountFactory) amount = Faker('random_number', digits=5) reference = Sequence(lambda n: f"Reference {n}") - other_account_number = Faker('random_number', digits=10) - other_routing_number = Faker('random_number', digits=8) + other_account_number = Faker("iban") + other_routing_number = Faker("swift") other_name = Faker('word') imported_at = LazyAttribute(lambda o: session.utcnow().date() - timedelta(days=4)) posted_on = LazyAttribute(lambda o: o.imported_at + timedelta(days=1)) diff --git a/tests/lib/test_finance.py b/tests/lib/test_finance.py index 016952c1d..a7bcd49de 100644 --- a/tests/lib/test_finance.py +++ b/tests/lib/test_finance.py @@ -18,7 +18,10 @@ estimate_balance, post_transactions_for_membership_fee, get_users_with_payment_in_default, end_payment_in_default_memberships, - take_actions_for_payment_in_default_users) + take_actions_for_payment_in_default_users, + get_activities_to_return, + generate_activities_return_sepaxml, +) from pycroft.model.finance import ( Transaction, Split, @@ -654,3 +657,65 @@ def test_last_imported_at(self, session: Session): assert finance.get_last_import_date(session) == datetime( 2020, 1, 1, tzinfo=timezone.utc ) + + +class TestReturnNonAttributable: + @pytest.mark.parametrize( + "expected, set_transaction_id, amount_negative, imported_at_old", + [ + (0, False, False, False), # too young + (0, True, False, False), # too young, already attributed + (1, False, False, True), + (0, True, False, True), # already attributed + (0, False, True, False), # negative amount, too young + (0, True, True, False), # negative amount, too young, already attributed + (0, False, True, True), # negative amount + (0, True, True, True), # negative amount, already attributed + ], + ) + def test_activities_to_return( + self, + session: Session, + utcnow, + expected, + set_transaction_id, + amount_negative, + imported_at_old, + ): + kwargs = {} + + if amount_negative: + kwargs["amount"] = -1000 + if imported_at_old: + kwargs["imported_at"] = utcnow.date() - timedelta(days=20) + + activity = BankAccountActivityFactory.create(**kwargs) + + if set_transaction_id: + user = UserFactory.create() + + debit_account = user.account + credit_account = activity.bank_account.account + transaction = finance.simple_transaction( + description=activity.reference, + debit_account=debit_account, + credit_account=credit_account, + amount=activity.amount, + author=user, + valid_on=activity.valid_on, + ) + activity.split = next( + split for split in transaction.splits if split.account_id == credit_account.id + ) + + session.add(activity) + + activities_to_return = get_activities_to_return(session) + + assert len(activities_to_return) == expected + + def test_generate_sepa_xml(self, session: Session, utcnow): + BankAccountActivityFactory.create(imported_at=utcnow.date() - timedelta(days=20)) + BankAccountActivityFactory.create(imported_at=utcnow.date() - timedelta(days=21)) + + generate_activities_return_sepaxml(get_activities_to_return(session))