2017-10-25 01:58:05 +02:00
|
|
|
import os
|
2020-05-26 07:16:25 +02:00
|
|
|
from unittest import mock
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-10-25 01:58:05 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zproject.email_backends import get_forward_address
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-10-25 01:58:05 +02:00
|
|
|
class EmailLogTest(ZulipTestCase):
|
2018-08-06 11:40:31 +02:00
|
|
|
def test_generate_and_clear_email_log(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
with self.settings(EMAIL_BACKEND="zproject.email_backends.EmailLogBackEnd"), mock.patch(
|
2022-05-03 03:46:20 +02:00
|
|
|
"zproject.email_backends.EmailLogBackEnd._do_send_messages", lambda *args: 1
|
2021-02-12 08:19:30 +01:00
|
|
|
), self.assertLogs(level="INFO") as m, self.settings(DEVELOPMENT_LOG_EMAILS=True):
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_get("/emails/generate/")
|
2018-08-06 11:40:31 +02:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIn("emails", result["Location"])
|
2017-10-25 01:58:05 +02:00
|
|
|
|
2018-08-06 11:40:31 +02:00
|
|
|
result = self.client_get("/emails/")
|
2024-02-09 22:04:45 +01:00
|
|
|
self.assert_in_success_response(["All emails sent in the Zulip"], result)
|
2017-10-25 01:58:05 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_get("/emails/clear/")
|
2018-08-06 11:40:31 +02:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_get(result["Location"])
|
2024-02-09 22:04:45 +01:00
|
|
|
self.assertIn("Manually generate most emails", str(result.content))
|
2021-02-12 08:19:30 +01:00
|
|
|
output_log = (
|
|
|
|
"INFO:root:Emails sent in development are available at http://testserver/emails"
|
|
|
|
)
|
2023-04-24 17:46:49 +02:00
|
|
|
self.assertEqual(m.output, [output_log for i in range(18)])
|
2017-10-25 01:58:05 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_forward_address_details(self) -> None:
|
2022-07-21 15:53:49 +02:00
|
|
|
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)
|