2024-07-12 00:17:15 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
|
|
|
|
from unittest import TestCase
|
2024-12-18 11:28:08 -06:00
|
|
|
from unittest.mock import patch
|
2024-07-12 00:17:15 -05:00
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
from wuttjamaican.testing import FileTestCase, ConfigTestCase, DataTestCase
|
2024-07-12 00:17:15 -05:00
|
|
|
|
2024-12-18 15:09:16 -06:00
|
|
|
from asgiref.wsgi import WsgiToAsgi
|
2024-07-12 00:17:15 -05:00
|
|
|
from pyramid.config import Configurator
|
|
|
|
from pyramid.router import Router
|
|
|
|
|
|
|
|
from wuttaweb import app as mod
|
2024-08-06 18:52:54 -05:00
|
|
|
from wuttjamaican.conf import WuttaConfig
|
2025-06-29 09:16:44 -05:00
|
|
|
from wuttjamaican.app import AppHandler
|
|
|
|
from wuttjamaican.util import resource_path
|
2024-08-06 18:52:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
class TestWebAppProvider(TestCase):
|
|
|
|
|
|
|
|
def test_basic(self):
|
|
|
|
# nb. just normal usage here, confirm it does the one thing we
|
|
|
|
# need it to..
|
|
|
|
config = WuttaConfig()
|
|
|
|
app = config.get_app()
|
|
|
|
handler = app.get_web_handler()
|
2024-07-12 00:17:15 -05:00
|
|
|
|
|
|
|
|
2024-12-18 11:28:08 -06:00
|
|
|
class TestMakeWuttaConfig(FileTestCase):
|
2024-07-12 00:17:15 -05:00
|
|
|
|
|
|
|
def test_config_path_required(self):
|
|
|
|
|
|
|
|
# settings must define config path, else error
|
|
|
|
settings = {}
|
|
|
|
self.assertRaises(ValueError, mod.make_wutta_config, settings)
|
|
|
|
|
|
|
|
def test_basic(self):
|
|
|
|
|
|
|
|
# mock path to config file
|
|
|
|
myconf = self.write_file('my.conf', '')
|
|
|
|
settings = {'wutta.config': myconf}
|
|
|
|
|
|
|
|
# can make a config okay
|
|
|
|
config = mod.make_wutta_config(settings)
|
|
|
|
|
|
|
|
# and that config is also stored in settings
|
|
|
|
self.assertIn('wutta_config', settings)
|
|
|
|
self.assertIs(settings['wutta_config'], config)
|
|
|
|
|
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
class TestMakePyramidConfig(DataTestCase):
|
2024-07-12 00:17:15 -05:00
|
|
|
|
|
|
|
def test_basic(self):
|
2025-06-29 09:16:44 -05:00
|
|
|
with patch.object(AppHandler, 'make_session', return_value=self.session):
|
|
|
|
settings = {'wutta_config': self.config}
|
|
|
|
config = mod.make_pyramid_config(settings)
|
|
|
|
self.assertIsInstance(config, Configurator)
|
|
|
|
self.assertEqual(settings['wuttaweb.theme'], 'default')
|
2024-07-12 00:17:15 -05:00
|
|
|
|
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
class TestMain(DataTestCase):
|
2024-07-12 00:17:15 -05:00
|
|
|
|
|
|
|
def test_basic(self):
|
2025-06-29 09:16:44 -05:00
|
|
|
with patch.object(AppHandler, 'make_session', return_value=self.session):
|
|
|
|
global_config = None
|
|
|
|
myconf = self.write_file('my.conf', '')
|
|
|
|
settings = {'wutta.config': myconf}
|
|
|
|
app = mod.main(global_config, **settings)
|
|
|
|
self.assertIsInstance(app, Router)
|
2024-12-18 11:28:08 -06:00
|
|
|
|
|
|
|
|
|
|
|
def mock_main(global_config, **settings):
|
|
|
|
|
|
|
|
wutta_config = mod.make_wutta_config(settings)
|
|
|
|
pyramid_config = mod.make_pyramid_config(settings)
|
|
|
|
|
|
|
|
pyramid_config.include('wuttaweb.static')
|
|
|
|
pyramid_config.include('wuttaweb.subscribers')
|
|
|
|
pyramid_config.include('wuttaweb.views')
|
|
|
|
|
|
|
|
return pyramid_config.make_wsgi_app()
|
|
|
|
|
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
class TestMakeWsgiApp(DataTestCase):
|
2024-12-18 11:28:08 -06:00
|
|
|
|
|
|
|
def test_with_callable(self):
|
2025-06-29 09:16:44 -05:00
|
|
|
with patch.object(self.app, 'make_session', return_value=self.session):
|
2024-12-18 11:28:08 -06:00
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
# specify config
|
|
|
|
wsgi = mod.make_wsgi_app(mock_main, config=self.config)
|
2024-12-18 11:28:08 -06:00
|
|
|
self.assertIsInstance(wsgi, Router)
|
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
# auto config
|
|
|
|
with patch.object(mod, 'make_config', return_value=self.config):
|
|
|
|
wsgi = mod.make_wsgi_app(mock_main)
|
|
|
|
self.assertIsInstance(wsgi, Router)
|
|
|
|
|
2024-12-18 11:28:08 -06:00
|
|
|
def test_with_spec(self):
|
|
|
|
|
|
|
|
# specify config
|
|
|
|
wsgi = mod.make_wsgi_app('tests.test_app:mock_main', config=self.config)
|
|
|
|
self.assertIsInstance(wsgi, Router)
|
|
|
|
|
|
|
|
# auto config
|
|
|
|
with patch.object(mod, 'make_config', return_value=self.config):
|
|
|
|
wsgi = mod.make_wsgi_app('tests.test_app:mock_main')
|
|
|
|
self.assertIsInstance(wsgi, Router)
|
|
|
|
|
|
|
|
def test_invalid(self):
|
|
|
|
self.assertRaises(ValueError, mod.make_wsgi_app, 42, config=self.config)
|
2024-12-18 15:09:16 -06:00
|
|
|
|
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
class TestMakeAsgiApp(DataTestCase):
|
2024-12-18 15:09:16 -06:00
|
|
|
|
|
|
|
def test_with_callable(self):
|
2025-06-29 09:16:44 -05:00
|
|
|
with patch.object(self.app, 'make_session', return_value=self.session):
|
2024-12-18 15:09:16 -06:00
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
# specify config
|
|
|
|
asgi = mod.make_asgi_app(mock_main, config=self.config)
|
2024-12-18 15:09:16 -06:00
|
|
|
self.assertIsInstance(asgi, WsgiToAsgi)
|
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
# auto config
|
|
|
|
with patch.object(mod, 'make_config', return_value=self.config):
|
|
|
|
asgi = mod.make_asgi_app(mock_main)
|
|
|
|
self.assertIsInstance(asgi, WsgiToAsgi)
|
2024-12-18 15:09:16 -06:00
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
def test_with_spec(self):
|
|
|
|
with patch.object(self.app, 'make_session', return_value=self.session):
|
2024-12-18 15:09:16 -06:00
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
# specify config
|
|
|
|
asgi = mod.make_asgi_app('tests.test_app:mock_main', config=self.config)
|
2024-12-18 15:09:16 -06:00
|
|
|
self.assertIsInstance(asgi, WsgiToAsgi)
|
|
|
|
|
2025-06-29 09:16:44 -05:00
|
|
|
# auto config
|
|
|
|
with patch.object(mod, 'make_config', return_value=self.config):
|
|
|
|
asgi = mod.make_asgi_app('tests.test_app:mock_main')
|
|
|
|
self.assertIsInstance(asgi, WsgiToAsgi)
|
|
|
|
|
2024-12-18 15:09:16 -06:00
|
|
|
def test_invalid(self):
|
|
|
|
self.assertRaises(ValueError, mod.make_asgi_app, 42, config=self.config)
|
2025-06-29 09:16:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
class TestEstablishTheme(DataTestCase):
|
|
|
|
|
|
|
|
def test_default(self):
|
|
|
|
settings = {
|
|
|
|
'wutta_config': self.config,
|
|
|
|
'mako.directories': ['wuttaweb:templates'],
|
|
|
|
}
|
|
|
|
mod.establish_theme(settings)
|
|
|
|
self.assertEqual(settings['wuttaweb.theme'], 'default')
|
|
|
|
self.assertEqual(settings['mako.directories'], [
|
|
|
|
resource_path('wuttaweb:templates/themes/default'),
|
|
|
|
'wuttaweb:templates',
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_mako_dirs_as_string(self):
|
|
|
|
settings = {
|
|
|
|
'wutta_config': self.config,
|
|
|
|
'mako.directories': 'wuttaweb:templates',
|
|
|
|
}
|
|
|
|
mod.establish_theme(settings)
|
|
|
|
self.assertEqual(settings['wuttaweb.theme'], 'default')
|
|
|
|
self.assertEqual(settings['mako.directories'], [
|
|
|
|
resource_path('wuttaweb:templates/themes/default'),
|
|
|
|
'wuttaweb:templates',
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_butterfly(self):
|
|
|
|
settings = {
|
|
|
|
'wutta_config': self.config,
|
|
|
|
'mako.directories': 'wuttaweb:templates',
|
|
|
|
}
|
|
|
|
self.app.save_setting(self.session, 'wuttaweb.theme', 'butterfly')
|
|
|
|
self.session.commit()
|
|
|
|
mod.establish_theme(settings)
|
|
|
|
self.assertEqual(settings['wuttaweb.theme'], 'butterfly')
|
|
|
|
self.assertEqual(settings['mako.directories'], [
|
|
|
|
resource_path('wuttaweb:templates/themes/butterfly'),
|
|
|
|
'wuttaweb:templates',
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_custom(self):
|
|
|
|
settings = {
|
|
|
|
'wutta_config': self.config,
|
|
|
|
'mako.directories': 'wuttaweb:templates',
|
|
|
|
}
|
|
|
|
self.config.setdefault('wuttaweb.themes.keys', 'anotherone')
|
|
|
|
self.app.save_setting(self.session, 'wuttaweb.theme', 'anotherone')
|
|
|
|
self.session.commit()
|
|
|
|
mod.establish_theme(settings)
|
|
|
|
self.assertEqual(settings['wuttaweb.theme'], 'anotherone')
|
|
|
|
self.assertEqual(settings['mako.directories'], [
|
|
|
|
resource_path('wuttaweb:templates/themes/anotherone'),
|
|
|
|
'wuttaweb:templates',
|
|
|
|
])
|