matrix-webhook/tests/tests.py

170 lines
5.8 KiB
Python
Raw Normal View History

2021-06-16 19:23:25 -04:00
"""Main test module."""
2021-07-12 19:16:24 -04:00
import unittest
2021-06-16 19:23:25 -04:00
import nio
2021-07-13 04:28:40 -04:00
from .start import FULL_ID, KEY, MATRIX_ID, MATRIX_PW, MATRIX_URL, bot_req
2021-07-12 19:16:24 -04:00
class BotTest(unittest.IsolatedAsyncioTestCase):
2021-06-16 19:23:25 -04:00
"""Main test class."""
2021-07-13 05:28:13 -04:00
2021-07-12 19:16:24 -04:00
def test_errors(self):
2021-06-16 19:23:25 -04:00
"""Check the bot's error paths."""
2021-07-13 05:28:13 -04:00
self.assertEqual(bot_req(), {"status": 400, "ret": "Invalid JSON"})
self.assertEqual(
bot_req({"toto": 3}),
{"status": 400, "ret": "Missing body, key, room_id"},
2021-07-13 05:28:13 -04:00
)
self.assertEqual(
bot_req({"body": 3}, "wrong_key", "wrong_room"),
{"status": 401, "ret": "Invalid API key"},
)
self.assertEqual(
bot_req({"body": 3}, "wrong_key", "wrong_room", key_as_param=True),
{"status": 401, "ret": "Invalid API key"},
2021-07-13 05:28:13 -04:00
)
2021-07-31 07:06:52 -04:00
self.assertEqual(
bot_req({"body": 3}, KEY, params={"formatter": "wrong_formatter"}),
{"status": 400, "ret": "Unknown formatter"},
)
2021-08-27 12:04:34 -04:00
# TODO: if the client from matrix_webhook has olm support,
# this won't be a 403 from synapse, but a LocalProtocolError from matrix_webhook
2021-07-14 17:25:24 -04:00
self.assertEqual(
bot_req({"body": 3}, KEY, "wrong_room"),
{"status": 403, "ret": "Unknown room"},
2021-07-14 17:25:24 -04:00
)
self.assertEqual(
bot_req({"body": 3}, KEY, "wrong_room", key_as_param=True),
{"status": 403, "ret": "Unknown room"},
)
2021-06-16 19:23:25 -04:00
async def test_message(self):
2021-07-31 05:21:29 -04:00
"""Send a markdown message with the old format, and check the result."""
2021-07-13 05:28:13 -04:00
text = "# Hello"
2021-06-16 19:23:25 -04:00
messages = []
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
await client.login(MATRIX_PW)
room = await client.room_create()
2021-07-13 05:28:13 -04:00
self.assertEqual(
bot_req({"text": text}, KEY, room.room_id), {"status": 200, "ret": "OK"}
)
2021-06-16 19:23:25 -04:00
sync = await client.sync()
messages = await client.room_messages(room.room_id, sync.next_batch)
2021-07-12 19:16:24 -04:00
await client.close()
2021-06-16 19:23:25 -04:00
message = messages.chunk[0]
2021-07-12 19:16:24 -04:00
self.assertEqual(message.sender, FULL_ID)
2021-06-16 19:23:25 -04:00
self.assertEqual(message.body, text)
2021-07-13 05:28:13 -04:00
self.assertEqual(message.formatted_body, "<h1>Hello</h1>")
2021-07-14 17:25:24 -04:00
async def test_room_id_req(self):
"""Send a markdown message in a room given as data, and check the result."""
body = "# Hello"
messages = []
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
await client.login(MATRIX_PW)
room = await client.room_create()
self.assertEqual(
bot_req({"body": body, "room_id": room.room_id}, KEY, room.room_id),
{"status": 200, "ret": "OK"},
)
sync = await client.sync()
messages = await client.room_messages(room.room_id, sync.next_batch)
await client.close()
message = messages.chunk[0]
self.assertEqual(message.sender, FULL_ID)
self.assertEqual(message.body, body)
self.assertEqual(message.formatted_body, "<h1>Hello</h1>")
async def test_room_id_parameter(self):
"""Send a markdown message in a room given as parameter, and check the result."""
body = "# Hello"
messages = []
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
await client.login(MATRIX_PW)
room = await client.room_create()
self.assertEqual(
bot_req({"body": body}, KEY, room.room_id, room_as_parameter=True),
{"status": 200, "ret": "OK"},
)
sync = await client.sync()
messages = await client.room_messages(room.room_id, sync.next_batch)
await client.close()
message = messages.chunk[0]
self.assertEqual(message.sender, FULL_ID)
self.assertEqual(message.body, body)
self.assertEqual(message.formatted_body, "<h1>Hello</h1>")
2021-07-31 05:21:29 -04:00
async def test_markdown_body(self):
"""Send a markdown message, and check the result."""
body = "# Hello"
messages = []
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
await client.login(MATRIX_PW)
room = await client.room_create()
self.assertEqual(
bot_req({"body": body}, KEY, room.room_id), {"status": 200, "ret": "OK"}
)
sync = await client.sync()
messages = await client.room_messages(room.room_id, sync.next_batch)
await client.close()
message = messages.chunk[0]
self.assertEqual(message.sender, FULL_ID)
self.assertEqual(message.body, body)
self.assertEqual(message.formatted_body, "<h1>Hello</h1>")
async def test_formatted_body(self):
"""Send a formatted message, and check the result."""
body = "Formatted message"
formatted_body = "<del>markdown</del><strong>Formatted</strong> message"
messages = []
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
await client.login(MATRIX_PW)
room = await client.room_create()
self.assertEqual(
bot_req(
{"body": body, "formatted_body": formatted_body}, KEY, room.room_id
),
{"status": 200, "ret": "OK"},
)
sync = await client.sync()
messages = await client.room_messages(room.room_id, sync.next_batch)
await client.close()
message = messages.chunk[0]
self.assertEqual(message.sender, FULL_ID)
self.assertEqual(message.body, body)
self.assertEqual(message.formatted_body, formatted_body)
2021-07-14 17:25:24 -04:00
async def test_reconnect(self):
"""Check the reconnecting path."""
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
await client.login(MATRIX_PW)
room = await client.room_create()
await client.logout(all_devices=True)
await client.close()
self.assertEqual(
2021-07-31 05:21:29 -04:00
bot_req({"body": "Re"}, KEY, room.room_id),
2021-07-14 17:25:24 -04:00
{"status": 200, "ret": "OK"},
)