Replace .format with python 3.6 f-strings in tests

This commit is contained in:
Micah Lee 2019-10-20 10:46:14 -07:00
parent 25b2f389db
commit 098625621c
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
8 changed files with 35 additions and 55 deletions

View File

@ -129,7 +129,7 @@ class GuiBaseTest(object):
if type(mode) == ReceiveMode: if type(mode) == ReceiveMode:
# Upload a file # Upload a file
files = {"file[]": open("/tmp/test.txt", "rb")} 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: if public_mode:
r = requests.post(url, files=files) r = requests.post(url, files=files)
else: else:
@ -142,7 +142,7 @@ class GuiBaseTest(object):
if type(mode) == ShareMode: if type(mode) == ShareMode:
# Download files # 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: if public_mode:
r = requests.get(url) r = requests.get(url)
else: else:
@ -201,7 +201,7 @@ class GuiBaseTest(object):
def web_server_is_running(self): def web_server_is_running(self):
"""Test that the web server has started""" """Test that the web server has started"""
try: 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) self.assertTrue(True)
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
self.assertTrue(False) self.assertTrue(False)
@ -230,15 +230,11 @@ class GuiBaseTest(object):
) )
clipboard = self.gui.qtapp.clipboard() clipboard = self.gui.qtapp.clipboard()
if public_mode: if public_mode:
self.assertEqual( self.assertEqual(clipboard.text(), f"http://127.0.0.1:{self.gui.app.port}")
clipboard.text(), "http://127.0.0.1:{}".format(self.gui.app.port)
)
else: else:
self.assertEqual( self.assertEqual(
clipboard.text(), clipboard.text(),
"http://onionshare:{}@127.0.0.1:{}".format( f"http://onionshare:{mode.server_status.web.password}@127.0.0.1:{self.gui.app.port}",
mode.server_status.web.password, self.gui.app.port
),
) )
def server_status_indicator_says_started(self, mode): def server_status_indicator_says_started(self, mode):
@ -257,7 +253,7 @@ class GuiBaseTest(object):
def web_page(self, mode, string, public_mode): def web_page(self, mode, string, public_mode):
"""Test that the web page contains a string""" """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: if public_mode:
r = requests.get(url) r = requests.get(url)
else: else:
@ -293,7 +289,7 @@ class GuiBaseTest(object):
QtTest.QTest.qWait(2000) QtTest.QTest.qWait(2000)
try: 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) self.assertTrue(False)
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
self.assertTrue(True) self.assertTrue(True)

View File

@ -19,7 +19,7 @@ class GuiReceiveTest(GuiBaseTest):
QtTest.QTest.qWait(2000) QtTest.QTest.qWait(2000)
files = {"file[]": open(file_to_upload, "rb")} 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: if public_mode:
r = requests.post(url, files=files) r = requests.post(url, files=files)
if identical_files_at_once: if identical_files_at_once:
@ -68,7 +68,7 @@ class GuiReceiveTest(GuiBaseTest):
def upload_file_should_fail(self, public_mode): 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""" """Test that we can't upload the file when permissions are wrong, and expected content is shown"""
files = {"file[]": open("/tmp/test.txt", "rb")} 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: if public_mode:
r = requests.post(url, files=files) r = requests.post(url, files=files)
else: else:
@ -88,9 +88,9 @@ class GuiReceiveTest(GuiBaseTest):
os.chmod("/tmp/OnionShare", mode) os.chmod("/tmp/OnionShare", mode)
def try_without_auth_in_non_public_mode(self): 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) 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) self.assertEqual(r.status_code, 401)
# 'Grouped' tests follow from here # 'Grouped' tests follow from here

View File

@ -105,7 +105,7 @@ class GuiShareTest(GuiBaseTest):
def download_share(self, public_mode): def download_share(self, public_mode):
"""Test that we can download the share""" """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: if public_mode:
r = requests.get(url) r = requests.get(url)
else: else:
@ -126,8 +126,8 @@ class GuiShareTest(GuiBaseTest):
def individual_file_is_viewable_or_not(self, public_mode, stay_open): 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)""" """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) url = f"http://127.0.0.1:{self.gui.app.port}"
download_file_url = "http://127.0.0.1:{}/test.txt".format(self.gui.app.port) download_file_url = f"http://127.0.0.1:{self.gui.app.port}/test.txt"
if public_mode: if public_mode:
r = requests.get(url) r = requests.get(url)
else: else:
@ -175,7 +175,7 @@ class GuiShareTest(GuiBaseTest):
def hit_401(self, public_mode): def hit_401(self, public_mode):
"""Test that the server stops after too many 401s, or doesn't when in 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): for _ in range(20):
password_guess = self.gui.common.build_password() password_guess = self.gui.common.build_password()

View File

@ -67,7 +67,7 @@ class GuiWebsiteTest(GuiShareTest):
def view_website(self, public_mode): def view_website(self, public_mode):
"""Test that we can download the share""" """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: if public_mode:
r = requests.get(url) r = requests.get(url)
else: else:
@ -83,7 +83,7 @@ class GuiWebsiteTest(GuiShareTest):
def check_csp_header(self, public_mode, csp_header_disabled): def check_csp_header(self, public_mode, csp_header_disabled):
"""Test that the CSP header is present when enabled or vice versa""" """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: if public_mode:
r = requests.get(url) r = requests.get(url)
else: else:

View File

@ -79,28 +79,24 @@ class TorGuiBaseTest(GuiBaseTest):
(socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port() (socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port()
session = requests.session() session = requests.session()
session.proxies = {} session.proxies = {}
session.proxies["http"] = "socks5h://{}:{}".format(socks_address, socks_port) session.proxies["http"] = f"socks5h://{socks_address}:{socks_port}"
if type(mode) == ReceiveMode: if type(mode) == ReceiveMode:
# Upload a file # Upload a file
files = {"file[]": open("/tmp/test.txt", "rb")} files = {"file[]": open("/tmp/test.txt", "rb")}
if not public_mode: if not public_mode:
path = "http://{}/{}/upload".format( path = f"http://{self.gui.app.onion_host}/{mode.web.password}/upload"
self.gui.app.onion_host, mode.web.password
)
else: 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) response = session.post(path, files=files)
QtTest.QTest.qWait(4000) QtTest.QTest.qWait(4000)
if type(mode) == ShareMode: if type(mode) == ShareMode:
# Download files # Download files
if public_mode: if public_mode:
path = "http://{}/download".format(self.gui.app.onion_host) path = f"http://{self.gui.app.onion_host}/download"
else: else:
path = "http://{}/{}/download".format( path = f"http://{self.gui.app.onion_host}/{mode.web.password}/download"
self.gui.app.onion_host, mode.web.password
)
response = session.get(path) response = session.get(path)
QtTest.QTest.qWait(4000) QtTest.QTest.qWait(4000)
@ -124,11 +120,11 @@ class TorGuiBaseTest(GuiBaseTest):
s.settimeout(60) s.settimeout(60)
s.connect((self.gui.app.onion_host, 80)) s.connect((self.gui.app.onion_host, 80))
if not public_mode: if not public_mode:
path = "/{}".format(mode.server_status.web.password) path = f"/{mode.server_status.web.password}"
else: else:
path = "/" path = "/"
http_request = "GET {} HTTP/1.0\r\n".format(path) http_request = f"GET {path} HTTP/1.0\r\n"
http_request += "Host: {}\r\n".format(self.gui.app.onion_host) http_request += f"Host: {self.gui.app.onion_host}\r\n"
http_request += "\r\n" http_request += "\r\n"
s.sendall(http_request.encode("utf-8")) s.sendall(http_request.encode("utf-8"))
with open("/tmp/webpage", "wb") as file_to_write: with open("/tmp/webpage", "wb") as file_to_write:
@ -151,15 +147,11 @@ class TorGuiBaseTest(GuiBaseTest):
) )
clipboard = self.gui.qtapp.clipboard() clipboard = self.gui.qtapp.clipboard()
if public_mode: if public_mode:
self.assertEqual( self.assertEqual(clipboard.text(), f"http://{self.gui.app.onion_host}")
clipboard.text(), "http://{}".format(self.gui.app.onion_host)
)
else: else:
self.assertEqual( self.assertEqual(
clipboard.text(), clipboard.text(),
"http://{}/{}".format( f"http://{self.gui.app.onion_host}/{mode.server_status.web.password}",
self.gui.app.onion_host, mode.server_status.web.password
),
) )
# Stealth tests # Stealth tests

View File

@ -10,14 +10,12 @@ class TorGuiReceiveTest(TorGuiBaseTest):
(socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port() (socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port()
session = requests.session() session = requests.session()
session.proxies = {} 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")} files = {"file[]": open(file_to_upload, "rb")}
if not public_mode: if not public_mode:
path = "http://{}/{}/upload".format( path = f"http://{self.gui.app.onion_host}/{self.gui.receive_mode.web.password}/upload"
self.gui.app.onion_host, self.gui.receive_mode.web.password
)
else: 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) response = session.post(path, files=files)
QtTest.QTest.qWait(4000) QtTest.QTest.qWait(4000)
self.assertTrue(os.path.isfile(expected_file)) self.assertTrue(os.path.isfile(expected_file))

View File

@ -12,15 +12,13 @@ class TorGuiShareTest(TorGuiBaseTest, GuiShareTest):
(socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port() (socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port()
session = requests.session() session = requests.session()
session.proxies = {} session.proxies = {}
session.proxies["http"] = "socks5h://{}:{}".format(socks_address, socks_port) session.proxies["http"] = f"socks5h://{socks_address}:{socks_port}"
# Download files # Download files
if public_mode: if public_mode:
path = "http://{}/download".format(self.gui.app.onion_host) path = f"http://{self.gui.app.onion_host}/download"
else: else:
path = "http://{}/{}/download".format( path = f"http://{self.gui.app.onion_host}/{self.gui.share_mode.web.password}/download"
self.gui.app.onion_host, self.gui.share_mode.web.password
)
response = session.get(path, stream=True) response = session.get(path, stream=True)
QtTest.QTest.qWait(4000) QtTest.QTest.qWait(4000)

View File

@ -166,7 +166,7 @@ class TestWeb:
assert res.status_code == 401 assert res.status_code == 401
# But static resources should work without auth # 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() res.get_data()
assert res.status_code == 200 assert res.status_code == 200
@ -186,11 +186,7 @@ class TestZipWriterDefault:
@pytest.mark.parametrize( @pytest.mark.parametrize(
"test_input", "test_input",
( (
"onionshare_{}.zip".format( f"onionshare_{''.join(random.choice('abcdefghijklmnopqrstuvwxyz234567') for _ in range(6))}.zip"
"".join(
random.choice("abcdefghijklmnopqrstuvwxyz234567") for _ in range(6)
)
)
for _ in range(50) for _ in range(50)
), ),
) )