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.

This commit is contained in:
Max Metz 2024-07-31 12:30:37 +02:00
parent 0de47e2627
commit b078386520
4 changed files with 33 additions and 4 deletions

View File

@ -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='

View File

@ -54,8 +54,9 @@ class Notifier():
update_database = True if not is_test else False # if_test, the database will not be updated. 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. 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 # 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() 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 if len(eligible_notifications) > 0: # only perform a login when there are eligible notifications
try: try:
# login in advance, so the email handler uses a shared connection. It disconnects only once at the end of the call. # 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: for notification in eligible_notifications:
eligible_users = Notifier.get_eligible_users(notification) eligible_users = Notifier.get_eligible_users(notification)
# create an Email and send it to each eligible_user. # 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 # #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: finally:
email_handler.close() email_handler.close()
return return

View File

@ -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