| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  | # -*- coding: utf-8; -*- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from unittest.mock import MagicMock, patch | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from wuttjamaican.testing import ConfigTestCase | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from wuttaweb.cli import webapp as mod | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestWebapp(ConfigTestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def make_context(self, **kwargs): | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         params = {"auto_reload": False} | 
					
						
							|  |  |  |         params.update(kwargs.get("params", {})) | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |         ctx = MagicMock(params=params) | 
					
						
							|  |  |  |         ctx.parent.wutta_config = self.config | 
					
						
							|  |  |  |         return ctx | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_missing_config_file(self): | 
					
						
							|  |  |  |         # nb. our default config has no files, so can test w/ that | 
					
						
							|  |  |  |         ctx = self.make_context() | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         with patch.object(mod, "sys") as sys: | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |             sys.exit.side_effect = RuntimeError | 
					
						
							|  |  |  |             self.assertRaises(RuntimeError, mod.webapp, ctx) | 
					
						
							|  |  |  |             sys.stderr.write.assert_called_once_with("no config files found!\n") | 
					
						
							|  |  |  |             sys.exit.assert_called_once_with(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_invalid_runner(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # make new config from file, with bad setting | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         path = self.write_file( | 
					
						
							|  |  |  |             "my.conf", | 
					
						
							|  |  |  |             """
 | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  | [wutta.web] | 
					
						
							|  |  |  | app.runner = bogus | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  | """,
 | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |         self.config = self.make_config(files=[path]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ctx = self.make_context() | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         with patch.object(mod, "sys") as sys: | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |             sys.exit.side_effect = RuntimeError | 
					
						
							|  |  |  |             self.assertRaises(RuntimeError, mod.webapp, ctx) | 
					
						
							|  |  |  |             sys.stderr.write.assert_called_once_with("unknown web app runner: bogus\n") | 
					
						
							|  |  |  |             sys.exit.assert_called_once_with(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_pserve(self): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         path = self.write_file( | 
					
						
							|  |  |  |             "my.conf", | 
					
						
							|  |  |  |             """
 | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  | [wutta.web] | 
					
						
							|  |  |  | app.runner = pserve | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  | """,
 | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |         self.config = self.make_config(files=[path]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # normal | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         with patch.object(mod, "pserve") as pserve: | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |             ctx = self.make_context() | 
					
						
							|  |  |  |             mod.webapp(ctx) | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             pserve.main.assert_called_once_with(argv=["pserve", f"file+ini:{path}"]) | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # with reload | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         with patch.object(mod, "pserve") as pserve: | 
					
						
							|  |  |  |             ctx = self.make_context(params={"auto_reload": True}) | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |             mod.webapp(ctx) | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             pserve.main.assert_called_once_with( | 
					
						
							|  |  |  |                 argv=["pserve", f"file+ini:{path}", "--reload"] | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_uvicorn(self): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         path = self.write_file( | 
					
						
							|  |  |  |             "my.conf", | 
					
						
							|  |  |  |             """
 | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  | [wutta.web] | 
					
						
							|  |  |  | app.runner = uvicorn | 
					
						
							|  |  |  | app.spec = wuttaweb.app:make_wsgi_app | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  | """,
 | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |         self.config = self.make_config(files=[path]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         orig_import = __import__ | 
					
						
							|  |  |  |         uvicorn = MagicMock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def mock_import(name, *args, **kwargs): | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             if name == "uvicorn": | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |                 return uvicorn | 
					
						
							|  |  |  |             return orig_import(name, *args, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # normal | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         with patch("builtins.__import__", side_effect=mock_import): | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |             ctx = self.make_context() | 
					
						
							|  |  |  |             mod.webapp(ctx) | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             uvicorn.run.assert_called_once_with( | 
					
						
							|  |  |  |                 "wuttaweb.app:make_wsgi_app", | 
					
						
							|  |  |  |                 host="127.0.0.1", | 
					
						
							|  |  |  |                 port=8000, | 
					
						
							|  |  |  |                 reload=False, | 
					
						
							|  |  |  |                 reload_dirs=None, | 
					
						
							|  |  |  |                 factory=False, | 
					
						
							|  |  |  |                 interface="auto", | 
					
						
							|  |  |  |                 root_path="", | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # with reload | 
					
						
							|  |  |  |         uvicorn.run.reset_mock() | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |         with patch("builtins.__import__", side_effect=mock_import): | 
					
						
							|  |  |  |             ctx = self.make_context(params={"auto_reload": True}) | 
					
						
							| 
									
										
										
										
											2024-12-18 11:28:08 -06:00
										 |  |  |             mod.webapp(ctx) | 
					
						
							| 
									
										
										
										
											2025-08-31 12:26:43 -05:00
										 |  |  |             uvicorn.run.assert_called_once_with( | 
					
						
							|  |  |  |                 "wuttaweb.app:make_wsgi_app", | 
					
						
							|  |  |  |                 host="127.0.0.1", | 
					
						
							|  |  |  |                 port=8000, | 
					
						
							|  |  |  |                 reload=True, | 
					
						
							|  |  |  |                 reload_dirs=None, | 
					
						
							|  |  |  |                 factory=False, | 
					
						
							|  |  |  |                 interface="auto", | 
					
						
							|  |  |  |                 root_path="", | 
					
						
							|  |  |  |             ) |