Merge pull request #827 from dod-ccpo/stig-notifications
Create Notification System
This commit is contained in:
@@ -12,9 +12,9 @@ from atst.database import db as _db
|
||||
from atst.queue import queue as atst_queue
|
||||
import tests.factories as factories
|
||||
from tests.mocks import PDF_FILENAME, PDF_FILENAME2
|
||||
from tests.utils import FakeLogger
|
||||
from tests.utils import FakeLogger, FakeNotificationSender
|
||||
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from datetime import datetime, timedelta
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
from cryptography import x509
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
@@ -307,3 +307,13 @@ def mock_logger(app):
|
||||
yield app.logger
|
||||
|
||||
app.logger = real_logger
|
||||
|
||||
|
||||
@pytest.fixture(scope="function", autouse=True)
|
||||
def notification_sender(app):
|
||||
real_notification_sender = app.notification_sender
|
||||
app.notification_sender = FakeNotificationSender()
|
||||
|
||||
yield app.notification_sender
|
||||
|
||||
app.notification_sender = real_notification_sender
|
||||
|
@@ -318,3 +318,10 @@ class DD254Factory(Base):
|
||||
required_distribution = factory.LazyFunction(
|
||||
lambda: [random_choice(data.REQUIRED_DISTRIBUTIONS)]
|
||||
)
|
||||
|
||||
|
||||
class NotificationRecipientFactory(Base):
|
||||
class Meta:
|
||||
model = NotificationRecipient
|
||||
|
||||
email = factory.Faker("email")
|
||||
|
@@ -1,5 +1,7 @@
|
||||
import pytest
|
||||
from flask import url_for
|
||||
from copy import copy
|
||||
from tests.factories import UserFactory
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -20,3 +22,16 @@ def test_csrf_error(csrf_enabled_app, client):
|
||||
body = response.data.decode()
|
||||
assert "Session Expired" in body
|
||||
assert "Log in required" in body
|
||||
|
||||
|
||||
def test_errors_generate_notifications(app, client, user_session, notification_sender):
|
||||
user_session(UserFactory.create())
|
||||
new_app = copy(app)
|
||||
|
||||
@new_app.route("/throw")
|
||||
def throw():
|
||||
raise ValueError()
|
||||
|
||||
new_app.test_client().get("/throw")
|
||||
|
||||
notification_sender.send.assert_called_once()
|
||||
|
@@ -1,5 +1,8 @@
|
||||
from flask import template_rendered
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import Mock
|
||||
|
||||
from atst.utils.notification_sender import NotificationSender
|
||||
|
||||
|
||||
@contextmanager
|
||||
@@ -37,3 +40,6 @@ class FakeLogger:
|
||||
self.messages.append(msg)
|
||||
if "extra" in kwargs:
|
||||
self.extras.append(kwargs["extra"])
|
||||
|
||||
|
||||
FakeNotificationSender = lambda: Mock(spec=NotificationSender)
|
||||
|
27
tests/utils/test_notification_sender.py
Normal file
27
tests/utils/test_notification_sender.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import pytest
|
||||
from unittest.mock import Mock
|
||||
|
||||
from tests.factories import NotificationRecipientFactory
|
||||
from atst.utils.notification_sender import NotificationSender
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_queue(queue):
|
||||
return Mock(spec=queue)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def notification_sender(mock_queue):
|
||||
return NotificationSender(mock_queue)
|
||||
|
||||
|
||||
def test_can_send_notification(mock_queue, notification_sender):
|
||||
recipient_email = "test@example.com"
|
||||
email_body = "This is a test"
|
||||
|
||||
NotificationRecipientFactory.create(email=recipient_email)
|
||||
notification_sender.send(email_body)
|
||||
|
||||
mock_queue.send_notification_mail.assert_called_once_with(
|
||||
("test@example.com",), notification_sender.EMAIL_SUBJECT, email_body
|
||||
)
|
Reference in New Issue
Block a user