Skip to content

Commit

Permalink
Mail Results - Disable Plugin
Browse files Browse the repository at this point in the history
Plugin was attemtping SMTP connections regardless
of if the plugin config was using defaults.
Now checks on pre/post checks to stop attempted
SMTP connections before the plugin was disabled.

Signed-off-by: Harvey Lynden <[email protected]>
  • Loading branch information
harvey0100 committed Aug 22, 2024
1 parent 4c590e2 commit 4450fd7
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions optional_plugins/mail/avocado_result_mail/result_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def initialize(self):
settings.register_option(
section="plugins.mail",
key="recipient",
default="[email protected]",
default=None,
help_msg=help_msg,
)

Expand All @@ -38,31 +38,31 @@ def initialize(self):
settings.register_option(
section="plugins.mail",
key="sender",
default="[email protected]",
default=None,
help_msg=help_msg,
)

help_msg = "Mail server."
settings.register_option(
section="plugins.mail",
key="server",
default="localhost",
default=None,
help_msg=help_msg,
)

help_msg = "Mail server port."
settings.register_option(
section="plugins.mail",
key="port",
default=587,
default=None,
help_msg=help_msg,
)

help_msg = "Mail server Application Password."
settings.register_option(
section="plugins.mail",
key="password",
default="",
default=None,
help_msg=help_msg,
)

Expand Down Expand Up @@ -160,6 +160,10 @@ def _send_email(smtp, sender, rcpt, msg):

@staticmethod
def _smtp_login_and_send(job, msg):
if not Mail._validate_email_config(job):
job_log.error("Invalid email configuration. Plugin Disabled.")
return False

server, port, sender, password = Mail._get_smtp_config(job)
smtp = Mail._create_smtp_connection(server, port)
if smtp:
Expand Down Expand Up @@ -257,6 +261,7 @@ def _get_test_summary(job):

def pre(self, job):
if not self.enabled:
job_log.info("Email plugin disabled, skipping start notification.")
return

phase = "Start"
Expand All @@ -268,6 +273,9 @@ def pre(self, job):

def post(self, job):
if not self.enabled or not self.start_email_sent:
job_log.info(
"Email plugin disabled or start email not sent, skipping end notification."
)
return

phase = "Post"
Expand All @@ -282,17 +290,13 @@ def post(self, job):
msg = self._build_message(job, time_content, phase, finishedtime, test_summary)
self._smtp_login_and_send(job, msg)

def _validate_email_config(self, job):
required_keys = ["recipient", "sender", "server", "port"]
for key in required_keys:
if not job.config.get(f"plugins.mail.{key}"):
job_log.error(
f"Email configuration {key} is missing. Disabling Plugin."
)
return False
@staticmethod
def _validate_email_config(job):
server, port, sender, password = Mail._get_smtp_config(job)

if job.config.get("plugins.mail.sender") == "[email protected]":
job_log.error("Email sender is still set to default. Disabling Plugin.")
if not all([server, port, sender, password]):
job_log.error(
"Email configuration is missing or contains empty values. Disabling Plugin."
)
return False

return True

0 comments on commit 4450fd7

Please sign in to comment.