From cf242b2d846645ba6f2fff801b146cd9a77ba130 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 27 May 2018 11:50:09 +0200 Subject: [PATCH] Improve the tests, add a new debug endpoint --- .travis.yml | 4 ++-- app.py | 20 ++++++++++++++++++++ config.py | 10 +++++++++- tests/federation_test.py | 21 +++++++++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9c1a84f..8477299 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/app.py b/app.py index db0d98c..0b19ef0 100644 --- a/app.py +++ b/app.py @@ -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(): diff --git a/config.py b/config.py index 5bbebe5..e4c3912 100644 --- a/config.py +++ b/config.py @@ -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 = { diff --git a/tests/federation_test.py b/tests/federation_test.py index f291638..17cd747 100644 --- a/tests/federation_test.py +++ b/tests/federation_test.py @@ -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]