From bf1577a0ce58a4597fdc3bae6f139cf2f2846751 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Fri, 12 Jul 2019 09:31:08 -0600 Subject: [PATCH] Add status endpoint --- src/humulus/home.py | 14 +++++++++++++- tests/test_home.py | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/humulus/home.py b/src/humulus/home.py index ccbec62..7d3a167 100644 --- a/src/humulus/home.py +++ b/src/humulus/home.py @@ -14,7 +14,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from flask import Blueprint, redirect, url_for +from flask import Blueprint, redirect, url_for, request, jsonify + +from humulus.couch import get_db bp = Blueprint('home', __name__) @@ -23,3 +25,13 @@ bp = Blueprint('home', __name__) def index(): """Renders the homepage template""" return redirect(url_for('recipes.index')) + + +@bp.route('/status') +def status(): + if request.args.get('couch', default=False): + if get_db().exists(): + return jsonify({'ping': 'ok', 'couch': 'ok'}), 200 + else: + return jsonify({'ping': 'ok', 'couch': 'not_exist'}), 500 + return jsonify({'ping': 'ok'}), 200 diff --git a/tests/test_home.py b/tests/test_home.py index c03f6ba..50e3240 100644 --- a/tests/test_home.py +++ b/tests/test_home.py @@ -16,3 +16,27 @@ def test_home(client): response = client.get('/') assert response.status_code == 302 + + +def test_status(client, monkeypatch): + class MockDBTrue: + def exists(self): + return True + + class MockDBFalse: + def exists(self): + return False + + response = client.get('/status') + assert response.status_code == 200 + assert response.get_json() == {'ping': 'ok'} + + monkeypatch.setattr('humulus.home.get_db', MockDBTrue) + response = client.get('/status?couch=y') + assert response.status_code == 200 + assert response.get_json() == {'ping': 'ok', 'couch': 'ok'} + + monkeypatch.setattr('humulus.home.get_db', MockDBFalse) + response = client.get('/status?couch=y') + assert response.status_code == 500 + assert response.get_json() == {'ping': 'ok', 'couch': 'not_exist'}