diff --git a/jail2ban/__init__.py b/jail2ban/__init__.py index 889963d..da969d1 100644 --- a/jail2ban/__init__.py +++ b/jail2ban/__init__.py @@ -42,6 +42,15 @@ def create_app(): check_password_hash(users.get(username), password): return username + @app.route("/ping", methods=['GET']) + @auth.login_required + def ping(): + remote_user = auth.username() + app.logger.info('Received ping for' + f' anchor f2b-jail/{remote_user}') + return jsonify({'anchor': f'f2b-jail/{remote_user}', + 'operation': 'ping', + 'result': 'pong'}) @app.route("/flush/", methods=['GET']) @auth.login_required def flush(name): diff --git a/tests/test_ping.py b/tests/test_ping.py new file mode 100644 index 0000000..8384d9b --- /dev/null +++ b/tests/test_ping.py @@ -0,0 +1,10 @@ +def test_ping(client, mocker, valid_credentials): + ''' + Test application health check + ''' + + response = client.get("/ping", + headers={"Authorization": + "Basic " + valid_credentials}) + + assert response.json['operation'] == 'ping'