Move acting on pgwui.routes setting into PGWUI_Common
authorKarl O. Pinc <kop@karlpinc.com>
Wed, 2 Dec 2020 03:21:25 +0000 (21:21 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Wed, 2 Dec 2020 03:21:25 +0000 (21:21 -0600)
src/pgwui_server/exceptions.py
src/pgwui_server/pgwui_server.py
tests/test_pgwui_server.py

index a2c3ea7b93d29e34c44fc1a7061aab619b749d24..d312dd1a89792265ef8e509ec81e6798aa7972bc 100644 (file)
@@ -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):
index 269102973589f5f687d2b93a4d968e64c380ccbb..b950206cb2130ffeafe0349c746166c99387d2ca 100644 (file)
@@ -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
 
index db125e0b662531b2de97ffa18ba29550fe1a46c8..48d6d868c5b9e0f50e1a41f74b397d07e46afa87 100644 (file)
@@ -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