Skip to content

Commit

Permalink
Move FinTS client creation to seperate function
Browse files Browse the repository at this point in the history
  • Loading branch information
FestplattenSchnitzel committed Sep 29, 2024
1 parent 2a7f27a commit c9af60e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 23 deletions.
28 changes: 18 additions & 10 deletions pycroft/lib/finance/fints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -10,30 +11,37 @@
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
External service dependencies:
- 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,
Expand Down
37 changes: 29 additions & 8 deletions tests/external_services/test_fints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
15 changes: 10 additions & 5 deletions web/blueprints/finance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c9af60e

Please sign in to comment.