# Karl O. Pinc <kop@karlpinc.com>
+import pytest
import pgwui_common.exceptions as common_ex
from pgwui_server import checkset
# key_to_ini()
+@pytest.mark.unittest
def test_key_to_ini():
'''The return value is as expected
'''
# require_setting()
+@pytest.mark.unittest
def test_require_setting_missing():
'''Deliver exception when a required setting is missing'''
errors = []
assert isinstance(errors[0], common_ex.MissingSettingError)
+@pytest.mark.unittest
def test_require_setting_present():
'''Does nothing when a required setting is present'''
errors = []
# boolean_setting()
+@pytest.mark.unittest
def test_boolean_setting_missing():
'''Does nothing when the setting is not in the settings'''
errors = []
assert errors == []
+@pytest.mark.unittest
def test_boolean_setting_true():
'''Does nothing when the setting is "True"'''
errors = []
assert errors == []
+@pytest.mark.unittest
def test_boolean_setting_false():
'''Does nothing when the setting is "False"'''
errors = []
assert errors == []
+@pytest.mark.unittest
def test_boolean_setting_notboolean():
'''Deliver an exception when the setting does not evaluate to a boolean'''
errors = []
assert isinstance(errors[0], common_ex.NotBooleanSettingError)
+@pytest.mark.unittest
def test_boolean_setting_notparsable():
'''Deliver an exception when the setting does not evaluate to a
boolean because it is not parseable
# validate_setting_values()
+@pytest.mark.unittest
def test_validate_setting_values(mock_require_setting, mock_boolean_setting):
'''Calls require_setting() and boolean_setting()'''
# do_validate_hmac()
+@pytest.mark.unittest
def test_do_validate_hmac_none():
'''pgwui.validate_hmac defaults to True'''
assert checkset.do_validate_hmac({'pgwui': {}}) is True
+@pytest.mark.unittest
def test_do_validate_hmac_True():
'''Require hmac validation when pgwui.validate_hmac is True'''
result = checkset.do_validate_hmac(
assert result is True
+@pytest.mark.unittest
def test_do_validate_hmac_False():
'''No hmac validation when pgwui.validate_hmac is False'''
result = checkset.do_validate_hmac(
# validate_hmac()
+@pytest.mark.unittest
def test_validate_hmac_unvalidated(mock_do_validate_hmac):
'''No error is returned when hmac validation is off'''
mock_do_validate_hmac.return_value = False
assert errors == []
+@pytest.mark.unittest
def test_validate_hmac_success(mock_do_validate_hmac):
'''No error is returned when hmac is validated an the right length'''
mock_do_validate_hmac.return_value = True
assert errors == []
+@pytest.mark.unittest
def test_validate_hmac_missing(mock_do_validate_hmac):
'''Deliver error when hmac is validated and missing'''
mock_do_validate_hmac.return_value = True
assert isinstance(errors[0], server_ex.NoHMACError)
+@pytest.mark.unittest
def test_validate_hmac_length(mock_do_validate_hmac):
'''Deliver error when hmac is validated and the wrong length'''
mock_do_validate_hmac.return_value = True
# dot_to_component_settings()
+@pytest.mark.unittest
def test_dot_to_component_settings_new():
'''Adds a new dict and puts the settings in it
'''
assert settings == expected
+@pytest.mark.unittest
def test_dot_to_component_settings_old():
'''Extends an existing dict in the settings
'''
# component_setting_into_dict()
+@pytest.mark.unittest
def test_component_setting_into_dict_no_checker(
mock_dot_to_component_setting):
'''When there's no checker nothing is done
assert errors == []
+@pytest.mark.unittest
def test_component_setting_into_dict_checker(
mock_dot_to_component_setting):
'''When there's a checker its result is appended to the errors
# dot_to_dict()
+@pytest.mark.unittest
def test_dot_to_dict():
'''Removes pgwui.* settings, replaces them with a dict entry
'''
# parse_multiline_assigments()
+@pytest.mark.unittest
def test_parse_multiline_assignments_str():
'''Appends key/value string tuples and when there's no "=",
and more than just whitespace, a list is the result
# parse_assignments()
+@pytest.mark.unittest
def test_parse_assignments_str(mock_parse_multiline_assignments):
'''Calls parse_multiline_assignments'''
lines = ('key1 = value1\n' # whitespace around = is ignored
mock_parse_multiline_assignments.assert_called_once()
+@pytest.mark.unittest
def test_parse_assignments_dict(mock_parse_multiline_assignments):
'''Returns key value tuples.
'''
# setting_into_dict()
+@pytest.mark.unittest
def test_setting_into_dict_unknown(
mock_parse_assignments,
mock_component_setting_into_dict,
assert errors == []
+@pytest.mark.unittest
def test_setting_into_dict_bad(
mock_parse_assignments,
mock_component_setting_into_dict,
assert isinstance(errors[0], common_ex.UnknownSettingKeyError)
+@pytest.mark.unittest
def test_setting_into_dict_good(
mock_parse_assignments,
mock_component_setting_into_dict,
assert errors == []
+@pytest.mark.unittest
def test_setting_into_dict_plugin_component(
mock_parse_assignments,
mock_component_setting_into_dict,
# dictify_settings()
+@pytest.mark.unittest
def test_dictify_settings(mock_find_pgwui_check_settings,
mock_setting_into_dict,
mock_validate_setting_values,
return run
+@pytest.mark.unittest
def test_exit_reporting_errors_logged(
assert_exit1, monkeypatch, caplog, capsys):
'''All errors are logged at ERROR, and a extra one at CRITICAL
assert levels[-1] == logging.CRITICAL
+@pytest.mark.unittest
def test_exit_reporting_errors_printed(
assert_exit1, monkeypatch, capsys):
'''First and last (the extra) errors are printed on stderr
# exit_on_invalid_settings()
+@pytest.mark.unittest
def test_exit_on_invalid_settings_invalid(monkeypatch,
mock_exit_reporting_errors):
'''Calls dictify_settings and exit_reporting_errors() when
assert mock_exit_reporting_errors.called
+@pytest.mark.unittest
def test_exit_on_invalid_settings_valid(mock_dictify_settings):
'''Returns, without exiting, when all settings are valid
'''
# autoconfigurable_components()
+@pytest.mark.unittest
def test_autoconfiguable_components_no_autoconfig():
'''When the settings have no pgwui.autoconfigure return an empty list
'''
assert result == []
+@pytest.mark.unittest
def test_autoconfigurable_components_log_info(caplog):
'''When pyramid.include is in the settings an INFO message is logged
'''
assert level == logging.INFO
+@pytest.mark.unittest
def test_autoconfigurable_components_components_returned():
'''The suppiled components are returned when autoconfigure is True
'''
# add_routes()
+@pytest.mark.unittest
def test_add_routes_empty(mock_add_route):
'''When there is no pgwui.routes setting nothing gets added'''
with pyramid.testing.testConfig() as config:
assert not mocked_add_route.called
+@pytest.mark.unittest
def test_add_routes_notempty(mock_add_route, mock_parse_assignments):
'''When there is a pgwui.routes setting config.add_route() is called
for each route'''
# apply_component_defaults()
+@pytest.mark.unittest
def test_apply_component_defaults(monkeypatch, caplog,
mock_autoconfigurable_components,
mock_add_routes):
# pgwui_server_config()
+@pytest.mark.unittest
def test_pgwui_server_config(
mock_find_pgwui_components,
mock_apply_component_defaults, mock_exit_on_invalid_settings):
# main()
+@pytest.mark.unittest
def test_main(monkeypatch):
'''Returns a wsgi app'''
monkeypatch.setattr(pgwui_server, 'pgwui_server_config',
# Integration tests
+@pytest.mark.integrationtest
def test_main_integrated():
'''Does not raise errors or warnings'''
pgwui_server.main({}, **TEST_SETTINGS)