markdown: Fix use of pure_markdown for non-pure markdown rendering.
`render_markdown_path` renders Markdown, and also (since baff121115a1)
runs Jinja2 on the resulting HTML.
The `pure_markdown` flag was added in 0a99fa2fd669, and did two
things: retried the path directly in the filesystem if it wasn't found
by the Jinja2 resolver, and also skipped the subsequent Jinja2
templating step (regardless of where the content was found). In this
context, the name `pure_markdown` made some sense. The only two
callsites were the TOS and privacy policy renders, which might have
had user-supplied arbitrary paths, and we wished to handle absolute
paths in addition to ones inside `templates/`.
Unfortunately, the follow-up of 01bd55bbcbf7 did not refactor the
logic -- it changed it, by making `pure_markdown` only do the former
of the two behaviors. Passing `pure_markdown=True` after that commit
still caused it to always run Jinja2, but allowed it to look elsewhere
in the filesystem.
This set the stage for calls, such as the one introduced in
dedea237456b, which passed both a context for Jinja2, as well as
`pure_markdown=True` implying that Jinja2 was not to be used.
Split the two previous behaviors of the `pure_markdown` flag, and use
pre-existing data to control them, rather than an explicit flag. For
handling policy information which is stored at an absolute path
outside of the template root, we switch to using the template search
path if and only if the path is relative. This also closes the
potential inconsistency based on CWD when `pure_markdown=True` was
passed and the path was relative, not absolute.
Decide whether to run Jinja2 based on if a context is passed in at
all. This restores the behavior in the initial 0a99fa2fd669 where a
call to `rendar_markdown_path` could be made to just render markdown,
and not some other unmentioned and unrelated templating language as
well.
2023-03-10 02:47:44 +01:00
|
|
|
import tempfile
|
2022-04-17 01:47:25 +02:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest.mock import patch
|
2017-03-08 12:28:24 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import ldap
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2017-03-08 12:28:24 +01:00
|
|
|
from django.core import mail
|
2022-07-01 03:05:12 +02:00
|
|
|
from django.core.mail.message import EmailMultiAlternatives
|
2017-03-08 12:28:24 +01:00
|
|
|
from django.test import override_settings
|
2021-09-09 21:51:55 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2019-10-05 03:54:48 +02:00
|
|
|
from django_auth_ldap.config import LDAPSearch
|
2017-03-08 12:28:24 +01:00
|
|
|
|
2022-04-14 23:48:28 +02:00
|
|
|
from zerver.actions.users import do_change_user_role
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.email_notifications import (
|
|
|
|
enqueue_welcome_emails,
|
2023-03-15 18:09:26 +01:00
|
|
|
get_onboarding_email_schedule,
|
2023-06-30 13:27:25 +02:00
|
|
|
send_account_registered_email,
|
2017-05-08 17:54:11 +02:00
|
|
|
)
|
2023-08-03 19:52:55 +02:00
|
|
|
from zerver.lib.send_email import deliver_scheduled_emails, send_custom_email
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2023-08-03 22:20:37 +02:00
|
|
|
from zerver.models import Realm, ScheduledEmail, UserProfile, get_realm
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-03-08 12:28:24 +01:00
|
|
|
|
2020-04-09 13:39:16 +02:00
|
|
|
class TestCustomEmails(ZulipTestCase):
|
|
|
|
def test_send_custom_email_argument(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
email_subject = "subject_test"
|
|
|
|
reply_to = "reply_to_test"
|
2020-04-09 13:39:16 +02:00
|
|
|
from_name = "from_name_test"
|
markdown: Fix use of pure_markdown for non-pure markdown rendering.
`render_markdown_path` renders Markdown, and also (since baff121115a1)
runs Jinja2 on the resulting HTML.
The `pure_markdown` flag was added in 0a99fa2fd669, and did two
things: retried the path directly in the filesystem if it wasn't found
by the Jinja2 resolver, and also skipped the subsequent Jinja2
templating step (regardless of where the content was found). In this
context, the name `pure_markdown` made some sense. The only two
callsites were the TOS and privacy policy renders, which might have
had user-supplied arbitrary paths, and we wished to handle absolute
paths in addition to ones inside `templates/`.
Unfortunately, the follow-up of 01bd55bbcbf7 did not refactor the
logic -- it changed it, by making `pure_markdown` only do the former
of the two behaviors. Passing `pure_markdown=True` after that commit
still caused it to always run Jinja2, but allowed it to look elsewhere
in the filesystem.
This set the stage for calls, such as the one introduced in
dedea237456b, which passed both a context for Jinja2, as well as
`pure_markdown=True` implying that Jinja2 was not to be used.
Split the two previous behaviors of the `pure_markdown` flag, and use
pre-existing data to control them, rather than an explicit flag. For
handling policy information which is stored at an absolute path
outside of the template root, we switch to using the template search
path if and only if the path is relative. This also closes the
potential inconsistency based on CWD when `pure_markdown=True` was
passed and the path was relative, not absolute.
Decide whether to run Jinja2 based on if a context is passed in at
all. This restores the behavior in the initial 0a99fa2fd669 where a
call to `rendar_markdown_path` could be made to just render markdown,
and not some other unmentioned and unrelated templating language as
well.
2023-03-10 02:47:44 +01:00
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile() as markdown_template:
|
|
|
|
markdown_template.write(b"# Some heading\n\nSome content\n{{ realm_name }}")
|
|
|
|
markdown_template.flush()
|
|
|
|
send_custom_email(
|
2023-08-03 22:20:37 +02:00
|
|
|
UserProfile.objects.filter(id=hamlet.id),
|
markdown: Fix use of pure_markdown for non-pure markdown rendering.
`render_markdown_path` renders Markdown, and also (since baff121115a1)
runs Jinja2 on the resulting HTML.
The `pure_markdown` flag was added in 0a99fa2fd669, and did two
things: retried the path directly in the filesystem if it wasn't found
by the Jinja2 resolver, and also skipped the subsequent Jinja2
templating step (regardless of where the content was found). In this
context, the name `pure_markdown` made some sense. The only two
callsites were the TOS and privacy policy renders, which might have
had user-supplied arbitrary paths, and we wished to handle absolute
paths in addition to ones inside `templates/`.
Unfortunately, the follow-up of 01bd55bbcbf7 did not refactor the
logic -- it changed it, by making `pure_markdown` only do the former
of the two behaviors. Passing `pure_markdown=True` after that commit
still caused it to always run Jinja2, but allowed it to look elsewhere
in the filesystem.
This set the stage for calls, such as the one introduced in
dedea237456b, which passed both a context for Jinja2, as well as
`pure_markdown=True` implying that Jinja2 was not to be used.
Split the two previous behaviors of the `pure_markdown` flag, and use
pre-existing data to control them, rather than an explicit flag. For
handling policy information which is stored at an absolute path
outside of the template root, we switch to using the template search
path if and only if the path is relative. This also closes the
potential inconsistency based on CWD when `pure_markdown=True` was
passed and the path was relative, not absolute.
Decide whether to run Jinja2 based on if a context is passed in at
all. This restores the behavior in the initial 0a99fa2fd669 where a
call to `rendar_markdown_path` could be made to just render markdown,
and not some other unmentioned and unrelated templating language as
well.
2023-03-10 02:47:44 +01:00
|
|
|
options={
|
|
|
|
"markdown_template_path": markdown_template.name,
|
|
|
|
"reply_to": reply_to,
|
|
|
|
"subject": email_subject,
|
|
|
|
"from_name": from_name,
|
|
|
|
"dry_run": False,
|
|
|
|
},
|
|
|
|
)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 1)
|
2020-04-09 13:39:16 +02:00
|
|
|
msg = mail.outbox[0]
|
|
|
|
self.assertEqual(msg.subject, email_subject)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(msg.reply_to, 1)
|
2020-04-09 13:39:16 +02:00
|
|
|
self.assertEqual(msg.reply_to[0], reply_to)
|
|
|
|
self.assertNotIn("{% block content %}", msg.body)
|
markdown: Fix use of pure_markdown for non-pure markdown rendering.
`render_markdown_path` renders Markdown, and also (since baff121115a1)
runs Jinja2 on the resulting HTML.
The `pure_markdown` flag was added in 0a99fa2fd669, and did two
things: retried the path directly in the filesystem if it wasn't found
by the Jinja2 resolver, and also skipped the subsequent Jinja2
templating step (regardless of where the content was found). In this
context, the name `pure_markdown` made some sense. The only two
callsites were the TOS and privacy policy renders, which might have
had user-supplied arbitrary paths, and we wished to handle absolute
paths in addition to ones inside `templates/`.
Unfortunately, the follow-up of 01bd55bbcbf7 did not refactor the
logic -- it changed it, by making `pure_markdown` only do the former
of the two behaviors. Passing `pure_markdown=True` after that commit
still caused it to always run Jinja2, but allowed it to look elsewhere
in the filesystem.
This set the stage for calls, such as the one introduced in
dedea237456b, which passed both a context for Jinja2, as well as
`pure_markdown=True` implying that Jinja2 was not to be used.
Split the two previous behaviors of the `pure_markdown` flag, and use
pre-existing data to control them, rather than an explicit flag. For
handling policy information which is stored at an absolute path
outside of the template root, we switch to using the template search
path if and only if the path is relative. This also closes the
potential inconsistency based on CWD when `pure_markdown=True` was
passed and the path was relative, not absolute.
Decide whether to run Jinja2 based on if a context is passed in at
all. This restores the behavior in the initial 0a99fa2fd669 where a
call to `rendar_markdown_path` could be made to just render markdown,
and not some other unmentioned and unrelated templating language as
well.
2023-03-10 02:47:44 +01:00
|
|
|
self.assertIn("# Some heading", msg.body)
|
|
|
|
self.assertIn("Zulip Dev", msg.body)
|
2023-08-03 19:57:21 +02:00
|
|
|
self.assertNotIn("{{ realm_name }}", msg.body)
|
|
|
|
self.assertNotIn("</div>", msg.body)
|
markdown: Fix use of pure_markdown for non-pure markdown rendering.
`render_markdown_path` renders Markdown, and also (since baff121115a1)
runs Jinja2 on the resulting HTML.
The `pure_markdown` flag was added in 0a99fa2fd669, and did two
things: retried the path directly in the filesystem if it wasn't found
by the Jinja2 resolver, and also skipped the subsequent Jinja2
templating step (regardless of where the content was found). In this
context, the name `pure_markdown` made some sense. The only two
callsites were the TOS and privacy policy renders, which might have
had user-supplied arbitrary paths, and we wished to handle absolute
paths in addition to ones inside `templates/`.
Unfortunately, the follow-up of 01bd55bbcbf7 did not refactor the
logic -- it changed it, by making `pure_markdown` only do the former
of the two behaviors. Passing `pure_markdown=True` after that commit
still caused it to always run Jinja2, but allowed it to look elsewhere
in the filesystem.
This set the stage for calls, such as the one introduced in
dedea237456b, which passed both a context for Jinja2, as well as
`pure_markdown=True` implying that Jinja2 was not to be used.
Split the two previous behaviors of the `pure_markdown` flag, and use
pre-existing data to control them, rather than an explicit flag. For
handling policy information which is stored at an absolute path
outside of the template root, we switch to using the template search
path if and only if the path is relative. This also closes the
potential inconsistency based on CWD when `pure_markdown=True` was
passed and the path was relative, not absolute.
Decide whether to run Jinja2 based on if a context is passed in at
all. This restores the behavior in the initial 0a99fa2fd669 where a
call to `rendar_markdown_path` could be made to just render markdown,
and not some other unmentioned and unrelated templating language as
well.
2023-03-10 02:47:44 +01:00
|
|
|
|
|
|
|
assert isinstance(msg, EmailMultiAlternatives)
|
|
|
|
self.assertIn("Some heading</h1>", str(msg.alternatives[0][0]))
|
2023-08-03 19:57:21 +02:00
|
|
|
self.assertNotIn("{{ realm_name }}", str(msg.alternatives[0][0]))
|
2020-04-09 13:39:16 +02:00
|
|
|
|
2021-12-15 02:09:12 +01:00
|
|
|
def test_send_custom_email_remote_server(self) -> None:
|
|
|
|
email_subject = "subject_test"
|
|
|
|
reply_to = "reply_to_test"
|
|
|
|
from_name = "from_name_test"
|
|
|
|
contact_email = "zulip-admin@example.com"
|
|
|
|
markdown_template_path = "templates/corporate/policies/index.md"
|
|
|
|
send_custom_email(
|
2023-08-03 22:20:37 +02:00
|
|
|
UserProfile.objects.none(),
|
2021-12-15 02:09:12 +01:00
|
|
|
target_emails=[contact_email],
|
|
|
|
options={
|
|
|
|
"markdown_template_path": markdown_template_path,
|
|
|
|
"reply_to": reply_to,
|
|
|
|
"subject": email_subject,
|
|
|
|
"from_name": from_name,
|
|
|
|
"dry_run": False,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assert_length(mail.outbox, 1)
|
|
|
|
msg = mail.outbox[0]
|
|
|
|
self.assertEqual(msg.subject, email_subject)
|
|
|
|
self.assertEqual(msg.to, [contact_email])
|
|
|
|
self.assert_length(msg.reply_to, 1)
|
|
|
|
self.assertEqual(msg.reply_to[0], reply_to)
|
|
|
|
self.assertNotIn("{% block content %}", msg.body)
|
|
|
|
# Verify that the HTML version contains the footer.
|
markdown: Fix use of pure_markdown for non-pure markdown rendering.
`render_markdown_path` renders Markdown, and also (since baff121115a1)
runs Jinja2 on the resulting HTML.
The `pure_markdown` flag was added in 0a99fa2fd669, and did two
things: retried the path directly in the filesystem if it wasn't found
by the Jinja2 resolver, and also skipped the subsequent Jinja2
templating step (regardless of where the content was found). In this
context, the name `pure_markdown` made some sense. The only two
callsites were the TOS and privacy policy renders, which might have
had user-supplied arbitrary paths, and we wished to handle absolute
paths in addition to ones inside `templates/`.
Unfortunately, the follow-up of 01bd55bbcbf7 did not refactor the
logic -- it changed it, by making `pure_markdown` only do the former
of the two behaviors. Passing `pure_markdown=True` after that commit
still caused it to always run Jinja2, but allowed it to look elsewhere
in the filesystem.
This set the stage for calls, such as the one introduced in
dedea237456b, which passed both a context for Jinja2, as well as
`pure_markdown=True` implying that Jinja2 was not to be used.
Split the two previous behaviors of the `pure_markdown` flag, and use
pre-existing data to control them, rather than an explicit flag. For
handling policy information which is stored at an absolute path
outside of the template root, we switch to using the template search
path if and only if the path is relative. This also closes the
potential inconsistency based on CWD when `pure_markdown=True` was
passed and the path was relative, not absolute.
Decide whether to run Jinja2 based on if a context is passed in at
all. This restores the behavior in the initial 0a99fa2fd669 where a
call to `rendar_markdown_path` could be made to just render markdown,
and not some other unmentioned and unrelated templating language as
well.
2023-03-10 02:47:44 +01:00
|
|
|
assert isinstance(msg, EmailMultiAlternatives)
|
2021-12-15 02:09:12 +01:00
|
|
|
self.assertIn(
|
|
|
|
"You are receiving this email to update you about important changes to Zulip",
|
markdown: Fix use of pure_markdown for non-pure markdown rendering.
`render_markdown_path` renders Markdown, and also (since baff121115a1)
runs Jinja2 on the resulting HTML.
The `pure_markdown` flag was added in 0a99fa2fd669, and did two
things: retried the path directly in the filesystem if it wasn't found
by the Jinja2 resolver, and also skipped the subsequent Jinja2
templating step (regardless of where the content was found). In this
context, the name `pure_markdown` made some sense. The only two
callsites were the TOS and privacy policy renders, which might have
had user-supplied arbitrary paths, and we wished to handle absolute
paths in addition to ones inside `templates/`.
Unfortunately, the follow-up of 01bd55bbcbf7 did not refactor the
logic -- it changed it, by making `pure_markdown` only do the former
of the two behaviors. Passing `pure_markdown=True` after that commit
still caused it to always run Jinja2, but allowed it to look elsewhere
in the filesystem.
This set the stage for calls, such as the one introduced in
dedea237456b, which passed both a context for Jinja2, as well as
`pure_markdown=True` implying that Jinja2 was not to be used.
Split the two previous behaviors of the `pure_markdown` flag, and use
pre-existing data to control them, rather than an explicit flag. For
handling policy information which is stored at an absolute path
outside of the template root, we switch to using the template search
path if and only if the path is relative. This also closes the
potential inconsistency based on CWD when `pure_markdown=True` was
passed and the path was relative, not absolute.
Decide whether to run Jinja2 based on if a context is passed in at
all. This restores the behavior in the initial 0a99fa2fd669 where a
call to `rendar_markdown_path` could be made to just render markdown,
and not some other unmentioned and unrelated templating language as
well.
2023-03-10 02:47:44 +01:00
|
|
|
str(msg.alternatives[0][0]),
|
2021-12-15 02:09:12 +01:00
|
|
|
)
|
|
|
|
|
2020-04-09 13:39:16 +02:00
|
|
|
def test_send_custom_email_headers(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
2021-02-12 08:19:30 +01:00
|
|
|
markdown_template_path = (
|
2023-08-03 23:07:36 +02:00
|
|
|
"zerver/tests/fixtures/email/custom_emails/email_base_headers_test.md"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
send_custom_email(
|
2023-08-03 22:20:37 +02:00
|
|
|
UserProfile.objects.filter(id=hamlet.id),
|
2021-12-15 01:45:35 +01:00
|
|
|
options={
|
2021-02-12 08:19:30 +01:00
|
|
|
"markdown_template_path": markdown_template_path,
|
2021-04-07 01:27:02 +02:00
|
|
|
"dry_run": False,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 1)
|
2020-04-09 13:39:16 +02:00
|
|
|
msg = mail.outbox[0]
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assertEqual(msg.subject, "Test subject")
|
2020-04-09 13:39:16 +02:00
|
|
|
self.assertFalse(msg.reply_to)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual("Test body", msg.body)
|
2020-04-09 13:39:16 +02:00
|
|
|
|
|
|
|
def test_send_custom_email_no_argument(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
2020-04-09 13:39:16 +02:00
|
|
|
from_name = "from_name_test"
|
2021-02-12 08:20:45 +01:00
|
|
|
email_subject = "subject_test"
|
2023-04-05 11:19:58 +02:00
|
|
|
markdown_template_path = (
|
2023-08-03 23:07:36 +02:00
|
|
|
"zerver/tests/fixtures/email/custom_emails/email_base_headers_no_headers_test.md"
|
2023-04-05 11:19:58 +02:00
|
|
|
)
|
2020-04-09 13:39:16 +02:00
|
|
|
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.send_email import NoEmailArgumentError
|
2020-04-09 13:39:16 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertRaises(
|
2022-11-17 09:30:48 +01:00
|
|
|
NoEmailArgumentError,
|
2021-02-12 08:19:30 +01:00
|
|
|
send_custom_email,
|
2023-08-03 22:20:37 +02:00
|
|
|
UserProfile.objects.filter(id=hamlet.id),
|
2021-12-15 01:45:35 +01:00
|
|
|
options={
|
2021-02-12 08:19:30 +01:00
|
|
|
"markdown_template_path": markdown_template_path,
|
|
|
|
"from_name": from_name,
|
2021-04-07 01:27:02 +02:00
|
|
|
"dry_run": False,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertRaises(
|
2022-11-17 09:30:48 +01:00
|
|
|
NoEmailArgumentError,
|
2021-02-12 08:19:30 +01:00
|
|
|
send_custom_email,
|
2023-08-03 22:20:37 +02:00
|
|
|
UserProfile.objects.filter(id=hamlet.id),
|
2021-12-15 01:45:35 +01:00
|
|
|
options={
|
2021-02-12 08:19:30 +01:00
|
|
|
"markdown_template_path": markdown_template_path,
|
|
|
|
"subject": email_subject,
|
2021-04-07 01:27:02 +02:00
|
|
|
"dry_run": False,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2020-04-09 13:39:16 +02:00
|
|
|
|
|
|
|
def test_send_custom_email_doubled_arguments(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
2020-04-09 13:39:16 +02:00
|
|
|
from_name = "from_name_test"
|
2021-02-12 08:20:45 +01:00
|
|
|
email_subject = "subject_test"
|
2021-02-12 08:19:30 +01:00
|
|
|
markdown_template_path = (
|
2023-08-03 23:07:36 +02:00
|
|
|
"zerver/tests/fixtures/email/custom_emails/email_base_headers_test.md"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-04-09 13:39:16 +02:00
|
|
|
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.send_email import DoubledEmailArgumentError
|
2020-04-09 13:39:16 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertRaises(
|
2022-11-17 09:30:48 +01:00
|
|
|
DoubledEmailArgumentError,
|
2021-02-12 08:19:30 +01:00
|
|
|
send_custom_email,
|
2023-08-03 22:20:37 +02:00
|
|
|
UserProfile.objects.filter(id=hamlet.id),
|
2021-12-15 01:45:35 +01:00
|
|
|
options={
|
2021-02-12 08:19:30 +01:00
|
|
|
"markdown_template_path": markdown_template_path,
|
|
|
|
"subject": email_subject,
|
2021-04-07 01:27:02 +02:00
|
|
|
"dry_run": False,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertRaises(
|
2022-11-17 09:30:48 +01:00
|
|
|
DoubledEmailArgumentError,
|
2021-02-12 08:19:30 +01:00
|
|
|
send_custom_email,
|
2023-08-03 22:20:37 +02:00
|
|
|
UserProfile.objects.filter(id=hamlet.id),
|
2021-12-15 01:45:35 +01:00
|
|
|
options={
|
2021-02-12 08:19:30 +01:00
|
|
|
"markdown_template_path": markdown_template_path,
|
|
|
|
"from_name": from_name,
|
2021-04-07 01:27:02 +02:00
|
|
|
"dry_run": False,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2020-04-09 13:39:16 +02:00
|
|
|
|
2020-04-18 15:51:29 +02:00
|
|
|
def test_send_custom_email_admins_only(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
admin_user = self.example_user("hamlet")
|
2021-03-27 05:13:46 +01:00
|
|
|
do_change_user_role(admin_user, UserProfile.ROLE_REALM_ADMINISTRATOR, acting_user=None)
|
2020-04-18 15:51:29 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
non_admin_user = self.example_user("cordelia")
|
2020-04-18 15:51:29 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
markdown_template_path = (
|
2023-08-03 23:07:36 +02:00
|
|
|
"zerver/tests/fixtures/email/custom_emails/email_base_headers_test.md"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
send_custom_email(
|
2023-08-03 22:20:37 +02:00
|
|
|
UserProfile.objects.filter(id__in=(admin_user.id, non_admin_user.id)),
|
2021-12-15 01:45:35 +01:00
|
|
|
options={
|
2021-02-12 08:19:30 +01:00
|
|
|
"markdown_template_path": markdown_template_path,
|
|
|
|
"admins_only": True,
|
2021-04-07 01:27:02 +02:00
|
|
|
"dry_run": False,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 1)
|
2020-04-18 15:51:29 +02:00
|
|
|
self.assertIn(admin_user.delivery_email, mail.outbox[0].to[0])
|
|
|
|
|
2021-04-07 01:27:02 +02:00
|
|
|
def test_send_custom_email_dry_run(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
email_subject = "subject_test"
|
|
|
|
reply_to = "reply_to_test"
|
|
|
|
from_name = "from_name_test"
|
|
|
|
markdown_template_path = "templates/zerver/tests/markdown/test_nested_code_blocks.md"
|
|
|
|
with patch("builtins.print") as _:
|
|
|
|
send_custom_email(
|
2023-08-03 22:20:37 +02:00
|
|
|
UserProfile.objects.filter(id=hamlet.id),
|
2021-12-15 01:45:35 +01:00
|
|
|
options={
|
2021-04-07 01:27:02 +02:00
|
|
|
"markdown_template_path": markdown_template_path,
|
|
|
|
"reply_to": reply_to,
|
|
|
|
"subject": email_subject,
|
|
|
|
"from_name": from_name,
|
|
|
|
"dry_run": True,
|
|
|
|
},
|
|
|
|
)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(mail.outbox, 0)
|
2021-04-07 01:27:02 +02:00
|
|
|
|
2020-04-09 13:39:16 +02:00
|
|
|
|
2018-11-14 12:46:56 +01:00
|
|
|
class TestFollowupEmails(ZulipTestCase):
|
|
|
|
def test_day1_email_context(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
2023-06-30 13:27:25 +02:00
|
|
|
send_account_registered_email(hamlet)
|
2023-03-15 20:18:09 +01:00
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=hamlet).order_by(
|
|
|
|
"scheduled_timestamp"
|
|
|
|
)
|
2020-08-07 01:09:47 +02:00
|
|
|
email_data = orjson.loads(scheduled_emails[0].data)
|
2018-11-14 12:46:56 +01:00
|
|
|
self.assertEqual(email_data["context"]["email"], self.example_email("hamlet"))
|
|
|
|
self.assertEqual(email_data["context"]["is_realm_admin"], False)
|
2023-02-03 02:16:43 +01:00
|
|
|
self.assertEqual(
|
|
|
|
email_data["context"]["getting_user_started_link"],
|
|
|
|
"http://zulip.testserver/help/getting-started-with-zulip",
|
|
|
|
)
|
2018-11-14 12:46:56 +01:00
|
|
|
self.assertNotIn("ldap_username", email_data["context"])
|
|
|
|
|
|
|
|
ScheduledEmail.objects.all().delete()
|
|
|
|
|
|
|
|
iago = self.example_user("iago")
|
2023-06-30 13:27:25 +02:00
|
|
|
send_account_registered_email(iago)
|
2023-03-15 20:18:09 +01:00
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=iago).order_by("scheduled_timestamp")
|
2020-08-07 01:09:47 +02:00
|
|
|
email_data = orjson.loads(scheduled_emails[0].data)
|
2018-11-14 12:46:56 +01:00
|
|
|
self.assertEqual(email_data["context"]["email"], self.example_email("iago"))
|
|
|
|
self.assertEqual(email_data["context"]["is_realm_admin"], True)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2023-02-03 02:16:43 +01:00
|
|
|
email_data["context"]["getting_organization_started_link"],
|
2021-02-12 08:19:30 +01:00
|
|
|
"http://zulip.testserver/help/getting-your-organization-started-with-zulip",
|
|
|
|
)
|
2023-02-03 02:16:43 +01:00
|
|
|
self.assertEqual(
|
|
|
|
email_data["context"]["getting_user_started_link"],
|
|
|
|
"http://zulip.testserver/help/getting-started-with-zulip",
|
|
|
|
)
|
2018-11-14 12:46:56 +01:00
|
|
|
self.assertNotIn("ldap_username", email_data["context"])
|
|
|
|
|
2018-11-29 16:32:17 +01:00
|
|
|
# See https://zulip.readthedocs.io/en/latest/production/authentication-methods.html#ldap-including-active-directory
|
|
|
|
# for case details.
|
2021-02-12 08:19:30 +01:00
|
|
|
@override_settings(
|
|
|
|
AUTHENTICATION_BACKENDS=(
|
2021-02-12 08:20:45 +01:00
|
|
|
"zproject.backends.ZulipLDAPAuthBackend",
|
|
|
|
"zproject.backends.ZulipDummyBackend",
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
# configure email search for email address in the uid attribute:
|
|
|
|
AUTH_LDAP_REVERSE_EMAIL_SEARCH=LDAPSearch(
|
|
|
|
"ou=users,dc=zulip,dc=com", ldap.SCOPE_ONELEVEL, "(uid=%(email)s)"
|
|
|
|
),
|
|
|
|
)
|
2018-11-29 16:32:17 +01:00
|
|
|
def test_day1_email_ldap_case_a_login_credentials(self) -> None:
|
2019-10-16 18:27:53 +02:00
|
|
|
self.init_default_ldap_database()
|
2021-02-12 08:20:45 +01:00
|
|
|
ldap_user_attr_map = {"full_name": "cn"}
|
2019-10-16 18:27:53 +02:00
|
|
|
|
|
|
|
with self.settings(AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map):
|
2021-02-12 08:19:30 +01:00
|
|
|
self.login_with_return(
|
|
|
|
"newuser_email_as_uid@zulip.com",
|
|
|
|
self.ldap_password("newuser_email_as_uid@zulip.com"),
|
|
|
|
)
|
2020-03-12 14:17:25 +01:00
|
|
|
user = UserProfile.objects.get(delivery_email="newuser_email_as_uid@zulip.com")
|
2023-03-15 20:18:09 +01:00
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=user).order_by(
|
|
|
|
"scheduled_timestamp"
|
|
|
|
)
|
2018-11-29 16:32:17 +01:00
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assert_length(scheduled_emails, 3)
|
2020-08-07 01:09:47 +02:00
|
|
|
email_data = orjson.loads(scheduled_emails[0].data)
|
2018-11-29 16:32:17 +01:00
|
|
|
self.assertEqual(email_data["context"]["ldap"], True)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
email_data["context"]["ldap_username"], "newuser_email_as_uid@zulip.com"
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
AUTHENTICATION_BACKENDS=(
|
2021-02-12 08:20:45 +01:00
|
|
|
"zproject.backends.ZulipLDAPAuthBackend",
|
|
|
|
"zproject.backends.ZulipDummyBackend",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
)
|
2018-11-29 16:32:17 +01:00
|
|
|
def test_day1_email_ldap_case_b_login_credentials(self) -> None:
|
2019-10-16 18:27:53 +02:00
|
|
|
self.init_default_ldap_database()
|
2021-02-12 08:20:45 +01:00
|
|
|
ldap_user_attr_map = {"full_name": "cn"}
|
2018-11-14 12:46:56 +01:00
|
|
|
|
|
|
|
with self.settings(
|
2021-02-12 08:20:45 +01:00
|
|
|
LDAP_APPEND_DOMAIN="zulip.com",
|
2021-02-12 08:19:30 +01:00
|
|
|
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
|
2019-10-16 18:27:53 +02:00
|
|
|
):
|
2020-02-19 19:40:49 +01:00
|
|
|
self.login_with_return("newuser@zulip.com", self.ldap_password("newuser"))
|
2018-11-29 16:32:17 +01:00
|
|
|
|
2020-03-12 14:17:25 +01:00
|
|
|
user = UserProfile.objects.get(delivery_email="newuser@zulip.com")
|
2023-03-15 20:18:09 +01:00
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=user).order_by(
|
|
|
|
"scheduled_timestamp"
|
|
|
|
)
|
|
|
|
self.assert_length(scheduled_emails, 3)
|
2020-08-07 01:09:47 +02:00
|
|
|
email_data = orjson.loads(scheduled_emails[0].data)
|
2018-11-29 16:32:17 +01:00
|
|
|
self.assertEqual(email_data["context"]["ldap"], True)
|
|
|
|
self.assertEqual(email_data["context"]["ldap_username"], "newuser")
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
@override_settings(
|
|
|
|
AUTHENTICATION_BACKENDS=(
|
2021-02-12 08:20:45 +01:00
|
|
|
"zproject.backends.ZulipLDAPAuthBackend",
|
|
|
|
"zproject.backends.ZulipDummyBackend",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
)
|
2018-11-29 16:32:17 +01:00
|
|
|
def test_day1_email_ldap_case_c_login_credentials(self) -> None:
|
2019-10-16 18:27:53 +02:00
|
|
|
self.init_default_ldap_database()
|
2021-02-12 08:20:45 +01:00
|
|
|
ldap_user_attr_map = {"full_name": "cn"}
|
2018-11-29 16:32:17 +01:00
|
|
|
|
|
|
|
with self.settings(
|
2021-02-12 08:20:45 +01:00
|
|
|
LDAP_EMAIL_ATTR="mail",
|
2021-02-12 08:19:30 +01:00
|
|
|
AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
|
2019-10-16 18:27:53 +02:00
|
|
|
):
|
2020-02-19 19:40:49 +01:00
|
|
|
self.login_with_return("newuser_with_email", self.ldap_password("newuser_with_email"))
|
2020-03-12 14:17:25 +01:00
|
|
|
user = UserProfile.objects.get(delivery_email="newuser_email@zulip.com")
|
2023-03-15 20:18:09 +01:00
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=user).order_by(
|
|
|
|
"scheduled_timestamp"
|
|
|
|
)
|
|
|
|
self.assert_length(scheduled_emails, 3)
|
2020-08-07 01:09:47 +02:00
|
|
|
email_data = orjson.loads(scheduled_emails[0].data)
|
2018-11-29 16:32:17 +01:00
|
|
|
self.assertEqual(email_data["context"]["ldap"], True)
|
2019-11-05 02:29:03 +01:00
|
|
|
self.assertEqual(email_data["context"]["ldap_username"], "newuser_with_email")
|
2018-11-14 12:46:56 +01:00
|
|
|
|
2018-11-14 12:58:35 +01:00
|
|
|
def test_followup_emails_count(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
2023-03-15 20:18:09 +01:00
|
|
|
iago = self.example_user("iago")
|
2018-11-14 12:58:35 +01:00
|
|
|
cordelia = self.example_user("cordelia")
|
2023-03-15 20:18:09 +01:00
|
|
|
realm = get_realm("zulip")
|
2018-11-14 12:58:35 +01:00
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
# Hamlet has account only in Zulip realm so day1, day2 and zulip_guide emails should be sent
|
2023-06-30 13:27:25 +02:00
|
|
|
send_account_registered_email(self.example_user("hamlet"))
|
2023-03-15 20:18:09 +01:00
|
|
|
enqueue_welcome_emails(self.example_user("hamlet"))
|
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=hamlet).order_by(
|
|
|
|
"scheduled_timestamp"
|
|
|
|
)
|
|
|
|
self.assert_length(scheduled_emails, 3)
|
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[0].data)["template_prefix"], "zerver/emails/followup_day1"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[1].data)["template_prefix"], "zerver/emails/followup_day2"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[2].data)["template_prefix"],
|
|
|
|
"zerver/emails/onboarding_zulip_guide",
|
|
|
|
)
|
|
|
|
|
|
|
|
ScheduledEmail.objects.all().delete()
|
|
|
|
|
|
|
|
# The onboarding_zulip_guide email should not be sent to non-admin users in organizations
|
|
|
|
# that are sent the `/for/communities/` guide; see note in enqueue_welcome_emails.
|
|
|
|
realm.org_type = Realm.ORG_TYPES["community"]["id"]
|
|
|
|
realm.save()
|
|
|
|
|
|
|
|
# Hamlet is not an admin so the `/for/communities/` zulip_guide should not be sent
|
2023-06-30 13:27:25 +02:00
|
|
|
send_account_registered_email(self.example_user("hamlet"))
|
2018-11-14 12:58:35 +01:00
|
|
|
enqueue_welcome_emails(self.example_user("hamlet"))
|
2019-04-29 07:00:03 +02:00
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=hamlet).order_by(
|
2021-02-12 08:19:30 +01:00
|
|
|
"scheduled_timestamp"
|
|
|
|
)
|
2021-07-13 19:39:37 +02:00
|
|
|
self.assert_length(scheduled_emails, 2)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[0].data)["template_prefix"], "zerver/emails/followup_day1"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[1].data)["template_prefix"], "zerver/emails/followup_day2"
|
|
|
|
)
|
|
|
|
|
|
|
|
ScheduledEmail.objects.all().delete()
|
|
|
|
|
|
|
|
# Iago is an admin so the `/for/communities/` zulip_guide should be sent
|
2023-06-30 13:27:25 +02:00
|
|
|
send_account_registered_email(self.example_user("iago"))
|
2023-03-15 20:18:09 +01:00
|
|
|
enqueue_welcome_emails(self.example_user("iago"))
|
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=iago).order_by("scheduled_timestamp")
|
|
|
|
self.assert_length(scheduled_emails, 3)
|
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[0].data)["template_prefix"], "zerver/emails/followup_day1"
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
orjson.loads(scheduled_emails[1].data)["template_prefix"], "zerver/emails/followup_day2"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[2].data)["template_prefix"],
|
|
|
|
"zerver/emails/onboarding_zulip_guide",
|
|
|
|
)
|
|
|
|
|
|
|
|
ScheduledEmail.objects.all().delete()
|
|
|
|
|
|
|
|
# The organization_type context for "education_nonprofit" orgs is simplified to be "education"
|
|
|
|
realm.org_type = Realm.ORG_TYPES["education_nonprofit"]["id"]
|
|
|
|
realm.save()
|
|
|
|
|
|
|
|
# Cordelia has account in more than 1 realm so day2 email should not be sent
|
2023-06-30 13:27:25 +02:00
|
|
|
send_account_registered_email(self.example_user("cordelia"))
|
2023-03-15 20:18:09 +01:00
|
|
|
enqueue_welcome_emails(self.example_user("cordelia"))
|
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=cordelia).order_by(
|
|
|
|
"scheduled_timestamp"
|
|
|
|
)
|
|
|
|
self.assert_length(scheduled_emails, 2)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
orjson.loads(scheduled_emails[0].data)["template_prefix"], "zerver/emails/followup_day1"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[1].data)["template_prefix"],
|
|
|
|
"zerver/emails/onboarding_zulip_guide",
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[1].data)["context"]["organization_type"],
|
|
|
|
"education",
|
|
|
|
)
|
2018-11-14 12:58:35 +01:00
|
|
|
|
|
|
|
ScheduledEmail.objects.all().delete()
|
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
# Only a subset of Realm.ORG_TYPES are sent the zulip_guide_followup email
|
|
|
|
realm.org_type = Realm.ORG_TYPES["other"]["id"]
|
|
|
|
realm.save()
|
|
|
|
|
|
|
|
# In this case, Cordelia should only be sent the day1 email
|
2023-06-30 13:27:25 +02:00
|
|
|
send_account_registered_email(self.example_user("cordelia"))
|
2023-03-15 20:18:09 +01:00
|
|
|
enqueue_welcome_emails(self.example_user("cordelia"))
|
2019-01-04 01:50:21 +01:00
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=cordelia)
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(scheduled_emails, 1)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[0].data)["template_prefix"], "zerver/emails/followup_day1"
|
|
|
|
)
|
2018-11-14 12:58:35 +01:00
|
|
|
|
2021-09-09 21:51:55 +02:00
|
|
|
def test_followup_emails_for_regular_realms(self) -> None:
|
|
|
|
cordelia = self.example_user("cordelia")
|
2023-06-30 13:27:25 +02:00
|
|
|
send_account_registered_email(self.example_user("cordelia"), realm_creation=True)
|
|
|
|
enqueue_welcome_emails(self.example_user("cordelia"))
|
2023-03-15 20:18:09 +01:00
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=cordelia).order_by(
|
|
|
|
"scheduled_timestamp"
|
|
|
|
)
|
|
|
|
assert scheduled_emails is not None
|
|
|
|
self.assert_length(scheduled_emails, 2)
|
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[0].data)["template_prefix"], "zerver/emails/followup_day1"
|
|
|
|
)
|
2021-09-09 21:51:55 +02:00
|
|
|
self.assertEqual(
|
2023-03-15 20:18:09 +01:00
|
|
|
orjson.loads(scheduled_emails[1].data)["template_prefix"],
|
|
|
|
"zerver/emails/onboarding_zulip_guide",
|
2021-09-09 21:51:55 +02:00
|
|
|
)
|
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
deliver_scheduled_emails(scheduled_emails[0])
|
2021-09-09 21:51:55 +02:00
|
|
|
from django.core.mail import outbox
|
|
|
|
|
|
|
|
self.assert_length(outbox, 1)
|
|
|
|
|
|
|
|
message = outbox[0]
|
2023-02-03 02:16:43 +01:00
|
|
|
self.assertIn("you have created a new Zulip organization", message.body)
|
2021-09-09 21:51:55 +02:00
|
|
|
self.assertNotIn("demo org", message.body)
|
|
|
|
|
|
|
|
def test_followup_emails_for_demo_realms(self) -> None:
|
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
cordelia.realm.demo_organization_scheduled_deletion_date = timezone_now() + timedelta(
|
|
|
|
days=30
|
|
|
|
)
|
|
|
|
cordelia.realm.save()
|
2023-06-30 13:27:25 +02:00
|
|
|
send_account_registered_email(self.example_user("cordelia"), realm_creation=True)
|
|
|
|
enqueue_welcome_emails(self.example_user("cordelia"))
|
2023-03-15 20:18:09 +01:00
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=cordelia).order_by(
|
|
|
|
"scheduled_timestamp"
|
|
|
|
)
|
|
|
|
assert scheduled_emails is not None
|
|
|
|
self.assert_length(scheduled_emails, 2)
|
|
|
|
self.assertEqual(
|
|
|
|
orjson.loads(scheduled_emails[0].data)["template_prefix"], "zerver/emails/followup_day1"
|
|
|
|
)
|
2021-09-09 21:51:55 +02:00
|
|
|
self.assertEqual(
|
2023-03-15 20:18:09 +01:00
|
|
|
orjson.loads(scheduled_emails[1].data)["template_prefix"],
|
|
|
|
"zerver/emails/onboarding_zulip_guide",
|
2021-09-09 21:51:55 +02:00
|
|
|
)
|
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
deliver_scheduled_emails(scheduled_emails[0])
|
2021-09-09 21:51:55 +02:00
|
|
|
from django.core.mail import outbox
|
|
|
|
|
|
|
|
self.assert_length(outbox, 1)
|
|
|
|
|
|
|
|
message = outbox[0]
|
2023-02-03 02:16:43 +01:00
|
|
|
self.assertIn("you have created a new demo Zulip organization", message.body)
|
2021-09-09 21:51:55 +02:00
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
def test_onboarding_zulip_guide_with_invalid_org_type(self) -> None:
|
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
|
|
|
|
invalid_org_type_id = 999
|
|
|
|
realm.org_type = invalid_org_type_id
|
|
|
|
realm.save()
|
|
|
|
|
|
|
|
with self.assertLogs(level="ERROR") as m:
|
|
|
|
enqueue_welcome_emails(self.example_user("cordelia"))
|
|
|
|
|
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=cordelia)
|
2023-06-30 13:27:25 +02:00
|
|
|
self.assert_length(scheduled_emails, 0)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual(
|
|
|
|
m.output,
|
|
|
|
[f"ERROR:root:Unknown organization type '{invalid_org_type_id}'"],
|
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-04-17 01:47:25 +02:00
|
|
|
class TestFollowupEmailDelay(ZulipTestCase):
|
2023-03-15 18:09:26 +01:00
|
|
|
def test_get_onboarding_email_schedule(self) -> None:
|
2022-04-17 01:47:25 +02:00
|
|
|
user_profile = self.example_user("hamlet")
|
2023-03-15 18:09:26 +01:00
|
|
|
dates_joined = {
|
|
|
|
"Monday": datetime(2018, 1, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
|
|
|
|
"Tuesday": datetime(2018, 1, 2, 1, 0, 0, 0, tzinfo=timezone.utc),
|
|
|
|
"Wednesday": datetime(2018, 1, 3, 1, 0, 0, 0, tzinfo=timezone.utc),
|
|
|
|
"Thursday": datetime(2018, 1, 4, 1, 0, 0, 0, tzinfo=timezone.utc),
|
|
|
|
"Friday": datetime(2018, 1, 5, 1, 0, 0, 0, tzinfo=timezone.utc),
|
|
|
|
"Saturday": datetime(2018, 1, 6, 1, 0, 0, 0, tzinfo=timezone.utc),
|
|
|
|
"Sunday": datetime(2018, 1, 7, 1, 0, 0, 0, tzinfo=timezone.utc),
|
|
|
|
}
|
2023-03-15 20:18:09 +01:00
|
|
|
days_delayed = {
|
|
|
|
"2": timedelta(days=2, hours=-1),
|
|
|
|
"4": timedelta(days=4, hours=-1),
|
|
|
|
"6": timedelta(days=6, hours=-1),
|
|
|
|
}
|
2023-03-15 18:09:26 +01:00
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
# joined Monday
|
2023-03-15 18:09:26 +01:00
|
|
|
user_profile.date_joined = dates_joined["Monday"]
|
|
|
|
onboarding_email_schedule = get_onboarding_email_schedule(user_profile)
|
2023-03-15 20:18:09 +01:00
|
|
|
|
2023-03-15 18:09:26 +01:00
|
|
|
# followup_day2 email sent on Wednesday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["followup_day2"],
|
2023-03-15 20:18:09 +01:00
|
|
|
days_delayed["2"],
|
2023-03-15 18:09:26 +01:00
|
|
|
)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual((dates_joined["Monday"] + days_delayed["2"]).isoweekday(), 3)
|
2023-03-15 18:09:26 +01:00
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
# onboarding_zulip_guide sent on Friday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["onboarding_zulip_guide"],
|
|
|
|
days_delayed["4"],
|
|
|
|
)
|
|
|
|
self.assertEqual((dates_joined["Monday"] + days_delayed["4"]).isoweekday(), 5)
|
|
|
|
|
|
|
|
# joined Tuesday
|
2023-03-15 18:09:26 +01:00
|
|
|
user_profile.date_joined = dates_joined["Tuesday"]
|
|
|
|
onboarding_email_schedule = get_onboarding_email_schedule(user_profile)
|
2023-03-15 20:18:09 +01:00
|
|
|
|
2023-03-15 18:09:26 +01:00
|
|
|
# followup_day2 email sent on Thursday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["followup_day2"],
|
2023-03-15 20:18:09 +01:00
|
|
|
days_delayed["2"],
|
2023-03-15 18:09:26 +01:00
|
|
|
)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual((dates_joined["Tuesday"] + days_delayed["2"]).isoweekday(), 4)
|
2023-03-15 18:09:26 +01:00
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
# onboarding_zulip_guide sent on Monday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["onboarding_zulip_guide"],
|
|
|
|
days_delayed["6"],
|
|
|
|
)
|
|
|
|
self.assertEqual((dates_joined["Tuesday"] + days_delayed["6"]).isoweekday(), 1)
|
|
|
|
|
|
|
|
# joined Wednesday
|
2023-03-15 18:09:26 +01:00
|
|
|
user_profile.date_joined = dates_joined["Wednesday"]
|
|
|
|
onboarding_email_schedule = get_onboarding_email_schedule(user_profile)
|
2023-03-15 20:18:09 +01:00
|
|
|
|
2023-03-15 18:09:26 +01:00
|
|
|
# followup_day2 email sent on Friday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["followup_day2"],
|
2023-03-15 20:18:09 +01:00
|
|
|
days_delayed["2"],
|
2023-03-15 18:09:26 +01:00
|
|
|
)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual((dates_joined["Wednesday"] + days_delayed["2"]).isoweekday(), 5)
|
2023-03-15 18:09:26 +01:00
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
# onboarding_zulip_guide sent on Tuesday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["onboarding_zulip_guide"],
|
|
|
|
days_delayed["6"],
|
|
|
|
)
|
|
|
|
self.assertEqual((dates_joined["Wednesday"] + days_delayed["6"]).isoweekday(), 2)
|
|
|
|
|
|
|
|
# joined Thursday
|
2023-03-15 18:09:26 +01:00
|
|
|
user_profile.date_joined = dates_joined["Thursday"]
|
|
|
|
onboarding_email_schedule = get_onboarding_email_schedule(user_profile)
|
2023-03-15 20:18:09 +01:00
|
|
|
|
|
|
|
# followup_day2 email sent on Monday
|
2023-03-15 18:09:26 +01:00
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["followup_day2"],
|
2023-03-15 20:18:09 +01:00
|
|
|
days_delayed["4"],
|
|
|
|
)
|
|
|
|
self.assertEqual((dates_joined["Thursday"] + days_delayed["4"]).isoweekday(), 1)
|
|
|
|
|
|
|
|
# onboarding_zulip_guide sent on Wednesday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["onboarding_zulip_guide"],
|
|
|
|
days_delayed["6"],
|
2023-03-15 18:09:26 +01:00
|
|
|
)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual((dates_joined["Thursday"] + days_delayed["6"]).isoweekday(), 3)
|
2023-03-15 18:09:26 +01:00
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
# joined Friday
|
2023-03-15 18:09:26 +01:00
|
|
|
user_profile.date_joined = dates_joined["Friday"]
|
|
|
|
onboarding_email_schedule = get_onboarding_email_schedule(user_profile)
|
2023-03-15 20:18:09 +01:00
|
|
|
|
|
|
|
# followup_day2 email sent on Tuesday
|
2023-03-15 18:09:26 +01:00
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["followup_day2"],
|
2023-03-15 20:18:09 +01:00
|
|
|
days_delayed["4"],
|
2023-03-15 18:09:26 +01:00
|
|
|
)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual((dates_joined["Friday"] + days_delayed["4"]).isoweekday(), 2)
|
2023-03-15 18:09:26 +01:00
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
# onboarding_zulip_guide sent on Thursday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["onboarding_zulip_guide"],
|
|
|
|
days_delayed["6"],
|
|
|
|
)
|
|
|
|
self.assertEqual((dates_joined["Friday"] + days_delayed["6"]).isoweekday(), 4)
|
|
|
|
|
|
|
|
# joined Saturday
|
2023-03-15 18:09:26 +01:00
|
|
|
user_profile.date_joined = dates_joined["Saturday"]
|
|
|
|
onboarding_email_schedule = get_onboarding_email_schedule(user_profile)
|
2023-03-15 20:18:09 +01:00
|
|
|
|
2023-03-15 18:09:26 +01:00
|
|
|
# followup_day2 email sent on Monday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["followup_day2"],
|
2023-03-15 20:18:09 +01:00
|
|
|
days_delayed["2"],
|
2023-03-15 18:09:26 +01:00
|
|
|
)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual((dates_joined["Saturday"] + days_delayed["2"]).isoweekday(), 1)
|
2023-03-15 18:09:26 +01:00
|
|
|
|
2023-03-15 20:18:09 +01:00
|
|
|
# onboarding_zulip_guide sent on Wednesday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["onboarding_zulip_guide"],
|
|
|
|
days_delayed["4"],
|
|
|
|
)
|
|
|
|
self.assertEqual((dates_joined["Saturday"] + days_delayed["4"]).isoweekday(), 3)
|
|
|
|
|
|
|
|
# joined Sunday
|
2023-03-15 18:09:26 +01:00
|
|
|
user_profile.date_joined = dates_joined["Sunday"]
|
|
|
|
onboarding_email_schedule = get_onboarding_email_schedule(user_profile)
|
2023-03-15 20:18:09 +01:00
|
|
|
|
2023-03-15 18:09:26 +01:00
|
|
|
# followup_day2 email sent on Tuesday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["followup_day2"],
|
2023-03-15 20:18:09 +01:00
|
|
|
days_delayed["2"],
|
2023-03-15 18:09:26 +01:00
|
|
|
)
|
2023-03-15 20:18:09 +01:00
|
|
|
self.assertEqual((dates_joined["Sunday"] + days_delayed["2"]).isoweekday(), 2)
|
|
|
|
|
|
|
|
# onboarding_zulip_guide sent on Thursday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["onboarding_zulip_guide"],
|
|
|
|
days_delayed["4"],
|
|
|
|
)
|
|
|
|
self.assertEqual((dates_joined["Sunday"] + days_delayed["4"]).isoweekday(), 4)
|
2023-03-15 18:09:26 +01:00
|
|
|
|
|
|
|
# Time offset of America/Phoenix is -07:00
|
|
|
|
user_profile.timezone = "America/Phoenix"
|
2023-03-15 20:18:09 +01:00
|
|
|
|
2023-03-15 18:09:26 +01:00
|
|
|
# Test date_joined == Friday in UTC, but Thursday in the user's time zone
|
2022-04-17 01:47:25 +02:00
|
|
|
user_profile.date_joined = datetime(2018, 1, 5, 1, 0, 0, 0, tzinfo=timezone.utc)
|
2023-03-15 18:09:26 +01:00
|
|
|
onboarding_email_schedule = get_onboarding_email_schedule(user_profile)
|
2023-03-15 20:18:09 +01:00
|
|
|
|
|
|
|
# followup_day2 email sent on Monday
|
2023-03-15 18:09:26 +01:00
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["followup_day2"],
|
2023-03-15 20:18:09 +01:00
|
|
|
days_delayed["4"],
|
|
|
|
)
|
|
|
|
|
|
|
|
# onboarding_zulip_guide sent on Wednesday
|
|
|
|
self.assertEqual(
|
|
|
|
onboarding_email_schedule["onboarding_zulip_guide"],
|
|
|
|
days_delayed["6"],
|
2023-03-15 18:09:26 +01:00
|
|
|
)
|
2022-08-02 07:16:49 +02:00
|
|
|
|
|
|
|
|
2023-06-30 13:27:25 +02:00
|
|
|
class TestCustomWelcomeEmailSender(ZulipTestCase):
|
|
|
|
def test_custom_welcome_email_sender(self) -> None:
|
2022-08-02 07:16:49 +02:00
|
|
|
name = "Nonreg Email"
|
|
|
|
email = self.nonreg_email("test")
|
|
|
|
with override_settings(
|
|
|
|
WELCOME_EMAIL_SENDER={
|
|
|
|
"name": name,
|
|
|
|
"email": email,
|
|
|
|
}
|
|
|
|
):
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
enqueue_welcome_emails(hamlet)
|
2023-03-15 20:18:09 +01:00
|
|
|
scheduled_emails = ScheduledEmail.objects.filter(users=hamlet).order_by(
|
|
|
|
"scheduled_timestamp"
|
|
|
|
)
|
2022-08-02 07:16:49 +02:00
|
|
|
email_data = orjson.loads(scheduled_emails[0].data)
|
|
|
|
self.assertEqual(email_data["from_name"], name)
|
|
|
|
self.assertEqual(email_data["from_address"], email)
|