Skip to content

Commit

Permalink
Add tests for email_mirror management command.
Browse files Browse the repository at this point in the history
  • Loading branch information
sharmaeklavya2 authored and timabbott committed Jul 30, 2016
1 parent 9fb0719 commit 5691ca6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
7 changes: 7 additions & 0 deletions zerver/fixtures/email/simple.txt
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.
41 changes: 40 additions & 1 deletion zerver/tests/test_email_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit 5691ca6

Please sign in to comment.