From: Karl O. Pinc Date: Sun, 15 Nov 2020 23:08:40 +0000 (-0600) Subject: Move code out of __init__.py X-Git-Url: https://papio.biology.duke.edu/gitweb/?a=commitdiff_plain;h=6246373b1a09e031cc1506e9ca2b759152ac68bf;p=pgwui_common Move code out of __init__.py --- diff --git a/src/pgwui_common/__init__.py b/src/pgwui_common/__init__.py index 9508b90..1d3c7b3 100644 --- a/src/pgwui_common/__init__.py +++ b/src/pgwui_common/__init__.py @@ -19,56 +19,4 @@ # Karl O. Pinc -'''Provide a way to configure PGWUI. -''' - -DEFAULT_HOME_ROUTE = '/' - - -def base_view(wrapped): - '''Decorator for any view which includes base.mk. - ''' - def wrapper(request): - '''Add variables missing but needed by base.mk to the response. - ''' - response = wrapped(request) - pgwui = response.get('pgwui', {}) - url = pgwui.setdefault('url', dict()) - url.setdefault('css', - request.static_url('pgwui_common:static/pgwui.css')) - route = pgwui.setdefault('route', dict()) - route.setdefault('home', - request.route_url('home')) - response['pgwui'] = pgwui - return response - return wrapper - - -def auth_base_view(wrapped): - '''Decorator for any view which includes auth_base.mk. - ''' - def wrapper(request): - '''Add variables needed by auth_base.mk to the response. - ''' - response = base_view(wrapped)(request) - pgwui = response['pgwui'] - try: - logout_route = request.route_url('pgwui_logout') - except KeyError: - pass # A logout route is not required - else: - pgwui['route'].setdefault('pgwui_logout', logout_route) - return response - return wrapper - - -def includeme(config): - '''Pyramid configuration for PGWUI_Common - ''' - config.include('pyramid_mako') - config.include('pyramid_beaker') - config.add_static_view( - 'static', - 'pgwui_common:static/', - cache_max_age=3600) - config.add_route('home', DEFAULT_HOME_ROUTE) +from .pgwui_common import includeme # noqa: F401 diff --git a/src/pgwui_common/pgwui_common.py b/src/pgwui_common/pgwui_common.py new file mode 100644 index 0000000..9508b90 --- /dev/null +++ b/src/pgwui_common/pgwui_common.py @@ -0,0 +1,74 @@ +# Copyright (C) 2018, 2020 The Meme Factory, Inc. http://www.karlpinc.com/ + +# This file is part of PGWUI_Common. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +# Karl O. Pinc + +'''Provide a way to configure PGWUI. +''' + +DEFAULT_HOME_ROUTE = '/' + + +def base_view(wrapped): + '''Decorator for any view which includes base.mk. + ''' + def wrapper(request): + '''Add variables missing but needed by base.mk to the response. + ''' + response = wrapped(request) + pgwui = response.get('pgwui', {}) + url = pgwui.setdefault('url', dict()) + url.setdefault('css', + request.static_url('pgwui_common:static/pgwui.css')) + route = pgwui.setdefault('route', dict()) + route.setdefault('home', + request.route_url('home')) + response['pgwui'] = pgwui + return response + return wrapper + + +def auth_base_view(wrapped): + '''Decorator for any view which includes auth_base.mk. + ''' + def wrapper(request): + '''Add variables needed by auth_base.mk to the response. + ''' + response = base_view(wrapped)(request) + pgwui = response['pgwui'] + try: + logout_route = request.route_url('pgwui_logout') + except KeyError: + pass # A logout route is not required + else: + pgwui['route'].setdefault('pgwui_logout', logout_route) + return response + return wrapper + + +def includeme(config): + '''Pyramid configuration for PGWUI_Common + ''' + config.include('pyramid_mako') + config.include('pyramid_beaker') + config.add_static_view( + 'static', + 'pgwui_common:static/', + cache_max_age=3600) + config.add_route('home', DEFAULT_HOME_ROUTE) diff --git a/tests/test___init__.py b/tests/test___init__.py deleted file mode 100644 index e51d3cf..0000000 --- a/tests/test___init__.py +++ /dev/null @@ -1,135 +0,0 @@ -# Copyright (C) 2018, 2020 The Meme Factory, Inc. http://www.karlpinc.com/ - -# This file is part of PGWUI_Common. -# -# This program is free software: you can redistribute it and/or -# modify it under the terms of the GNU Affero General Public License -# as published by the Free Software Foundation, either version 3 of -# the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public -# License along with this program. If not, see -# . -# - -# Karl O. Pinc - -import pyramid.config -from pyramid.threadlocal import get_current_request -import pgwui_common.__init__ as pgwui_common_init - -# Activiate our pytest plugin -pytest_plugins = ("pgwui",) - - -# Helper functions and constants - -CSS_URL = 'foo://bar/' - - -def mock_view(request): - return {'pgwui': {'url': {'css': CSS_URL}}} - - -def check_base_view_results(request, pgwui): - assert pgwui['url']['css'] == CSS_URL - url = (request.application_url - + pgwui_common_init.DEFAULT_HOME_ROUTE) - assert pgwui['route']['home'] == url - - -# Unit tests - -# base_view() -def test_base_view_add(pyramid_request_config): - '''The response adds all expected variables''' - def mock_view(request): - return {} - - pgwui_common_init.includeme(pyramid_request_config) - wrapper = pgwui_common_init.base_view(mock_view) - response = wrapper(get_current_request()) - assert response['pgwui']['url']['css'][0:4] == 'http' - - -def test_base_view_default(pyramid_request_config): - '''The response retains the mock view's variables''' - pgwui_common_init.includeme(pyramid_request_config) - wrapper = pgwui_common_init.base_view(mock_view) - request = get_current_request() - response = wrapper(request) - pgwui = response['pgwui'] - check_base_view_results(request, pgwui) - - -# auth_base_view() - -def test_auth_base_view_logout(pyramid_request_config): - '''The response contains base_view and auth_base_view variables - when there is a logout route - ''' - pgwui_common_init.includeme(pyramid_request_config) - - logout_route = '/logout' - pyramid_request_config.add_route('pgwui_logout', logout_route) - wrapper = pgwui_common_init.auth_base_view(mock_view) - request = get_current_request() - - response = wrapper(request) - pgwui = response['pgwui'] - check_base_view_results(request, pgwui) - assert pgwui['route']['pgwui_logout'] == (request.application_url - + logout_route) - - -def test_auth_base_view_nologout(pyramid_request_config): - '''The response contains base_view and auth_base_view variables - when there is no logout route - ''' - pgwui_common_init.includeme(pyramid_request_config) - - wrapper = pgwui_common_init.auth_base_view(mock_view) - request = get_current_request() - - response = wrapper(request) - pgwui = response['pgwui'] - check_base_view_results(request, pgwui) - - -# includeme() - -def test_includeme_configurecalled(): - '''Pyramid Configure() methods are called''' - class MockConfig(): - def __init__(self): - self.include_called = False - self.add_static_view_called = False - self.home_route = None - - def include(self, *args): - self.include_called = True - - def add_static_view(self, *args, **kwargs): - self.add_static_view_called = True - - def add_route(self, name, route): - if name == 'home': - self.home_route = route - - config = MockConfig() - pgwui_common_init.includeme(config) - assert config.include_called - assert config.add_static_view_called - assert config.home_route == '/' - - -# Integration tests - -def test_includeme(): - config = pyramid.config.Configurator() - pgwui_common_init.includeme(config) diff --git a/tests/test_pgwui_common.py b/tests/test_pgwui_common.py new file mode 100644 index 0000000..bab7ff3 --- /dev/null +++ b/tests/test_pgwui_common.py @@ -0,0 +1,135 @@ +# Copyright (C) 2018, 2020 The Meme Factory, Inc. http://www.karlpinc.com/ + +# This file is part of PGWUI_Common. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +# Karl O. Pinc + +import pyramid.config +from pyramid.threadlocal import get_current_request +import pgwui_common.pgwui_common as pgwui_common + +# Activiate our pytest plugin +pytest_plugins = ("pgwui",) + + +# Helper functions and constants + +CSS_URL = 'foo://bar/' + + +def mock_view(request): + return {'pgwui': {'url': {'css': CSS_URL}}} + + +def check_base_view_results(request, pgwui): + assert pgwui['url']['css'] == CSS_URL + url = (request.application_url + + pgwui_common.DEFAULT_HOME_ROUTE) + assert pgwui['route']['home'] == url + + +# Unit tests + +# base_view() +def test_base_view_add(pyramid_request_config): + '''The response adds all expected variables''' + def mock_view(request): + return {} + + pgwui_common.includeme(pyramid_request_config) + wrapper = pgwui_common.base_view(mock_view) + response = wrapper(get_current_request()) + assert response['pgwui']['url']['css'][0:4] == 'http' + + +def test_base_view_default(pyramid_request_config): + '''The response retains the mock view's variables''' + pgwui_common.includeme(pyramid_request_config) + wrapper = pgwui_common.base_view(mock_view) + request = get_current_request() + response = wrapper(request) + pgwui = response['pgwui'] + check_base_view_results(request, pgwui) + + +# auth_base_view() + +def test_auth_base_view_logout(pyramid_request_config): + '''The response contains base_view and auth_base_view variables + when there is a logout route + ''' + pgwui_common.includeme(pyramid_request_config) + + logout_route = '/logout' + pyramid_request_config.add_route('pgwui_logout', logout_route) + wrapper = pgwui_common.auth_base_view(mock_view) + request = get_current_request() + + response = wrapper(request) + pgwui = response['pgwui'] + check_base_view_results(request, pgwui) + assert pgwui['route']['pgwui_logout'] == (request.application_url + + logout_route) + + +def test_auth_base_view_nologout(pyramid_request_config): + '''The response contains base_view and auth_base_view variables + when there is no logout route + ''' + pgwui_common.includeme(pyramid_request_config) + + wrapper = pgwui_common.auth_base_view(mock_view) + request = get_current_request() + + response = wrapper(request) + pgwui = response['pgwui'] + check_base_view_results(request, pgwui) + + +# includeme() + +def test_includeme_configurecalled(): + '''Pyramid Configure() methods are called''' + class MockConfig(): + def __init__(self): + self.include_called = False + self.add_static_view_called = False + self.home_route = None + + def include(self, *args): + self.include_called = True + + def add_static_view(self, *args, **kwargs): + self.add_static_view_called = True + + def add_route(self, name, route): + if name == 'home': + self.home_route = route + + config = MockConfig() + pgwui_common.includeme(config) + assert config.include_called + assert config.add_static_view_called + assert config.home_route == '/' + + +# Integration tests + +def test_includeme(): + config = pyramid.config.Configurator() + pgwui_common.includeme(config)