From 37293b6c7ad42f52573e37e5eb882eed4a083f89 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 17 Mar 2017 16:21:41 +0000 Subject: [PATCH] Added a test to ensure that all blueprints have a registered before_request method. Added a test for the status endpoint. --- app/__init__.py | 29 +++---------------- .../notifications_ses_callback.py | 2 +- .../notifications_sms_callback.py | 2 +- tests/app/status/test_status.py | 13 +++++++++ tests/app/test_route_authentication.py | 12 ++++++++ 5 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 tests/app/status/test_status.py create mode 100644 tests/app/test_route_authentication.py diff --git a/app/__init__.py b/app/__init__.py index 50a2d58a0..17bd96179 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -92,7 +92,6 @@ def register_blueprint(application): from app.notifications.receive_notifications import receive_notifications_blueprint from app.notifications.notifications_ses_callback import ses_callback_blueprint from app.notifications.notifications_sms_callback import sms_callback_blueprint - from app.status.healthcheck import status from app.authentication.auth import requires_admin_auth, requires_auth, requires_no_auth service_blueprint.before_request(requires_admin_auth) @@ -107,9 +106,6 @@ def register_blueprint(application): status_blueprint.before_request(requires_no_auth) application.register_blueprint(status_blueprint) - notifications_blueprint.before_request(requires_auth) - receive_notifications_blueprint.before_request(requires_no_auth) - ses_callback_blueprint.before_request(requires_no_auth) application.register_blueprint(ses_callback_blueprint) @@ -149,13 +145,11 @@ def register_blueprint(application): organisation_blueprint.before_request(requires_admin_auth) application.register_blueprint(organisation_blueprint, url_prefix='/organisation') - status.before_request(requires_no_auth) - application.register_blueprint(status) - def register_v2_blueprints(application): from app.v2.notifications.post_notifications import v2_notification_blueprint as post_notifications from app.v2.notifications.get_notifications import v2_notification_blueprint as get_notifications + from app.v2.template.get_template import template_blueprint from app.authentication.auth import requires_auth post_notifications.before_request(requires_auth) @@ -164,26 +158,11 @@ def register_v2_blueprints(application): get_notifications.before_request(requires_auth) application.register_blueprint(get_notifications) + template_blueprint.before_request(requires_auth) + application.register_blueprint(template_blueprint) + def init_app(app): - # @app.before_request - # def required_authentication(): - # no_auth_req = [ - # url_for('status.show_status'), - # url_for('notifications.process_ses_response'), - # url_for('notifications.process_firetext_response'), - # url_for('notifications.process_mmg_response'), - # url_for('notifications.receive_mmg_sms'), - # url_for('status.show_delivery_status'), - # url_for('spec.get_spec') - # ] - # - # if request.path not in no_auth_req: - # from app.authentication import auth - # error = auth.requires_auth() - # if error: - # return error - @app.before_request def record_user_agent(): statsd_client.incr("user-agent.{}".format(process_user_agent(request.headers.get('User-Agent', None)))) diff --git a/app/notifications/notifications_ses_callback.py b/app/notifications/notifications_ses_callback.py index a39ab5a36..3980f5add 100644 --- a/app/notifications/notifications_ses_callback.py +++ b/app/notifications/notifications_ses_callback.py @@ -16,7 +16,7 @@ from app.dao import ( from app.notifications.process_client_response import validate_callback_data -ses_callback_blueprint = Blueprint('notifications_ses_callback_', __name__) +ses_callback_blueprint = Blueprint('notifications_ses_callback', __name__) from app.errors import ( register_errors, diff --git a/app/notifications/notifications_sms_callback.py b/app/notifications/notifications_sms_callback.py index 24a699d0c..3c569e158 100644 --- a/app/notifications/notifications_sms_callback.py +++ b/app/notifications/notifications_sms_callback.py @@ -5,7 +5,7 @@ from flask import request, jsonify from app.errors import InvalidRequest, register_errors from app.notifications.process_client_response import validate_callback_data, process_sms_client_response -sms_callback_blueprint = Blueprint("sms_callback_blueprint", __name__, url_prefix="/notifications/sms") +sms_callback_blueprint = Blueprint("sms_callback", __name__, url_prefix="/notifications/sms") register_errors(sms_callback_blueprint) diff --git a/tests/app/status/test_status.py b/tests/app/status/test_status.py new file mode 100644 index 000000000..4203a4e4d --- /dev/null +++ b/tests/app/status/test_status.py @@ -0,0 +1,13 @@ +from flask import json + + +def test_get_status_all_ok(client): + path = '/_status' + response = client.get(path) + assert response.status_code == 200 + resp_json = json.loads(response.get_data(as_text=True)) + assert resp_json['status'] == 'ok' + assert resp_json['db_version'] + assert resp_json['travis_commit'] + assert resp_json['travis_build_number'] + assert resp_json['build_time'] diff --git a/tests/app/test_route_authentication.py b/tests/app/test_route_authentication.py new file mode 100644 index 000000000..18bb7bbc2 --- /dev/null +++ b/tests/app/test_route_authentication.py @@ -0,0 +1,12 @@ +def test_all_routes_have_authentication(client): + # This tests that each blueprint registered on the application has a before_request function registered. + # The None row is removed from the comparison as that is not blueprint specific but app specific. + before_req_funcs = set(x for x in client.application.before_request_funcs if x is not None) + + blueprint_names = set(client.application.blueprints.keys()) + assert blueprint_names == before_req_funcs + + # The static route is always available by default for a Flask app to serve anything in the static folder. + routes_blueprint_names = set( + [x.split('.')[0] for x in client.application.view_functions.keys() if x.split('.')[0] != 'static']) + assert sorted(blueprint_names) == sorted(routes_blueprint_names)