diff --git a/src/wuttaweb/testing.py b/src/wuttaweb/testing.py new file mode 100644 index 0000000..0a1916b --- /dev/null +++ b/src/wuttaweb/testing.py @@ -0,0 +1,84 @@ +# -*- coding: utf-8; -*- +################################################################################ +# +# wuttaweb -- Web App for Wutta Framework +# Copyright © 2024 Lance Edgar +# +# This file is part of Wutta Framework. +# +# Wutta Framework is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# Wutta Framework 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 General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# Wutta Framework. If not, see . +# +################################################################################ +""" +WuttaWeb - test utilities +""" + +from unittest.mock import MagicMock + +import fanstatic +from pyramid import testing + +from wuttjamaican.testing import DataTestCase + +from wuttaweb import subscribers + + +class WebTestCase(DataTestCase): + """ + Base class for test suites requiring a full (typical) web app. + """ + + def setUp(self): + self.setup_web() + + def setup_web(self): + self.setup_db() + self.request = self.make_request() + self.pyramid_config = testing.setUp(request=self.request, settings={ + 'wutta_config': self.config, + 'mako.directories': ['wuttaweb:templates'], + 'pyramid_deform.template_search_path': 'wuttaweb:templates/deform', + }) + + # init web + self.pyramid_config.include('pyramid_deform') + self.pyramid_config.include('pyramid_mako') + self.pyramid_config.add_directive('add_wutta_permission_group', + 'wuttaweb.auth.add_permission_group') + self.pyramid_config.add_directive('add_wutta_permission', + 'wuttaweb.auth.add_permission') + self.pyramid_config.add_subscriber('wuttaweb.subscribers.before_render', + 'pyramid.events.BeforeRender') + self.pyramid_config.include('wuttaweb.static') + + # nb. mock out fanstatic env..good enough for now to avoid errors.. + needed = fanstatic.init_needed() + self.request.environ[fanstatic.NEEDED] = needed + + # setup new request w/ anonymous user + event = MagicMock(request=self.request) + subscribers.new_request(event) + def user_getter(request, **kwargs): pass + subscribers.new_request_set_user(event, db_session=self.session, + user_getter=user_getter) + + def tearDown(self): + self.teardown_web() + + def teardown_web(self): + testing.tearDown() + self.teardown_db() + + def make_request(self): + return testing.DummyRequest() diff --git a/tests/db/test_continuum.py b/tests/db/test_continuum.py index dbaa411..0503fd1 100644 --- a/tests/db/test_continuum.py +++ b/tests/db/test_continuum.py @@ -4,9 +4,8 @@ from unittest.mock import patch, MagicMock import pytest -from tests.util import WebTestCase - from wuttaweb.db import continuum as mod +from wuttaweb.testing import WebTestCase class TestWuttaWebContinuumPlugin(WebTestCase): diff --git a/tests/forms/test_schema.py b/tests/forms/test_schema.py index 4dfc962..80a40e2 100644 --- a/tests/forms/test_schema.py +++ b/tests/forms/test_schema.py @@ -12,7 +12,7 @@ from sqlalchemy import orm from wuttjamaican.conf import WuttaConfig from wuttaweb.forms import schema as mod from wuttaweb.forms import widgets -from tests.util import DataTestCase, WebTestCase +from wuttaweb.testing import DataTestCase, WebTestCase class TestWutaDateTime(TestCase): diff --git a/tests/forms/test_widgets.py b/tests/forms/test_widgets.py index 7752dfe..47aed58 100644 --- a/tests/forms/test_widgets.py +++ b/tests/forms/test_widgets.py @@ -13,7 +13,7 @@ from wuttaweb.forms import widgets as mod from wuttaweb.forms import schema from wuttaweb.forms.schema import (FileDownload, PersonRef, RoleRefs, UserRefs, Permissions, WuttaDateTime, EmailRecipients) -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestObjectRefWidget(WebTestCase): diff --git a/tests/grids/test_base.py b/tests/grids/test_base.py index 28478d2..4282554 100644 --- a/tests/grids/test_base.py +++ b/tests/grids/test_base.py @@ -16,7 +16,7 @@ from wuttaweb.grids import base as mod from wuttaweb.grids.filters import GridFilter, StringAlchemyFilter, default_sqlalchemy_filters from wuttaweb.util import FieldList from wuttaweb.forms import Form -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestGrid(WebTestCase): diff --git a/tests/grids/test_filters.py b/tests/grids/test_filters.py index c58d1af..bbc6611 100644 --- a/tests/grids/test_filters.py +++ b/tests/grids/test_filters.py @@ -9,7 +9,7 @@ import sqlalchemy as sa from wuttjamaican.db.model import Base from wuttaweb.grids import filters as mod -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestGridFilter(WebTestCase): diff --git a/tests/test_auth.py b/tests/test_auth.py index f7705c8..a7dfe16 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -8,7 +8,7 @@ from pyramid import testing from wuttjamaican.conf import WuttaConfig from wuttaweb import auth as mod -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestLoginUser(TestCase): diff --git a/tests/test_handler.py b/tests/test_handler.py index effb413..7c360fe 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -4,7 +4,7 @@ from wuttaweb import handler as mod, static from wuttaweb.forms import Form from wuttaweb.grids import Grid from wuttaweb.menus import MenuHandler -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestWebHandler(WebTestCase): diff --git a/tests/test_menus.py b/tests/test_menus.py index 84c4ee5..bca7bda 100644 --- a/tests/test_menus.py +++ b/tests/test_menus.py @@ -4,7 +4,7 @@ from unittest import TestCase from unittest.mock import patch, MagicMock from wuttaweb import menus as mod -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestMenuHandler(WebTestCase): diff --git a/tests/util.py b/tests/util.py index e292253..e56d012 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,14 +1,7 @@ # -*- coding: utf-8; -*- -from unittest import TestCase -from unittest.mock import MagicMock - -import fanstatic -from pyramid import testing - from wuttjamaican.conf import WuttaConfig from wuttjamaican.testing import FileConfigTestCase -from wuttaweb import subscribers from wuttaweb.menus import MenuHandler @@ -39,56 +32,6 @@ class DataTestCase(FileConfigTestCase): self.teardown_files() -class WebTestCase(DataTestCase): - """ - Base class for test suites requiring a full (typical) web app. - """ - - def setUp(self): - self.setup_web() - - def setup_web(self): - self.setup_db() - self.request = self.make_request() - self.pyramid_config = testing.setUp(request=self.request, settings={ - 'wutta_config': self.config, - 'mako.directories': ['wuttaweb:templates'], - 'pyramid_deform.template_search_path': 'wuttaweb:templates/deform', - }) - - # init web - self.pyramid_config.include('pyramid_deform') - self.pyramid_config.include('pyramid_mako') - self.pyramid_config.add_directive('add_wutta_permission_group', - 'wuttaweb.auth.add_permission_group') - self.pyramid_config.add_directive('add_wutta_permission', - 'wuttaweb.auth.add_permission') - self.pyramid_config.add_subscriber('wuttaweb.subscribers.before_render', - 'pyramid.events.BeforeRender') - self.pyramid_config.include('wuttaweb.static') - - # nb. mock out fanstatic env..good enough for now to avoid errors.. - needed = fanstatic.init_needed() - self.request.environ[fanstatic.NEEDED] = needed - - # setup new request w/ anonymous user - event = MagicMock(request=self.request) - subscribers.new_request(event) - def user_getter(request, **kwargs): pass - subscribers.new_request_set_user(event, db_session=self.session, - user_getter=user_getter) - - def tearDown(self): - self.teardown_web() - - def teardown_web(self): - testing.tearDown() - self.teardown_db() - - def make_request(self): - return testing.DummyRequest() - - class NullMenuHandler(MenuHandler): """ Dummy menu handler for testing. diff --git a/tests/views/test___init__.py b/tests/views/test___init__.py index 6da63f0..1506a69 100644 --- a/tests/views/test___init__.py +++ b/tests/views/test___init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8; -*- -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestIncludeMe(WebTestCase): diff --git a/tests/views/test_auth.py b/tests/views/test_auth.py index 5e7607f..c64ed3b 100644 --- a/tests/views/test_auth.py +++ b/tests/views/test_auth.py @@ -5,7 +5,7 @@ from unittest.mock import MagicMock, patch from pyramid.httpexceptions import HTTPFound, HTTPForbidden from wuttaweb.views import auth as mod -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestAuthView(WebTestCase): diff --git a/tests/views/test_base.py b/tests/views/test_base.py index 9601212..4b3c4ab 100644 --- a/tests/views/test_base.py +++ b/tests/views/test_base.py @@ -5,7 +5,7 @@ from pyramid.httpexceptions import HTTPFound, HTTPForbidden, HTTPNotFound from wuttaweb.views import base as mod from wuttaweb.forms import Form from wuttaweb.grids import Grid, GridAction -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestView(WebTestCase): diff --git a/tests/views/test_batch.py b/tests/views/test_batch.py index 9b1db0f..26e570a 100644 --- a/tests/views/test_batch.py +++ b/tests/views/test_batch.py @@ -10,7 +10,7 @@ from wuttjamaican.db import model from wuttjamaican.batch import BatchHandler from wuttaweb.views import MasterView, batch as mod from wuttaweb.progress import SessionProgress -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class MockBatch(model.BatchMixin, model.Base): diff --git a/tests/views/test_common.py b/tests/views/test_common.py index 4d86ba6..f889c00 100644 --- a/tests/views/test_common.py +++ b/tests/views/test_common.py @@ -5,7 +5,7 @@ from unittest.mock import patch import colander from wuttaweb.views import common as mod -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestCommonView(WebTestCase): diff --git a/tests/views/test_email.py b/tests/views/test_email.py index a56bf66..7303f06 100644 --- a/tests/views/test_email.py +++ b/tests/views/test_email.py @@ -9,7 +9,7 @@ from pyramid.httpexceptions import HTTPNotFound from pyramid.response import Response from wuttaweb.views import email as mod -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestEmailSettingViews(WebTestCase): diff --git a/tests/views/test_essential.py b/tests/views/test_essential.py index 9c3dc5d..afb29ba 100644 --- a/tests/views/test_essential.py +++ b/tests/views/test_essential.py @@ -1,7 +1,7 @@ # -*- coding: utf-8; -*- from wuttaweb.views import essential as mod -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestEssentialViews(WebTestCase): diff --git a/tests/views/test_master.py b/tests/views/test_master.py index 3d06f49..56c51c2 100644 --- a/tests/views/test_master.py +++ b/tests/views/test_master.py @@ -16,7 +16,7 @@ from wuttaweb.views import master as mod from wuttaweb.views import View from wuttaweb.progress import SessionProgress from wuttaweb.subscribers import new_request_set_user -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestMasterView(WebTestCase): diff --git a/tests/views/test_people.py b/tests/views/test_people.py index d3341f8..25abf9e 100644 --- a/tests/views/test_people.py +++ b/tests/views/test_people.py @@ -7,7 +7,7 @@ from sqlalchemy import orm from pyramid.httpexceptions import HTTPNotFound from wuttaweb.views import people -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestPersonView(WebTestCase): diff --git a/tests/views/test_progress.py b/tests/views/test_progress.py index 06a67f8..00edd69 100644 --- a/tests/views/test_progress.py +++ b/tests/views/test_progress.py @@ -4,7 +4,7 @@ from pyramid import testing from wuttaweb.views import progress as mod from wuttaweb.progress import get_progress_session -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestProgressView(WebTestCase): diff --git a/tests/views/test_roles.py b/tests/views/test_roles.py index 22a30d2..909739e 100644 --- a/tests/views/test_roles.py +++ b/tests/views/test_roles.py @@ -8,7 +8,7 @@ import colander from wuttaweb.views import roles as mod from wuttaweb.forms.schema import RoleRef -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestRoleView(WebTestCase): diff --git a/tests/views/test_settings.py b/tests/views/test_settings.py index c0810fa..ffc1fc8 100644 --- a/tests/views/test_settings.py +++ b/tests/views/test_settings.py @@ -6,7 +6,7 @@ import colander from pyramid.httpexceptions import HTTPNotFound from wuttaweb.views import settings as mod -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestAppInfoView(WebTestCase): diff --git a/tests/views/test_upgrades.py b/tests/views/test_upgrades.py index 1a1d626..e468ce3 100644 --- a/tests/views/test_upgrades.py +++ b/tests/views/test_upgrades.py @@ -8,7 +8,7 @@ from unittest.mock import patch, MagicMock from wuttaweb.views import upgrades as mod from wuttjamaican.exc import ConfigurationError from wuttaweb.progress import get_progress_session -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestUpgradeView(WebTestCase): diff --git a/tests/views/test_users.py b/tests/views/test_users.py index ea49d49..1fa08f2 100644 --- a/tests/views/test_users.py +++ b/tests/views/test_users.py @@ -7,7 +7,7 @@ from sqlalchemy import orm import colander from wuttaweb.views import users as mod -from tests.util import WebTestCase +from wuttaweb.testing import WebTestCase class TestUserView(WebTestCase):