From c9af60e36c92e2b9e46e95d9d38a45387ead681c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregor=20D=C3=BCster?= Date: Sat, 28 Sep 2024 21:39:18 +0200 Subject: [PATCH] Move FinTS client creation to seperate function --- pycroft/lib/finance/fints.py | 28 ++++++++++++-------- tests/external_services/test_fints.py | 37 +++++++++++++++++++++------ web/blueprints/finance/__init__.py | 15 +++++++---- 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/pycroft/lib/finance/fints.py b/pycroft/lib/finance/fints.py index 7e15b75e9..e828214f4 100644 --- a/pycroft/lib/finance/fints.py +++ b/pycroft/lib/finance/fints.py @@ -2,6 +2,7 @@ # This file is part of the Pycroft project and licensed under the terms of # the Apache License, Version 2.0. See the LICENSE file for details +import typing as t from datetime import date from mt940.models import Transaction as MT940Transaction @@ -10,15 +11,30 @@ from pycroft.model.finance import BankAccount -def get_fints_transactions( +def get_fints_client( *, product_id: str, user_id: int, secret_pin: str, bank_account: BankAccount, + **kwargs: t.Any, +) -> FinTS3Client: + return FinTS3Client( + bank_identifier=bank_account.routing_number, + user_id=user_id, + pin=secret_pin, + server=bank_account.fints_endpoint, + product_id=product_id, + **kwargs, + ) + + +def get_fints_transactions( + *, start_date: date, end_date: date, - FinTSClient: type[FinTS3Client] = FinTS3Client, + bank_account: BankAccount, + fints_client: FinTS3Client, ) -> tuple[list[MT940Transaction], list[StatementError]]: """Get the transactions from FinTS @@ -26,14 +42,6 @@ def get_fints_transactions( - FinTS (:module:`pycroft.external_services.fints`) """ - # login with fints - fints_client = FinTSClient( - bank_identifier=bank_account.routing_number, - user_id=user_id, - pin=secret_pin, - server=bank_account.fints_endpoint, - product_id=product_id, - ) acc = next( (a for a in fints_client.get_sepa_accounts() if a.iban == bank_account.iban), None, diff --git a/tests/external_services/test_fints.py b/tests/external_services/test_fints.py index 3379eae1b..e1568897a 100644 --- a/tests/external_services/test_fints.py +++ b/tests/external_services/test_fints.py @@ -70,27 +70,48 @@ class BankAccountFactory(BankAccountFactory_): @pytest.fixture(scope="session") def default_transaction_args() -> dict: return { - "product_id": "1", - "user_id": 1, - "secret_pin": "123456", "start_date": today() - timedelta(days=30), "end_date": today(), - "FinTSClient": StubFintsClient, } -def test_fints_connection(default_transaction_args): +@pytest.fixture(scope="session") +def default_fints_client_args() -> dict: + return { + "product_id": "1", + "user_id": 1, + "pin": "123456", + } + + +def test_fints_connection(default_fints_client_args, default_transaction_args): + bank_account = BankAccountFactory.build(iban="DE61850503003120219540") + fints_client = StubFintsClient( + **default_fints_client_args, + bank_identifier=bank_account.routing_number, + server=bank_account.fints_endpoint, + ) + transactions, errors = get_fints_transactions( **default_transaction_args, - bank_account=BankAccountFactory.build(iban="DE61850503003120219540"), + bank_account=bank_account, + fints_client=fints_client, ) assert transactions == [] assert errors == [] -def test_transactions_unknown_iban(default_transaction_args): +def test_transactions_unknown_iban(default_fints_client_args, default_transaction_args): + bank_account = BankAccountFactory.build() + fints_client = StubFintsClient( + **default_fints_client_args, + bank_identifier=bank_account.routing_number, + server=bank_account.fints_endpoint, + ) + with pytest.raises(KeyError, match="BankAccount with IBAN.*not found"): get_fints_transactions( **default_transaction_args, - bank_account=BankAccountFactory.build(), + bank_account=bank_account, + fints_client=fints_client, ) diff --git a/web/blueprints/finance/__init__.py b/web/blueprints/finance/__init__.py index 3ed3865e2..0b57792b0 100644 --- a/web/blueprints/finance/__init__.py +++ b/web/blueprints/finance/__init__.py @@ -78,7 +78,7 @@ get_last_import_date, get_last_membership_fee, ) -from pycroft.lib.finance.fints import get_fints_transactions +from pycroft.lib.finance.fints import get_fints_transactions, get_fints_client from pycroft.lib.finance.matching import UserMatching, AccountMatching from pycroft.lib.mail import MemberNegativeBalance from pycroft.lib.user import encode_type2_user_id, user_send_mails @@ -307,15 +307,20 @@ def display_form_response( if not form.validate(): return display_form_response(imported) + fints_client = get_fints_client( + product_id=config.fints_product_id, + user_id=form.user.data, + secret_pin=form.secret_pin.data, + bank_account=bank_account, + ) + try: with flash_fints_errors(): statement, errors = get_fints_transactions( - product_id=config.fints_product_id, - user_id=form.user.data, - secret_pin=form.secret_pin.data, - bank_account=bank_account, start_date=form.start_date.data, end_date=form.end_date.data, + bank_account=bank_account, + fints_client=fints_client, ) except PycroftException: return display_form_response(imported)