matrix-webhook/tests/tests.py

40 lines
1.4 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-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-12 19:16:24 -04:00
self.assertEqual(bot_req(), {'status': 400, 'ret': 'Invalid JSON'})
self.assertEqual(bot_req({'toto': 3}), {'status': 400, 'ret': 'Missing text and/or API key property'})
self.assertEqual(bot_req({'text': 3, 'key': None}), {'status': 401, 'ret': 'Invalid API key'})
# TODO: we are not sending to a real room, so this should not be "OK"
self.assertEqual(bot_req({'text': 3}, KEY), {'status': 200, 'ret': 'OK'})
2021-06-16 19:23:25 -04:00
async def test_message(self):
"""Send a markdown message, and check the result."""
text = '# Hello'
messages = []
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
await client.login(MATRIX_PW)
room = await client.room_create()
2021-07-12 19:16:24 -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)
self.assertEqual(message.formatted_body, '<h1>Hello</h1>')