2018-06-17 14:51:47 -04:00
|
|
|
from matrix_bridge import check_zulip_message_validity
|
|
|
|
|
2018-06-03 02:14:16 -04:00
|
|
|
from unittest import TestCase
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
import os
|
|
|
|
|
2018-06-03 12:37:17 -04:00
|
|
|
import shutil
|
|
|
|
|
|
|
|
from contextlib import contextmanager
|
|
|
|
from tempfile import mkdtemp
|
|
|
|
|
2018-06-03 02:14:16 -04:00
|
|
|
script_file = "matrix_bridge.py"
|
|
|
|
script_dir = os.path.dirname(__file__)
|
|
|
|
script = os.path.join(script_dir, script_file)
|
|
|
|
|
2018-06-03 12:37:17 -04:00
|
|
|
from typing import List, Iterator
|
|
|
|
|
|
|
|
sample_config_path = "matrix_test.conf"
|
|
|
|
|
|
|
|
sample_config_text = """[matrix]
|
|
|
|
host = https://matrix.org
|
|
|
|
username = username
|
|
|
|
password = password
|
|
|
|
room_id = #zulip:matrix.org
|
|
|
|
|
|
|
|
[zulip]
|
|
|
|
email = glitch-bot@chat.zulip.org
|
|
|
|
api_key = aPiKeY
|
|
|
|
site = https://chat.zulip.org
|
|
|
|
stream = test here
|
|
|
|
topic = matrix
|
2018-06-03 02:18:07 -04:00
|
|
|
|
2018-06-03 12:37:17 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def new_temp_dir():
|
|
|
|
# type: () -> Iterator[str]
|
|
|
|
path = mkdtemp()
|
|
|
|
yield path
|
|
|
|
shutil.rmtree(path)
|
2018-06-03 02:14:16 -04:00
|
|
|
|
2018-06-17 14:51:47 -04:00
|
|
|
class MatrixBridgeScriptTests(TestCase):
|
2018-06-03 02:18:07 -04:00
|
|
|
def output_from_script(self, options):
|
|
|
|
# type: (List[str]) -> List[str]
|
|
|
|
popen = Popen(["python", script] + options, stdin=PIPE, stdout=PIPE, universal_newlines=True)
|
|
|
|
return popen.communicate()[0].strip().split("\n")
|
|
|
|
|
2018-06-03 02:14:16 -04:00
|
|
|
def test_no_args(self):
|
|
|
|
# type: () -> None
|
2018-06-03 02:18:07 -04:00
|
|
|
output_lines = self.output_from_script([])
|
2018-06-03 02:14:16 -04:00
|
|
|
expected_lines = [
|
|
|
|
"Options required: -c or --config to run, OR --write-sample-config.",
|
|
|
|
"usage: {} [-h]".format(script_file)
|
|
|
|
]
|
|
|
|
for expected, output in zip(expected_lines, output_lines):
|
|
|
|
self.assertIn(expected, output)
|
|
|
|
|
|
|
|
def test_help_usage_and_description(self):
|
|
|
|
# type: () -> None
|
2018-06-03 02:18:07 -04:00
|
|
|
output_lines = self.output_from_script(["-h"])
|
2018-06-03 02:14:16 -04:00
|
|
|
usage = "usage: {} [-h]".format(script_file)
|
|
|
|
description = "Script to bridge"
|
|
|
|
self.assertIn(usage, output_lines[0])
|
|
|
|
blank_lines = [num for num, line in enumerate(output_lines) if line == '']
|
|
|
|
# There should be blank lines in the output
|
|
|
|
self.assertTrue(blank_lines)
|
|
|
|
# There should be finite output
|
|
|
|
self.assertTrue(len(output_lines) > blank_lines[0])
|
|
|
|
# Minimal description should be in the first line of the 2nd "paragraph"
|
|
|
|
self.assertIn(description, output_lines[blank_lines[0] + 1])
|
2018-06-03 12:37:17 -04:00
|
|
|
|
|
|
|
def test_write_sample_config(self):
|
|
|
|
# type: () -> None
|
|
|
|
with new_temp_dir() as tempdir:
|
|
|
|
path = os.path.join(tempdir, sample_config_path)
|
|
|
|
output_lines = self.output_from_script(["--write-sample-config", path])
|
|
|
|
self.assertEqual(output_lines, ["Wrote sample configuration to '{}'".format(path)])
|
|
|
|
|
|
|
|
with open(path) as sample_file:
|
|
|
|
self.assertEqual(sample_file.read(), sample_config_text)
|
2018-06-17 14:51:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
class MatrixBridgeZulipToMatrixTests(TestCase):
|
|
|
|
def test_zulip_message_validity_success(self):
|
|
|
|
# type: () -> None
|
|
|
|
zulip_config = dict(stream="some stream",
|
|
|
|
topic="some topic",
|
|
|
|
email="some@email")
|
|
|
|
msg = dict(
|
|
|
|
sender_email="John@Smith.smith", # must not be equal to config:email
|
|
|
|
type="stream", # Can only mirror Zulip streams
|
|
|
|
display_recipient=zulip_config['stream'],
|
|
|
|
subject=zulip_config['topic']
|
|
|
|
)
|
|
|
|
# Ensure the test inputs are valid for success
|
|
|
|
assert msg['sender_email'] != zulip_config['email']
|
|
|
|
|
|
|
|
self.assertTrue(check_zulip_message_validity(msg, zulip_config))
|
2018-06-17 14:56:22 -04:00
|
|
|
|
|
|
|
def test_zulip_message_validity_failure(self):
|
|
|
|
# type: () -> None
|
|
|
|
zulip_config = dict(stream="some stream",
|
|
|
|
topic="some topic",
|
|
|
|
email="some@email")
|
|
|
|
msg = dict(
|
|
|
|
sender_email="John@Smith.smith", # must not be equal to config:email
|
|
|
|
type="stream", # Can only mirror Zulip streams
|
|
|
|
display_recipient=zulip_config['stream'],
|
|
|
|
subject=zulip_config['topic']
|
|
|
|
)
|
|
|
|
|
|
|
|
msg_wrong_stream = dict(msg, display_recipient='foo')
|
|
|
|
self.assertFalse(check_zulip_message_validity(msg_wrong_stream, zulip_config))
|
|
|
|
|
|
|
|
msg_wrong_topic = dict(msg, subject='foo')
|
|
|
|
self.assertFalse(check_zulip_message_validity(msg_wrong_topic, zulip_config))
|
|
|
|
|
|
|
|
msg_not_stream = dict(msg, type="private")
|
|
|
|
self.assertFalse(check_zulip_message_validity(msg_not_stream, zulip_config))
|
|
|
|
|
|
|
|
msg_from_bot = dict(msg, sender_email=zulip_config['email'])
|
|
|
|
self.assertFalse(check_zulip_message_validity(msg_from_bot, zulip_config))
|