matrix-webhook/tests/utils.py

51 lines
1.8 KiB
Python
Raw Normal View History

2021-06-16 19:23:25 -04:00
"""Utility tools to run tests."""
import asyncio
import os
import time
import unittest
import aiohttp
import yaml
from synapse._scripts.register_new_matrix_user import request_registration
2021-07-11 10:17:09 -04:00
BOT_URL = 'http://localhost:4785'
2021-06-16 19:23:25 -04:00
MATRIX_URL, MATRIX_ID, MATRIX_PW = (os.environ[v] for v in ['MATRIX_URL', 'MATRIX_ID', 'MATRIX_PW'])
class AbstractBotTest(unittest.IsolatedAsyncioTestCase):
"""Abstract test class."""
async def asyncSetUp(self):
"""Set up the test environment."""
# Wait for synapse and the bot to answer
self.assertTrue(
all(await asyncio.gather(
wait_available(f'{MATRIX_URL}/_matrix/client/r0/login', 'flows'),
2021-07-11 10:17:09 -04:00
wait_available(BOT_URL, 'status'),
2021-06-16 19:23:25 -04:00
)))
# Try to register an user for the bot. Don't worry if it already exists.
2021-07-11 10:17:09 -04:00
with open('/srv/homeserver.yaml') as f:
2021-06-16 19:23:25 -04:00
secret = yaml.safe_load(f.read()).get("registration_shared_secret", None)
request_registration(MATRIX_ID, MATRIX_PW, MATRIX_URL, secret, admin=False, user_type=None, exit=lambda x: x)
async def check_json(session: aiohttp.ClientSession, url: str, key: str) -> bool:
"""Ensure a service at a given url answers with valid json containing a certain key."""
try:
async with session.get(url) as response:
data = await response.json()
return key in data
except aiohttp.client_exceptions.ClientConnectorError:
return False
async def wait_available(url: str, key: str, timeout: int = 60) -> bool:
"""Wait until a service answer correctly or timeout."""
start = time.time()
async with aiohttp.ClientSession() as session:
while True:
if await check_json(session, url, key):
return True
if time.time() > start + timeout:
return False