From 098625621cc4e943c5e467b03b6386115b89aa44 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 20 Oct 2019 10:46:14 -0700 Subject: [PATCH] Replace .format with python 3.6 f-strings in tests --- tests/GuiBaseTest.py | 18 +++++++----------- tests/GuiReceiveTest.py | 8 ++++---- tests/GuiShareTest.py | 8 ++++---- tests/GuiWebsiteTest.py | 4 ++-- tests/TorGuiBaseTest.py | 28 ++++++++++------------------ tests/TorGuiReceiveTest.py | 8 +++----- tests/TorGuiShareTest.py | 8 +++----- tests/test_onionshare_web.py | 8 ++------ 8 files changed, 35 insertions(+), 55 deletions(-) diff --git a/tests/GuiBaseTest.py b/tests/GuiBaseTest.py index 6d6340d1..daa7cb09 100644 --- a/tests/GuiBaseTest.py +++ b/tests/GuiBaseTest.py @@ -129,7 +129,7 @@ class GuiBaseTest(object): if type(mode) == ReceiveMode: # Upload a file files = {"file[]": open("/tmp/test.txt", "rb")} - url = "http://127.0.0.1:{}/upload".format(self.gui.app.port) + url = f"http://127.0.0.1:{self.gui.app.port}/upload" if public_mode: r = requests.post(url, files=files) else: @@ -142,7 +142,7 @@ class GuiBaseTest(object): if type(mode) == ShareMode: # Download files - url = "http://127.0.0.1:{}/download".format(self.gui.app.port) + url = f"http://127.0.0.1:{self.gui.app.port}/download" if public_mode: r = requests.get(url) else: @@ -201,7 +201,7 @@ class GuiBaseTest(object): def web_server_is_running(self): """Test that the web server has started""" try: - r = requests.get("http://127.0.0.1:{}/".format(self.gui.app.port)) + r = requests.get(f"http://127.0.0.1:{self.gui.app.port}/") self.assertTrue(True) except requests.exceptions.ConnectionError: self.assertTrue(False) @@ -230,15 +230,11 @@ class GuiBaseTest(object): ) clipboard = self.gui.qtapp.clipboard() if public_mode: - self.assertEqual( - clipboard.text(), "http://127.0.0.1:{}".format(self.gui.app.port) - ) + self.assertEqual(clipboard.text(), f"http://127.0.0.1:{self.gui.app.port}") else: self.assertEqual( clipboard.text(), - "http://onionshare:{}@127.0.0.1:{}".format( - mode.server_status.web.password, self.gui.app.port - ), + f"http://onionshare:{mode.server_status.web.password}@127.0.0.1:{self.gui.app.port}", ) def server_status_indicator_says_started(self, mode): @@ -257,7 +253,7 @@ class GuiBaseTest(object): def web_page(self, mode, string, public_mode): """Test that the web page contains a string""" - url = "http://127.0.0.1:{}/".format(self.gui.app.port) + url = f"http://127.0.0.1:{self.gui.app.port}/" if public_mode: r = requests.get(url) else: @@ -293,7 +289,7 @@ class GuiBaseTest(object): QtTest.QTest.qWait(2000) try: - r = requests.get("http://127.0.0.1:{}/".format(self.gui.app.port)) + r = requests.get(f"http://127.0.0.1:{self.gui.app.port}/") self.assertTrue(False) except requests.exceptions.ConnectionError: self.assertTrue(True) diff --git a/tests/GuiReceiveTest.py b/tests/GuiReceiveTest.py index 34db1a94..380702fd 100644 --- a/tests/GuiReceiveTest.py +++ b/tests/GuiReceiveTest.py @@ -19,7 +19,7 @@ class GuiReceiveTest(GuiBaseTest): QtTest.QTest.qWait(2000) files = {"file[]": open(file_to_upload, "rb")} - url = "http://127.0.0.1:{}/upload".format(self.gui.app.port) + url = f"http://127.0.0.1:{self.gui.app.port}/upload" if public_mode: r = requests.post(url, files=files) if identical_files_at_once: @@ -68,7 +68,7 @@ class GuiReceiveTest(GuiBaseTest): def upload_file_should_fail(self, public_mode): """Test that we can't upload the file when permissions are wrong, and expected content is shown""" files = {"file[]": open("/tmp/test.txt", "rb")} - url = "http://127.0.0.1:{}/upload".format(self.gui.app.port) + url = f"http://127.0.0.1:{self.gui.app.port}/upload" if public_mode: r = requests.post(url, files=files) else: @@ -88,9 +88,9 @@ class GuiReceiveTest(GuiBaseTest): os.chmod("/tmp/OnionShare", mode) def try_without_auth_in_non_public_mode(self): - r = requests.post("http://127.0.0.1:{}/upload".format(self.gui.app.port)) + r = requests.post(f"http://127.0.0.1:{self.gui.app.port}/upload") self.assertEqual(r.status_code, 401) - r = requests.get("http://127.0.0.1:{}/close".format(self.gui.app.port)) + r = requests.get(f"http://127.0.0.1:{self.gui.app.port}/close") self.assertEqual(r.status_code, 401) # 'Grouped' tests follow from here diff --git a/tests/GuiShareTest.py b/tests/GuiShareTest.py index 630d0562..4a30dad5 100644 --- a/tests/GuiShareTest.py +++ b/tests/GuiShareTest.py @@ -105,7 +105,7 @@ class GuiShareTest(GuiBaseTest): def download_share(self, public_mode): """Test that we can download the share""" - url = "http://127.0.0.1:{}/download".format(self.gui.app.port) + url = f"http://127.0.0.1:{self.gui.app.port}/download" if public_mode: r = requests.get(url) else: @@ -126,8 +126,8 @@ class GuiShareTest(GuiBaseTest): def individual_file_is_viewable_or_not(self, public_mode, stay_open): """Test whether an individual file is viewable (when in stay_open mode) and that it isn't (when not in stay_open mode)""" - url = "http://127.0.0.1:{}".format(self.gui.app.port) - download_file_url = "http://127.0.0.1:{}/test.txt".format(self.gui.app.port) + url = f"http://127.0.0.1:{self.gui.app.port}" + download_file_url = f"http://127.0.0.1:{self.gui.app.port}/test.txt" if public_mode: r = requests.get(url) else: @@ -175,7 +175,7 @@ class GuiShareTest(GuiBaseTest): def hit_401(self, public_mode): """Test that the server stops after too many 401s, or doesn't when in public_mode""" - url = "http://127.0.0.1:{}/".format(self.gui.app.port) + url = f"http://127.0.0.1:{self.gui.app.port}/" for _ in range(20): password_guess = self.gui.common.build_password() diff --git a/tests/GuiWebsiteTest.py b/tests/GuiWebsiteTest.py index f6b67112..79b44e2e 100644 --- a/tests/GuiWebsiteTest.py +++ b/tests/GuiWebsiteTest.py @@ -67,7 +67,7 @@ class GuiWebsiteTest(GuiShareTest): def view_website(self, public_mode): """Test that we can download the share""" - url = "http://127.0.0.1:{}/".format(self.gui.app.port) + url = f"http://127.0.0.1:{self.gui.app.port}/" if public_mode: r = requests.get(url) else: @@ -83,7 +83,7 @@ class GuiWebsiteTest(GuiShareTest): def check_csp_header(self, public_mode, csp_header_disabled): """Test that the CSP header is present when enabled or vice versa""" - url = "http://127.0.0.1:{}/".format(self.gui.app.port) + url = f"http://127.0.0.1:{self.gui.app.port}/" if public_mode: r = requests.get(url) else: diff --git a/tests/TorGuiBaseTest.py b/tests/TorGuiBaseTest.py index 434a525d..611d3efa 100644 --- a/tests/TorGuiBaseTest.py +++ b/tests/TorGuiBaseTest.py @@ -79,28 +79,24 @@ class TorGuiBaseTest(GuiBaseTest): (socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port() session = requests.session() session.proxies = {} - session.proxies["http"] = "socks5h://{}:{}".format(socks_address, socks_port) + session.proxies["http"] = f"socks5h://{socks_address}:{socks_port}" if type(mode) == ReceiveMode: # Upload a file files = {"file[]": open("/tmp/test.txt", "rb")} if not public_mode: - path = "http://{}/{}/upload".format( - self.gui.app.onion_host, mode.web.password - ) + path = f"http://{self.gui.app.onion_host}/{mode.web.password}/upload" else: - path = "http://{}/upload".format(self.gui.app.onion_host) + path = f"http://{self.gui.app.onion_host}/upload" response = session.post(path, files=files) QtTest.QTest.qWait(4000) if type(mode) == ShareMode: # Download files if public_mode: - path = "http://{}/download".format(self.gui.app.onion_host) + path = f"http://{self.gui.app.onion_host}/download" else: - path = "http://{}/{}/download".format( - self.gui.app.onion_host, mode.web.password - ) + path = f"http://{self.gui.app.onion_host}/{mode.web.password}/download" response = session.get(path) QtTest.QTest.qWait(4000) @@ -124,11 +120,11 @@ class TorGuiBaseTest(GuiBaseTest): s.settimeout(60) s.connect((self.gui.app.onion_host, 80)) if not public_mode: - path = "/{}".format(mode.server_status.web.password) + path = f"/{mode.server_status.web.password}" else: path = "/" - http_request = "GET {} HTTP/1.0\r\n".format(path) - http_request += "Host: {}\r\n".format(self.gui.app.onion_host) + http_request = f"GET {path} HTTP/1.0\r\n" + http_request += f"Host: {self.gui.app.onion_host}\r\n" http_request += "\r\n" s.sendall(http_request.encode("utf-8")) with open("/tmp/webpage", "wb") as file_to_write: @@ -151,15 +147,11 @@ class TorGuiBaseTest(GuiBaseTest): ) clipboard = self.gui.qtapp.clipboard() if public_mode: - self.assertEqual( - clipboard.text(), "http://{}".format(self.gui.app.onion_host) - ) + self.assertEqual(clipboard.text(), f"http://{self.gui.app.onion_host}") else: self.assertEqual( clipboard.text(), - "http://{}/{}".format( - self.gui.app.onion_host, mode.server_status.web.password - ), + f"http://{self.gui.app.onion_host}/{mode.server_status.web.password}", ) # Stealth tests diff --git a/tests/TorGuiReceiveTest.py b/tests/TorGuiReceiveTest.py index 18a00643..a8944363 100644 --- a/tests/TorGuiReceiveTest.py +++ b/tests/TorGuiReceiveTest.py @@ -10,14 +10,12 @@ class TorGuiReceiveTest(TorGuiBaseTest): (socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port() session = requests.session() session.proxies = {} - session.proxies["http"] = "socks5h://{}:{}".format(socks_address, socks_port) + session.proxies["http"] = f"socks5h://{socks_address}:{socks_port}" files = {"file[]": open(file_to_upload, "rb")} if not public_mode: - path = "http://{}/{}/upload".format( - self.gui.app.onion_host, self.gui.receive_mode.web.password - ) + path = f"http://{self.gui.app.onion_host}/{self.gui.receive_mode.web.password}/upload" else: - path = "http://{}/upload".format(self.gui.app.onion_host) + path = f"http://{self.gui.app.onion_host}/upload" response = session.post(path, files=files) QtTest.QTest.qWait(4000) self.assertTrue(os.path.isfile(expected_file)) diff --git a/tests/TorGuiShareTest.py b/tests/TorGuiShareTest.py index e86a6927..1c9c5b40 100644 --- a/tests/TorGuiShareTest.py +++ b/tests/TorGuiShareTest.py @@ -12,15 +12,13 @@ class TorGuiShareTest(TorGuiBaseTest, GuiShareTest): (socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port() session = requests.session() session.proxies = {} - session.proxies["http"] = "socks5h://{}:{}".format(socks_address, socks_port) + session.proxies["http"] = f"socks5h://{socks_address}:{socks_port}" # Download files if public_mode: - path = "http://{}/download".format(self.gui.app.onion_host) + path = f"http://{self.gui.app.onion_host}/download" else: - path = "http://{}/{}/download".format( - self.gui.app.onion_host, self.gui.share_mode.web.password - ) + path = f"http://{self.gui.app.onion_host}/{self.gui.share_mode.web.password}/download" response = session.get(path, stream=True) QtTest.QTest.qWait(4000) diff --git a/tests/test_onionshare_web.py b/tests/test_onionshare_web.py index 43a8929b..c3a0807c 100644 --- a/tests/test_onionshare_web.py +++ b/tests/test_onionshare_web.py @@ -166,7 +166,7 @@ class TestWeb: assert res.status_code == 401 # But static resources should work without auth - res = c.get("{}/css/style.css".format(web.static_url_path)) + res = c.get(f"{web.static_url_path}/css/style.css") res.get_data() assert res.status_code == 200 @@ -186,11 +186,7 @@ class TestZipWriterDefault: @pytest.mark.parametrize( "test_input", ( - "onionshare_{}.zip".format( - "".join( - random.choice("abcdefghijklmnopqrstuvwxyz234567") for _ in range(6) - ) - ) + f"onionshare_{''.join(random.choice('abcdefghijklmnopqrstuvwxyz234567') for _ in range(6))}.zip" for _ in range(50) ), )