From 3faba128a550cf17a7ffb6062248e4e519b010bc Mon Sep 17 00:00:00 2001 From: Maurice Lambert <50479118+mauricelambert@users.noreply.github.com> Date: Thu, 11 Apr 2024 10:36:29 +0000 Subject: [PATCH] Fix: syntax for python3.8 with black code format --- test/TestCommons.py | 25 ++-- test/TestPages.py | 257 +++++++++++++++------------------- test/TestUtils.py | 17 +-- test/TestWebScripts.py | 37 ++--- test/TestWebScriptsImports.py | 26 ++-- 5 files changed, 155 insertions(+), 207 deletions(-) diff --git a/test/TestCommons.py b/test/TestCommons.py index 457bb591..310d9582 100644 --- a/test/TestCommons.py +++ b/test/TestCommons.py @@ -204,21 +204,17 @@ def test_get_scripts_from_configuration(self): configuration.scripts = {"test.py": "test"} configuration.force_file_permissions = True - with ( - self.assertRaises(WebScriptsConfigurationError), - patch.object( - WebScripts.commons, "check_file_permission", return_value=True - ), + with self.assertRaises(WebScriptsConfigurationError), patch.object( + WebScripts.commons, "check_file_permission", return_value=True ): ScriptConfig.get_scripts_from_configuration(configuration, None) configuration.test = {} - with ( - patch.object(ScriptConfig, "get_script_path") as mock_config_path, - patch.object( - WebScripts.commons, "check_file_permission", return_value=True - ), + with patch.object( + ScriptConfig, "get_script_path" + ) as mock_config_path, patch.object( + WebScripts.commons, "check_file_permission", return_value=True ): mock_config_path.return_value = "/fake/path/test.py" script_configs = ScriptConfig.get_scripts_from_configuration( @@ -239,13 +235,8 @@ def test_get_scripts_from_configuration(self): WebScripts.commons, "get_real_path" ) as mock_real_path: mock_real_path.return_value = "/fake/path/test.py" - with ( - self.assertRaises(WebScriptsConfigurationError), - patch.object( - WebScripts.commons, - "check_file_permission", - return_value=True, - ), + with self.assertRaises(WebScriptsConfigurationError), patch.object( + WebScripts.commons, "check_file_permission", return_value=True ): ScriptConfig.get_scripts_from_configuration( configuration, configuration diff --git a/test/TestPages.py b/test/TestPages.py index 459e433a..9d36a2c3 100644 --- a/test/TestPages.py +++ b/test/TestPages.py @@ -96,20 +96,19 @@ def test_execute_scripts(self): script = Mock(path="path", launcher=None) Pages.scripts = {"test": script} - with ( - patch.object( - Module, - "get_environ", - return_value={"test": "test1"}, - ) as m_get_environ, - patch.object(Module, "Popen", return_value="process") as m_popen, - patch.object( - Module, - "start_process", - return_value=("stdout", "stderr", "key", "error", "code"), - ) as m_start_process, - patch.object(Module, "execution_logs") as m_logs, - ): + with patch.object( + Module, + "get_environ", + return_value={"test": "test1"}, + ) as m_get_environ, patch.object( + Module, "Popen", return_value="process" + ) as m_popen, patch.object( + Module, + "start_process", + return_value=("stdout", "stderr", "key", "error", "code"), + ) as m_start_process, patch.object( + Module, "execution_logs" + ) as m_logs: out, err, key, code, error = execute_scripts( "test", "User", @@ -144,20 +143,19 @@ def test_execute_scripts(self): script = Mock(path="path", launcher="launcher") Pages.scripts = {"test": script} - with ( - patch.object( - Module, - "get_environ", - return_value={"test": "test1"}, - ) as m_get_environ, - patch.object(Module, "Popen", return_value="process") as m_popen, - patch.object( - Module, - "start_process", - return_value=("stdout", "stderr", "key", "error", "code"), - ) as m_start_process, - patch.object(Module, "execution_logs") as m_logs, - ): + with patch.object( + Module, + "get_environ", + return_value={"test": "test1"}, + ) as m_get_environ, patch.object( + Module, "Popen", return_value="process" + ) as m_popen, patch.object( + Module, + "start_process", + return_value=("stdout", "stderr", "key", "error", "code"), + ) as m_start_process, patch.object( + Module, "execution_logs" + ) as m_logs: out, err, key, code, error = execute_scripts( "test", "User", @@ -419,14 +417,11 @@ class MyObjectTest: self.assertFalse(check_categories_scripts_access(user, configuration)) def test_decode_output(self): - with ( - patch.object( - WebScripts.utils, "getpreferredencoding", return_value=None - ) as m1, - patch.object( - WebScripts.utils, "device_encoding", return_value=None - ) as m2, - ): + with patch.object( + WebScripts.utils, "getpreferredencoding", return_value=None + ) as m1, patch.object( + WebScripts.utils, "device_encoding", return_value=None + ) as m2: self.assertEqual("\u2588", decode_output(b"\xe2\x96\x88")) self.assertEqual("\xff\xfe\xef", decode_output(b"\xff\xfe\xef")) self.assertEqual("€", decode_output(b"\x80")) @@ -701,16 +696,14 @@ def test_scripts(self): ) user.check_csrf = True - with ( - patch.object( - Module, - "execute_scripts", - return_value=(b"result", b"error", "key", 0, "TimeoutError"), - ) as mock, - patch.object(Module.TokenCSRF, "check_csrf", return_value=True), - patch.object( - Module.TokenCSRF, "build_token", return_value="token" - ), + with patch.object( + Module, + "execute_scripts", + return_value=(b"result", b"error", "key", 0, "TimeoutError"), + ) as mock, patch.object( + Module.TokenCSRF, "check_csrf", return_value=True + ), patch.object( + Module.TokenCSRF, "build_token", return_value="token" ): code, headers, data = self.api.scripts( Mock(), user, server, "other.sh", Mock(), Mock(), "token" @@ -802,31 +795,25 @@ def test_doc(self): server = Mock() env = Mock() - with ( - patch.object( - Module, - "check_right", - return_value=True, - ) as rigth, - patch.object( - Module, - "Popen", - return_value=Mock(), - ) as popen, - patch.object( - Module, "get_environ", return_value={"env": "env"} - ) as getenv, - patch.object( - Module.ScriptConfig, - "get_docfile_from_configuration", - return_value=None, - ) as get_docfile, - patch.object( - Module, - "get_real_path", - return_value="file.txt", - ) as get_file, - ): + with patch.object( + Module, + "check_right", + return_value=True, + ) as rigth, patch.object( + Module, + "Popen", + return_value=Mock(), + ) as popen, patch.object( + Module, "get_environ", return_value={"env": "env"} + ) as getenv, patch.object( + Module.ScriptConfig, + "get_docfile_from_configuration", + return_value=None, + ) as get_docfile, patch.object( + Module, + "get_real_path", + return_value="file.txt", + ) as get_file: code, headers, data = self.web.doc( env, user, server, "test.go", Mock(), Mock(), Mock() ) @@ -846,28 +833,23 @@ def test_doc(self): script.command_generate_documentation = None script.get_dict = None - with ( - patch.object( - Module, - "check_right", - return_value=True, - ) as rigth, - patch.object( - Module.path, - "isfile", - return_value=True, - ) as isfile, - patch.object( - Module, - "get_file_content", - return_value=b"data", - ) as getcontent, - patch.object( - Module, - "get_real_path", - return_value="file.txt", - ) as get_file, - ): + with patch.object( + Module, + "check_right", + return_value=True, + ) as rigth, patch.object( + Module.path, + "isfile", + return_value=True, + ) as isfile, patch.object( + Module, + "get_file_content", + return_value=b"data", + ) as getcontent, patch.object( + Module, + "get_real_path", + return_value="file.txt", + ) as get_file: code, headers, data = self.web.doc( env, user, server, "test.go", Mock(), Mock(), Mock() ) @@ -881,28 +863,23 @@ def test_doc(self): self.assertDictEqual(headers, {"Content-Type": "html; charset=utf-8"}) self.assertEqual(data, b"data") - with ( - patch.object( - Module, - "check_right", - return_value=True, - ) as rigth, - patch.object( - Module.ScriptConfig, - "get_docfile_from_configuration", - return_value="file.txt", - ) as get_docfile, - patch.object( - Module, - "get_file_content", - return_value=b"data", - ) as getcontent, - patch.object( - Module, - "get_real_path", - return_value="file.txt", - ) as get_file, - ): + with patch.object( + Module, + "check_right", + return_value=True, + ) as rigth, patch.object( + Module.ScriptConfig, + "get_docfile_from_configuration", + return_value="file.txt", + ) as get_docfile, patch.object( + Module, + "get_file_content", + return_value=b"data", + ) as getcontent, patch.object( + Module, + "get_real_path", + return_value="file.txt", + ) as get_file: code, headers, data = self.web.doc( env, user, server, "test.go", Mock(), Mock(), Mock() ) @@ -967,10 +944,9 @@ def test_scripts(self): Pages.scripts["other.sh"] = "other" - with ( - patch.object(Module, "check_right", return_value=True), - patch.object(Module, "CallableFile", return_value=None), - ): + with patch.object( + Module, "check_right", return_value=True + ), patch.object(Module, "CallableFile", return_value=None): code, headers, data = self.web.scripts( Mock(), Mock(), server, "other.sh", Mock(), Mock() ) @@ -988,11 +964,10 @@ def test_scripts(self): self.assertDictEqual({}, headers) self.assertEqual(data, b"") - with ( - patch.object(Module, "check_right", return_value=True), - patch.object( - Module, "CallableFile", return_value=Mock(return_value="test") - ), + with patch.object( + Module, "check_right", return_value=True + ), patch.object( + Module, "CallableFile", return_value=Mock(return_value="test") ): self.assertEqual( "test", @@ -1152,25 +1127,21 @@ def test_auth(self): ) user_session = Mock(id=1) - with ( - patch.object( - Module, - "execute_scripts", - return_value=( - b'{"data":"data"}', - b"", - "key", - 0, - "TimeoutError", - ), - ) as mock, - patch.object( - Module.User, "default_build", return_value=user_session - ) as getuser, - patch.object( - Module.Session, "build_session", return_value="cookie" - ) as session, - ): + with patch.object( + Module, + "execute_scripts", + return_value=( + b'{"data":"data"}', + b"", + "key", + 0, + "TimeoutError", + ), + ) as mock, patch.object( + Module.User, "default_build", return_value=user_session + ) as getuser, patch.object( + Module.Session, "build_session", return_value="cookie" + ) as session: code, headers, data = self.pages.auth( env, user, server, script, command, inputs ) diff --git a/test/TestUtils.py b/test/TestUtils.py index f249f984..2309d157 100644 --- a/test/TestUtils.py +++ b/test/TestUtils.py @@ -622,8 +622,8 @@ def st_mode(self): getpwuid = getattr(WebScripts.utils, "getpwuid", None) WebScripts.utils.user = "root" - WebScripts.utils.getpwuid = lambda x: ( - Mock(pw_name="root") if x == 0 else Mock() + WebScripts.utils.getpwuid = ( + lambda x: Mock(pw_name="root") if x == 0 else Mock() ) class SpecialMock(Mock): @@ -634,14 +634,11 @@ def __call__(self, file): isfile = WebScripts.utils.isfile mock = WebScripts.utils.isfile = SpecialMock() - with ( - patch.object( - WebScripts.utils, "stat", return_value=stat_response - ) as file, - patch.object( - WebScripts.utils, "isdir", return_value=True - ) as mock2, - ): + with patch.object( + WebScripts.utils, "stat", return_value=stat_response + ) as file, patch.object( + WebScripts.utils, "isdir", return_value=True + ) as mock2: self.assertTrue( check_file_permission(config, "test/test", False, False, True) ) diff --git a/test/TestWebScripts.py b/test/TestWebScripts.py index 48d7abae..1d9df3ff 100644 --- a/test/TestWebScripts.py +++ b/test/TestWebScripts.py @@ -335,9 +335,9 @@ def test_set_default_headers(self): Server.set_default_headers(h, True, c) - headers["Strict-Transport-Security"] = ( - "max-age=63072000; includeSubDomains; preload" - ) + headers[ + "Strict-Transport-Security" + ] = "max-age=63072000; includeSubDomains; preload" headers["Content-Security-Policy"] = ( "default-src 'self'; navigate-to 'self'; worker-src " "'none'; style-src-elem 'self'; style-src-attr 'none';" @@ -352,9 +352,9 @@ def test_set_default_headers(self): headers["X-XSS-Protection"] = "1; mode=block" headers["X-Content-Type-Options"] = "nosniff" headers["Referrer-Policy"] = "origin-when-cross-origin" - headers["Cache-Control"] = ( - "no-cache, no-store, must-revalidate, private" - ) + headers[ + "Cache-Control" + ] = "no-cache, no-store, must-revalidate, private" headers["Pragma"] = "no-cache" headers["Expires"] = "0" headers["Clear-Site-Data"] = '"cache", "executionContexts"' @@ -362,9 +362,9 @@ def test_set_default_headers(self): "payment 'none'; geolocation 'none'; " "microphone 'none'; camera 'none'" ) - headers["Permissions-Policy"] = ( - "microphone=(),camera=(),payment=(),geolocation=()" - ) + headers[ + "Permissions-Policy" + ] = "microphone=(),camera=(),payment=(),geolocation=()" headers["Cross-Origin-Embedder-Policy"] = "require-corp" headers["Cross-Origin-Opener-Policy"] = "same-origin" headers["Cross-Origin-Resource-Policy"] = "same-origin" @@ -1538,11 +1538,9 @@ def test_get_server_config(self): with patch.object( WebScripts.WebScripts, "system", - return_value=( - "Linux" - if WebScripts.WebScripts.system() == "Windows" - else "Windows" - ), + return_value="Linux" + if WebScripts.WebScripts.system() == "Windows" + else "Windows", ) as mock_method: for configurations in get_server_config(arguments): self.assertTrue(isinstance(configurations, dict)) @@ -1568,14 +1566,9 @@ def test_logs_configuration(self): def test_configure_logs_system(self): global WebScripts - with ( - patch.object( - WebScripts.WebScripts, - "check_file_permission", - return_value=False, - ), - self.assertRaises(WebScriptsSecurityError), - ): + with patch.object( + WebScripts.WebScripts, "check_file_permission", return_value=False + ), self.assertRaises(WebScriptsSecurityError): configure_logs_system() self.assertTrue(path.isdir("logs")) self.assertFalse(path.isfile(path.join("logs", "root.logs"))) diff --git a/test/TestWebScriptsImports.py b/test/TestWebScriptsImports.py index 7831cdb5..67052b2d 100644 --- a/test/TestWebScriptsImports.py +++ b/test/TestWebScriptsImports.py @@ -216,10 +216,9 @@ def isdir(directory): sys.modules["os"].mkdir = OsModule.mkdir -with ( - patch.object(sys.modules["os.path"], "isdir", return_value=False), - patch.object(sys.modules["path"], "isdir", return_value=False), -): +with patch.object( + sys.modules["os.path"], "isdir", return_value=False +), patch.object(sys.modules["path"], "isdir", return_value=False): import WebScripts real_check_file_permission = WebScripts.check_file_permission @@ -375,17 +374,14 @@ def change_WINDOWS_LOGS(*args): # lambda *x, **y: "Linux" if system() == "Windows" else "Windows" # ) -with ( - patch.object( - sys.modules["platform"], - "system", - return_value=("Linux" if system() == "Windows" else "Windows"), - ), - patch.object( - sys.modules["subprocess"], - "check_call", - return_value=0, - ), +with patch.object( + sys.modules["platform"], + "system", + return_value=("Linux" if system() == "Windows" else "Windows"), +), patch.object( + sys.modules["subprocess"], + "check_call", + return_value=0, ): utils.get_real_path("/test/whynot", no_error=True) _exec(utils.__spec__, utils)