generated from ghga-de/microservice-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send email notifications with automated testing (GSI 9) (#4)
* Add test server with authentication Update config to include new params Update dev reqs to include aiosmtpd * Add more errors to Notifier port * Create smtp client port * Implement SMTP client adapter * Update notifier to use smtp client * Update tests to use new dummy server * Silence mypy error on message_from_bytes * Tweak host values in config * Add random free port testing util Update smtp client and dummy server with methods to set hostname/port * Update example config * Connect consumer to notifier * Prefix record_email() with underscore Co-authored-by: Kersten Breuer <[email protected]> * Remove setters from DummyServer/make values public Update use of record_email to _record_email * Reorder tests so quicker ones run first * Move failed auth test to own test function DRY out parametrized notification * Remove DummySmtpClient entirely * Reflect in tests that HTML template is mandatory * Add SmtpClientConfig * Add NotifierConfig * Make BadTemplateFormat error more specific Remove condition for using html template * Add test config with dynamic port Remove port modification for dummy server/smtp client * Expand error handling for other SMTP exceptions --------- Co-authored-by: Byron Himes <[email protected]> Co-authored-by: Kersten Breuer <[email protected]>
- Loading branch information
1 parent
3855120
commit dbecdbe
Showing
19 changed files
with
539 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
# Please only mention the non-default settings here: | ||
|
||
notification_event_topic: "notifications" | ||
notification_event_type: "notification" | ||
notification_event_topic: notifications | ||
notification_event_type: notification | ||
service_instance_id: 001 | ||
kafka_servers: ["kafka:9092"] | ||
plaintext_email_template: "Dear $recipient_name,\n\n$plaintext_body\n\nWarm regards,\n\nThe GHGA Team" | ||
html_email_template: '<!DOCTYPE html><html><head></head><body style="color: #00393f;padding: 12px;"><h2>Dear $recipient_name,</h2><p>$plaintext_body</p><p>Warm regards,</p><h3>The GHGA Team</h3></body></html>' | ||
smtp_host: 127.0.0.1 | ||
smtp_port: 587 | ||
login_user: "[email protected]" | ||
login_password: test | ||
from_address: "[email protected]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
from_address: [email protected] | ||
html_email_template: '<!DOCTYPE html><html><head></head><body style="color: #00393f;padding: | ||
12px;"><h2>Dear $recipient_name,</h2><p>$plaintext_body</p><p>Warm regards,</p><h3>The | ||
GHGA Team</h3></body></html>' | ||
kafka_servers: | ||
- kafka:9092 | ||
login_password: test | ||
login_user: [email protected] | ||
notification_event_topic: notifications | ||
notification_event_type: notification | ||
plaintext_email_template: 'Dear $recipient_name, | ||
|
@@ -17,3 +20,5 @@ plaintext_email_template: 'Dear $recipient_name, | |
The GHGA Team' | ||
service_instance_id: '1' | ||
service_name: ns | ||
smtp_host: 127.0.0.1 | ||
smtp_port: 587 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright 2021 - 2023 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln | ||
# for the German Human Genome-Phenome Archive (GHGA) | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2021 - 2023 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln | ||
# for the German Human Genome-Phenome Archive (GHGA) | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
"""Contains the smtp client adapter""" | ||
import smtplib | ||
import ssl | ||
from email.message import EmailMessage | ||
|
||
from pydantic import BaseSettings, Field | ||
|
||
from ns.ports.outbound.smtp_client import SmtpClientPort | ||
|
||
|
||
class SmtpClientConfig(BaseSettings): | ||
"""Configuration details for the SmtpClient""" | ||
|
||
smtp_host: str = Field(..., description="The mail server host to connect to") | ||
smtp_port: int = Field(..., description="The port for the mail server connection") | ||
login_user: str = Field(..., description="The login username or email") | ||
login_password: str = Field(..., description="The login password") | ||
|
||
|
||
class SmtpClient(SmtpClientPort): | ||
"""Concrete implementation of an SmtpClientPort""" | ||
|
||
def __init__(self, config: SmtpClientConfig, debugging: bool = False): | ||
"""Assign config, which should contain all needed info""" | ||
self._config = config | ||
self._debugging = debugging | ||
|
||
def send_email_message(self, message: EmailMessage): | ||
# create ssl security context per Python's Security considerations | ||
context = ssl.create_default_context() | ||
|
||
try: | ||
with smtplib.SMTP(self._config.smtp_host, self._config.smtp_port) as server: | ||
if not self._debugging: | ||
server.starttls(context=context) | ||
try: | ||
server.login(self._config.login_user, self._config.login_password) | ||
except smtplib.SMTPAuthenticationError as err: | ||
raise self.FailedLoginError() from err | ||
|
||
# check for a connection | ||
if server.noop()[0] != 250: | ||
raise self.ConnectionError() | ||
server.send_message(msg=message) | ||
except smtplib.SMTPException as exc: | ||
raise self.GeneralSmtpException(error_info=exc.args[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.