forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for email_mirror management command.
- Loading branch information
1 parent
9fb0719
commit 5691ca6
Showing
2 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
To: {stream_to_address} | ||
From: {sender} | ||
Subject: Testing Email Mirror | ||
Content-Type: text/plain; charset=utf-8; format=flowed | ||
Content-Transfer-Encoding: 7bit | ||
|
||
This is a plain-text message for testing Zulip. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
|
||
from zerver.models import ( | ||
get_display_recipient, get_stream, get_user_profile_by_email, | ||
Recipient, | ||
Recipient, get_realm, | ||
) | ||
|
||
from zerver.lib.actions import ( | ||
|
@@ -35,6 +35,12 @@ | |
import re | ||
import ujson | ||
import mock | ||
import os | ||
import sys | ||
from os.path import dirname, abspath | ||
from six.moves import cStringIO as StringIO | ||
|
||
from typing import Any, Callable, Mapping, Union | ||
|
||
|
||
class TestStreamEmailMessagesSuccess(AuthedTestCase): | ||
|
@@ -282,3 +288,36 @@ def test_reply_is_extracted_from_html(self): | |
message = most_recent_message(user_profile) | ||
|
||
self.assertEqual(message.content, 'Reply') | ||
|
||
MAILS_DIR = os.path.join(dirname(dirname(abspath(__file__))), "fixtures", "email") | ||
|
||
class TestCommandMTA(TestCase): | ||
|
||
@mock.patch('zerver.lib.queue.queue_json_publish') | ||
def test_success(self, mock_queue_json_publish): | ||
# type: (mock.Mock) -> None | ||
|
||
sender = "[email protected]" | ||
stream = get_stream("Denmark", get_realm("zulip.com")) | ||
stream_to_address = encode_email_address(stream) | ||
|
||
template_path = os.path.join(MAILS_DIR, "simple.txt") | ||
with open(template_path) as template_file: | ||
mail_template = template_file.read() | ||
mail = mail_template.format(stream_to_address=stream_to_address, sender=sender) | ||
|
||
def check_queue_json_publish(queue_name, event, processor): | ||
# type: (str, Union[Mapping[str, Any], str], Callable[[Any], None]) -> None | ||
self.assertEqual(queue_name, "email_mirror") | ||
self.assertEqual(event, {"rcpt_to": stream_to_address, "message": mail}) | ||
mock_queue_json_publish.side_effect = check_queue_json_publish | ||
|
||
original_stdin = sys.stdin | ||
try: | ||
sys.stdin = StringIO(mail) | ||
|
||
from zerver.management.commands import email_mirror | ||
command = email_mirror.Command() | ||
command.handle(recipient=stream_to_address) | ||
finally: | ||
sys.stdin = original_stdin |