def apply_component_defaults(settings, components):
'''Apply component default settings to existing settings
'''
- components_to_config = autoconfigurable_components(settings, components)
+ config = Configurator(settings=settings)
+ config.include('pgwui_common')
+ components_to_config = autoconfigurable_components(settings, components)
rp = settings['pgwui'].get('route_prefix')
- with Configurator(settings=settings, route_prefix=rp) as config:
- config.include('pgwui_common')
+ with config.route_prefix_context(rp):
for component in components_to_config:
log.debug('Autoconfiguring PGWUI component: {}'.format(component))
config.include(component)
pytestmark = pytest.mark.unittest
-# Use contextlib.AbstractContextManager for Python >= 3.6
-# (Or, better, use the magic mock maker that's not yet integrated.)
-class MockConfigurator():
- def __init__(self, **kwargs):
- pass
-
- def __enter__(self):
- return self
-
- def __exit__(self, *args):
- pass
-
- def make_wsgi_app(self):
- return 'wsgi_app'
-
- def include(self, *args):
- pass
-
+MockConfigurator = testing.make_magicmock_fixture(
+ pgwui_server, 'Configurator')
mock_find_pgwui_components = testing.make_mock_fixture(
pgwui_common.plugin, 'find_pgwui_components')
# apply_component_defaults()
-def test_apply_component_defaults(monkeypatch, caplog,
+def test_apply_component_defaults(caplog,
+ MockConfigurator,
mock_autoconfigurable_components,
mock_add_routes):
'''A configurator is returned, a debug log entry is made for
mock_autoconfigurable_components.return_value = \
['pgwui_mock_component_name']
- monkeypatch.setattr(pgwui_server, 'Configurator',
- MockConfigurator)
result = pgwui_server.apply_component_defaults({'pgwui': {}}, [])
- assert isinstance(result, MockConfigurator)
+ assert isinstance(result, type(MockConfigurator()))
logs = caplog.record_tuples
assert result == test_configurator
+mock_pgwui_server_config = testing.make_mock_fixture(
+ pgwui_server, 'pgwui_server_config')
+
+
# main()
-def test_main(monkeypatch):
+def test_main(MockConfigurator, mock_pgwui_server_config):
'''Returns a wsgi app'''
- monkeypatch.setattr(pgwui_server, 'pgwui_server_config',
- lambda *args: MockConfigurator())
-
+ config = MockConfigurator()
+ mock_pgwui_server_config.return_value = config
+ config.make_wsgi_app.return_value = 'wsgi_app'
result = pgwui_server.main({})
+
assert result == 'wsgi_app'
def check_route(config, name, expected):
route_i = config.introspector.get('routes', name)
- assert route_i['pattern'] == expected
+ if route_i is None:
+ assert expected is None
+ else:
+ assert route_i['pattern'] == expected
def updated_dict(old, new):
@pytest.mark.parametrize(
- ('settings', 'logout_path', 'menu_path'), [
+ ('settings', 'logout_path', 'menu_path', 'menu_page_path'), [
(REFERENCE_SETTINGS,
pgwui_logout.pgwui_logout.DEFAULT_LOGOUT_ROUTE,
- pgwui_menu.pgwui_menu.DEFAULT_MENU_ROUTE),
+ pgwui_menu.pgwui_menu.DEFAULT_MENU_ROUTE,
+ None),
(updated_dict(REFERENCE_SETTINGS,
- {'pgwui.route_prefix': '/foo'}),
+ {'pgwui.route_prefix': '/foo',
+ 'pgwui.menu_page': {'type': 'file',
+ 'source': '/tmp/nofile.html',
+ 'url_path': '/menu'}}),
'foo' + pgwui_logout.pgwui_logout.DEFAULT_LOGOUT_ROUTE,
- 'foo' + pgwui_menu.pgwui_menu.DEFAULT_MENU_ROUTE)])
+ 'foo' + pgwui_menu.pgwui_menu.DEFAULT_MENU_ROUTE,
+ '/menu')])
def test_pgwui_server_config_no_route_prefix(
- settings, logout_path, menu_path):
+ settings, logout_path, menu_path, menu_page_path):
'''The given route_prefix is applied to the routes
'''
config = pgwui_server.pgwui_server_config(settings)
check_route(config, 'pgwui_logout', logout_path)
check_route(config, 'pgwui_menu', menu_path)
+ check_route(config, 'pgwui_common.menu_page', menu_page_path)