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 28, 2024
1 parent a1f5a45 commit 3496e0b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
27 changes: 17 additions & 10 deletions pycroft/lib/finance/fints.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,37 @@
from pycroft.model.finance import BankAccount


def get_fints_transactions(
def get_fints_client(

Check failure on line 13 in pycroft/lib/finance/fints.py

View workflow job for this annotation

GitHub Actions / python-lint

Error

Function is missing a type annotation for one or more arguments [no-untyped-def]
*,
product_id: str,
user_id: int,
secret_pin: str,
bank_account: BankAccount,
**kwargs,
) -> 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 3496e0b

Please sign in to comment.