clean tests

This commit is contained in:
Guilhem Saurel 2021-07-13 10:28:40 +02:00
parent 2d8c68665e
commit 6633020fba
5 changed files with 29 additions and 39 deletions

View file

@ -4,11 +4,9 @@ jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
- name: Checkout
uses: actions/checkout@v2
- name: Start
run: docker-compose -f test.yml up -d
- name: Tests
run: docker-compose -f test.yml run --entrypoint "" tests ./tests/start.py
- name: "Upload coverage to Codecov"
run: docker-compose -f test.yml up --exit-code-from tests
- name: Coverage
uses: codecov/codecov-action@v1

View file

@ -5,6 +5,7 @@ services:
build:
context: .
dockerfile: tests/Dockerfile
entrypoint: ""
env_file:
- tests/.env
volumes:

View file

@ -15,3 +15,5 @@ RUN chown -R 991:991 .
RUN pip install --no-cache-dir markdown matrix-nio httpx coverage
WORKDIR /app
CMD ./tests/start.py

View file

@ -11,7 +11,16 @@ import yaml
from synapse._scripts.register_new_matrix_user import request_registration
BOT_URL = 'http://localhost:4785'
MATRIX_URL, MATRIX_ID, MATRIX_PW = (environ[v] for v in ['MATRIX_URL', 'MATRIX_ID', 'MATRIX_PW'])
KEY, MATRIX_URL, MATRIX_ID, MATRIX_PW = (environ[v] for v in ['API_KEY', 'MATRIX_URL', 'MATRIX_ID', 'MATRIX_PW'])
FULL_ID = f'@{MATRIX_ID}:{MATRIX_URL.split("/")[2]}'
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()
def wait_available(url: str, key: str, timeout: int = 10) -> bool:
@ -34,21 +43,31 @@ def wait_available(url: str, key: str, timeout: int = 10) -> bool:
def run_and_test():
"""Launch the bot and its tests."""
# Start the server, and wait for it
srv = Popen(['python', '-m', 'synapse.app.homeserver', '--config-path', '/srv/homeserver.yaml'])
if not wait_available(f'{MATRIX_URL}/_matrix/client/r0/login', 'flows'):
return False
# Try to register a user for the bot.
# Register a user for the bot.
with open('/srv/homeserver.yaml') as f:
secret = yaml.safe_load(f.read()).get("registration_shared_secret", None)
request_registration(MATRIX_ID, MATRIX_PW, MATRIX_URL, secret, admin=True)
# Start the bot, and wait for it
bot = Popen(['coverage', 'run', 'matrix_webhook.py'])
if not wait_available(BOT_URL, 'status'):
return False
# Run the main unittest module
ret = main(module=None, exit=False).result.wasSuccessful()
srv.terminate()
# TODO Check what the bot says when the server is offline
# print(bot_req({'text': 'bye'}, KEY), {'status': 200, 'ret': 'OK'})
bot.terminate()
for cmd in ['report', 'html', 'xml']:
run(['coverage', cmd])
return ret

View file

@ -1,23 +1,10 @@
"""Main test module."""
import os
import unittest
import httpx
import nio
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]}'
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()
from .start import FULL_ID, KEY, MATRIX_ID, MATRIX_PW, MATRIX_URL, bot_req
class BotTest(unittest.IsolatedAsyncioTestCase):
@ -50,20 +37,3 @@ class BotTest(unittest.IsolatedAsyncioTestCase):
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'})