mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-26 04:28:39 +02:00
Merge pull request #1321 from cisagov/nmb/1294-slow-roll
#1294: Send transition domain invitations to particular email addresses
This commit is contained in:
commit
e8c3f3fa70
3 changed files with 69 additions and 9 deletions
|
@ -37,14 +37,24 @@ class Command(BaseCommand):
|
||||||
help="Send emails ",
|
help="Send emails ",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser.add_argument("emails", nargs="*", help="Email addresses to send invitations to")
|
||||||
|
|
||||||
def handle(self, **options):
|
def handle(self, **options):
|
||||||
"""Process the objects in TransitionDomain."""
|
"""Process the objects in TransitionDomain."""
|
||||||
|
|
||||||
logger.info("checking domains and preparing emails")
|
logger.info("checking domains and preparing emails")
|
||||||
# Get all TransitionDomain objects
|
|
||||||
self.transition_domains = TransitionDomain.objects.filter(
|
if options["emails"]:
|
||||||
email_sent=False,
|
# this option is a list of email addresses
|
||||||
).order_by("username")
|
self.transition_domains = TransitionDomain.objects.filter(
|
||||||
|
username__in=options["emails"],
|
||||||
|
email_sent=False,
|
||||||
|
).order_by("username")
|
||||||
|
else:
|
||||||
|
# Get all TransitionDomain objects
|
||||||
|
self.transition_domains = TransitionDomain.objects.filter(
|
||||||
|
email_sent=False,
|
||||||
|
).order_by("username")
|
||||||
logger.info("Found %d transition domains", len(self.transition_domains))
|
logger.info("Found %d transition domains", len(self.transition_domains))
|
||||||
|
|
||||||
self.build_emails_to_send_array()
|
self.build_emails_to_send_array()
|
||||||
|
|
|
@ -58,7 +58,7 @@ def get_handlers():
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def less_console_noise():
|
def less_console_noise(output_stream=None):
|
||||||
"""
|
"""
|
||||||
Context manager to use in tests to silence console logging.
|
Context manager to use in tests to silence console logging.
|
||||||
|
|
||||||
|
@ -66,14 +66,19 @@ def less_console_noise():
|
||||||
(such as errors) which are normal and expected.
|
(such as errors) which are normal and expected.
|
||||||
|
|
||||||
It can easily be removed to debug a failing test.
|
It can easily be removed to debug a failing test.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
`output_stream`: a stream to redirect every handler to. If it's
|
||||||
|
not provided, use /dev/null.
|
||||||
"""
|
"""
|
||||||
restore = {}
|
restore = {}
|
||||||
handlers = get_handlers()
|
handlers = get_handlers()
|
||||||
devnull = open(os.devnull, "w")
|
if output_stream is None:
|
||||||
|
output_stream = open(os.devnull, "w")
|
||||||
|
|
||||||
# redirect all the streams
|
# redirect all the streams
|
||||||
for handler in handlers.values():
|
for handler in handlers.values():
|
||||||
prior = handler.setStream(devnull)
|
prior = handler.setStream(output_stream)
|
||||||
restore[handler.name] = prior
|
restore[handler.name] = prior
|
||||||
try:
|
try:
|
||||||
# run the test
|
# run the test
|
||||||
|
@ -82,8 +87,9 @@ def less_console_noise():
|
||||||
# restore the streams
|
# restore the streams
|
||||||
for handler in handlers.values():
|
for handler in handlers.values():
|
||||||
handler.setStream(restore[handler.name])
|
handler.setStream(restore[handler.name])
|
||||||
# close the file we opened
|
if output_stream is None:
|
||||||
devnull.close()
|
# we opened output_stream so we have to close it
|
||||||
|
output_stream.close()
|
||||||
|
|
||||||
|
|
||||||
class MockUserLogin:
|
class MockUserLogin:
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
from io import StringIO
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from registrar.models import (
|
from registrar.models import (
|
||||||
|
@ -13,6 +16,8 @@ from registrar.models import (
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from .common import less_console_noise
|
||||||
|
|
||||||
|
|
||||||
class TestMigrations(TestCase):
|
class TestMigrations(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -369,3 +374,42 @@ class TestMigrations(TestCase):
|
||||||
expected_missing_domain_informations,
|
expected_missing_domain_informations,
|
||||||
expected_missing_domain_invitations,
|
expected_missing_domain_invitations,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_send_domain_invitations_email(self):
|
||||||
|
"""Can send only a single domain invitation email."""
|
||||||
|
with less_console_noise():
|
||||||
|
self.run_load_domains()
|
||||||
|
self.run_transfer_domains()
|
||||||
|
|
||||||
|
# this is one of the email addresses in data/test_contacts.txt
|
||||||
|
output_stream = StringIO()
|
||||||
|
# also have to re-point the logging handlers to output_stream
|
||||||
|
with less_console_noise(output_stream):
|
||||||
|
call_command("send_domain_invitations", "testuser@gmail.com", stdout=output_stream)
|
||||||
|
|
||||||
|
# Check that we had the right numbers in our output
|
||||||
|
output = output_stream.getvalue()
|
||||||
|
# should only be one domain we send email for
|
||||||
|
self.assertIn("Found 1 transition domains", output)
|
||||||
|
self.assertTrue("would send email to testuser@gmail.com", output)
|
||||||
|
|
||||||
|
def test_send_domain_invitations_two_emails(self):
|
||||||
|
"""Can send only a single domain invitation email."""
|
||||||
|
with less_console_noise():
|
||||||
|
self.run_load_domains()
|
||||||
|
self.run_transfer_domains()
|
||||||
|
|
||||||
|
# these are two email addresses in data/test_contacts.txt
|
||||||
|
output_stream = StringIO()
|
||||||
|
# also have to re-point the logging handlers to output_stream
|
||||||
|
with less_console_noise(output_stream):
|
||||||
|
call_command(
|
||||||
|
"send_domain_invitations", "testuser@gmail.com", "agustina.wyman7@test.com", stdout=output_stream
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check that we had the right numbers in our output
|
||||||
|
output = output_stream.getvalue()
|
||||||
|
# should only be one domain we send email for
|
||||||
|
self.assertIn("Found 2 transition domains", output)
|
||||||
|
self.assertTrue("would send email to testuser@gmail.com", output)
|
||||||
|
self.assertTrue("would send email to agustina.wyman7@test.com", output)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue