Files
zulip/zerver/tests/test_email_log.py
Lauryn Menard f391bfeec6 emails: Add new onboarding email with guide for organization type.
Adds a new welcome email, `onboarding_zulip_guide`, to be sent four
days after a new user registers with a Zulip organization if the
organization has specified a particular organization type that has
a guide in the corporate `/for/.../` pages. If there is no guide,
then no email is scheduled or sent.

The current `for/communities/` page is not very useful for users
who are not organization administrators, so these onboarding guide
emails are further restricted for those organization types to
only go to new users who are invited/registered as admins for the
organzation.

Adds two database queries for new user registrations: one to get
the organization's type and one to create the scheduled email.

Adds two email logs because the email is sent both to a new user
who registers with an existing organization and to the organization
owner when they register a new organization.

Co-authored by: Alya Abbott <alya@zulip.com>
2023-04-10 08:38:09 -07:00

51 lines
2.3 KiB
Python

import os
from unittest import mock
from django.conf import settings
from zerver.lib.test_classes import ZulipTestCase
from zproject.email_backends import get_forward_address
class EmailLogTest(ZulipTestCase):
def test_generate_and_clear_email_log(self) -> None:
with self.settings(EMAIL_BACKEND="zproject.email_backends.EmailLogBackEnd"), mock.patch(
"zproject.email_backends.EmailLogBackEnd._do_send_messages", lambda *args: 1
), self.assertLogs(level="INFO") as m, self.settings(DEVELOPMENT_LOG_EMAILS=True):
result = self.client_get("/emails/generate/")
self.assertEqual(result.status_code, 302)
self.assertIn("emails", result["Location"])
result = self.client_get("/emails/")
self.assert_in_success_response(["All the emails sent in the Zulip"], result)
result = self.client_get("/emails/clear/")
self.assertEqual(result.status_code, 302)
result = self.client_get(result["Location"])
self.assertIn("manually generate most of the emails by clicking", str(result.content))
output_log = (
"INFO:root:Emails sent in development are available at http://testserver/emails"
)
self.assertEqual(m.output, [output_log for i in range(17)])
def test_forward_address_details(self) -> None:
try:
forward_address = "forward-to@example.com"
result = self.client_post("/emails/", {"forward_address": forward_address})
self.assert_json_success(result)
self.assertEqual(get_forward_address(), forward_address)
with self.settings(EMAIL_BACKEND="zproject.email_backends.EmailLogBackEnd"), mock.patch(
"zproject.email_backends.EmailLogBackEnd._do_send_messages", lambda *args: 1
):
result = self.client_get("/emails/generate/")
self.assertEqual(result.status_code, 302)
self.assertIn("emails", result["Location"])
result = self.client_get(result["Location"])
self.assert_in_success_response([forward_address], result)
# Remove this file, even if the test fails, so that it does
# not impact the state of the development environment.
finally:
os.remove(settings.FORWARD_ADDRESS_CONFIG_FILE)