Improve the tests, add a new debug endpoint

This commit is contained in:
Thomas Sileo 2018-05-27 11:50:09 +02:00
parent 25a75a9cef
commit cf242b2d84
4 changed files with 52 additions and 3 deletions

View file

@ -22,6 +22,6 @@ script:
- WEB_PORT=5007 CONFIG_DIR=./tests/fixtures/instance2/config docker-compose -p instance2 -f docker-compose-tests.yml up -d
- docker-compose -p instance2 -f docker-compose-tests.yml ps
# Integration tests first
- pytest -v --ignore data -k integration
- python -m pytest -v --ignore data -k integration
# Federation tests (with two local instances)
- pytest -v -s --ignore data -k federation
- python -m pytest -v -s --ignore data -k federation

20
app.py
View file

@ -21,6 +21,7 @@ from flask import redirect
from flask import Response
from flask import render_template
from flask import session
from flask import jsonify as flask_jsonify
from flask import url_for
from html2text import html2text
from itsdangerous import JSONWebSignatureSerializer
@ -47,6 +48,8 @@ from config import OBJECT_SERVICE
from config import PASS
from config import HEADERS
from config import VERSION
from config import DEBUG_MODE
from config import _drop_db
from config import custom_cache_purge_hook
from utils.httpsig import HTTPSigAuth, verify_request
from utils.key import get_secret_key
@ -710,6 +713,23 @@ def inbox():
)
@app.route('/api/debug', methods=['GET', 'DELETE'])
@api_required
def api_debug():
"""Endpoint used/needed for testing, only works in DEBUG_MODE."""
if not DEBUG_MODE:
return flask_jsonify(message='DEBUG_MODE is off')
if request.method == 'DELETE':
_drop_db()
return flask_jsonify(message='DB dropped')
return flask_jsonify(
inbox=DB.inbox.count(),
outbox=DB.outbox.count(),
)
@app.route('/api/upload', methods=['POST'])
@api_required
def api_upload():

View file

@ -62,7 +62,15 @@ mongo_client = MongoClient(
host=[os.getenv('MICROBLOGPUB_MONGODB_HOST', 'localhost:27017')],
)
DB = mongo_client['{}_{}'.format(USERNAME, DOMAIN.replace('.', '_'))]
DB_NAME = '{}_{}'.format(USERNAME, DOMAIN.replace('.', '_'))
DB = mongo_client[DB_NAME]
def _drop_db():
if not DEBUG_MODE:
return
mongo_client.drop_database(DB_NAME)
KEY = Key(USERNAME, DOMAIN, create=True)
ME = {

View file

@ -24,6 +24,18 @@ class Instance(object):
resp.raise_for_status()
assert resp.status_code == 200
def debug(self):
resp = self.session.get(f'{self.host_url}/api/debug', headers={'Accept': 'application/json'})
resp.raise_for_status()
return resp.json()
def drop_db(self):
resp = self.session.delete(f'{self.host_url}/api/debug', headers={'Accept': 'application/json'})
resp.raise_for_status()
return resp.json()
def login(self):
resp = self.session.post(f'{self.host_url}/login', data={'pass': 'hello'})
resp.raise_for_status()
@ -63,9 +75,11 @@ def test_federation():
"""Ensure the homepage is accessible."""
instance1 = Instance('http://localhost:5006', 'http://instance1_web_1:5005')
instance1.ping()
instance1.drop_db()
instance2 = Instance('http://localhost:5007', 'http://instance2_web_1:5005')
instance2.ping()
instance2.drop_db()
# Login
instance1.login()
@ -73,6 +87,13 @@ def test_federation():
# Instance1 follows instance2
instance1.follow(instance2)
instance1_debug = instance1.debug()
assert instance1_debug['inbox'] == 1 # An Accept activity should be there
assert instance1_debug['outbox'] == 1 # We've sent a Follow activity
instance2_debug = instance2.debug()
assert instance1_debug['inbox'] == 1 # An Follow activity should be there
assert instance1_debug['outbox'] == 1 # We've sent a Accept activity
assert instance2.followers() == [instance1.docker_url]
assert instance1.following() == [instance2.docker_url]