clean tests
This commit is contained in:
parent
2d8c68665e
commit
6633020fba
5 changed files with 29 additions and 39 deletions
8
.github/workflows/test.yml
vendored
8
.github/workflows/test.yml
vendored
|
@ -4,11 +4,9 @@ jobs:
|
||||||
tests:
|
tests:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Check out repository code
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
- name: Start
|
|
||||||
run: docker-compose -f test.yml up -d
|
|
||||||
- name: Tests
|
- name: Tests
|
||||||
run: docker-compose -f test.yml run --entrypoint "" tests ./tests/start.py
|
run: docker-compose -f test.yml up --exit-code-from tests
|
||||||
- name: "Upload coverage to Codecov"
|
- name: Coverage
|
||||||
uses: codecov/codecov-action@v1
|
uses: codecov/codecov-action@v1
|
||||||
|
|
1
test.yml
1
test.yml
|
@ -5,6 +5,7 @@ services:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: tests/Dockerfile
|
dockerfile: tests/Dockerfile
|
||||||
|
entrypoint: ""
|
||||||
env_file:
|
env_file:
|
||||||
- tests/.env
|
- tests/.env
|
||||||
volumes:
|
volumes:
|
||||||
|
|
|
@ -15,3 +15,5 @@ RUN chown -R 991:991 .
|
||||||
RUN pip install --no-cache-dir markdown matrix-nio httpx coverage
|
RUN pip install --no-cache-dir markdown matrix-nio httpx coverage
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
CMD ./tests/start.py
|
||||||
|
|
|
@ -11,7 +11,16 @@ import yaml
|
||||||
from synapse._scripts.register_new_matrix_user import request_registration
|
from synapse._scripts.register_new_matrix_user import request_registration
|
||||||
|
|
||||||
BOT_URL = 'http://localhost:4785'
|
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:
|
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():
|
def run_and_test():
|
||||||
"""Launch the bot and its tests."""
|
"""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'):
|
if not wait_available(f'{MATRIX_URL}/_matrix/client/r0/login', 'flows'):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Try to register a user for the bot.
|
# Register a user for the bot.
|
||||||
with open('/srv/homeserver.yaml') as f:
|
with open('/srv/homeserver.yaml') as f:
|
||||||
secret = yaml.safe_load(f.read()).get("registration_shared_secret", None)
|
secret = yaml.safe_load(f.read()).get("registration_shared_secret", None)
|
||||||
request_registration(MATRIX_ID, MATRIX_PW, MATRIX_URL, secret, admin=True)
|
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'])
|
bot = Popen(['coverage', 'run', 'matrix_webhook.py'])
|
||||||
|
|
||||||
if not wait_available(BOT_URL, 'status'):
|
if not wait_available(BOT_URL, 'status'):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Run the main unittest module
|
||||||
ret = main(module=None, exit=False).result.wasSuccessful()
|
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()
|
bot.terminate()
|
||||||
|
|
||||||
for cmd in ['report', 'html', 'xml']:
|
for cmd in ['report', 'html', 'xml']:
|
||||||
run(['coverage', cmd])
|
run(['coverage', cmd])
|
||||||
return ret
|
return ret
|
||||||
|
|
|
@ -1,23 +1,10 @@
|
||||||
"""Main test module."""
|
"""Main test module."""
|
||||||
|
|
||||||
import os
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import httpx
|
|
||||||
import nio
|
import nio
|
||||||
|
|
||||||
from .start import BOT_URL, MATRIX_ID, MATRIX_PW, MATRIX_URL
|
from .start import FULL_ID, KEY, MATRIX_ID, MATRIX_PW, MATRIX_URL, bot_req
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
class BotTest(unittest.IsolatedAsyncioTestCase):
|
class BotTest(unittest.IsolatedAsyncioTestCase):
|
||||||
|
@ -50,20 +37,3 @@ class BotTest(unittest.IsolatedAsyncioTestCase):
|
||||||
self.assertEqual(message.sender, FULL_ID)
|
self.assertEqual(message.sender, FULL_ID)
|
||||||
self.assertEqual(message.body, text)
|
self.assertEqual(message.body, text)
|
||||||
self.assertEqual(message.formatted_body, '<h1>Hello</h1>')
|
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'})
|
|
||||||
|
|
Loading…
Reference in a new issue