clean tests

This commit is contained in:
Guilhem Saurel
2021-07-13 01:16:24 +02:00
parent 999b824874
commit 6a2e0336d9
4 changed files with 80 additions and 72 deletions

View File

@@ -1,30 +1,35 @@
"""Main test module."""
import json
import os
import unittest
import aiohttp
import httpx
import nio
from .utils import BOT_URL, MATRIX_ID, MATRIX_PW, MATRIX_URL, AbstractBotTest
from .start import BOT_URL, MATRIX_ID, MATRIX_PW, MATRIX_URL
KEY = os.environ['API_KEY']
FULL_ID = f'@{MATRIX_ID}:{MATRIX_URL.split("/")[2]}'
class BotTest(AbstractBotTest):
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):
"""Main test class."""
async def test_errors(self):
def test_errors(self):
"""Check the bot's error paths."""
async with aiohttp.ClientSession() as session:
async with session.get(BOT_URL) as response:
self.assertEqual(await response.json(), {'status': 400, 'ret': 'Invalid JSON'})
async with session.post(BOT_URL, data=json.dumps({'toto': 3})) as response:
self.assertEqual(await response.json(), {'status': 400, 'ret': 'Missing text and/or API key property'})
async with session.post(BOT_URL, data=json.dumps({'text': 3, 'key': None})) as response:
self.assertEqual(await response.json(), {'status': 401, 'ret': 'Invalid API key'})
async with session.post(BOT_URL, data=json.dumps({'text': 3, 'key': KEY})) as response:
# TODO: we are not sending to a real room, so this should not be "OK"
self.assertEqual(await response.json(), {'status': 200, 'ret': 'OK'})
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'})
async def test_message(self):
"""Send a markdown message, and check the result."""
@@ -33,19 +38,32 @@ class BotTest(AbstractBotTest):
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
await client.login(MATRIX_PW)
room = await client.room_create()
url = f'{BOT_URL}/{room.room_id}'
async with aiohttp.ClientSession() as session:
async with session.post(url, data=json.dumps({'text': text, 'key': KEY})) as response:
self.assertEqual(await response.json(), {'status': 200, 'ret': 'OK'})
self.assertEqual(bot_req({'text': text}, 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, '@bot:tests')
self.assertEqual(message.sender, FULL_ID)
self.assertEqual(message.body, text)
self.assertEqual(message.formatted_body, '<h1>Hello</h1>')
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)
await client.close()
# TODO: I was hopping that one wouldn't be happy
self.assertEqual(bot_req({'text': 'bye'}, KEY), {'status': 200, 'ret': 'OK'})