-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0e2c4c
commit 3d8bf8d
Showing
6 changed files
with
100 additions
and
0 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
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,4 @@ | ||
import django | ||
|
||
if django.VERSION < (3, 2): | ||
default_app_config = "health_check.contrib.mail.apps.HealthCheckConfig" |
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,12 @@ | ||
from django.apps import AppConfig | ||
|
||
from health_check.plugins import plugin_dir | ||
|
||
|
||
class HealthCheckConfig(AppConfig): | ||
name = "health_check.contrib.mail" | ||
|
||
def ready(self): | ||
from .backends import MailHealthCheck | ||
|
||
plugin_dir.register(MailHealthCheck) |
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,28 @@ | ||
import logging | ||
|
||
from django.core.mail import get_connection | ||
|
||
from health_check.backends import BaseHealthCheckBackend | ||
from health_check.conf import HEALTH_CHECK | ||
from health_check.exceptions import ServiceUnavailable | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class MailHealthCheck(BaseHealthCheckBackend): | ||
"""Check that mail backend is working.""" | ||
|
||
def check_status(self) -> None: | ||
"""Open and close connection email server.""" | ||
try: | ||
connection = get_connection(fail_silently=False) | ||
connection.timeout = HEALTH_CHECK.get("MAIL_TIMEOUT", 15) | ||
logger.debug("Trying to open connection to mail backend.") | ||
connection.open() | ||
connection.close() | ||
logger.debug("Connection established. Mail backend is healthy.") | ||
except Exception as error: | ||
self.add_error( | ||
error=ServiceUnavailable(error), | ||
cause=error, | ||
) |
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,44 @@ | ||
from unittest import mock | ||
|
||
from health_check.contrib.mail.backends import MailHealthCheck | ||
|
||
|
||
class TestMailHealthCheck: | ||
"""Test mail health check.""" | ||
|
||
@mock.patch("health_check.contrib.mail.backends.get_connection") | ||
def test_mail_conn_ok(self, mocked_backend): | ||
"""Test everything is OK.""" | ||
|
||
# instantiates the class | ||
mail_health_checker = MailHealthCheck() | ||
|
||
# invokes the method check_status() | ||
mail_health_checker.check_status() | ||
assert len(mail_health_checker.errors) == 0, mail_health_checker.errors | ||
|
||
# mock assertions | ||
assert mocked_backend.return_value.timeout == 15 | ||
mocked_backend.return_value.open.assert_called_once() | ||
mocked_backend.return_value.close.assert_called_once() | ||
|
||
@mock.patch("health_check.contrib.mail.backends.get_connection") | ||
def test_mail_conn_refused(self, mocked_backend): | ||
"""Test case then connection refused.""" | ||
|
||
mocked_backend.return_value.open.side_effect = ConnectionRefusedError( | ||
"Refused connection" | ||
) | ||
# instantiates the class | ||
mail_health_checker = MailHealthCheck() | ||
|
||
# invokes the method check_status() | ||
mail_health_checker.check_status() | ||
assert len(mail_health_checker.errors) == 1, mail_health_checker.errors | ||
assert ( | ||
mail_health_checker.errors[0].message | ||
== mocked_backend.return_value.open.side_effect | ||
) | ||
|
||
# mock assertions | ||
mocked_backend.return_value.open.assert_called_once() |
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