Files
puppetboard/test/test_app_error.py
Mike Terzo 86488280c9 Test error conditions.
Fix 412 template to use standard styling that the other 400 templates use.
Update forbidden error to return status code 403 instead of 400.

Signed-off-by: Mike Terzo <mike@terzo.org>
2017-01-23 19:53:55 -05:00

74 lines
2.3 KiB
Python

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