| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         myconf = self.write_file("my.conf", "") | 
					
						
							|  |  |  |         settings = {"wutta.config": myconf} | 
					
						
							| 
									
										
										
										
											2024-07-12 00:17:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # can make a config okay | 
					
						
							|  |  |  |         config = mod.make_wutta_config(settings) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # and that config is also stored in settings | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         self.assertIn("wutta_config", settings) | 
					
						
							|  |  |  |         self.assertIs(settings["wutta_config"], config) | 
					
						
							| 
									
										
										
										
											2024-07-12 00:17:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  | class TestMakePyramidConfig(DataTestCase): | 
					
						
							| 
									
										
										
										
											2024-07-12 00:17:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_basic(self): | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         with patch.object(AppHandler, "make_session", return_value=self.session): | 
					
						
							|  |  |  |             settings = {"wutta_config": self.config} | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |             config = mod.make_pyramid_config(settings) | 
					
						
							|  |  |  |             self.assertIsInstance(config, Configurator) | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             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-08-31 12:26:43 -05:00
										 |  |  |         with patch.object(AppHandler, "make_session", return_value=self.session): | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |             global_config = None | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             myconf = self.write_file("my.conf", "") | 
					
						
							|  |  |  |             settings = {"wutta.config": myconf} | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |             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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |     pyramid_config.include("wuttaweb.static") | 
					
						
							|  |  |  |     pyramid_config.include("wuttaweb.subscribers") | 
					
						
							|  |  |  |     pyramid_config.include("wuttaweb.views") | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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-08-31 12:26:43 -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 | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             with patch.object(mod, "make_config", return_value=self.config): | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |                 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 | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         wsgi = mod.make_wsgi_app("tests.test_app:mock_main", config=self.config) | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |         self.assertIsInstance(wsgi, Router) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # auto config | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         with patch.object(mod, "make_config", return_value=self.config): | 
					
						
							|  |  |  |             wsgi = mod.make_wsgi_app("tests.test_app:mock_main") | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |             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-08-31 12:26:43 -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 | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             with patch.object(mod, "make_config", return_value=self.config): | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |                 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): | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -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 | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             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 | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             with patch.object(mod, "make_config", return_value=self.config): | 
					
						
							|  |  |  |                 asgi = mod.make_asgi_app("tests.test_app:mock_main") | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |                 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 = { | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             "wutta_config": self.config, | 
					
						
							|  |  |  |             "mako.directories": ["wuttaweb:templates"], | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         mod.establish_theme(settings) | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         self.assertEqual(settings["wuttaweb.theme"], "default") | 
					
						
							|  |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             settings["mako.directories"], | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 resource_path("wuttaweb:templates/themes/default"), | 
					
						
							|  |  |  |                 "wuttaweb:templates", | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_mako_dirs_as_string(self): | 
					
						
							|  |  |  |         settings = { | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             "wutta_config": self.config, | 
					
						
							|  |  |  |             "mako.directories": "wuttaweb:templates", | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         mod.establish_theme(settings) | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         self.assertEqual(settings["wuttaweb.theme"], "default") | 
					
						
							|  |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             settings["mako.directories"], | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 resource_path("wuttaweb:templates/themes/default"), | 
					
						
							|  |  |  |                 "wuttaweb:templates", | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_butterfly(self): | 
					
						
							|  |  |  |         settings = { | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             "wutta_config": self.config, | 
					
						
							|  |  |  |             "mako.directories": "wuttaweb:templates", | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         self.app.save_setting(self.session, "wuttaweb.theme", "butterfly") | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |         self.session.commit() | 
					
						
							|  |  |  |         mod.establish_theme(settings) | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         self.assertEqual(settings["wuttaweb.theme"], "butterfly") | 
					
						
							|  |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             settings["mako.directories"], | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 resource_path("wuttaweb:templates/themes/butterfly"), | 
					
						
							|  |  |  |                 "wuttaweb:templates", | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_custom(self): | 
					
						
							|  |  |  |         settings = { | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             "wutta_config": self.config, | 
					
						
							|  |  |  |             "mako.directories": "wuttaweb:templates", | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         self.config.setdefault("wuttaweb.themes.keys", "anotherone") | 
					
						
							|  |  |  |         self.app.save_setting(self.session, "wuttaweb.theme", "anotherone") | 
					
						
							| 
									
										
										
										
											2025-06-29 09:16:44 -05:00
										 |  |  |         self.session.commit() | 
					
						
							|  |  |  |         mod.establish_theme(settings) | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         self.assertEqual(settings["wuttaweb.theme"], "anotherone") | 
					
						
							|  |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             settings["mako.directories"], | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 resource_path("wuttaweb:templates/themes/anotherone"), | 
					
						
							|  |  |  |                 "wuttaweb:templates", | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ) |