2021-06-16 19:23:25 -04:00
|
|
|
"""Main test module."""
|
|
|
|
|
|
|
|
import os
|
2021-07-12 19:16:24 -04:00
|
|
|
import unittest
|
2021-06-16 19:23:25 -04:00
|
|
|
|
2021-07-12 19:16:24 -04:00
|
|
|
import httpx
|
2021-06-16 19:23:25 -04:00
|
|
|
import nio
|
|
|
|
|
2021-07-12 19:16:24 -04:00
|
|
|
from .start import BOT_URL, MATRIX_ID, MATRIX_PW, MATRIX_URL
|
2021-06-16 19:23:25 -04:00
|
|
|
|
|
|
|
KEY = os.environ['API_KEY']
|
2021-07-12 19:16:24 -04:00
|
|
|
FULL_ID = f'@{MATRIX_ID}:{MATRIX_URL.split("/")[2]}'
|
2021-06-16 19:23:25 -04:00
|
|
|
|
|
|
|
|
2021-07-12 19:16:24 -04:00
|
|
|
def bot_req(req=None, key=None, room_id=None):
|
|
|
|
"""Bot requests boilerplate."""
|
|
|
|
if key is not None:
|
|
|
|
req['key'] = key
|
|
|
|
url = BOT_URL if room_id is None else f'{BOT_URL}/{room_id}'
|
|
|
|
return httpx.post(url, json=req).json()
|
|
|
|
|
|
|
|
|
|
|
|
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>')
|
2021-07-12 19:16:24 -04:00
|
|
|
|
|
|
|
async def test_z_disconnected(self):
|
|
|
|
"""Send a message after disconnection, and check the error."""
|
|
|
|
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
|
|
|
|
await client.login(MATRIX_PW)
|
|
|
|
token = client.access_token
|
|
|
|
|
|
|
|
resp = httpx.post(f'{MATRIX_URL}/_synapse/admin/v1/deactivate/{FULL_ID}',
|
|
|
|
json={'erase': True},
|
|
|
|
params={'access_token': token})
|
|
|
|
self.assertEqual(resp.json(), {'id_server_unbind_result': 'success'})
|
|
|
|
|
|
|
|
await client.logout(all_devices=True)
|
2021-06-16 19:23:25 -04:00
|
|
|
await client.close()
|
2021-07-12 19:16:24 -04:00
|
|
|
|
|
|
|
# TODO: I was hopping that one wouldn't be happy
|
|
|
|
self.assertEqual(bot_req({'text': 'bye'}, KEY), {'status': 200, 'ret': 'OK'})
|