From 431608cd62172a25f27620033e6d7b9fa0a05aac Mon Sep 17 00:00:00 2001 From: Max Metz Date: Wed, 31 Jul 2024 12:30:37 +0200 Subject: [PATCH] refactored the account-data of the Email-server, so it can be easily adapted later on. Decoupled into a novel file notifications/accounts.py with some simple unit tests. --- src/server/BreCal/notifications/accounts.py | 7 +++++++ src/server/BreCal/notifications/notifier.py | 9 ++++---- src/server/tests/notifications/__init__.py | 0 .../tests/notifications/test_accounts.py | 21 +++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 src/server/BreCal/notifications/accounts.py create mode 100644 src/server/tests/notifications/__init__.py create mode 100644 src/server/tests/notifications/test_accounts.py diff --git a/src/server/BreCal/notifications/accounts.py b/src/server/BreCal/notifications/accounts.py new file mode 100644 index 0000000..0e3bbd0 --- /dev/null +++ b/src/server/BreCal/notifications/accounts.py @@ -0,0 +1,7 @@ +"""This file contains login information to register into distinct notification accounts.""" + + +mail_server = 'w01d5503.kasserver.com' +mail_port=465 +mail_address="max.metz@scope-sorting.com" +mail_pwd = b'gAAAAABmqJlkXbtJTL1tFiyQNHhF_Y7sgtVI0xEx07ybwbX70Ro1Vp73CLDq49eFSYG-1SswIDQ2JBSORYlWaR-Vh2kIwPHy_lX8SxkySrRvBRzkyZP5x0I=' diff --git a/src/server/BreCal/notifications/notifier.py b/src/server/BreCal/notifications/notifier.py index 916059f..3f0f591 100644 --- a/src/server/BreCal/notifications/notifier.py +++ b/src/server/BreCal/notifications/notifier.py @@ -54,8 +54,9 @@ class Notifier(): update_database = True if not is_test else False # if_test, the database will not be updated. time_diff_threshold = time_diff_threshold if not is_test else 0.0 # 0.0 delay when is_test is set. - email_handler = EmailHandler(mail_server='w01d5503.kasserver.com', mail_port=465, mail_address="max.metz@scope-sorting.com") - pwd = b'gAAAAABmqJlkXbtJTL1tFiyQNHhF_Y7sgtVI0xEx07ybwbX70Ro1Vp73CLDq49eFSYG-1SswIDQ2JBSORYlWaR-Vh2kIwPHy_lX8SxkySrRvBRzkyZP5x0I=' + + from BreCal.notifications.accounts import mail_server, mail_port, mail_address, mail_pwd + email_handler = EmailHandler(mail_server=mail_server, mail_port=mail_port, mail_address=mail_address) # get candidates: find all eligible shipcalls, where the evaluation state is yellow or red & the notifications are not yet sent eligible_shipcalls = Notifier.get_eligible_shipcalls() @@ -67,14 +68,14 @@ class Notifier(): if len(eligible_notifications) > 0: # only perform a login when there are eligible notifications try: # login in advance, so the email handler uses a shared connection. It disconnects only once at the end of the call. - email_handler.login(interactive=False, pwd=pwd) + email_handler.login(interactive=False, pwd=mail_pwd) for notification in eligible_notifications: eligible_users = Notifier.get_eligible_users(notification) # create an Email and send it to each eligible_user. # #TODO: this method must be a distributor. It should send emails for those, who want emails, and provide placeholders for other types of notifications - Notifier.create_and_send_email_notification(email_handler, pwd, eligible_users, notification, update_database=update_database, debug=debug) + Notifier.create_and_send_email_notification(email_handler, mail_pwd, eligible_users, notification, update_database=update_database, debug=debug) finally: email_handler.close() return diff --git a/src/server/tests/notifications/__init__.py b/src/server/tests/notifications/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/server/tests/notifications/test_accounts.py b/src/server/tests/notifications/test_accounts.py new file mode 100644 index 0000000..8cf21fe --- /dev/null +++ b/src/server/tests/notifications/test_accounts.py @@ -0,0 +1,21 @@ +import pytest +from BreCal.notifications.accounts import mail_server, mail_port, mail_address, mail_pwd + + +def test_mail_server(): + assert isinstance(mail_server, str) + assert not "@" in mail_server + return + +def test_mail_port(): + assert isinstance(mail_port, int) + return + +def test_mail_address(): + assert isinstance(mail_address, str) + assert "@" in mail_address + return + +def test_mail_pwd(): + assert isinstance(mail_pwd, bytes), f"must be a bytes-encoded password to protect the account" + return