Refactor out some global space to allow for testable code

and handling error conditions before flask is completely
initialized.
This commit is contained in:
Mike Terzo
2017-07-01 02:10:31 -04:00
parent df915834a4
commit 9488e8cb83
6 changed files with 155 additions and 87 deletions

View File

@@ -1,7 +1,9 @@
import pytest
from flask import Flask, current_app
from werkzeug.exceptions import InternalServerError
from puppetboard import app
from puppetboard.errors import (bad_request, forbidden, not_found,
precond_failed, server_error)
from bs4 import BeautifulSoup
@@ -16,9 +18,17 @@ def mock_puppetdb_environments(mocker):
return_value=environemnts)
@pytest.fixture
def mock_server_error(mocker):
def raiseInternalServerError():
raise InternalServerError('Hello world')
return mocker.patch('puppetboard.core.environments',
side_effect=raiseInternalServerError)
def test_error_bad_request(mock_puppetdb_environments):
with app.app.test_request_context():
(output, error_code) = app.bad_request(None)
(output, error_code) = bad_request(None)
soup = BeautifulSoup(output, 'html.parser')
assert 'The request sent to PuppetDB was invalid' in soup.p.text
@@ -27,7 +37,7 @@ def test_error_bad_request(mock_puppetdb_environments):
def test_error_forbidden(mock_puppetdb_environments):
with app.app.test_request_context():
(output, error_code) = app.forbidden(None)
(output, error_code) = forbidden(None)
soup = BeautifulSoup(output, 'html.parser')
long_string = "%s %s" % ('What you were looking for has',
@@ -38,7 +48,7 @@ def test_error_forbidden(mock_puppetdb_environments):
def test_error_not_found(mock_puppetdb_environments):
with app.app.test_request_context():
(output, error_code) = app.not_found(None)
(output, error_code) = not_found(None)
soup = BeautifulSoup(output, 'html.parser')
long_string = "%s %s" % ('What you were looking for could not',
@@ -49,7 +59,7 @@ def test_error_not_found(mock_puppetdb_environments):
def test_error_precond(mock_puppetdb_environments):
with app.app.test_request_context():
(output, error_code) = app.precond_failed(None)
(output, error_code) = precond_failed(None)
soup = BeautifulSoup(output, 'html.parser')
long_string = "%s %s" % ('You\'ve configured Puppetboard with an API',
@@ -60,8 +70,16 @@ def test_error_precond(mock_puppetdb_environments):
def test_error_server(mock_puppetdb_environments):
with app.app.test_request_context():
(output, error_code) = app.server_error(None)
(output, error_code) = server_error(None)
soup = BeautifulSoup(output, 'html.parser')
assert 'Internal Server Error' in soup.h2.text
assert error_code == 500
def test_early_error_server(mock_server_error):
with app.app.test_request_context():
(output, error_code) = server_error(None)
soup = BeautifulSoup(output, 'html.parser')
assert 'Internal Server Error' in soup.h2.text
assert error_code == 500

View File

@@ -1,7 +1,7 @@
import pytest
import os
from puppetboard import docker_settings
from puppetboard import app
import puppetboard.core
try:
import future.utils
@@ -100,12 +100,14 @@ def test_graph_facts_custom(cleanUpEnv):
assert 'extra' in facts
def test_bad_log_value(cleanUpEnv):
def test_bad_log_value(cleanUpEnv, mocker):
os.environ['LOGLEVEL'] = 'g'
os.environ['PUPPETBOARD_SETTINGS'] = '../puppetboard/docker_settings.py'
reload(docker_settings)
puppetboard.core.APP = None
with pytest.raises(ValueError) as error:
reload(app)
puppetboard.core.get_app()
def test_default_table_selctor(cleanUpEnv):