diff --git a/puppetboard/app.py b/puppetboard/app.py index a709ed2..f236306 100644 --- a/puppetboard/app.py +++ b/puppetboard/app.py @@ -121,7 +121,7 @@ def bad_request(e): @app.errorhandler(403) def forbidden(e): envs = environments() - return render_template('403.html', envs=envs), 400 + return render_template('403.html', envs=envs), 403 @app.errorhandler(404) diff --git a/puppetboard/templates/412.html b/puppetboard/templates/412.html index caa0867..5b829d4 100644 --- a/puppetboard/templates/412.html +++ b/puppetboard/templates/412.html @@ -1,11 +1,5 @@ {% extends 'layout.html' %} -{% block row_fluid %} -
You've configured Puppetboard with an API version that does not support this feature.
-You've configured Puppetboard with an API version that does not support this feature.
{% endblock %} diff --git a/test/test_app_error.py b/test/test_app_error.py new file mode 100644 index 0000000..7b20d84 --- /dev/null +++ b/test/test_app_error.py @@ -0,0 +1,73 @@ +import pytest +from flask import Flask, current_app +from puppetboard import app + +from bs4 import BeautifulSoup + + +@pytest.fixture +def mock_puppetdb_environments(mocker): + environemnts = [ + {'name': 'production'}, + {'name': 'staging'} + ] + + return mocker.patch.object(app.puppetdb, 'environments', + return_value=environemnts) + + +def test_error_no_content(): + result = app.no_content(None) + assert result[0] == '' + assert result[1] == 204 + + +def test_error_bad_request(mock_puppetdb_environments): + with app.app.test_request_context(): + (output, error_code) = app.bad_request(None) + soup = BeautifulSoup(output, 'html.parser') + + assert 'The request sent to PuppetDB was invalid' in soup.p.text + assert error_code == 400 + + +def test_error_forbidden(mock_puppetdb_environments): + with app.app.test_request_context(): + (output, error_code) = app.forbidden(None) + soup = BeautifulSoup(output, 'html.parser') + + long_string = "%s %s" % ('What you were looking for has', + 'been disabled by the administrator') + assert long_string in soup.p.text + assert error_code == 403 + + +def test_error_not_found(mock_puppetdb_environments): + with app.app.test_request_context(): + (output, error_code) = app.not_found(None) + soup = BeautifulSoup(output, 'html.parser') + + long_string = "%s %s" % ('What you were looking for could not', + 'be found in PuppetDB.') + assert long_string in soup.p.text + assert error_code == 404 + + +def test_error_precond(mock_puppetdb_environments): + with app.app.test_request_context(): + (output, error_code) = app.precond_failed(None) + soup = BeautifulSoup(output, 'html.parser') + + long_string = "%s %s" % ('You\'ve configured Puppetboard with an API', + 'version that does not support this feature.') + assert long_string in soup.p.text + assert error_code == 412 + + +def test_error_server(mock_puppetdb_environments): + with app.app.test_request_context(): + (output, error_code) = app.server_error(None) + soup = BeautifulSoup(output, 'html.parser') + + assert 'Internal Server Error' in soup.h2.text + assert error_code == 500