Fix test case for sending invitations by email

This commit is contained in:
Neil Martinsen-Burrell 2023-11-09 15:39:53 -06:00
parent ab35221724
commit a7804b35ea
No known key found for this signature in database
GPG key ID: 6A3C818CC10D0184
2 changed files with 15 additions and 8 deletions

View file

@ -58,7 +58,7 @@ def get_handlers():
@contextmanager
def less_console_noise():
def less_console_noise(output_stream=None):
"""
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.
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 = {}
handlers = get_handlers()
devnull = open(os.devnull, "w")
if output_stream is None:
output_stream = open(os.devnull, "w")
# redirect all the streams
for handler in handlers.values():
prior = handler.setStream(devnull)
prior = handler.setStream(output_stream)
restore[handler.name] = prior
try:
# run the test
@ -82,8 +87,9 @@ def less_console_noise():
# restore the streams
for handler in handlers.values():
handler.setStream(restore[handler.name])
# close the file we opened
devnull.close()
if output_stream is None:
# we opened output_stream so we have to close it
output_stream.close()
class MockUserLogin:

View file

@ -272,11 +272,12 @@ class TestLogins(TestCase):
# this is one of the email addresses in data/test_contacts.txt
output_stream = StringIO()
call_command("send_domain_invitations",
stdout=output_stream)
# 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()
print("Output:", output)
# 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)