2021-06-17 01:23:25 +02:00
|
|
|
"""Main test module."""
|
|
|
|
|
2021-07-13 01:16:24 +02:00
|
|
|
import unittest
|
2021-06-17 01:23:25 +02:00
|
|
|
|
|
|
|
import nio
|
|
|
|
|
2021-07-13 10:28:40 +02:00
|
|
|
from .start import FULL_ID, KEY, MATRIX_ID, MATRIX_PW, MATRIX_URL, bot_req
|
2021-07-13 01:16:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BotTest(unittest.IsolatedAsyncioTestCase):
|
2021-06-17 01:23:25 +02:00
|
|
|
"""Main test class."""
|
2021-07-13 11:28:13 +02:00
|
|
|
|
2021-07-13 01:16:24 +02:00
|
|
|
def test_errors(self):
|
2021-06-17 01:23:25 +02:00
|
|
|
"""Check the bot's error paths."""
|
2021-07-13 11:28:13 +02:00
|
|
|
self.assertEqual(bot_req(), {"status": 400, "ret": "Invalid JSON"})
|
|
|
|
self.assertEqual(
|
|
|
|
bot_req({"toto": 3}),
|
2021-07-31 15:16:07 +02:00
|
|
|
{"status": 400, "ret": "Missing body, key, room_id"},
|
2021-07-13 11:28:13 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2021-07-31 15:16:07 +02:00
|
|
|
bot_req({"body": 3}, "wrong_key", "wrong_room"),
|
|
|
|
{"status": 401, "ret": "Invalid API key"},
|
2021-07-31 11:31:31 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2021-07-31 15:16:07 +02:00
|
|
|
bot_req({"body": 3}, "wrong_key", "wrong_room", key_as_param=True),
|
2021-07-31 11:31:31 +02:00
|
|
|
{"status": 401, "ret": "Invalid API key"},
|
2021-07-13 11:28:13 +02:00
|
|
|
)
|
2021-07-31 13:06:52 +02:00
|
|
|
self.assertEqual(
|
|
|
|
bot_req({"body": 3}, KEY, params={"formatter": "wrong_formatter"}),
|
|
|
|
{"status": 400, "ret": "Unknown formatter"},
|
|
|
|
)
|
2021-08-27 18:04:34 +02: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 23:25:24 +02:00
|
|
|
self.assertEqual(
|
2021-07-31 15:16:07 +02:00
|
|
|
bot_req({"body": 3}, KEY, "wrong_room"),
|
2021-09-28 10:39:41 +02:00
|
|
|
{"status": 400, "ret": "wrong_room was not legal room ID or room alias"},
|
2021-07-14 23:25:24 +02:00
|
|
|
)
|
2021-07-31 11:31:31 +02:00
|
|
|
self.assertEqual(
|
2021-07-31 15:16:07 +02:00
|
|
|
bot_req({"body": 3}, KEY, "wrong_room", key_as_param=True),
|
2021-09-28 10:39:41 +02:00
|
|
|
{"status": 400, "ret": "wrong_room was not legal room ID or room alias"},
|
2021-07-31 11:31:31 +02:00
|
|
|
)
|
2021-06-17 01:23:25 +02:00
|
|
|
|
|
|
|
async def test_message(self):
|
2021-07-31 11:21:29 +02:00
|
|
|
"""Send a markdown message with the old format, and check the result."""
|
2021-07-13 11:28:13 +02:00
|
|
|
text = "# Hello"
|
2021-06-17 01:23:25 +02:00
|
|
|
messages = []
|
|
|
|
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
|
|
|
|
|
|
|
|
await client.login(MATRIX_PW)
|
|
|
|
room = await client.room_create()
|
|
|
|
|
2021-07-13 11:28:13 +02:00
|
|
|
self.assertEqual(
|
|
|
|
bot_req({"text": text}, KEY, room.room_id), {"status": 200, "ret": "OK"}
|
|
|
|
)
|
2021-06-17 01:23:25 +02:00
|
|
|
|
|
|
|
sync = await client.sync()
|
|
|
|
messages = await client.room_messages(room.room_id, sync.next_batch)
|
2021-07-13 01:16:24 +02:00
|
|
|
await client.close()
|
2021-06-17 01:23:25 +02:00
|
|
|
|
|
|
|
message = messages.chunk[0]
|
2021-07-13 01:16:24 +02:00
|
|
|
self.assertEqual(message.sender, FULL_ID)
|
2021-06-17 01:23:25 +02:00
|
|
|
self.assertEqual(message.body, text)
|
2021-07-13 11:28:13 +02:00
|
|
|
self.assertEqual(message.formatted_body, "<h1>Hello</h1>")
|
2021-07-14 23:25:24 +02:00
|
|
|
|
2021-07-31 15:16:07 +02: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):
|
2021-08-27 23:47:07 +02:00
|
|
|
"""Send a markdown message in a room given as parameter."""
|
2021-07-31 15:16:07 +02:00
|
|
|
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 11:21:29 +02: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>")
|
|
|
|
|
2021-07-31 12:03:26 +02:00
|
|
|
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 23:25:24 +02: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 11:21:29 +02:00
|
|
|
bot_req({"body": "Re"}, KEY, room.room_id),
|
2021-07-14 23:25:24 +02:00
|
|
|
{"status": 200, "ret": "OK"},
|
|
|
|
)
|