From 3a0b00daf915efd32806ddebe8e3bef291dc3d56 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Tue, 1 Dec 2020 21:21:25 -0600 Subject: [PATCH] Move acting on pgwui.routes setting into PGWUI_Common --- src/pgwui_server/exceptions.py | 13 ++++--- src/pgwui_server/pgwui_server.py | 21 +++--------- tests/test_pgwui_server.py | 59 +++----------------------------- 3 files changed, 14 insertions(+), 79 deletions(-) diff --git a/src/pgwui_server/exceptions.py b/src/pgwui_server/exceptions.py index a2c3ea7..d312dd1 100644 --- a/src/pgwui_server/exceptions.py +++ b/src/pgwui_server/exceptions.py @@ -30,17 +30,16 @@ class ServerError(exceptions.Error): pass -class AutoconfigureConflict(ServerError): - def __init__(self): - super().__init__( - 'Autoconfigure is True and there is a pyramid.include setting') +class ServerInfo(exceptions.Info): + '''Information exceptions; which don't stop the program + ''' + pass -class MenuPageInRoutes(ServerError): +class AutoconfigureConflict(ServerInfo): def __init__(self): super().__init__( - 'The pgwui_menu in the pgwui.routes setting is ignored ' - 'and the pgwui.menu_page setting used instead') + 'Autoconfigure is True and there is a pyramid.include setting') class BadSettingsAbort(ServerError): diff --git a/src/pgwui_server/pgwui_server.py b/src/pgwui_server/pgwui_server.py index 2691029..b950206 100644 --- a/src/pgwui_server/pgwui_server.py +++ b/src/pgwui_server/pgwui_server.py @@ -31,6 +31,7 @@ import sys from . import exceptions as server_ex from . import checkset from pgwui_common import exceptions as common_ex +from pgwui_common import routes from pgwui_common import plugin # Constants @@ -42,14 +43,14 @@ SETTINGS = set( 'default_db', 'dry_run', 'route_prefix', - 'routes', 'validate_hmac', 'autoconfigure', ]) # All the multi-valued settings recognized by PGWUI_Server/Core MULTI_SETTINGS = set( - ['home_page', + ['routes', + 'home_page', 'menu_page', ]) @@ -186,20 +187,6 @@ def exit_on_invalid_settings(settings, components): exit_reporting_errors(errors) -def add_routes(config, settings): - '''Add routes found in pgwui.routes setting - ''' - pgwui_settings = settings['pgwui'] - if 'routes' in pgwui_settings: - menu_page = 'menu_page' in pgwui_settings - routes = parse_assignments(pgwui_settings['routes']) - for name, route in routes: - if menu_page and name == 'pgwui_menu': - log.info(server_ex.MenuPageInRoutes()) - else: - config.add_route(name, route) - - def autoconfigurable_components(settings, components): '''Automatic pgwui component discovery ''' @@ -224,7 +211,7 @@ def apply_component_defaults(settings, components): for component in components_to_config: log.debug('Autoconfiguring PGWUI component: {}'.format(component)) config.include(component) - add_routes(config, settings) + routes.add_routes(config, settings) log.debug('Done autoconfiguring PGWUI components') return config diff --git a/tests/test_pgwui_server.py b/tests/test_pgwui_server.py index db125e0..48d6d86 100644 --- a/tests/test_pgwui_server.py +++ b/tests/test_pgwui_server.py @@ -25,10 +25,9 @@ import logging import pytest import sys -import pyramid.testing - import pgwui_common.exceptions as common_ex import pgwui_common.plugin +import pgwui_common.routes # Use as a regular module, not a plugin, so lint checks work from pgwui_testing import testing @@ -63,8 +62,6 @@ class MockConfigurator(): pass -mock_add_route = testing.instance_method_mock_fixture('add_route') - mock_find_pgwui_components = testing.make_mock_fixture( pgwui_common.plugin, 'find_pgwui_components') mock_find_pgwui_check_settings = testing.make_mock_fixture( @@ -75,6 +72,9 @@ mock_validate_setting_values = testing.make_mock_fixture( mock_validate_hmac = testing.make_mock_fixture( pgwui_server.checkset, 'validate_hmac') +mock_add_routes = testing.make_mock_fixture( + pgwui_common.routes, 'add_routes') + # Unit tests @@ -519,57 +519,6 @@ mock_autoconfigurable_components = testing.make_mock_fixture( pgwui_server, 'autoconfigurable_components') -# 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: - mocked_add_route = mock_add_route(config) - pgwui_server.add_routes(config, {'pgwui': {}}) - - 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''' - test_routes = [('name1', 'route1'), - ('name2', 'route2')] - mock_parse_assignments.return_value = test_routes - with pyramid.testing.testConfig() as config: - mocked_add_route = mock_add_route(config) - pgwui_server.add_routes(config, {'pgwui': {'routes': ''}}) - - assert mocked_add_route.call_count == len(test_routes) - - -@pytest.mark.unittest -def test_add_routes_menu(mock_add_route, mock_parse_assignments, caplog): - '''When there is a a route for pgwui_menu, but there is a menu_page - setting, no route is added and an INFO message is logged - ''' - caplog.set_level(logging.DEBUG) - - test_routes = [('pgwui_menu', 'notused')] - mock_parse_assignments.return_value = test_routes - with pyramid.testing.testConfig() as config: - mocked_add_route = mock_add_route(config) - pgwui_server.add_routes(config, {'pgwui': {'routes': 'notused', - 'menu_page': 'anything'}}) - - mocked_add_route.assert_not_called() - - logs = caplog.record_tuples - assert len(logs) == 1 - assert logs[0][1] == logging.INFO - - -mock_add_routes = testing.make_mock_fixture( - pgwui_server, 'add_routes') - - # apply_component_defaults() @pytest.mark.unittest -- 2.34.1