Loading Dockerfile +0 −1 Original line number Diff line number Diff line Loading @@ -9,5 +9,4 @@ COPY src/ tmp/src RUN set -eux && pip install --upgrade pip && \ cd /tmp/src/ && pip install -e . && \ pip install pytest pytest-cov CMD ['sh', 'tail', '-f', '/dev/null'] docker-compose.test.yaml +2 −1 Original line number Diff line number Diff line Loading @@ -9,7 +9,8 @@ services: env_file: - ./test_envfile #command: ["sh", "-c", "pytest --cov=common /test/test.py"] command: ["sh", "-c", "test-db-conn && pytest -v --cov=common /test/test.py"] #command: ["sh", "-c", "test-db-conn && pytest -v --cov=common /test/test.py"] command: ["python3", "/test/test.py"] depends_on: - postgres Loading src/common/mail.py +30 −4 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import smtplib import os import ssl from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication Loading @@ -20,8 +21,12 @@ def send_email(subject, text, files=None): :return: None """ logger = create_logger() from_address = ce('EMAIL_SENDER') smtp_address = ce('SMTP_ADDRESS', 'smtp.ornl.gov') relay_address = ce('RELAY_ADDRESS') relay_port = ce('RELAY_PORT') relay_password = ce('RELAY_PASSWORD') smtp_port = ce('SMTP_PORT', 25) if from_address is None: logger.critical('Unable to send email as no EMAIL_SENDER set') Loading @@ -38,6 +43,7 @@ def send_email(subject, text, files=None): msg['To'] = to_address msg['Subject'] = subject msg.attach(MIMEText(text, 'plain')) # if attachment files have been passed.. if isinstance(files, list) and len(files) > 0: for f in files: Loading @@ -63,12 +69,32 @@ def send_email(subject, text, files=None): logger.error(f'Failed to attach files "{files}". Please ensure that ' '"files" is passed as type "list"') logger.info(f'Sending email to {to_address} from {from_address}') try: # try: if all(var is None for var in [relay_address, relay_port, relay_password]): with smtplib.SMTP(smtp_address, smtp_port) as server: server.set_debuglevel(ce('EMAIL_DEBUG_LEVEL', 0)) server.send_message(msg) server.quit() logger.info(f'Email successfully sent to {to_address}.') except smtplib.SMTPException as error: logger.error(f'Error sending email: {error}') elif all(var is not None for var in [relay_address, relay_port, relay_address]): # allow ssl connection context = ssl.create_default_context() with smtplib.SMTP(relay_address, relay_port) as conn: conn.ehlo() conn.starttls(context=context) conn.login(from_address, relay_password) conn.send_message(msg) conn.quit() logger.info(f'Email successfully sent to {to_address}.') else: logger.error("Misconfigured environment variables detected. " + "Please specify either all environment variables " + "for relay addresses or none of them. Relay " + "environment variables: 'RELAY_ADDRESS', " + "'RELAY_PORT', 'RELAY_PASSWORD'") # except smtplib.SMTPException as error: # logger.error(f'Error sending email: {error}') return None src/requirements.txt +3 −0 Original line number Diff line number Diff line rich==10.11.0 pandas==1.3.3 psycopg2-binary==2.9.1 ndg-httpsclient~=0.5.1 pyOpenSSL~=22.0.0 pyasn1~=0.4.8 test_envfile +5 −1 Original line number Diff line number Diff line Loading @@ -8,5 +8,9 @@ DATABASE_LOG_FILE=/tmp/test.log DATABASE_LOG_MODE=w DATABASE_LOG_ENCODING=utf-8 LOGLEVEL=debug EMAIL_SENDER=hamakerab@ornl.gov EMAIL_SENDER=common.package.test@gmail.com #EMAIL_SENDER=hamakerab@ornl.gov EMAIL_RECIPIENTS=hamakerab@ornl.gov RELAY_ADDRESS=smtp.gmail.com RELAY_PASSWORD=ABK@4n6Fc4Qb@gW8rpg5ZR3Nph%w$ RELAY_PORT=587 Loading
Dockerfile +0 −1 Original line number Diff line number Diff line Loading @@ -9,5 +9,4 @@ COPY src/ tmp/src RUN set -eux && pip install --upgrade pip && \ cd /tmp/src/ && pip install -e . && \ pip install pytest pytest-cov CMD ['sh', 'tail', '-f', '/dev/null']
docker-compose.test.yaml +2 −1 Original line number Diff line number Diff line Loading @@ -9,7 +9,8 @@ services: env_file: - ./test_envfile #command: ["sh", "-c", "pytest --cov=common /test/test.py"] command: ["sh", "-c", "test-db-conn && pytest -v --cov=common /test/test.py"] #command: ["sh", "-c", "test-db-conn && pytest -v --cov=common /test/test.py"] command: ["python3", "/test/test.py"] depends_on: - postgres Loading
src/common/mail.py +30 −4 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import smtplib import os import ssl from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication Loading @@ -20,8 +21,12 @@ def send_email(subject, text, files=None): :return: None """ logger = create_logger() from_address = ce('EMAIL_SENDER') smtp_address = ce('SMTP_ADDRESS', 'smtp.ornl.gov') relay_address = ce('RELAY_ADDRESS') relay_port = ce('RELAY_PORT') relay_password = ce('RELAY_PASSWORD') smtp_port = ce('SMTP_PORT', 25) if from_address is None: logger.critical('Unable to send email as no EMAIL_SENDER set') Loading @@ -38,6 +43,7 @@ def send_email(subject, text, files=None): msg['To'] = to_address msg['Subject'] = subject msg.attach(MIMEText(text, 'plain')) # if attachment files have been passed.. if isinstance(files, list) and len(files) > 0: for f in files: Loading @@ -63,12 +69,32 @@ def send_email(subject, text, files=None): logger.error(f'Failed to attach files "{files}". Please ensure that ' '"files" is passed as type "list"') logger.info(f'Sending email to {to_address} from {from_address}') try: # try: if all(var is None for var in [relay_address, relay_port, relay_password]): with smtplib.SMTP(smtp_address, smtp_port) as server: server.set_debuglevel(ce('EMAIL_DEBUG_LEVEL', 0)) server.send_message(msg) server.quit() logger.info(f'Email successfully sent to {to_address}.') except smtplib.SMTPException as error: logger.error(f'Error sending email: {error}') elif all(var is not None for var in [relay_address, relay_port, relay_address]): # allow ssl connection context = ssl.create_default_context() with smtplib.SMTP(relay_address, relay_port) as conn: conn.ehlo() conn.starttls(context=context) conn.login(from_address, relay_password) conn.send_message(msg) conn.quit() logger.info(f'Email successfully sent to {to_address}.') else: logger.error("Misconfigured environment variables detected. " + "Please specify either all environment variables " + "for relay addresses or none of them. Relay " + "environment variables: 'RELAY_ADDRESS', " + "'RELAY_PORT', 'RELAY_PASSWORD'") # except smtplib.SMTPException as error: # logger.error(f'Error sending email: {error}') return None
src/requirements.txt +3 −0 Original line number Diff line number Diff line rich==10.11.0 pandas==1.3.3 psycopg2-binary==2.9.1 ndg-httpsclient~=0.5.1 pyOpenSSL~=22.0.0 pyasn1~=0.4.8
test_envfile +5 −1 Original line number Diff line number Diff line Loading @@ -8,5 +8,9 @@ DATABASE_LOG_FILE=/tmp/test.log DATABASE_LOG_MODE=w DATABASE_LOG_ENCODING=utf-8 LOGLEVEL=debug EMAIL_SENDER=hamakerab@ornl.gov EMAIL_SENDER=common.package.test@gmail.com #EMAIL_SENDER=hamakerab@ornl.gov EMAIL_RECIPIENTS=hamakerab@ornl.gov RELAY_ADDRESS=smtp.gmail.com RELAY_PASSWORD=ABK@4n6Fc4Qb@gW8rpg5ZR3Nph%w$ RELAY_PORT=587