Format all code using black

This commit is contained in:
Micah Lee 2019-10-12 21:01:25 -07:00
parent 8010b279e7
commit 88e151d414
87 changed files with 4293 additions and 2374 deletions

View file

@ -20,17 +20,17 @@ from onionshare_gui.mode.website_mode import WebsiteMode
class GuiBaseTest(object):
@staticmethod
def set_up(test_settings):
'''Create GUI with given settings'''
"""Create GUI with given settings"""
# Create our test file
testfile = open('/tmp/test.txt', 'w')
testfile.write('onionshare')
testfile = open("/tmp/test.txt", "w")
testfile.write("onionshare")
testfile.close()
# Create a test dir and files
if not os.path.exists('/tmp/testdir'):
testdir = os.mkdir('/tmp/testdir')
testfile = open('/tmp/testdir/test', 'w')
testfile.write('onionshare')
if not os.path.exists("/tmp/testdir"):
testdir = os.mkdir("/tmp/testdir")
testfile = open("/tmp/testdir/test", "w")
testfile.write("onionshare")
testfile.close()
common = Common()
@ -39,7 +39,7 @@ class GuiBaseTest(object):
strings.load_strings(common)
# Get all of the settings in test_settings
test_settings['data_dir'] = '/tmp/OnionShare'
test_settings["data_dir"] = "/tmp/OnionShare"
for key, val in common.settings.default_settings.items():
if key not in test_settings:
test_settings[key] = val
@ -51,53 +51,55 @@ class GuiBaseTest(object):
app = OnionShare(common, testonion, True, 0)
web = Web(common, False, True)
open('/tmp/settings.json', 'w').write(json.dumps(test_settings))
open("/tmp/settings.json", "w").write(json.dumps(test_settings))
gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/test.txt', '/tmp/testdir'], '/tmp/settings.json', True)
gui = OnionShareGui(
common,
testonion,
qtapp,
app,
["/tmp/test.txt", "/tmp/testdir"],
"/tmp/settings.json",
True,
)
return gui
@staticmethod
def tear_down():
'''Clean up after tests'''
"""Clean up after tests"""
try:
os.remove('/tmp/test.txt')
os.remove('/tmp/settings.json')
os.remove('/tmp/large_file')
os.remove('/tmp/download.zip')
os.remove('/tmp/webpage')
shutil.rmtree('/tmp/testdir')
shutil.rmtree('/tmp/OnionShare')
os.remove("/tmp/test.txt")
os.remove("/tmp/settings.json")
os.remove("/tmp/large_file")
os.remove("/tmp/download.zip")
os.remove("/tmp/webpage")
shutil.rmtree("/tmp/testdir")
shutil.rmtree("/tmp/OnionShare")
except:
pass
def gui_loaded(self):
'''Test that the GUI actually is shown'''
"""Test that the GUI actually is shown"""
self.assertTrue(self.gui.show)
def windowTitle_seen(self):
'''Test that the window title is OnionShare'''
self.assertEqual(self.gui.windowTitle(), 'OnionShare')
"""Test that the window title is OnionShare"""
self.assertEqual(self.gui.windowTitle(), "OnionShare")
def settings_button_is_visible(self):
'''Test that the settings button is visible'''
"""Test that the settings button is visible"""
self.assertTrue(self.gui.settings_button.isVisible())
def settings_button_is_hidden(self):
'''Test that the settings button is hidden when the server starts'''
"""Test that the settings button is hidden when the server starts"""
self.assertFalse(self.gui.settings_button.isVisible())
def server_status_bar_is_visible(self):
'''Test that the status bar is visible'''
"""Test that the status bar is visible"""
self.assertTrue(self.gui.status_bar.isVisible())
def click_mode(self, mode):
'''Test that we can switch Mode by clicking the button'''
"""Test that we can switch Mode by clicking the button"""
if type(mode) == ReceiveMode:
QtTest.QTest.mouseClick(self.gui.receive_mode_button, QtCore.Qt.LeftButton)
self.assertTrue(self.gui.mode, self.gui.MODE_RECEIVE)
@ -108,16 +110,14 @@ class GuiBaseTest(object):
QtTest.QTest.mouseClick(self.gui.website_mode_button, QtCore.Qt.LeftButton)
self.assertTrue(self.gui.mode, self.gui.MODE_WEBSITE)
def click_toggle_history(self, mode):
'''Test that we can toggle Download or Upload history by clicking the toggle button'''
"""Test that we can toggle Download or Upload history by clicking the toggle button"""
currently_visible = mode.history.isVisible()
QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton)
self.assertEqual(mode.history.isVisible(), not currently_visible)
def history_indicator(self, mode, public_mode, indicator_count="1"):
'''Test that we can make sure the history is toggled off, do an action, and the indiciator works'''
"""Test that we can make sure the history is toggled off, do an action, and the indiciator works"""
# Make sure history is toggled off
if mode.history.isVisible():
QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton)
@ -128,12 +128,16 @@ 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)
files = {"file[]": open("/tmp/test.txt", "rb")}
url = "http://127.0.0.1:{}/upload".format(self.gui.app.port)
if public_mode:
r = requests.post(url, files=files)
else:
r = requests.post(url, files=files, auth=requests.auth.HTTPBasicAuth('onionshare', mode.web.password))
r = requests.post(
url,
files=files,
auth=requests.auth.HTTPBasicAuth("onionshare", mode.web.password),
)
QtTest.QTest.qWait(2000)
if type(mode) == ShareMode:
@ -142,7 +146,10 @@ class GuiBaseTest(object):
if public_mode:
r = requests.get(url)
else:
r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', mode.web.password))
r = requests.get(
url,
auth=requests.auth.HTTPBasicAuth("onionshare", mode.web.password),
)
QtTest.QTest.qWait(2000)
# Indicator should be visible, have a value of "1"
@ -153,19 +160,16 @@ class GuiBaseTest(object):
QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton)
self.assertFalse(mode.toggle_history.indicator_label.isVisible())
def history_is_not_visible(self, mode):
'''Test that the History section is not visible'''
"""Test that the History section is not visible"""
self.assertFalse(mode.history.isVisible())
def history_is_visible(self, mode):
'''Test that the History section is visible'''
"""Test that the History section is visible"""
self.assertTrue(mode.history.isVisible())
def server_working_on_start_button_pressed(self, mode):
'''Test we can start the service'''
"""Test we can start the service"""
# Should be in SERVER_WORKING state
QtTest.QTest.mouseClick(mode.server_status.server_button, QtCore.Qt.LeftButton)
self.assertEqual(mode.server_status.status, 1)
@ -175,115 +179,143 @@ class GuiBaseTest(object):
self.assertFalse(mode.toggle_history.indicator_label.isVisible())
def server_status_indicator_says_starting(self, mode):
'''Test that the Server Status indicator shows we are Starting'''
self.assertEqual(mode.server_status_label.text(), strings._('gui_status_indicator_share_working'))
"""Test that the Server Status indicator shows we are Starting"""
self.assertEqual(
mode.server_status_label.text(),
strings._("gui_status_indicator_share_working"),
)
def server_status_indicator_says_scheduled(self, mode):
'''Test that the Server Status indicator shows we are Scheduled'''
self.assertEqual(mode.server_status_label.text(), strings._('gui_status_indicator_share_scheduled'))
"""Test that the Server Status indicator shows we are Scheduled"""
self.assertEqual(
mode.server_status_label.text(),
strings._("gui_status_indicator_share_scheduled"),
)
def server_is_started(self, mode, startup_time=2000):
'''Test that the server has started'''
"""Test that the server has started"""
QtTest.QTest.qWait(startup_time)
# Should now be in SERVER_STARTED state
self.assertEqual(mode.server_status.status, 2)
def web_server_is_running(self):
'''Test that the web server has started'''
"""Test that the web server has started"""
try:
r = requests.get('http://127.0.0.1:{}/'.format(self.gui.app.port))
r = requests.get("http://127.0.0.1:{}/".format(self.gui.app.port))
self.assertTrue(True)
except requests.exceptions.ConnectionError:
self.assertTrue(False)
def have_a_password(self, mode, public_mode):
'''Test that we have a valid password'''
"""Test that we have a valid password"""
if not public_mode:
self.assertRegex(mode.server_status.web.password, r'(\w+)-(\w+)')
self.assertRegex(mode.server_status.web.password, r"(\w+)-(\w+)")
else:
self.assertIsNone(mode.server_status.web.password, r'(\w+)-(\w+)')
self.assertIsNone(mode.server_status.web.password, r"(\w+)-(\w+)")
def add_button_visible(self, mode):
'''Test that the add button should be visible'''
"""Test that the add button should be visible"""
self.assertTrue(mode.server_status.file_selection.add_button.isVisible())
def url_description_shown(self, mode):
'''Test that the URL label is showing'''
"""Test that the URL label is showing"""
self.assertTrue(mode.server_status.url_description.isVisible())
def have_copy_url_button(self, mode, public_mode):
'''Test that the Copy URL button is shown and that the clipboard is correct'''
"""Test that the Copy URL button is shown and that the clipboard is correct"""
self.assertTrue(mode.server_status.copy_url_button.isVisible())
QtTest.QTest.mouseClick(mode.server_status.copy_url_button, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(
mode.server_status.copy_url_button, QtCore.Qt.LeftButton
)
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(), "http://127.0.0.1:{}".format(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))
self.assertEqual(
clipboard.text(),
"http://onionshare:{}@127.0.0.1:{}".format(
mode.server_status.web.password, self.gui.app.port
),
)
def server_status_indicator_says_started(self, mode):
'''Test that the Server Status indicator shows we are started'''
"""Test that the Server Status indicator shows we are started"""
if type(mode) == ReceiveMode:
self.assertEqual(mode.server_status_label.text(), strings._('gui_status_indicator_receive_started'))
self.assertEqual(
mode.server_status_label.text(),
strings._("gui_status_indicator_receive_started"),
)
if type(mode) == ShareMode:
self.assertEqual(mode.server_status_label.text(), strings._('gui_status_indicator_share_started'))
self.assertEqual(
mode.server_status_label.text(),
strings._("gui_status_indicator_share_started"),
)
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)
if public_mode:
r = requests.get(url)
else:
r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', mode.web.password))
r = requests.get(
url, auth=requests.auth.HTTPBasicAuth("onionshare", mode.web.password)
)
self.assertTrue(string in r.text)
def history_widgets_present(self, mode):
'''Test that the relevant widgets are present in the history view after activity has taken place'''
"""Test that the relevant widgets are present in the history view after activity has taken place"""
self.assertFalse(mode.history.empty.isVisible())
self.assertTrue(mode.history.not_empty.isVisible())
def counter_incremented(self, mode, count):
'''Test that the counter has incremented'''
"""Test that the counter has incremented"""
self.assertEqual(mode.history.completed_count, count)
def server_is_stopped(self, mode, stay_open):
'''Test that the server stops when we click Stop'''
if type(mode) == ReceiveMode or (type(mode) == ShareMode and stay_open) or (type(mode) == WebsiteMode):
QtTest.QTest.mouseClick(mode.server_status.server_button, QtCore.Qt.LeftButton)
"""Test that the server stops when we click Stop"""
if (
type(mode) == ReceiveMode
or (type(mode) == ShareMode and stay_open)
or (type(mode) == WebsiteMode)
):
QtTest.QTest.mouseClick(
mode.server_status.server_button, QtCore.Qt.LeftButton
)
self.assertEqual(mode.server_status.status, 0)
def web_server_is_stopped(self):
'''Test that the web server also stopped'''
"""Test that the web server also stopped"""
QtTest.QTest.qWait(2000)
try:
r = requests.get('http://127.0.0.1:{}/'.format(self.gui.app.port))
r = requests.get("http://127.0.0.1:{}/".format(self.gui.app.port))
self.assertTrue(False)
except requests.exceptions.ConnectionError:
self.assertTrue(True)
def server_status_indicator_says_closed(self, mode, stay_open):
'''Test that the Server Status indicator shows we closed'''
"""Test that the Server Status indicator shows we closed"""
if type(mode) == ReceiveMode:
self.assertEqual(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_receive_stopped'))
self.assertEqual(
self.gui.receive_mode.server_status_label.text(),
strings._("gui_status_indicator_receive_stopped"),
)
if type(mode) == ShareMode:
if stay_open:
self.assertEqual(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_stopped'))
self.assertEqual(
self.gui.share_mode.server_status_label.text(),
strings._("gui_status_indicator_share_stopped"),
)
else:
self.assertEqual(self.gui.share_mode.server_status_label.text(), strings._('closing_automatically'))
self.assertEqual(
self.gui.share_mode.server_status_label.text(),
strings._("closing_automatically"),
)
def clear_all_history_items(self, mode, count):
if count == 0:
@ -292,41 +324,40 @@ class GuiBaseTest(object):
# Auto-stop timer tests
def set_timeout(self, mode, timeout):
'''Test that the timeout can be set'''
"""Test that the timeout can be set"""
timer = QtCore.QDateTime.currentDateTime().addSecs(timeout)
mode.server_status.autostop_timer_widget.setDateTime(timer)
self.assertTrue(mode.server_status.autostop_timer_widget.dateTime(), timer)
def autostop_timer_widget_hidden(self, mode):
'''Test that the auto-stop timer widget is hidden when share has started'''
"""Test that the auto-stop timer widget is hidden when share has started"""
self.assertFalse(mode.server_status.autostop_timer_container.isVisible())
def server_timed_out(self, mode, wait):
'''Test that the server has timed out after the timer ran out'''
"""Test that the server has timed out after the timer ran out"""
QtTest.QTest.qWait(wait)
# We should have timed out now
self.assertEqual(mode.server_status.status, 0)
# Auto-start timer tests
def set_autostart_timer(self, mode, timer):
'''Test that the timer can be set'''
"""Test that the timer can be set"""
schedule = QtCore.QDateTime.currentDateTime().addSecs(timer)
mode.server_status.autostart_timer_widget.setDateTime(schedule)
self.assertTrue(mode.server_status.autostart_timer_widget.dateTime(), schedule)
def autostart_timer_widget_hidden(self, mode):
'''Test that the auto-start timer widget is hidden when share has started'''
"""Test that the auto-start timer widget is hidden when share has started"""
self.assertFalse(mode.server_status.autostart_timer_container.isVisible())
def scheduled_service_started(self, mode, wait):
'''Test that the server has timed out after the timer ran out'''
"""Test that the server has timed out after the timer ran out"""
QtTest.QTest.qWait(wait)
# We should have started now
self.assertEqual(mode.server_status.status, 2)
def cancel_the_share(self, mode):
'''Test that we can cancel a share before it's started up '''
"""Test that we can cancel a share before it's started up """
self.server_working_on_start_button_pressed(mode)
self.server_status_indicator_says_scheduled(mode)
self.add_delete_buttons_hidden()
@ -334,7 +365,9 @@ class GuiBaseTest(object):
self.set_autostart_timer(mode, 10)
QtTest.QTest.mousePress(mode.server_status.server_button, QtCore.Qt.LeftButton)
QtTest.QTest.qWait(2000)
QtTest.QTest.mouseRelease(mode.server_status.server_button, QtCore.Qt.LeftButton)
QtTest.QTest.mouseRelease(
mode.server_status.server_button, QtCore.Qt.LeftButton
)
self.assertEqual(mode.server_status.status, 0)
self.server_is_stopped(mode, False)
self.web_server_is_stopped()

View file

@ -4,25 +4,44 @@ from datetime import datetime, timedelta
from PyQt5 import QtCore, QtTest
from .GuiBaseTest import GuiBaseTest
class GuiReceiveTest(GuiBaseTest):
def upload_file(self, public_mode, file_to_upload, expected_basename, identical_files_at_once=False):
'''Test that we can upload the file'''
def upload_file(
self,
public_mode,
file_to_upload,
expected_basename,
identical_files_at_once=False,
):
"""Test that we can upload the file"""
# Wait 2 seconds to make sure the filename, based on timestamp, isn't accidentally reused
QtTest.QTest.qWait(2000)
files = {'file[]': open(file_to_upload, 'rb')}
url = 'http://127.0.0.1:{}/upload'.format(self.gui.app.port)
files = {"file[]": open(file_to_upload, "rb")}
url = "http://127.0.0.1:{}/upload".format(self.gui.app.port)
if public_mode:
r = requests.post(url, files=files)
if identical_files_at_once:
# Send a duplicate upload to test for collisions
r = requests.post(url, files=files)
else:
r = requests.post(url, files=files, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.receive_mode.web.password))
r = requests.post(
url,
files=files,
auth=requests.auth.HTTPBasicAuth(
"onionshare", self.gui.receive_mode.web.password
),
)
if identical_files_at_once:
# Send a duplicate upload to test for collisions
r = requests.post(url, files=files, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.receive_mode.web.password))
r = requests.post(
url,
files=files,
auth=requests.auth.HTTPBasicAuth(
"onionshare", self.gui.receive_mode.web.password
),
)
QtTest.QTest.qWait(2000)
@ -35,7 +54,9 @@ class GuiReceiveTest(GuiBaseTest):
time_dir = now.strftime("%H.%M.%S-1")
else:
time_dir = now.strftime("%H.%M.%S")
receive_mode_dir = os.path.join(self.gui.common.settings.get('data_dir'), date_dir, time_dir)
receive_mode_dir = os.path.join(
self.gui.common.settings.get("data_dir"), date_dir, time_dir
)
expected_filename = os.path.join(receive_mode_dir, expected_basename)
if os.path.exists(expected_filename):
exists = True
@ -45,31 +66,37 @@ class GuiReceiveTest(GuiBaseTest):
self.assertTrue(exists)
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)
"""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)
if public_mode:
r = requests.post(url, files=files)
else:
r = requests.post(url, files=files, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.receive_mode.web.password))
r = requests.post(
url,
files=files,
auth=requests.auth.HTTPBasicAuth(
"onionshare", self.gui.receive_mode.web.password
),
)
QtCore.QTimer.singleShot(1000, self.accept_dialog)
self.assertTrue('Error uploading, please inform the OnionShare user' in r.text)
self.assertTrue("Error uploading, please inform the OnionShare user" in r.text)
def upload_dir_permissions(self, mode=0o755):
'''Manipulate the permissions on the upload dir in between tests'''
os.chmod('/tmp/OnionShare', mode)
"""Manipulate the permissions on the upload dir in between tests"""
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("http://127.0.0.1:{}/upload".format(self.gui.app.port))
self.assertEqual(r.status_code, 401)
r = requests.get('http://127.0.0.1:{}/close'.format(self.gui.app.port))
r = requests.get("http://127.0.0.1:{}/close".format(self.gui.app.port))
self.assertEqual(r.status_code, 401)
# 'Grouped' tests follow from here
def run_all_receive_mode_setup_tests(self, public_mode):
'''Set up a share in Receive mode and start it'''
"""Set up a share in Receive mode and start it"""
self.click_mode(self.gui.receive_mode)
self.history_is_not_visible(self.gui.receive_mode)
self.click_toggle_history(self.gui.receive_mode)
@ -83,24 +110,28 @@ class GuiReceiveTest(GuiBaseTest):
self.url_description_shown(self.gui.receive_mode)
self.have_copy_url_button(self.gui.receive_mode, public_mode)
self.server_status_indicator_says_started(self.gui.receive_mode)
self.web_page(self.gui.receive_mode, 'Select the files you want to send, then click', public_mode)
self.web_page(
self.gui.receive_mode,
"Select the files you want to send, then click",
public_mode,
)
def run_all_receive_mode_tests(self, public_mode):
'''Upload files in receive mode and stop the share'''
"""Upload files in receive mode and stop the share"""
self.run_all_receive_mode_setup_tests(public_mode)
if not public_mode:
self.try_without_auth_in_non_public_mode()
self.upload_file(public_mode, '/tmp/test.txt', 'test.txt')
self.upload_file(public_mode, "/tmp/test.txt", "test.txt")
self.history_widgets_present(self.gui.receive_mode)
self.counter_incremented(self.gui.receive_mode, 1)
self.upload_file(public_mode, '/tmp/test.txt', 'test.txt')
self.upload_file(public_mode, "/tmp/test.txt", "test.txt")
self.counter_incremented(self.gui.receive_mode, 2)
self.upload_file(public_mode, '/tmp/testdir/test', 'test')
self.upload_file(public_mode, "/tmp/testdir/test", "test")
self.counter_incremented(self.gui.receive_mode, 3)
self.upload_file(public_mode, '/tmp/testdir/test', 'test')
self.upload_file(public_mode, "/tmp/testdir/test", "test")
self.counter_incremented(self.gui.receive_mode, 4)
# Test uploading the same file twice at the same time, and make sure no collisions
self.upload_file(public_mode, '/tmp/test.txt', 'test.txt', True)
self.upload_file(public_mode, "/tmp/test.txt", "test.txt", True)
self.counter_incremented(self.gui.receive_mode, 6)
self.history_indicator(self.gui.receive_mode, public_mode, "2")
self.server_is_stopped(self.gui.receive_mode, False)
@ -111,7 +142,7 @@ class GuiReceiveTest(GuiBaseTest):
self.history_indicator(self.gui.receive_mode, public_mode, "2")
def run_all_receive_mode_unwritable_dir_tests(self, public_mode):
'''Attempt to upload (unwritable) files in receive mode and stop the share'''
"""Attempt to upload (unwritable) files in receive mode and stop the share"""
self.run_all_receive_mode_setup_tests(public_mode)
self.upload_dir_permissions(0o400)
self.upload_file_should_fail(public_mode)
@ -131,8 +162,8 @@ class GuiReceiveTest(GuiBaseTest):
def run_all_clear_all_button_tests(self, public_mode):
"""Test the Clear All history button"""
self.run_all_receive_mode_setup_tests(public_mode)
self.upload_file(public_mode, '/tmp/test.txt', 'test.txt')
self.upload_file(public_mode, "/tmp/test.txt", "test.txt")
self.history_widgets_present(self.gui.receive_mode)
self.clear_all_history_items(self.gui.receive_mode, 0)
self.upload_file(public_mode, '/tmp/test.txt', 'test.txt')
self.upload_file(public_mode, "/tmp/test.txt", "test.txt")
self.clear_all_history_items(self.gui.receive_mode, 2)

View file

@ -6,89 +6,137 @@ import tempfile
from PyQt5 import QtCore, QtTest
from .GuiBaseTest import GuiBaseTest
class GuiShareTest(GuiBaseTest):
# Persistence tests
def have_same_password(self, password):
'''Test that we have the same password'''
"""Test that we have the same password"""
self.assertEqual(self.gui.share_mode.server_status.web.password, password)
# Share-specific tests
def file_selection_widget_has_files(self, num=2):
'''Test that the number of items in the list is as expected'''
self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), num)
"""Test that the number of items in the list is as expected"""
self.assertEqual(
self.gui.share_mode.server_status.file_selection.get_num_files(), num
)
def deleting_all_files_hides_delete_button(self):
'''Test that clicking on the file item shows the delete button. Test that deleting the only item in the list hides the delete button'''
rect = self.gui.share_mode.server_status.file_selection.file_list.visualItemRect(self.gui.share_mode.server_status.file_selection.file_list.item(0))
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.file_list.viewport(), QtCore.Qt.LeftButton, pos=rect.center())
"""Test that clicking on the file item shows the delete button. Test that deleting the only item in the list hides the delete button"""
rect = self.gui.share_mode.server_status.file_selection.file_list.visualItemRect(
self.gui.share_mode.server_status.file_selection.file_list.item(0)
)
QtTest.QTest.mouseClick(
self.gui.share_mode.server_status.file_selection.file_list.viewport(),
QtCore.Qt.LeftButton,
pos=rect.center(),
)
# Delete button should be visible
self.assertTrue(self.gui.share_mode.server_status.file_selection.delete_button.isVisible())
self.assertTrue(
self.gui.share_mode.server_status.file_selection.delete_button.isVisible()
)
# Click delete, delete button should still be visible since we have one more file
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.delete_button, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(
self.gui.share_mode.server_status.file_selection.delete_button,
QtCore.Qt.LeftButton,
)
rect = self.gui.share_mode.server_status.file_selection.file_list.visualItemRect(self.gui.share_mode.server_status.file_selection.file_list.item(0))
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.file_list.viewport(), QtCore.Qt.LeftButton, pos=rect.center())
self.assertTrue(self.gui.share_mode.server_status.file_selection.delete_button.isVisible())
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.delete_button, QtCore.Qt.LeftButton)
rect = self.gui.share_mode.server_status.file_selection.file_list.visualItemRect(
self.gui.share_mode.server_status.file_selection.file_list.item(0)
)
QtTest.QTest.mouseClick(
self.gui.share_mode.server_status.file_selection.file_list.viewport(),
QtCore.Qt.LeftButton,
pos=rect.center(),
)
self.assertTrue(
self.gui.share_mode.server_status.file_selection.delete_button.isVisible()
)
QtTest.QTest.mouseClick(
self.gui.share_mode.server_status.file_selection.delete_button,
QtCore.Qt.LeftButton,
)
# No more files, the delete button should be hidden
self.assertFalse(self.gui.share_mode.server_status.file_selection.delete_button.isVisible())
self.assertFalse(
self.gui.share_mode.server_status.file_selection.delete_button.isVisible()
)
def add_a_file_and_delete_using_its_delete_widget(self):
'''Test that we can also delete a file by clicking on its [X] widget'''
self.gui.share_mode.server_status.file_selection.file_list.add_file('/etc/hosts')
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.file_list.item(0).item_button, QtCore.Qt.LeftButton)
"""Test that we can also delete a file by clicking on its [X] widget"""
self.gui.share_mode.server_status.file_selection.file_list.add_file(
"/etc/hosts"
)
QtTest.QTest.mouseClick(
self.gui.share_mode.server_status.file_selection.file_list.item(
0
).item_button,
QtCore.Qt.LeftButton,
)
self.file_selection_widget_has_files(0)
def file_selection_widget_read_files(self):
'''Re-add some files to the list so we can share'''
self.gui.share_mode.server_status.file_selection.file_list.add_file('/etc/hosts')
self.gui.share_mode.server_status.file_selection.file_list.add_file('/tmp/test.txt')
"""Re-add some files to the list so we can share"""
self.gui.share_mode.server_status.file_selection.file_list.add_file(
"/etc/hosts"
)
self.gui.share_mode.server_status.file_selection.file_list.add_file(
"/tmp/test.txt"
)
self.file_selection_widget_has_files(2)
def add_large_file(self):
'''Add a large file to the share'''
size = 1024*1024*155
with open('/tmp/large_file', 'wb') as fout:
"""Add a large file to the share"""
size = 1024 * 1024 * 155
with open("/tmp/large_file", "wb") as fout:
fout.write(os.urandom(size))
self.gui.share_mode.server_status.file_selection.file_list.add_file('/tmp/large_file')
self.gui.share_mode.server_status.file_selection.file_list.add_file(
"/tmp/large_file"
)
def add_delete_buttons_hidden(self):
'''Test that the add and delete buttons are hidden when the server starts'''
self.assertFalse(self.gui.share_mode.server_status.file_selection.add_button.isVisible())
self.assertFalse(self.gui.share_mode.server_status.file_selection.delete_button.isVisible())
"""Test that the add and delete buttons are hidden when the server starts"""
self.assertFalse(
self.gui.share_mode.server_status.file_selection.add_button.isVisible()
)
self.assertFalse(
self.gui.share_mode.server_status.file_selection.delete_button.isVisible()
)
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)
if public_mode:
r = requests.get(url)
else:
r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.share_mode.server_status.web.password))
r = requests.get(
url,
auth=requests.auth.HTTPBasicAuth(
"onionshare", self.gui.share_mode.server_status.web.password
),
)
tmp_file = tempfile.NamedTemporaryFile()
with open(tmp_file.name, 'wb') as f:
with open(tmp_file.name, "wb") as f:
f.write(r.content)
zip = zipfile.ZipFile(tmp_file.name)
QtTest.QTest.qWait(2000)
self.assertEqual('onionshare', zip.read('test.txt').decode('utf-8'))
self.assertEqual("onionshare", zip.read("test.txt").decode("utf-8"))
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)
download_file_url = "http://127.0.0.1:{}/test.txt".format(self.gui.app.port)
if public_mode:
r = requests.get(url)
else:
r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.share_mode.server_status.web.password))
r = requests.get(
url,
auth=requests.auth.HTTPBasicAuth(
"onionshare", self.gui.share_mode.server_status.web.password
),
)
if stay_open:
self.assertTrue('a href="test.txt"' in r.text)
@ -96,32 +144,44 @@ class GuiShareTest(GuiBaseTest):
if public_mode:
r = requests.get(download_file_url)
else:
r = requests.get(download_file_url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.share_mode.server_status.web.password))
r = requests.get(
download_file_url,
auth=requests.auth.HTTPBasicAuth(
"onionshare", self.gui.share_mode.server_status.web.password
),
)
tmp_file = tempfile.NamedTemporaryFile()
with open(tmp_file.name, 'wb') as f:
with open(tmp_file.name, "wb") as f:
f.write(r.content)
with open(tmp_file.name, 'r') as f:
self.assertEqual('onionshare', f.read())
with open(tmp_file.name, "r") as f:
self.assertEqual("onionshare", f.read())
else:
self.assertFalse('a href="/test.txt"' in r.text)
if public_mode:
r = requests.get(download_file_url)
else:
r = requests.get(download_file_url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.share_mode.server_status.web.password))
r = requests.get(
download_file_url,
auth=requests.auth.HTTPBasicAuth(
"onionshare", self.gui.share_mode.server_status.web.password
),
)
self.assertEqual(r.status_code, 404)
self.download_share(public_mode)
QtTest.QTest.qWait(2000)
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)
for _ in range(20):
password_guess = self.gui.common.build_password()
r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', password_guess))
r = requests.get(
url, auth=requests.auth.HTTPBasicAuth("onionshare", password_guess)
)
# A nasty hack to avoid the Alert dialog that blocks the rest of the test
if not public_mode:
@ -134,7 +194,6 @@ class GuiShareTest(GuiBaseTest):
else:
self.web_server_is_stopped()
# 'Grouped' tests follow from here
def run_all_share_mode_setup_tests(self):
@ -148,7 +207,6 @@ class GuiShareTest(GuiBaseTest):
self.add_a_file_and_delete_using_its_delete_widget()
self.file_selection_widget_read_files()
def run_all_share_mode_started_tests(self, public_mode, startup_time=2000):
"""Tests in share mode after starting a share"""
self.server_working_on_start_button_pressed(self.gui.share_mode)
@ -162,10 +220,9 @@ class GuiShareTest(GuiBaseTest):
self.have_copy_url_button(self.gui.share_mode, public_mode)
self.server_status_indicator_says_started(self.gui.share_mode)
def run_all_share_mode_download_tests(self, public_mode, stay_open):
"""Tests in share mode after downloading a share"""
self.web_page(self.gui.share_mode, 'Total size', public_mode)
self.web_page(self.gui.share_mode, "Total size", public_mode)
self.download_share(public_mode)
self.history_widgets_present(self.gui.share_mode)
self.server_is_stopped(self.gui.share_mode, stay_open)
@ -179,7 +236,7 @@ class GuiShareTest(GuiBaseTest):
def run_all_share_mode_individual_file_download_tests(self, public_mode, stay_open):
"""Tests in share mode after downloading a share"""
self.web_page(self.gui.share_mode, 'Total size', public_mode)
self.web_page(self.gui.share_mode, "Total size", public_mode)
self.individual_file_is_viewable_or_not(public_mode, stay_open)
self.history_widgets_present(self.gui.share_mode)
self.server_is_stopped(self.gui.share_mode, stay_open)
@ -222,7 +279,6 @@ class GuiShareTest(GuiBaseTest):
self.web_server_is_stopped()
self.server_status_indicator_says_closed(self.gui.share_mode, stay_open)
def run_all_share_mode_persistent_tests(self, public_mode, stay_open):
"""Same as end-to-end share tests but also test the password is the same on multiple shared"""
self.run_all_share_mode_setup_tests()
@ -231,7 +287,6 @@ class GuiShareTest(GuiBaseTest):
self.run_all_share_mode_download_tests(public_mode, stay_open)
self.have_same_password(password)
def run_all_share_mode_timer_tests(self, public_mode):
"""Auto-stop timer tests in share mode"""
self.run_all_share_mode_setup_tests()
@ -258,12 +313,16 @@ class GuiShareTest(GuiBaseTest):
self.set_autostart_timer(self.gui.share_mode, 15)
self.set_timeout(self.gui.share_mode, 5)
QtCore.QTimer.singleShot(4000, self.accept_dialog)
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(
self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton
)
self.server_is_stopped(self.gui.share_mode, False)
def run_all_share_mode_unreadable_file_tests(self):
'''Attempt to share an unreadable file'''
"""Attempt to share an unreadable file"""
self.run_all_share_mode_setup_tests()
QtCore.QTimer.singleShot(1000, self.accept_dialog)
self.gui.share_mode.server_status.file_selection.file_list.add_file('/tmp/nonexistent.txt')
self.gui.share_mode.server_status.file_selection.file_list.add_file(
"/tmp/nonexistent.txt"
)
self.file_selection_widget_has_files(2)

View file

@ -13,13 +13,16 @@ from onionshare.web import Web
from onionshare_gui import Application, OnionShare, OnionShareGui
from .GuiShareTest import GuiShareTest
class GuiWebsiteTest(GuiShareTest):
@staticmethod
def set_up(test_settings):
'''Create GUI with given settings'''
"""Create GUI with given settings"""
# Create our test file
testfile = open('/tmp/index.html', 'w')
testfile.write('<html><body><p>This is a test website hosted by OnionShare</p></body></html>')
testfile = open("/tmp/index.html", "w")
testfile.write(
"<html><body><p>This is a test website hosted by OnionShare</p></body></html>"
)
testfile.close()
common = Common()
@ -28,7 +31,7 @@ class GuiWebsiteTest(GuiShareTest):
strings.load_strings(common)
# Get all of the settings in test_settings
test_settings['data_dir'] = '/tmp/OnionShare'
test_settings["data_dir"] = "/tmp/OnionShare"
for key, val in common.settings.default_settings.items():
if key not in test_settings:
test_settings[key] = val
@ -40,44 +43,62 @@ class GuiWebsiteTest(GuiShareTest):
app = OnionShare(common, testonion, True, 0)
web = Web(common, False, True)
open('/tmp/settings.json', 'w').write(json.dumps(test_settings))
open("/tmp/settings.json", "w").write(json.dumps(test_settings))
gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/index.html'], '/tmp/settings.json', True)
gui = OnionShareGui(
common,
testonion,
qtapp,
app,
["/tmp/index.html"],
"/tmp/settings.json",
True,
)
return gui
@staticmethod
def tear_down():
'''Clean up after tests'''
"""Clean up after tests"""
try:
os.remove('/tmp/index.html')
os.remove('/tmp/settings.json')
os.remove("/tmp/index.html")
os.remove("/tmp/settings.json")
except:
pass
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)
if public_mode:
r = requests.get(url)
else:
r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.website_mode.server_status.web.password))
r = requests.get(
url,
auth=requests.auth.HTTPBasicAuth(
"onionshare", self.gui.website_mode.server_status.web.password
),
)
QtTest.QTest.qWait(2000)
self.assertTrue('This is a test website hosted by OnionShare' in r.text)
self.assertTrue("This is a test website hosted by OnionShare" in r.text)
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)
if public_mode:
r = requests.get(url)
else:
r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.website_mode.server_status.web.password))
r = requests.get(
url,
auth=requests.auth.HTTPBasicAuth(
"onionshare", self.gui.website_mode.server_status.web.password
),
)
QtTest.QTest.qWait(2000)
if csp_header_disabled:
self.assertFalse('Content-Security-Policy' in r.headers)
self.assertFalse("Content-Security-Policy" in r.headers)
else:
self.assertTrue('Content-Security-Policy' in r.headers)
self.assertTrue("Content-Security-Policy" in r.headers)
def run_all_website_mode_setup_tests(self):
"""Tests in website mode prior to starting a share"""
@ -100,16 +121,16 @@ class GuiWebsiteTest(GuiShareTest):
self.have_copy_url_button(self.gui.website_mode, public_mode)
self.server_status_indicator_says_started(self.gui.website_mode)
def run_all_website_mode_download_tests(self, public_mode):
"""Tests in website mode after viewing the site"""
self.run_all_website_mode_setup_tests()
self.run_all_website_mode_started_tests(public_mode, startup_time=2000)
self.view_website(public_mode)
self.check_csp_header(public_mode, self.gui.common.settings.get('csp_header_disabled'))
self.check_csp_header(
public_mode, self.gui.common.settings.get("csp_header_disabled")
)
self.history_widgets_present(self.gui.website_mode)
self.server_is_stopped(self.gui.website_mode, False)
self.web_server_is_stopped()
self.server_status_indicator_says_closed(self.gui.website_mode, False)
self.add_button_visible(self.gui.website_mode)

View file

@ -23,17 +23,17 @@ class OnionStub(object):
class SettingsGuiBaseTest(object):
@staticmethod
def set_up():
'''Create the GUI'''
"""Create the GUI"""
# Default settings for the settings GUI tests
test_settings = {
"no_bridges": False,
"tor_bridges_use_custom_bridges": "Bridge 1.2.3.4:56 EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\nBridge 5.6.7.8:910 EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\nBridge 11.12.13.14:1516 EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n",
"no_bridges": False,
"tor_bridges_use_custom_bridges": "Bridge 1.2.3.4:56 EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\nBridge 5.6.7.8:910 EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\nBridge 11.12.13.14:1516 EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n",
}
# Create our test file
testfile = open('/tmp/test.txt', 'w')
testfile.write('onionshare')
testfile = open("/tmp/test.txt", "w")
testfile.write("onionshare")
testfile.close()
common = Common()
@ -51,22 +51,22 @@ class SettingsGuiBaseTest(object):
if key not in test_settings:
test_settings[key] = val
open('/tmp/settings.json', 'w').write(json.dumps(test_settings))
open("/tmp/settings.json", "w").write(json.dumps(test_settings))
gui = SettingsDialog(common, testonion, qtapp, '/tmp/settings.json', True)
gui = SettingsDialog(common, testonion, qtapp, "/tmp/settings.json", True)
return gui
@staticmethod
def tear_down():
'''Clean up after tests'''
os.remove('/tmp/settings.json')
"""Clean up after tests"""
os.remove("/tmp/settings.json")
def run_settings_gui_tests(self):
self.gui.show()
# Window is shown
self.assertTrue(self.gui.isVisible())
self.assertEqual(self.gui.windowTitle(), strings._('gui_settings_window_title'))
self.assertEqual(self.gui.windowTitle(), strings._("gui_settings_window_title"))
# Check for updates button is hidden
self.assertFalse(self.gui.check_for_updates_button.isVisible())
@ -74,13 +74,21 @@ class SettingsGuiBaseTest(object):
# public mode is off
self.assertFalse(self.gui.public_mode_checkbox.isChecked())
# enable public mode
QtTest.QTest.mouseClick(self.gui.public_mode_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.public_mode_checkbox.height()/2))
QtTest.QTest.mouseClick(
self.gui.public_mode_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(2, self.gui.public_mode_checkbox.height() / 2),
)
self.assertTrue(self.gui.public_mode_checkbox.isChecked())
# autostop timer is off
self.assertFalse(self.gui.autostop_timer_checkbox.isChecked())
# enable autostop timer
QtTest.QTest.mouseClick(self.gui.autostop_timer_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.autostop_timer_checkbox.height()/2))
QtTest.QTest.mouseClick(
self.gui.autostop_timer_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(2, self.gui.autostop_timer_checkbox.height() / 2),
)
self.assertTrue(self.gui.autostop_timer_checkbox.isChecked())
# legacy mode checkbox and related widgets
@ -96,32 +104,70 @@ class SettingsGuiBaseTest(object):
self.assertFalse(self.gui.hidservauth_copy_button.isVisible())
# enable legacy mode
QtTest.QTest.mouseClick(self.gui.use_legacy_v2_onions_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.use_legacy_v2_onions_checkbox.height()/2))
QtTest.QTest.mouseClick(
self.gui.use_legacy_v2_onions_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(
2, self.gui.use_legacy_v2_onions_checkbox.height() / 2
),
)
self.assertTrue(self.gui.use_legacy_v2_onions_checkbox.isChecked())
self.assertTrue(self.gui.save_private_key_checkbox.isVisible())
self.assertTrue(self.gui.use_stealth_widget.isVisible())
# enable persistent mode
QtTest.QTest.mouseClick(self.gui.save_private_key_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.save_private_key_checkbox.height()/2))
QtTest.QTest.mouseClick(
self.gui.save_private_key_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(
2, self.gui.save_private_key_checkbox.height() / 2
),
)
self.assertTrue(self.gui.save_private_key_checkbox.isChecked())
# enable stealth mode
QtTest.QTest.mouseClick(self.gui.stealth_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.stealth_checkbox.height()/2))
QtTest.QTest.mouseClick(
self.gui.stealth_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(2, self.gui.stealth_checkbox.height() / 2),
)
self.assertTrue(self.gui.stealth_checkbox.isChecked())
# now that stealth is enabled, we can't turn off legacy mode
self.assertFalse(self.gui.use_legacy_v2_onions_checkbox.isEnabled())
# disable stealth, persistence
QtTest.QTest.mouseClick(self.gui.save_private_key_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.save_private_key_checkbox.height()/2))
QtTest.QTest.mouseClick(self.gui.stealth_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.stealth_checkbox.height()/2))
QtTest.QTest.mouseClick(
self.gui.save_private_key_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(
2, self.gui.save_private_key_checkbox.height() / 2
),
)
QtTest.QTest.mouseClick(
self.gui.stealth_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(2, self.gui.stealth_checkbox.height() / 2),
)
# legacy mode checkbox is enabled again
self.assertTrue(self.gui.use_legacy_v2_onions_checkbox.isEnabled())
# uncheck legacy mode
QtTest.QTest.mouseClick(self.gui.use_legacy_v2_onions_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.use_legacy_v2_onions_checkbox.height()/2))
QtTest.QTest.mouseClick(
self.gui.use_legacy_v2_onions_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(
2, self.gui.use_legacy_v2_onions_checkbox.height() / 2
),
)
# legacy options hidden again
self.assertTrue(self.gui.save_private_key_widget.isVisible())
self.assertFalse(self.gui.use_stealth_widget.isVisible())
# re-enable legacy mode
QtTest.QTest.mouseClick(self.gui.use_legacy_v2_onions_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.use_legacy_v2_onions_checkbox.height()/2))
QtTest.QTest.mouseClick(
self.gui.use_legacy_v2_onions_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(
2, self.gui.use_legacy_v2_onions_checkbox.height() / 2
),
)
else:
# legacy mode setting is hidden
@ -131,8 +177,16 @@ class SettingsGuiBaseTest(object):
self.assertTrue(self.gui.use_stealth_widget.isVisible())
# enable them all again so that we can see the setting stick in settings.json
QtTest.QTest.mouseClick(self.gui.save_private_key_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.save_private_key_checkbox.height()/2))
QtTest.QTest.mouseClick(self.gui.stealth_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.stealth_checkbox.height()/2))
QtTest.QTest.mouseClick(
self.gui.save_private_key_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(2, self.gui.save_private_key_checkbox.height() / 2),
)
QtTest.QTest.mouseClick(
self.gui.stealth_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(2, self.gui.stealth_checkbox.height() / 2),
)
else:
# None of the onion settings should appear
self.assertFalse(self.gui.use_legacy_v2_onions_checkbox.isVisible())
@ -144,12 +198,17 @@ class SettingsGuiBaseTest(object):
# stay open toggled off, on
self.assertTrue(self.gui.close_after_first_download_checkbox.isChecked())
QtTest.QTest.mouseClick(self.gui.close_after_first_download_checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.close_after_first_download_checkbox.height()/2))
QtTest.QTest.mouseClick(
self.gui.close_after_first_download_checkbox,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(
2, self.gui.close_after_first_download_checkbox.height() / 2
),
)
self.assertFalse(self.gui.close_after_first_download_checkbox.isChecked())
# receive mode
self.gui.data_dir_lineedit.setText('/tmp/OnionShareSettingsTest')
self.gui.data_dir_lineedit.setText("/tmp/OnionShareSettingsTest")
# bundled mode is enabled
self.assertTrue(self.gui.connection_type_bundled_radio.isEnabled())
@ -161,7 +220,11 @@ class SettingsGuiBaseTest(object):
self.assertTrue(self.gui.tor_bridges_use_custom_radio.isChecked())
# switch to obfs4
QtTest.QTest.mouseClick(self.gui.tor_bridges_use_obfs4_radio, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.tor_bridges_use_obfs4_radio.height()/2))
QtTest.QTest.mouseClick(
self.gui.tor_bridges_use_obfs4_radio,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(2, self.gui.tor_bridges_use_obfs4_radio.height() / 2),
)
self.assertTrue(self.gui.tor_bridges_use_obfs4_radio.isChecked())
# custom bridges are hidden
@ -175,7 +238,11 @@ class SettingsGuiBaseTest(object):
self.assertFalse(self.gui.connection_type_socket_file_radio.isChecked())
# enable automatic mode
QtTest.QTest.mouseClick(self.gui.connection_type_automatic_radio, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.connection_type_automatic_radio.height()/2))
QtTest.QTest.mouseClick(
self.gui.connection_type_automatic_radio,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(2, self.gui.connection_type_automatic_radio.height() / 2),
)
self.assertTrue(self.gui.connection_type_automatic_radio.isChecked())
# bundled is off
self.assertFalse(self.gui.connection_type_bundled_radio.isChecked())
@ -187,7 +254,13 @@ class SettingsGuiBaseTest(object):
self.assertFalse(self.gui.authenticate_password_radio.isVisible())
# enable control port mode
QtTest.QTest.mouseClick(self.gui.connection_type_control_port_radio, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.connection_type_control_port_radio.height()/2))
QtTest.QTest.mouseClick(
self.gui.connection_type_control_port_radio,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(
2, self.gui.connection_type_control_port_radio.height() / 2
),
)
self.assertTrue(self.gui.connection_type_control_port_radio.isChecked())
# automatic is off
self.assertFalse(self.gui.connection_type_automatic_radio.isChecked())
@ -196,7 +269,13 @@ class SettingsGuiBaseTest(object):
self.assertTrue(self.gui.authenticate_password_radio.isVisible())
# enable socket mode
QtTest.QTest.mouseClick(self.gui.connection_type_socket_file_radio, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.connection_type_socket_file_radio.height()/2))
QtTest.QTest.mouseClick(
self.gui.connection_type_socket_file_radio,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(
2, self.gui.connection_type_socket_file_radio.height() / 2
),
)
self.assertTrue(self.gui.connection_type_socket_file_radio.isChecked())
# control port is off
self.assertFalse(self.gui.connection_type_control_port_radio.isChecked())
@ -205,20 +284,30 @@ class SettingsGuiBaseTest(object):
self.assertTrue(self.gui.authenticate_password_radio.isVisible())
# re-enable bundled mode
QtTest.QTest.mouseClick(self.gui.connection_type_bundled_radio, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.connection_type_bundled_radio.height()/2))
QtTest.QTest.mouseClick(
self.gui.connection_type_bundled_radio,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(2, self.gui.connection_type_bundled_radio.height() / 2),
)
# go back to custom bridges
QtTest.QTest.mouseClick(self.gui.tor_bridges_use_custom_radio, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2,self.gui.tor_bridges_use_custom_radio.height()/2))
QtTest.QTest.mouseClick(
self.gui.tor_bridges_use_custom_radio,
QtCore.Qt.LeftButton,
pos=QtCore.QPoint(2, self.gui.tor_bridges_use_custom_radio.height() / 2),
)
self.assertTrue(self.gui.tor_bridges_use_custom_radio.isChecked())
self.assertTrue(self.gui.tor_bridges_use_custom_textbox.isVisible())
self.assertFalse(self.gui.tor_bridges_use_obfs4_radio.isChecked())
self.gui.tor_bridges_use_custom_textbox.setPlainText('94.242.249.2:83 E25A95F1DADB739F0A83EB0223A37C02FD519306\n148.251.90.59:7510 019F727CA6DCA6CA5C90B55E477B7D87981E75BC\n93.80.47.217:41727 A6A0D497D98097FCFE91D639548EE9E34C15CDD3')
self.gui.tor_bridges_use_custom_textbox.setPlainText(
"94.242.249.2:83 E25A95F1DADB739F0A83EB0223A37C02FD519306\n148.251.90.59:7510 019F727CA6DCA6CA5C90B55E477B7D87981E75BC\n93.80.47.217:41727 A6A0D497D98097FCFE91D639548EE9E34C15CDD3"
)
# Test that the Settings Dialog can save the settings and close itself
QtTest.QTest.mouseClick(self.gui.save_button, QtCore.Qt.LeftButton)
self.assertFalse(self.gui.isVisible())
# Test our settings are reflected in the settings json
with open('/tmp/settings.json') as f:
with open("/tmp/settings.json") as f:
data = json.load(f)
self.assertTrue(data["public_mode"])
@ -238,4 +327,7 @@ class SettingsGuiBaseTest(object):
self.assertFalse(data["close_after_first_download"])
self.assertEqual(data["connection_type"], "bundled")
self.assertFalse(data["tor_bridges_use_obfs4"])
self.assertEqual(data["tor_bridges_use_custom_bridges"], "Bridge 94.242.249.2:83 E25A95F1DADB739F0A83EB0223A37C02FD519306\nBridge 148.251.90.59:7510 019F727CA6DCA6CA5C90B55E477B7D87981E75BC\nBridge 93.80.47.217:41727 A6A0D497D98097FCFE91D639548EE9E34C15CDD3\n")
self.assertEqual(
data["tor_bridges_use_custom_bridges"],
"Bridge 94.242.249.2:83 E25A95F1DADB739F0A83EB0223A37C02FD519306\nBridge 148.251.90.59:7510 019F727CA6DCA6CA5C90B55E477B7D87981E75BC\nBridge 93.80.47.217:41727 A6A0D497D98097FCFE91D639548EE9E34C15CDD3\n",
)

View file

@ -16,20 +16,21 @@ from onionshare_gui.mode.receive_mode import ReceiveMode
from .GuiBaseTest import GuiBaseTest
class TorGuiBaseTest(GuiBaseTest):
@staticmethod
def set_up(test_settings):
'''Create GUI with given settings'''
"""Create GUI with given settings"""
# Create our test file
testfile = open('/tmp/test.txt', 'w')
testfile.write('onionshare')
testfile = open("/tmp/test.txt", "w")
testfile.write("onionshare")
testfile.close()
# Create a test dir and files
if not os.path.exists('/tmp/testdir'):
testdir = os.mkdir('/tmp/testdir')
testfile = open('/tmp/testdir/test.txt', 'w')
testfile.write('onionshare')
if not os.path.exists("/tmp/testdir"):
testdir = os.mkdir("/tmp/testdir")
testfile = open("/tmp/testdir/test.txt", "w")
testfile.write("onionshare")
testfile.close()
common = Common()
@ -38,8 +39,8 @@ class TorGuiBaseTest(GuiBaseTest):
strings.load_strings(common)
# Get all of the settings in test_settings
test_settings['connection_type'] = 'automatic'
test_settings['data_dir'] = '/tmp/OnionShare'
test_settings["connection_type"] = "automatic"
test_settings["data_dir"] = "/tmp/OnionShare"
for key, val in common.settings.default_settings.items():
if key not in test_settings:
test_settings[key] = val
@ -51,13 +52,21 @@ class TorGuiBaseTest(GuiBaseTest):
app = OnionShare(common, testonion, False, 0)
web = Web(common, False, False)
open('/tmp/settings.json', 'w').write(json.dumps(test_settings))
open("/tmp/settings.json", "w").write(json.dumps(test_settings))
gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/test.txt', '/tmp/testdir'], '/tmp/settings.json', False)
gui = OnionShareGui(
common,
testonion,
qtapp,
app,
["/tmp/test.txt", "/tmp/testdir"],
"/tmp/settings.json",
False,
)
return gui
def history_indicator(self, mode, public_mode):
'''Test that we can make sure the history is toggled off, do an action, and the indiciator works'''
"""Test that we can make sure the history is toggled off, do an action, and the indiciator works"""
# Make sure history is toggled off
if mode.history.isVisible():
QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton)
@ -70,15 +79,17 @@ 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"] = "socks5h://{}:{}".format(socks_address, socks_port)
if type(mode) == ReceiveMode:
# Upload a file
files = {'file[]': open('/tmp/test.txt', 'rb')}
files = {"file[]": open("/tmp/test.txt", "rb")}
if not public_mode:
path = 'http://{}/{}/upload'.format(self.gui.app.onion_host, mode.web.password)
path = "http://{}/{}/upload".format(
self.gui.app.onion_host, mode.web.password
)
else:
path = 'http://{}/upload'.format(self.gui.app.onion_host)
path = "http://{}/upload".format(self.gui.app.onion_host)
response = session.post(path, files=files)
QtTest.QTest.qWait(4000)
@ -87,7 +98,9 @@ class TorGuiBaseTest(GuiBaseTest):
if public_mode:
path = "http://{}/download".format(self.gui.app.onion_host)
else:
path = "http://{}/{}/download".format(self.gui.app.onion_host, mode.web.password)
path = "http://{}/{}/download".format(
self.gui.app.onion_host, mode.web.password
)
response = session.get(path)
QtTest.QTest.qWait(4000)
@ -100,61 +113,72 @@ class TorGuiBaseTest(GuiBaseTest):
self.assertFalse(mode.toggle_history.indicator_label.isVisible())
def have_an_onion_service(self):
'''Test that we have a valid Onion URL'''
self.assertRegex(self.gui.app.onion_host, r'[a-z2-7].onion')
"""Test that we have a valid Onion URL"""
self.assertRegex(self.gui.app.onion_host, r"[a-z2-7].onion")
def web_page(self, mode, string, public_mode):
'''Test that the web page contains a string'''
"""Test that the web page contains a string"""
(socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port()
socks.set_default_proxy(socks.SOCKS5, socks_address, socks_port)
s = socks.socksocket()
s.settimeout(60)
s.connect((self.gui.app.onion_host, 80))
if not public_mode:
path = '/{}'.format(mode.server_status.web.password)
path = "/{}".format(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 += '\r\n'
s.sendall(http_request.encode('utf-8'))
with open('/tmp/webpage', 'wb') as file_to_write:
path = "/"
http_request = "GET {} HTTP/1.0\r\n".format(path)
http_request += "Host: {}\r\n".format(self.gui.app.onion_host)
http_request += "\r\n"
s.sendall(http_request.encode("utf-8"))
with open("/tmp/webpage", "wb") as file_to_write:
while True:
data = s.recv(1024)
if not data:
break
file_to_write.write(data)
data = s.recv(1024)
if not data:
break
file_to_write.write(data)
file_to_write.close()
f = open('/tmp/webpage')
f = open("/tmp/webpage")
self.assertTrue(string in f.read())
f.close()
def have_copy_url_button(self, mode, public_mode):
'''Test that the Copy URL button is shown and that the clipboard is correct'''
"""Test that the Copy URL button is shown and that the clipboard is correct"""
self.assertTrue(mode.server_status.copy_url_button.isVisible())
QtTest.QTest.mouseClick(mode.server_status.copy_url_button, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(
mode.server_status.copy_url_button, QtCore.Qt.LeftButton
)
clipboard = self.gui.qtapp.clipboard()
if public_mode:
self.assertEqual(clipboard.text(), 'http://{}'.format(self.gui.app.onion_host))
self.assertEqual(
clipboard.text(), "http://{}".format(self.gui.app.onion_host)
)
else:
self.assertEqual(clipboard.text(), 'http://{}/{}'.format(self.gui.app.onion_host, mode.server_status.web.password))
self.assertEqual(
clipboard.text(),
"http://{}/{}".format(
self.gui.app.onion_host, mode.server_status.web.password
),
)
# Stealth tests
def copy_have_hidserv_auth_button(self, mode):
'''Test that the Copy HidservAuth button is shown'''
"""Test that the Copy HidservAuth button is shown"""
self.assertTrue(mode.server_status.copy_hidservauth_button.isVisible())
def hidserv_auth_string(self):
'''Test the validity of the HidservAuth string'''
self.assertRegex(self.gui.app.auth_string, r'HidServAuth {} [a-zA-Z1-9]'.format(self.gui.app.onion_host))
"""Test the validity of the HidservAuth string"""
self.assertRegex(
self.gui.app.auth_string,
r"HidServAuth {} [a-zA-Z1-9]".format(self.gui.app.onion_host),
)
# Miscellaneous tests
def tor_killed_statusbar_message_shown(self, mode):
'''Test that the status bar message shows Tor was disconnected'''
"""Test that the status bar message shows Tor was disconnected"""
self.gui.app.onion.c = None
QtTest.QTest.qWait(1000)
self.assertTrue(mode.status_bar.currentMessage(), strings._('gui_tor_connection_lost'))
self.assertTrue(
mode.status_bar.currentMessage(), strings._("gui_tor_connection_lost")
)

View file

@ -3,28 +3,29 @@ import requests
from PyQt5 import QtTest
from .TorGuiBaseTest import TorGuiBaseTest
class TorGuiReceiveTest(TorGuiBaseTest):
class TorGuiReceiveTest(TorGuiBaseTest):
def upload_file(self, public_mode, file_to_upload, expected_file):
'''Test that we can upload the file'''
"""Test that we can upload the file"""
(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)
files = {'file[]': open(file_to_upload, 'rb')}
session.proxies["http"] = "socks5h://{}:{}".format(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 = "http://{}/{}/upload".format(
self.gui.app.onion_host, self.gui.receive_mode.web.password
)
else:
path = 'http://{}/upload'.format(self.gui.app.onion_host)
path = "http://{}/upload".format(self.gui.app.onion_host)
response = session.post(path, files=files)
QtTest.QTest.qWait(4000)
self.assertTrue(os.path.isfile(expected_file))
# 'Grouped' tests follow from here
def run_all_receive_mode_tests(self, public_mode, receive_allow_receiver_shutdown):
'''Run a full suite of tests in Receive mode'''
"""Run a full suite of tests in Receive mode"""
self.click_mode(self.gui.receive_mode)
self.history_is_not_visible(self.gui.receive_mode)
self.click_toggle_history(self.gui.receive_mode)
@ -39,15 +40,19 @@ class TorGuiReceiveTest(TorGuiBaseTest):
self.url_description_shown(self.gui.receive_mode)
self.have_copy_url_button(self.gui.receive_mode, public_mode)
self.server_status_indicator_says_started(self.gui.receive_mode)
self.web_page(self.gui.receive_mode, 'Select the files you want to send, then click', public_mode)
self.upload_file(public_mode, '/tmp/test.txt', '/tmp/OnionShare/test.txt')
self.web_page(
self.gui.receive_mode,
"Select the files you want to send, then click",
public_mode,
)
self.upload_file(public_mode, "/tmp/test.txt", "/tmp/OnionShare/test.txt")
self.history_widgets_present(self.gui.receive_mode)
self.counter_incremented(self.gui.receive_mode, 1)
self.upload_file(public_mode, '/tmp/test.txt', '/tmp/OnionShare/test-2.txt')
self.upload_file(public_mode, "/tmp/test.txt", "/tmp/OnionShare/test-2.txt")
self.counter_incremented(self.gui.receive_mode, 2)
self.upload_file(public_mode, '/tmp/testdir/test', '/tmp/OnionShare/test')
self.upload_file(public_mode, "/tmp/testdir/test", "/tmp/OnionShare/test")
self.counter_incremented(self.gui.receive_mode, 3)
self.upload_file(public_mode, '/tmp/testdir/test', '/tmp/OnionShare/test-2')
self.upload_file(public_mode, "/tmp/testdir/test", "/tmp/OnionShare/test-2")
self.counter_incremented(self.gui.receive_mode, 4)
self.history_indicator(self.gui.receive_mode, public_mode)
self.server_is_stopped(self.gui.receive_mode, False)

View file

@ -4,48 +4,50 @@ from PyQt5 import QtTest
from .TorGuiBaseTest import TorGuiBaseTest
from .GuiShareTest import GuiShareTest
class TorGuiShareTest(TorGuiBaseTest, GuiShareTest):
def download_share(self, public_mode):
'''Test downloading a share'''
"""Test downloading a share"""
# Set up connecting to the onion
(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"] = "socks5h://{}:{}".format(socks_address, socks_port)
# Download files
if public_mode:
path = "http://{}/download".format(self.gui.app.onion_host)
else:
path = "http://{}/{}/download".format(self.gui.app.onion_host, self.gui.share_mode.web.password)
path = "http://{}/{}/download".format(
self.gui.app.onion_host, self.gui.share_mode.web.password
)
response = session.get(path, stream=True)
QtTest.QTest.qWait(4000)
if response.status_code == 200:
with open('/tmp/download.zip', 'wb') as file_to_write:
with open("/tmp/download.zip", "wb") as file_to_write:
for chunk in response.iter_content(chunk_size=128):
file_to_write.write(chunk)
file_to_write.close()
zip = zipfile.ZipFile('/tmp/download.zip')
zip = zipfile.ZipFile("/tmp/download.zip")
QtTest.QTest.qWait(4000)
self.assertEqual('onionshare', zip.read('test.txt').decode('utf-8'))
self.assertEqual("onionshare", zip.read("test.txt").decode("utf-8"))
# Persistence tests
def have_same_onion(self, onion):
'''Test that we have the same onion'''
"""Test that we have the same onion"""
self.assertEqual(self.gui.app.onion_host, onion)
# legacy v2 onion test
def have_v2_onion(self):
'''Test that the onion is a v2 style onion'''
self.assertRegex(self.gui.app.onion_host, r'[a-z2-7].onion')
"""Test that the onion is a v2 style onion"""
self.assertRegex(self.gui.app.onion_host, r"[a-z2-7].onion")
self.assertEqual(len(self.gui.app.onion_host), 22)
# 'Grouped' tests follow from here
def run_all_share_mode_started_tests(self, public_mode):
'''Tests in share mode after starting a share'''
"""Tests in share mode after starting a share"""
self.server_working_on_start_button_pressed(self.gui.share_mode)
self.server_status_indicator_says_starting(self.gui.share_mode)
self.add_delete_buttons_hidden()
@ -58,10 +60,9 @@ class TorGuiShareTest(TorGuiBaseTest, GuiShareTest):
self.have_copy_url_button(self.gui.share_mode, public_mode)
self.server_status_indicator_says_started(self.gui.share_mode)
def run_all_share_mode_download_tests(self, public_mode, stay_open):
"""Tests in share mode after downloading a share"""
self.web_page(self.gui.share_mode, 'Total size', public_mode)
self.web_page(self.gui.share_mode, "Total size", public_mode)
self.download_share(public_mode)
self.history_widgets_present(self.gui.share_mode)
self.server_is_stopped(self.gui.share_mode, stay_open)
@ -72,7 +73,6 @@ class TorGuiShareTest(TorGuiBaseTest, GuiShareTest):
self.server_is_started(self.gui.share_mode, startup_time=45000)
self.history_indicator(self.gui.share_mode, public_mode)
def run_all_share_mode_persistent_tests(self, public_mode, stay_open):
"""Same as end-to-end share tests but also test the password is the same on multiple shared"""
self.run_all_share_mode_setup_tests()
@ -83,7 +83,6 @@ class TorGuiShareTest(TorGuiBaseTest, GuiShareTest):
self.have_same_onion(onion)
self.have_same_password(password)
def run_all_share_mode_timer_tests(self, public_mode):
"""Auto-stop timer tests in share mode"""
self.run_all_share_mode_setup_tests()

View file

@ -1,4 +1,5 @@
import sys
# Force tests to look for resources in the source code tree
sys.onionshare_dev_mode = True
@ -10,6 +11,7 @@ import pytest
from onionshare import common, web, settings, strings
def pytest_addoption(parser):
parser.addoption(
"--rungui", action="store_true", default=False, help="run GUI tests"
@ -27,7 +29,7 @@ def pytest_collection_modifyitems(config, items):
if "tor" in item.keywords:
item.add_marker(skip_tor)
if not config.getoption('--rungui'):
if not config.getoption("--rungui"):
# --rungui given in cli: do not skip GUI tests
skip_gui = pytest.mark.skip(reason="need --rungui option to run")
for item in items:
@ -43,8 +45,8 @@ def temp_dir_1024():
tmp_dir = tempfile.mkdtemp()
tmp_file, tmp_file_path = tempfile.mkstemp(dir=tmp_dir)
with open(tmp_file, 'wb') as f:
f.write(b'*' * 1024)
with open(tmp_file, "wb") as f:
f.write(b"*" * 1024)
return tmp_dir
@ -58,8 +60,8 @@ def temp_dir_1024_delete():
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_file, tmp_file_path = tempfile.mkstemp(dir=tmp_dir)
with open(tmp_file, 'wb') as f:
f.write(b'*' * 1024)
with open(tmp_file, "wb") as f:
f.write(b"*" * 1024)
yield tmp_dir
@ -68,7 +70,7 @@ def temp_file_1024():
""" Create a temporary file of a particular size (1024 bytes). """
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
tmp_file.write(b'*' * 1024)
tmp_file.write(b"*" * 1024)
return tmp_file.name
@ -81,18 +83,18 @@ def temp_file_1024_delete():
"""
with tempfile.NamedTemporaryFile() as tmp_file:
tmp_file.write(b'*' * 1024)
tmp_file.write(b"*" * 1024)
tmp_file.flush()
yield tmp_file.name
# pytest > 2.9 only needs @pytest.fixture
@pytest.yield_fixture(scope='session')
@pytest.yield_fixture(scope="session")
def custom_zw():
zw = web.share_mode.ZipWriter(
common.Common(),
zip_filename=common.Common.random_string(4, 6),
processed_size_callback=lambda _: 'custom_callback'
processed_size_callback=lambda _: "custom_callback",
)
yield zw
zw.close()
@ -100,7 +102,7 @@ def custom_zw():
# pytest > 2.9 only needs @pytest.fixture
@pytest.yield_fixture(scope='session')
@pytest.yield_fixture(scope="session")
def default_zw():
zw = web.share_mode.ZipWriter(common.Common())
yield zw
@ -111,76 +113,77 @@ def default_zw():
@pytest.fixture
def locale_en(monkeypatch):
monkeypatch.setattr('locale.getdefaultlocale', lambda: ('en_US', 'UTF-8'))
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("en_US", "UTF-8"))
@pytest.fixture
def locale_fr(monkeypatch):
monkeypatch.setattr('locale.getdefaultlocale', lambda: ('fr_FR', 'UTF-8'))
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("fr_FR", "UTF-8"))
@pytest.fixture
def locale_invalid(monkeypatch):
monkeypatch.setattr('locale.getdefaultlocale', lambda: ('xx_XX', 'UTF-8'))
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("xx_XX", "UTF-8"))
@pytest.fixture
def locale_ru(monkeypatch):
monkeypatch.setattr('locale.getdefaultlocale', lambda: ('ru_RU', 'UTF-8'))
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("ru_RU", "UTF-8"))
@pytest.fixture
def platform_darwin(monkeypatch):
monkeypatch.setattr('platform.system', lambda: 'Darwin')
monkeypatch.setattr("platform.system", lambda: "Darwin")
@pytest.fixture # (scope="session")
def platform_linux(monkeypatch):
monkeypatch.setattr('platform.system', lambda: 'Linux')
monkeypatch.setattr("platform.system", lambda: "Linux")
@pytest.fixture
def platform_windows(monkeypatch):
monkeypatch.setattr('platform.system', lambda: 'Windows')
monkeypatch.setattr("platform.system", lambda: "Windows")
@pytest.fixture
def sys_argv_sys_prefix(monkeypatch):
monkeypatch.setattr('sys.argv', [sys.prefix])
monkeypatch.setattr("sys.argv", [sys.prefix])
@pytest.fixture
def sys_frozen(monkeypatch):
monkeypatch.setattr('sys.frozen', True, raising=False)
monkeypatch.setattr("sys.frozen", True, raising=False)
@pytest.fixture
def sys_meipass(monkeypatch):
monkeypatch.setattr(
'sys._MEIPASS', os.path.expanduser('~'), raising=False)
monkeypatch.setattr("sys._MEIPASS", os.path.expanduser("~"), raising=False)
@pytest.fixture # (scope="session")
def sys_onionshare_dev_mode(monkeypatch):
monkeypatch.setattr('sys.onionshare_dev_mode', True, raising=False)
monkeypatch.setattr("sys.onionshare_dev_mode", True, raising=False)
@pytest.fixture
def time_time_100(monkeypatch):
monkeypatch.setattr('time.time', lambda: 100)
monkeypatch.setattr("time.time", lambda: 100)
@pytest.fixture
def time_strftime(monkeypatch):
monkeypatch.setattr('time.strftime', lambda _: 'Jun 06 2013 11:05:00')
monkeypatch.setattr("time.strftime", lambda _: "Jun 06 2013 11:05:00")
@pytest.fixture
def common_obj():
return common.Common()
@pytest.fixture
def settings_obj(sys_onionshare_dev_mode, platform_linux):
_common = common.Common()
_common.version = 'DUMMY_VERSION_1.2.3'
_common.version = "DUMMY_VERSION_1.2.3"
strings.load_strings(_common)
return settings.Settings(_common)

View file

@ -4,13 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class Local401PublicModeRateLimitTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"close_after_first_download": False,
"public_mode": True
}
test_settings = {"close_after_first_download": False, "public_mode": True}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -18,11 +16,12 @@ class Local401PublicModeRateLimitTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(True, True)
self.hit_401(True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class Local401RateLimitTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"close_after_first_download": False
}
test_settings = {"close_after_first_download": False}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -17,11 +16,12 @@ class Local401RateLimitTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(False, True)
self.hit_401(False)
if __name__ == "__main__":
unittest.main()

View file

@ -5,12 +5,11 @@ from PyQt5 import QtCore, QtTest
from .GuiShareTest import GuiShareTest
class LocalQuittingDuringSharePromptsWarningTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"close_after_first_download": False
}
test_settings = {"close_after_first_download": False}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -18,7 +17,7 @@ class LocalQuittingDuringSharePromptsWarningTest(unittest.TestCase, GuiShareTest
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(False, True)
@ -30,5 +29,6 @@ class LocalQuittingDuringSharePromptsWarningTest(unittest.TestCase, GuiShareTest
self.server_is_started(self.gui.share_mode, 0)
self.web_server_is_running()
if __name__ == "__main__":
unittest.main()

View file

@ -4,11 +4,11 @@ import unittest
from .GuiReceiveTest import GuiReceiveTest
class LocalReceiveModeClearAllButtonTest(unittest.TestCase, GuiReceiveTest):
@classmethod
def setUpClass(cls):
test_settings = {
}
test_settings = {}
cls.gui = GuiReceiveTest.set_up(test_settings)
@classmethod
@ -16,10 +16,11 @@ class LocalReceiveModeClearAllButtonTest(unittest.TestCase, GuiReceiveTest):
GuiReceiveTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_clear_all_button_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -4,13 +4,11 @@ import unittest
from .GuiReceiveTest import GuiReceiveTest
class LocalReceiveModeTimerTest(unittest.TestCase, GuiReceiveTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": False,
"autostop_timer": True,
}
test_settings = {"public_mode": False, "autostop_timer": True}
cls.gui = GuiReceiveTest.set_up(test_settings)
@classmethod
@ -18,10 +16,11 @@ class LocalReceiveModeTimerTest(unittest.TestCase, GuiReceiveTest):
GuiReceiveTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_receive_mode_timer_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .GuiReceiveTest import GuiReceiveTest
class LocalReceiveModeUnwritableTest(unittest.TestCase, GuiReceiveTest):
@classmethod
def setUpClass(cls):
test_settings = {
"receive_allow_receiver_shutdown": True
}
test_settings = {"receive_allow_receiver_shutdown": True}
cls.gui = GuiReceiveTest.set_up(test_settings)
@classmethod
@ -17,10 +16,11 @@ class LocalReceiveModeUnwritableTest(unittest.TestCase, GuiReceiveTest):
GuiReceiveTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_receive_mode_unwritable_dir_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -4,13 +4,11 @@ import unittest
from .GuiReceiveTest import GuiReceiveTest
class LocalReceivePublicModeUnwritableTest(unittest.TestCase, GuiReceiveTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": True,
"receive_allow_receiver_shutdown": True
}
test_settings = {"public_mode": True, "receive_allow_receiver_shutdown": True}
cls.gui = GuiReceiveTest.set_up(test_settings)
@classmethod
@ -18,10 +16,11 @@ class LocalReceivePublicModeUnwritableTest(unittest.TestCase, GuiReceiveTest):
GuiReceiveTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_receive_mode_unwritable_dir_tests(True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,13 +4,11 @@ import unittest
from .GuiReceiveTest import GuiReceiveTest
class LocalReceiveModePublicModeTest(unittest.TestCase, GuiReceiveTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": True,
"receive_allow_receiver_shutdown": True
}
test_settings = {"public_mode": True, "receive_allow_receiver_shutdown": True}
cls.gui = GuiReceiveTest.set_up(test_settings)
@classmethod
@ -18,10 +16,11 @@ class LocalReceiveModePublicModeTest(unittest.TestCase, GuiReceiveTest):
GuiReceiveTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_receive_mode_tests(True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .GuiReceiveTest import GuiReceiveTest
class LocalReceiveModeTest(unittest.TestCase, GuiReceiveTest):
@classmethod
def setUpClass(cls):
test_settings = {
"receive_allow_receiver_shutdown": True
}
test_settings = {"receive_allow_receiver_shutdown": True}
cls.gui = GuiReceiveTest.set_up(test_settings)
@classmethod
@ -17,10 +16,11 @@ class LocalReceiveModeTest(unittest.TestCase, GuiReceiveTest):
GuiReceiveTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_receive_mode_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -16,7 +16,7 @@ class SettingsGuiTest(unittest.TestCase, SettingsGuiBaseTest):
SettingsGuiBaseTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui_legacy_tor(self):
self.gui.onion = OnionStub(True, False)
self.gui.reload_settings()

View file

@ -16,7 +16,7 @@ class SettingsGuiTest(unittest.TestCase, SettingsGuiBaseTest):
SettingsGuiBaseTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui_no_tor(self):
self.gui.onion = OnionStub(False, False)
self.gui.reload_settings()

View file

@ -16,7 +16,7 @@ class SettingsGuiTest(unittest.TestCase, SettingsGuiBaseTest):
SettingsGuiBaseTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui_v3_tor(self):
self.gui.onion = OnionStub(True, True)
self.gui.reload_settings()

View file

@ -4,6 +4,7 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeAutoStartTimerTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
@ -19,10 +20,11 @@ class LocalShareModeAutoStartTimerTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_autostop_autostart_mismatch_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -4,13 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeAutoStartTimerTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": False,
"autostart_timer": True,
}
test_settings = {"public_mode": False, "autostart_timer": True}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -18,10 +16,11 @@ class LocalShareModeAutoStartTimerTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_autostart_timer_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -5,13 +5,11 @@ from PyQt5 import QtCore, QtTest
from .GuiShareTest import GuiShareTest
class LocalShareModeAutoStartTimerTooShortTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": False,
"autostart_timer": True,
}
test_settings = {"public_mode": False, "autostart_timer": True}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -19,7 +17,7 @@ class LocalShareModeAutoStartTimerTooShortTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_setup_tests()
@ -27,8 +25,11 @@ class LocalShareModeAutoStartTimerTooShortTest(unittest.TestCase, GuiShareTest):
self.set_autostart_timer(self.gui.share_mode, 2)
QtTest.QTest.qWait(3000)
QtCore.QTimer.singleShot(4000, self.accept_dialog)
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(
self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton
)
self.assertEqual(self.gui.share_mode.server_status.status, 0)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeCancelTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"autostart_timer": True,
}
test_settings = {"autostart_timer": True}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -17,11 +16,12 @@ class LocalShareModeCancelTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_setup_tests()
self.cancel_the_share(self.gui.share_mode)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeClearAllButtonTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"close_after_first_download": False,
}
test_settings = {"close_after_first_download": False}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -17,10 +16,11 @@ class LocalShareModeClearAllButtonTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_clear_all_button_tests(False, True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModePublicModeTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": True,
}
test_settings = {"public_mode": True}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -17,10 +16,11 @@ class LocalShareModePublicModeTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(True, False)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeStayOpenTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"close_after_first_download": False,
}
test_settings = {"close_after_first_download": False}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -17,10 +16,11 @@ class LocalShareModeStayOpenTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(False, True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,11 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
}
test_settings = {}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -16,10 +16,11 @@ class LocalShareModeTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(False, False)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeIndividualFileViewStayOpenTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"close_after_first_download": False,
}
test_settings = {"close_after_first_download": False}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -17,10 +16,11 @@ class LocalShareModeIndividualFileViewStayOpenTest(unittest.TestCase, GuiShareTe
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_individual_file_tests(False, True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeIndividualFileViewTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"close_after_first_download": True,
}
test_settings = {"close_after_first_download": True}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -17,10 +16,11 @@ class LocalShareModeIndividualFileViewTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_individual_file_tests(False, False)
if __name__ == "__main__":
unittest.main()

View file

@ -4,11 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeLargeDownloadTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
}
test_settings = {}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -16,10 +16,11 @@ class LocalShareModeLargeDownloadTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_large_file_tests(False, True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,6 +4,7 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModePersistentPasswordTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
@ -20,10 +21,11 @@ class LocalShareModePersistentPasswordTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_persistent_tests(False, True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,13 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeTimerTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": False,
"autostop_timer": True,
}
test_settings = {"public_mode": False, "autostop_timer": True}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -18,10 +16,11 @@ class LocalShareModeTimerTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_timer_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -5,13 +5,11 @@ from PyQt5 import QtCore, QtTest
from .GuiShareTest import GuiShareTest
class LocalShareModeTimerTooShortTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": False,
"autostop_timer": True,
}
test_settings = {"public_mode": False, "autostop_timer": True}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -19,7 +17,7 @@ class LocalShareModeTimerTooShortTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_setup_tests()
@ -27,8 +25,11 @@ class LocalShareModeTimerTooShortTest(unittest.TestCase, GuiShareTest):
self.set_timeout(self.gui.share_mode, 2)
QtTest.QTest.qWait(3000)
QtCore.QTimer.singleShot(4000, self.accept_dialog)
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton)
QtTest.QTest.mouseClick(
self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton
)
self.assertEqual(self.gui.share_mode.server_status.status, 0)
if __name__ == "__main__":
unittest.main()

View file

@ -4,11 +4,11 @@ import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeUnReadableFileTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
}
test_settings = {}
cls.gui = GuiShareTest.set_up(test_settings)
@classmethod
@ -16,10 +16,11 @@ class LocalShareModeUnReadableFileTest(unittest.TestCase, GuiShareTest):
GuiShareTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_unreadable_file_tests()
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .GuiWebsiteTest import GuiWebsiteTest
class LocalWebsiteModeCSPEnabledTest(unittest.TestCase, GuiWebsiteTest):
@classmethod
def setUpClass(cls):
test_settings = {
"csp_header_disabled": False,
}
test_settings = {"csp_header_disabled": False}
cls.gui = GuiWebsiteTest.set_up(test_settings)
@classmethod
@ -17,10 +16,11 @@ class LocalWebsiteModeCSPEnabledTest(unittest.TestCase, GuiWebsiteTest):
GuiWebsiteTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
#self.run_all_common_setup_tests()
# self.run_all_common_setup_tests()
self.run_all_website_mode_download_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .GuiWebsiteTest import GuiWebsiteTest
class LocalWebsiteModeTest(unittest.TestCase, GuiWebsiteTest):
@classmethod
def setUpClass(cls):
test_settings = {
"csp_header_disabled": True
}
test_settings = {"csp_header_disabled": True}
cls.gui = GuiWebsiteTest.set_up(test_settings)
@classmethod
@ -17,10 +16,11 @@ class LocalWebsiteModeTest(unittest.TestCase, GuiWebsiteTest):
GuiWebsiteTest.tear_down()
@pytest.mark.gui
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
#self.run_all_common_setup_tests()
# self.run_all_common_setup_tests()
self.run_all_website_mode_download_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -8,9 +8,7 @@ from .TorGuiShareTest import TorGuiShareTest
class ShareModeCancelSecondShareTest(unittest.TestCase, TorGuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"close_after_first_download": True
}
test_settings = {"close_after_first_download": True}
cls.gui = TorGuiShareTest.set_up(test_settings)
@classmethod
@ -19,7 +17,7 @@ class ShareModeCancelSecondShareTest(unittest.TestCase, TorGuiShareTest):
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(False, False)
@ -27,5 +25,6 @@ class ShareModeCancelSecondShareTest(unittest.TestCase, TorGuiShareTest):
self.server_is_stopped(self.gui.share_mode, False)
self.web_server_is_stopped()
if __name__ == "__main__":
unittest.main()

View file

@ -4,13 +4,11 @@ import unittest
from .TorGuiReceiveTest import TorGuiReceiveTest
class ReceiveModeTest(unittest.TestCase, TorGuiReceiveTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": True,
"receive_allow_receiver_shutdown": True
}
test_settings = {"public_mode": True, "receive_allow_receiver_shutdown": True}
cls.gui = TorGuiReceiveTest.set_up(test_settings)
@classmethod
@ -19,10 +17,11 @@ class ReceiveModeTest(unittest.TestCase, TorGuiReceiveTest):
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_receive_mode_tests(True, True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .TorGuiReceiveTest import TorGuiReceiveTest
class ReceiveModeTest(unittest.TestCase, TorGuiReceiveTest):
@classmethod
def setUpClass(cls):
test_settings = {
"receive_allow_receiver_shutdown": True
}
test_settings = {"receive_allow_receiver_shutdown": True}
cls.gui = TorGuiReceiveTest.set_up(test_settings)
@classmethod
@ -18,10 +17,11 @@ class ReceiveModeTest(unittest.TestCase, TorGuiReceiveTest):
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_receive_mode_tests(False, True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .TorGuiShareTest import TorGuiShareTest
class ShareModeCancelTest(unittest.TestCase, TorGuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"autostart_timer": True,
}
test_settings = {"autostart_timer": True}
cls.gui = TorGuiShareTest.set_up(test_settings)
@classmethod
@ -18,11 +17,12 @@ class ShareModeCancelTest(unittest.TestCase, TorGuiShareTest):
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_setup_tests()
self.cancel_the_share(self.gui.share_mode)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .TorGuiShareTest import TorGuiShareTest
class ShareModePublicModeTest(unittest.TestCase, TorGuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": True,
}
test_settings = {"public_mode": True}
cls.gui = TorGuiShareTest.set_up(test_settings)
@classmethod
@ -18,10 +17,11 @@ class ShareModePublicModeTest(unittest.TestCase, TorGuiShareTest):
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(True, False)
if __name__ == "__main__":
unittest.main()

View file

@ -4,12 +4,11 @@ import unittest
from .TorGuiShareTest import TorGuiShareTest
class ShareModeStayOpenTest(unittest.TestCase, TorGuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"close_after_first_download": False,
}
test_settings = {"close_after_first_download": False}
cls.gui = TorGuiShareTest.set_up(test_settings)
@classmethod
@ -18,10 +17,11 @@ class ShareModeStayOpenTest(unittest.TestCase, TorGuiShareTest):
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(False, True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,11 +4,11 @@ import unittest
from .TorGuiShareTest import TorGuiShareTest
class ShareModeTest(unittest.TestCase, TorGuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
}
test_settings = {}
cls.gui = TorGuiShareTest.set_up(test_settings)
@classmethod
@ -17,10 +17,11 @@ class ShareModeTest(unittest.TestCase, TorGuiShareTest):
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(False, False)
if __name__ == "__main__":
unittest.main()

View file

@ -4,6 +4,7 @@ import unittest
from .TorGuiShareTest import TorGuiShareTest
class ShareModePersistentPasswordTest(unittest.TestCase, TorGuiShareTest):
@classmethod
def setUpClass(cls):
@ -22,10 +23,11 @@ class ShareModePersistentPasswordTest(unittest.TestCase, TorGuiShareTest):
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_persistent_tests(False, True)
if __name__ == "__main__":
unittest.main()

View file

@ -4,13 +4,11 @@ import unittest
from .TorGuiShareTest import TorGuiShareTest
class ShareModeStealthTest(unittest.TestCase, TorGuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"use_legacy_v2_onions": True,
"use_stealth": True,
}
test_settings = {"use_legacy_v2_onions": True, "use_stealth": True}
cls.gui = TorGuiShareTest.set_up(test_settings)
@classmethod
@ -19,7 +17,7 @@ class ShareModeStealthTest(unittest.TestCase, TorGuiShareTest):
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_setup_tests()
@ -27,5 +25,6 @@ class ShareModeStealthTest(unittest.TestCase, TorGuiShareTest):
self.hidserv_auth_string()
self.copy_have_hidserv_auth_button(self.gui.share_mode)
if __name__ == "__main__":
unittest.main()

View file

@ -4,13 +4,11 @@ import unittest
from .TorGuiShareTest import TorGuiShareTest
class ShareModeTimerTest(unittest.TestCase, TorGuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": False,
"autostop_timer": True,
}
test_settings = {"public_mode": False, "autostop_timer": True}
cls.gui = TorGuiShareTest.set_up(test_settings)
@classmethod
@ -19,10 +17,11 @@ class ShareModeTimerTest(unittest.TestCase, TorGuiShareTest):
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_timer_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -4,16 +4,16 @@ import unittest
from .TorGuiShareTest import TorGuiShareTest
class ShareModeTorConnectionKilledTest(unittest.TestCase, TorGuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
}
test_settings = {}
cls.gui = TorGuiShareTest.set_up(test_settings)
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_setup_tests()

View file

@ -4,12 +4,11 @@ import unittest
from .TorGuiShareTest import TorGuiShareTest
class ShareModeV2OnionTest(unittest.TestCase, TorGuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"use_legacy_v2_onions": True,
}
test_settings = {"use_legacy_v2_onions": True}
cls.gui = TorGuiShareTest.set_up(test_settings)
@classmethod
@ -18,11 +17,12 @@ class ShareModeV2OnionTest(unittest.TestCase, TorGuiShareTest):
@pytest.mark.gui
@pytest.mark.tor
@pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest")
@pytest.mark.skipif(pytest.__version__ < "2.9", reason="requires newer pytest")
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(False, False)
self.have_v2_onion()
if __name__ == "__main__":
unittest.main()

View file

@ -20,7 +20,7 @@ import tempfile
import os
class MockSubprocess():
class MockSubprocess:
def __init__(self):
self.last_call = None

View file

@ -27,14 +27,14 @@ from onionshare.common import Common
class MyOnion:
def __init__(self, stealth=False):
self.auth_string = 'TestHidServAuth'
self.private_key = ''
self.auth_string = "TestHidServAuth"
self.private_key = ""
self.stealth = stealth
self.scheduled_key = None
@staticmethod
def start_onion_service(self, await_publication=True, save_scheduled_key=False):
return 'test_service_id.onion'
return "test_service_id.onion"
@pytest.fixture
@ -65,18 +65,17 @@ class TestOnionShare:
onionshare_obj.set_stealth(False)
onionshare_obj.start_onion_service()
assert 17600 <= onionshare_obj.port <= 17650
assert onionshare_obj.onion_host == 'test_service_id.onion'
assert onionshare_obj.onion_host == "test_service_id.onion"
def test_start_onion_service_stealth(self, onionshare_obj):
onionshare_obj.set_stealth(True)
onionshare_obj.start_onion_service()
assert onionshare_obj.auth_string == 'TestHidServAuth'
assert onionshare_obj.auth_string == "TestHidServAuth"
def test_start_onion_service_local_only(self, onionshare_obj):
onionshare_obj.local_only = True
onionshare_obj.start_onion_service()
assert onionshare_obj.onion_host == '127.0.0.1:{}'.format(
onionshare_obj.port)
assert onionshare_obj.onion_host == "127.0.0.1:{}".format(onionshare_obj.port)
def test_cleanup(self, onionshare_obj, temp_dir_1024, temp_file_1024):
onionshare_obj.cleanup_filenames = [temp_dir_1024, temp_file_1024]

View file

@ -29,37 +29,40 @@ import zipfile
import pytest
LOG_MSG_REGEX = re.compile(r"""
LOG_MSG_REGEX = re.compile(
r"""
^\[Jun\ 06\ 2013\ 11:05:00\]
\ TestModule\.<function\ TestLog\.test_output\.<locals>\.dummy_func
\ at\ 0x[a-f0-9]+>(:\ TEST_MSG)?$""", re.VERBOSE)
PASSWORD_REGEX = re.compile(r'^([a-z]+)(-[a-z]+)?-([a-z]+)(-[a-z]+)?$')
\ at\ 0x[a-f0-9]+>(:\ TEST_MSG)?$""",
re.VERBOSE,
)
PASSWORD_REGEX = re.compile(r"^([a-z]+)(-[a-z]+)?-([a-z]+)(-[a-z]+)?$")
# TODO: Improve the Common tests to test it all as a single class
class TestBuildPassword:
@pytest.mark.parametrize('test_input,expected', (
# VALID, two lowercase words, separated by a hyphen
('syrup-enzyme', True),
('caution-friday', True),
# VALID, two lowercase words, with one hyphenated compound word
('drop-down-thimble', True),
('unmixed-yo-yo', True),
# VALID, two lowercase hyphenated compound words, separated by hyphen
('yo-yo-drop-down', True),
('felt-tip-t-shirt', True),
('hello-world', True),
# INVALID
('Upper-Case', False),
('digits-123', False),
('too-many-hyphens-', False),
('symbols-!@#$%', False)
))
@pytest.mark.parametrize(
"test_input,expected",
(
# VALID, two lowercase words, separated by a hyphen
("syrup-enzyme", True),
("caution-friday", True),
# VALID, two lowercase words, with one hyphenated compound word
("drop-down-thimble", True),
("unmixed-yo-yo", True),
# VALID, two lowercase hyphenated compound words, separated by hyphen
("yo-yo-drop-down", True),
("felt-tip-t-shirt", True),
("hello-world", True),
# INVALID
("Upper-Case", False),
("digits-123", False),
("too-many-hyphens-", False),
("symbols-!@#$%", False),
),
)
def test_build_password_regex(self, test_input, expected):
""" Test that `PASSWORD_REGEX` accounts for the following patterns
@ -92,79 +95,87 @@ class TestDirSize:
class TestEstimatedTimeRemaining:
@pytest.mark.parametrize('test_input,expected', (
((2, 676, 12), '8h14m16s'),
((14, 1049, 30), '1h26m15s'),
((21, 450, 1), '33m42s'),
((31, 1115, 80), '11m39s'),
((336, 989, 32), '2m12s'),
((603, 949, 38), '36s'),
((971, 1009, 83), '1s')
))
@pytest.mark.parametrize(
"test_input,expected",
(
((2, 676, 12), "8h14m16s"),
((14, 1049, 30), "1h26m15s"),
((21, 450, 1), "33m42s"),
((31, 1115, 80), "11m39s"),
((336, 989, 32), "2m12s"),
((603, 949, 38), "36s"),
((971, 1009, 83), "1s"),
),
)
def test_estimated_time_remaining(
self, common_obj, test_input, expected, time_time_100):
self, common_obj, test_input, expected, time_time_100
):
assert common_obj.estimated_time_remaining(*test_input) == expected
@pytest.mark.parametrize('test_input', (
(10, 20, 100), # if `time_elapsed == 0`
(0, 37, 99) # if `download_rate == 0`
))
@pytest.mark.parametrize(
"test_input",
(
(10, 20, 100), # if `time_elapsed == 0`
(0, 37, 99), # if `download_rate == 0`
),
)
def test_raises_zero_division_error(self, common_obj, test_input, time_time_100):
with pytest.raises(ZeroDivisionError):
common_obj.estimated_time_remaining(*test_input)
class TestFormatSeconds:
@pytest.mark.parametrize('test_input,expected', (
(0, '0s'),
(26, '26s'),
(60, '1m'),
(947.35, '15m47s'),
(1847, '30m47s'),
(2193.94, '36m34s'),
(3600, '1h'),
(13426.83, '3h43m47s'),
(16293, '4h31m33s'),
(18392.14, '5h6m32s'),
(86400, '1d'),
(129674, '1d12h1m14s'),
(56404.12, '15h40m4s')
))
@pytest.mark.parametrize(
"test_input,expected",
(
(0, "0s"),
(26, "26s"),
(60, "1m"),
(947.35, "15m47s"),
(1847, "30m47s"),
(2193.94, "36m34s"),
(3600, "1h"),
(13426.83, "3h43m47s"),
(16293, "4h31m33s"),
(18392.14, "5h6m32s"),
(86400, "1d"),
(129674, "1d12h1m14s"),
(56404.12, "15h40m4s"),
),
)
def test_format_seconds(self, common_obj, test_input, expected):
assert common_obj.format_seconds(test_input) == expected
# TODO: test negative numbers?
@pytest.mark.parametrize('test_input', (
'string', lambda: None, [], {}, set()
))
@pytest.mark.parametrize("test_input", ("string", lambda: None, [], {}, set()))
def test_invalid_input_types(self, common_obj, test_input):
with pytest.raises(TypeError):
common_obj.format_seconds(test_input)
class TestGetAvailablePort:
@pytest.mark.parametrize('port_min,port_max', (
(random.randint(1024, 1500),
random.randint(1800, 2048)) for _ in range(50)
))
@pytest.mark.parametrize(
"port_min,port_max",
((random.randint(1024, 1500), random.randint(1800, 2048)) for _ in range(50)),
)
def test_returns_an_open_port(self, common_obj, port_min, port_max):
""" get_available_port() should return an open port within the range """
port = common_obj.get_available_port(port_min, port_max)
assert port_min <= port <= port_max
with socket.socket() as tmpsock:
tmpsock.bind(('127.0.0.1', port))
tmpsock.bind(("127.0.0.1", port))
class TestGetPlatform:
def test_darwin(self, platform_darwin, common_obj):
assert common_obj.platform == 'Darwin'
assert common_obj.platform == "Darwin"
def test_linux(self, platform_linux, common_obj):
assert common_obj.platform == 'Linux'
assert common_obj.platform == "Linux"
def test_windows(self, platform_windows, common_obj):
assert common_obj.platform == 'Windows'
assert common_obj.platform == "Windows"
# TODO: double-check these tests
@ -173,94 +184,114 @@ class TestGetResourcePath:
prefix = os.path.join(
os.path.dirname(
os.path.dirname(
os.path.abspath(
inspect.getfile(
inspect.currentframe())))), 'share')
assert (
common_obj.get_resource_path(os.path.join(prefix, 'test_filename')) ==
os.path.join(prefix, 'test_filename'))
os.path.abspath(inspect.getfile(inspect.currentframe()))
)
),
"share",
)
assert common_obj.get_resource_path(
os.path.join(prefix, "test_filename")
) == os.path.join(prefix, "test_filename")
def test_linux(self, common_obj, platform_linux, sys_argv_sys_prefix):
prefix = os.path.join(sys.prefix, 'share/onionshare')
assert (
common_obj.get_resource_path(os.path.join(prefix, 'test_filename')) ==
os.path.join(prefix, 'test_filename'))
prefix = os.path.join(sys.prefix, "share/onionshare")
assert common_obj.get_resource_path(
os.path.join(prefix, "test_filename")
) == os.path.join(prefix, "test_filename")
def test_frozen_darwin(self, common_obj, platform_darwin, sys_frozen, sys_meipass):
prefix = os.path.join(sys._MEIPASS, 'share')
assert (
common_obj.get_resource_path(os.path.join(prefix, 'test_filename')) ==
os.path.join(prefix, 'test_filename'))
prefix = os.path.join(sys._MEIPASS, "share")
assert common_obj.get_resource_path(
os.path.join(prefix, "test_filename")
) == os.path.join(prefix, "test_filename")
class TestGetTorPaths:
# @pytest.mark.skipif(sys.platform != 'Darwin', reason='requires MacOS') ?
def test_get_tor_paths_darwin(self, platform_darwin, common_obj, sys_frozen, sys_meipass):
def test_get_tor_paths_darwin(
self, platform_darwin, common_obj, sys_frozen, sys_meipass
):
base_path = os.path.dirname(
os.path.dirname(
os.path.dirname(
common_obj.get_resource_path(''))))
tor_path = os.path.join(
base_path, 'Resources', 'Tor', 'tor')
tor_geo_ip_file_path = os.path.join(
base_path, 'Resources', 'Tor', 'geoip')
tor_geo_ipv6_file_path = os.path.join(
base_path, 'Resources', 'Tor', 'geoip6')
obfs4proxy_file_path = os.path.join(
base_path, 'Resources', 'Tor', 'obfs4proxy')
assert (common_obj.get_tor_paths() ==
(tor_path, tor_geo_ip_file_path, tor_geo_ipv6_file_path, obfs4proxy_file_path))
os.path.dirname(os.path.dirname(common_obj.get_resource_path("")))
)
tor_path = os.path.join(base_path, "Resources", "Tor", "tor")
tor_geo_ip_file_path = os.path.join(base_path, "Resources", "Tor", "geoip")
tor_geo_ipv6_file_path = os.path.join(base_path, "Resources", "Tor", "geoip6")
obfs4proxy_file_path = os.path.join(base_path, "Resources", "Tor", "obfs4proxy")
assert common_obj.get_tor_paths() == (
tor_path,
tor_geo_ip_file_path,
tor_geo_ipv6_file_path,
obfs4proxy_file_path,
)
# @pytest.mark.skipif(sys.platform != 'Linux', reason='requires Linux') ?
def test_get_tor_paths_linux(self, platform_linux, common_obj):
assert (common_obj.get_tor_paths() ==
('/usr/bin/tor', '/usr/share/tor/geoip', '/usr/share/tor/geoip6', '/usr/bin/obfs4proxy'))
assert common_obj.get_tor_paths() == (
"/usr/bin/tor",
"/usr/share/tor/geoip",
"/usr/share/tor/geoip6",
"/usr/bin/obfs4proxy",
)
# @pytest.mark.skipif(sys.platform != 'Windows', reason='requires Windows') ?
def test_get_tor_paths_windows(self, platform_windows, common_obj, sys_frozen):
base_path = os.path.join(
os.path.dirname(
os.path.dirname(
common_obj.get_resource_path(''))), 'tor')
tor_path = os.path.join(
os.path.join(base_path, 'Tor'), 'tor.exe')
os.path.dirname(os.path.dirname(common_obj.get_resource_path(""))), "tor"
)
tor_path = os.path.join(os.path.join(base_path, "Tor"), "tor.exe")
obfs4proxy_file_path = os.path.join(
os.path.join(base_path, 'Tor'), 'obfs4proxy.exe')
os.path.join(base_path, "Tor"), "obfs4proxy.exe"
)
tor_geo_ip_file_path = os.path.join(
os.path.join(
os.path.join(base_path, 'Data'), 'Tor'), 'geoip')
os.path.join(os.path.join(base_path, "Data"), "Tor"), "geoip"
)
tor_geo_ipv6_file_path = os.path.join(
os.path.join(
os.path.join(base_path, 'Data'), 'Tor'), 'geoip6')
assert (common_obj.get_tor_paths() ==
(tor_path, tor_geo_ip_file_path, tor_geo_ipv6_file_path, obfs4proxy_file_path))
os.path.join(os.path.join(base_path, "Data"), "Tor"), "geoip6"
)
assert common_obj.get_tor_paths() == (
tor_path,
tor_geo_ip_file_path,
tor_geo_ipv6_file_path,
obfs4proxy_file_path,
)
class TestHumanReadableFilesize:
@pytest.mark.parametrize('test_input,expected', (
(1024 ** 0, '1.0 B'),
(1024 ** 1, '1.0 KiB'),
(1024 ** 2, '1.0 MiB'),
(1024 ** 3, '1.0 GiB'),
(1024 ** 4, '1.0 TiB'),
(1024 ** 5, '1.0 PiB'),
(1024 ** 6, '1.0 EiB'),
(1024 ** 7, '1.0 ZiB'),
(1024 ** 8, '1.0 YiB')
))
@pytest.mark.parametrize(
"test_input,expected",
(
(1024 ** 0, "1.0 B"),
(1024 ** 1, "1.0 KiB"),
(1024 ** 2, "1.0 MiB"),
(1024 ** 3, "1.0 GiB"),
(1024 ** 4, "1.0 TiB"),
(1024 ** 5, "1.0 PiB"),
(1024 ** 6, "1.0 EiB"),
(1024 ** 7, "1.0 ZiB"),
(1024 ** 8, "1.0 YiB"),
),
)
def test_human_readable_filesize(self, common_obj, test_input, expected):
assert common_obj.human_readable_filesize(test_input) == expected
class TestLog:
@pytest.mark.parametrize('test_input', (
('[Jun 06 2013 11:05:00]'
' TestModule.<function TestLog.test_output.<locals>.dummy_func'
' at 0xdeadbeef>'),
('[Jun 06 2013 11:05:00]'
' TestModule.<function TestLog.test_output.<locals>.dummy_func'
' at 0xdeadbeef>: TEST_MSG')
))
@pytest.mark.parametrize(
"test_input",
(
(
"[Jun 06 2013 11:05:00]"
" TestModule.<function TestLog.test_output.<locals>.dummy_func"
" at 0xdeadbeef>"
),
(
"[Jun 06 2013 11:05:00]"
" TestModule.<function TestLog.test_output.<locals>.dummy_func"
" at 0xdeadbeef>: TEST_MSG"
),
),
)
def test_log_msg_regex(self, test_input):
assert bool(LOG_MSG_REGEX.match(test_input))
@ -272,10 +303,10 @@ class TestLog:
# From: https://stackoverflow.com/questions/1218933
with io.StringIO() as buf, contextlib.redirect_stdout(buf):
common_obj.log('TestModule', dummy_func)
common_obj.log('TestModule', dummy_func, 'TEST_MSG')
common_obj.log("TestModule", dummy_func)
common_obj.log("TestModule", dummy_func, "TEST_MSG")
output = buf.getvalue()
line_one, line_two, _ = output.split('\n')
line_one, line_two, _ = output.split("\n")
assert LOG_MSG_REGEX.match(line_one)
assert LOG_MSG_REGEX.match(line_two)

View file

@ -28,86 +28,86 @@ from onionshare import common, settings, strings
@pytest.fixture
def os_path_expanduser(monkeypatch):
monkeypatch.setattr('os.path.expanduser', lambda path: path)
monkeypatch.setattr("os.path.expanduser", lambda path: path)
@pytest.fixture
def settings_obj(sys_onionshare_dev_mode, platform_linux):
_common = common.Common()
_common.version = 'DUMMY_VERSION_1.2.3'
_common.version = "DUMMY_VERSION_1.2.3"
return settings.Settings(_common)
class TestSettings:
def test_init(self, settings_obj):
expected_settings = {
'version': 'DUMMY_VERSION_1.2.3',
'connection_type': 'bundled',
'control_port_address': '127.0.0.1',
'control_port_port': 9051,
'socks_address': '127.0.0.1',
'socks_port': 9050,
'socket_file_path': '/var/run/tor/control',
'auth_type': 'no_auth',
'auth_password': '',
'close_after_first_download': True,
'autostop_timer': False,
'autostart_timer': False,
'use_stealth': False,
'use_autoupdate': True,
'autoupdate_timestamp': None,
'no_bridges': True,
'tor_bridges_use_obfs4': False,
'tor_bridges_use_meek_lite_azure': False,
'tor_bridges_use_custom_bridges': '',
'use_legacy_v2_onions': False,
'save_private_key': False,
'private_key': '',
'password': '',
'hidservauth_string': '',
'data_dir': os.path.expanduser('~/OnionShare'),
'public_mode': False,
'csp_header_disabled': False
"version": "DUMMY_VERSION_1.2.3",
"connection_type": "bundled",
"control_port_address": "127.0.0.1",
"control_port_port": 9051,
"socks_address": "127.0.0.1",
"socks_port": 9050,
"socket_file_path": "/var/run/tor/control",
"auth_type": "no_auth",
"auth_password": "",
"close_after_first_download": True,
"autostop_timer": False,
"autostart_timer": False,
"use_stealth": False,
"use_autoupdate": True,
"autoupdate_timestamp": None,
"no_bridges": True,
"tor_bridges_use_obfs4": False,
"tor_bridges_use_meek_lite_azure": False,
"tor_bridges_use_custom_bridges": "",
"use_legacy_v2_onions": False,
"save_private_key": False,
"private_key": "",
"password": "",
"hidservauth_string": "",
"data_dir": os.path.expanduser("~/OnionShare"),
"public_mode": False,
"csp_header_disabled": False,
}
for key in settings_obj._settings:
# Skip locale, it will not always default to the same thing
if key != 'locale':
if key != "locale":
assert settings_obj._settings[key] == settings_obj.default_settings[key]
assert settings_obj._settings[key] == expected_settings[key]
def test_fill_in_defaults(self, settings_obj):
del settings_obj._settings['version']
del settings_obj._settings["version"]
settings_obj.fill_in_defaults()
assert settings_obj._settings['version'] == 'DUMMY_VERSION_1.2.3'
assert settings_obj._settings["version"] == "DUMMY_VERSION_1.2.3"
def test_load(self, settings_obj):
custom_settings = {
'version': 'CUSTOM_VERSION',
'socks_port': 9999,
'use_stealth': True
"version": "CUSTOM_VERSION",
"socks_port": 9999,
"use_stealth": True,
}
tmp_file, tmp_file_path = tempfile.mkstemp()
with open(tmp_file, 'w') as f:
with open(tmp_file, "w") as f:
json.dump(custom_settings, f)
settings_obj.filename = tmp_file_path
settings_obj.load()
assert settings_obj._settings['version'] == 'CUSTOM_VERSION'
assert settings_obj._settings['socks_port'] == 9999
assert settings_obj._settings['use_stealth'] is True
assert settings_obj._settings["version"] == "CUSTOM_VERSION"
assert settings_obj._settings["socks_port"] == 9999
assert settings_obj._settings["use_stealth"] is True
os.remove(tmp_file_path)
assert os.path.exists(tmp_file_path) is False
def test_save(self, monkeypatch, settings_obj):
monkeypatch.setattr(strings, '_', lambda _: '')
monkeypatch.setattr(strings, "_", lambda _: "")
settings_filename = 'default_settings.json'
settings_filename = "default_settings.json"
tmp_dir = tempfile.gettempdir()
settings_path = os.path.join(tmp_dir, settings_filename)
settings_obj.filename = settings_path
settings_obj.save()
with open(settings_path, 'r') as f:
with open(settings_path, "r") as f:
settings = json.load(f)
assert settings_obj._settings == settings
@ -116,69 +116,64 @@ class TestSettings:
assert os.path.exists(settings_path) is False
def test_get(self, settings_obj):
assert settings_obj.get('version') == 'DUMMY_VERSION_1.2.3'
assert settings_obj.get('connection_type') == 'bundled'
assert settings_obj.get('control_port_address') == '127.0.0.1'
assert settings_obj.get('control_port_port') == 9051
assert settings_obj.get('socks_address') == '127.0.0.1'
assert settings_obj.get('socks_port') == 9050
assert settings_obj.get('socket_file_path') == '/var/run/tor/control'
assert settings_obj.get('auth_type') == 'no_auth'
assert settings_obj.get('auth_password') == ''
assert settings_obj.get('close_after_first_download') is True
assert settings_obj.get('use_stealth') is False
assert settings_obj.get('use_autoupdate') is True
assert settings_obj.get('autoupdate_timestamp') is None
assert settings_obj.get('autoupdate_timestamp') is None
assert settings_obj.get('no_bridges') is True
assert settings_obj.get('tor_bridges_use_obfs4') is False
assert settings_obj.get('tor_bridges_use_meek_lite_azure') is False
assert settings_obj.get('tor_bridges_use_custom_bridges') == ''
assert settings_obj.get("version") == "DUMMY_VERSION_1.2.3"
assert settings_obj.get("connection_type") == "bundled"
assert settings_obj.get("control_port_address") == "127.0.0.1"
assert settings_obj.get("control_port_port") == 9051
assert settings_obj.get("socks_address") == "127.0.0.1"
assert settings_obj.get("socks_port") == 9050
assert settings_obj.get("socket_file_path") == "/var/run/tor/control"
assert settings_obj.get("auth_type") == "no_auth"
assert settings_obj.get("auth_password") == ""
assert settings_obj.get("close_after_first_download") is True
assert settings_obj.get("use_stealth") is False
assert settings_obj.get("use_autoupdate") is True
assert settings_obj.get("autoupdate_timestamp") is None
assert settings_obj.get("autoupdate_timestamp") is None
assert settings_obj.get("no_bridges") is True
assert settings_obj.get("tor_bridges_use_obfs4") is False
assert settings_obj.get("tor_bridges_use_meek_lite_azure") is False
assert settings_obj.get("tor_bridges_use_custom_bridges") == ""
def test_set_version(self, settings_obj):
settings_obj.set('version', 'CUSTOM_VERSION')
assert settings_obj._settings['version'] == 'CUSTOM_VERSION'
settings_obj.set("version", "CUSTOM_VERSION")
assert settings_obj._settings["version"] == "CUSTOM_VERSION"
def test_set_control_port_port(self, settings_obj):
settings_obj.set('control_port_port', 999)
assert settings_obj._settings['control_port_port'] == 999
settings_obj.set("control_port_port", 999)
assert settings_obj._settings["control_port_port"] == 999
settings_obj.set('control_port_port', 'NON_INTEGER')
assert settings_obj._settings['control_port_port'] == 9051
settings_obj.set("control_port_port", "NON_INTEGER")
assert settings_obj._settings["control_port_port"] == 9051
def test_set_socks_port(self, settings_obj):
settings_obj.set('socks_port', 888)
assert settings_obj._settings['socks_port'] == 888
settings_obj.set("socks_port", 888)
assert settings_obj._settings["socks_port"] == 888
settings_obj.set('socks_port', 'NON_INTEGER')
assert settings_obj._settings['socks_port'] == 9050
settings_obj.set("socks_port", "NON_INTEGER")
assert settings_obj._settings["socks_port"] == 9050
def test_filename_darwin(
self,
monkeypatch,
os_path_expanduser,
platform_darwin):
def test_filename_darwin(self, monkeypatch, os_path_expanduser, platform_darwin):
obj = settings.Settings(common.Common())
assert (obj.filename ==
'~/Library/Application Support/OnionShare/onionshare.json')
assert (
obj.filename == "~/Library/Application Support/OnionShare/onionshare.json"
)
def test_filename_linux(
self,
monkeypatch,
os_path_expanduser,
platform_linux):
def test_filename_linux(self, monkeypatch, os_path_expanduser, platform_linux):
obj = settings.Settings(common.Common())
assert obj.filename == '~/.config/onionshare/onionshare.json'
assert obj.filename == "~/.config/onionshare/onionshare.json"
def test_filename_windows(
self,
monkeypatch,
platform_windows):
monkeypatch.setenv('APPDATA', 'C:')
def test_filename_windows(self, monkeypatch, platform_windows):
monkeypatch.setenv("APPDATA", "C:")
obj = settings.Settings(common.Common())
assert obj.filename.replace('/', '\\') == 'C:\\OnionShare\\onionshare.json'
assert obj.filename.replace("/", "\\") == "C:\\OnionShare\\onionshare.json"
def test_set_custom_bridge(self, settings_obj):
settings_obj.set('tor_bridges_use_custom_bridges', 'Bridge 45.3.20.65:9050 21300AD88890A49C429A6CB9959CFD44490A8F6E')
assert settings_obj._settings['tor_bridges_use_custom_bridges'] == 'Bridge 45.3.20.65:9050 21300AD88890A49C429A6CB9959CFD44490A8F6E'
settings_obj.set(
"tor_bridges_use_custom_bridges",
"Bridge 45.3.20.65:9050 21300AD88890A49C429A6CB9959CFD44490A8F6E",
)
assert (
settings_obj._settings["tor_bridges_use_custom_bridges"]
== "Bridge 45.3.20.65:9050 21300AD88890A49C429A6CB9959CFD44490A8F6E"
)

View file

@ -32,31 +32,34 @@ from onionshare.settings import Settings
# return path
# common.get_resource_path = get_resource_path
def test_underscore_is_function():
assert callable(strings._) and isinstance(strings._, types.FunctionType)
class TestLoadStrings:
def test_load_strings_defaults_to_english(
self, common_obj, locale_en, sys_onionshare_dev_mode):
self, common_obj, locale_en, sys_onionshare_dev_mode
):
""" load_strings() loads English by default """
common_obj.settings = Settings(common_obj)
strings.load_strings(common_obj)
assert strings._('preparing_files') == "Compressing files."
assert strings._("preparing_files") == "Compressing files."
def test_load_strings_loads_other_languages(
self, common_obj, locale_fr, sys_onionshare_dev_mode):
self, common_obj, locale_fr, sys_onionshare_dev_mode
):
""" load_strings() loads other languages in different locales """
common_obj.settings = Settings(common_obj)
common_obj.settings.set('locale', 'fr')
common_obj.settings.set("locale", "fr")
strings.load_strings(common_obj)
assert strings._('preparing_files') == "Compression des fichiers."
assert strings._("preparing_files") == "Compression des fichiers."
def test_load_invalid_locale(
self, common_obj, locale_invalid, sys_onionshare_dev_mode):
self, common_obj, locale_invalid, sys_onionshare_dev_mode
):
""" load_strings() raises a KeyError for an invalid locale """
with pytest.raises(KeyError):
common_obj.settings = Settings(common_obj)
common_obj.settings.set('locale', 'XX')
common_obj.settings.set("locale", "XX")
strings.load_strings(common_obj)

View file

@ -37,8 +37,8 @@ from onionshare import strings
from onionshare.web import Web
from onionshare.settings import Settings
DEFAULT_ZW_FILENAME_REGEX = re.compile(r'^onionshare_[a-z2-7]{6}.zip$')
RANDOM_STR_REGEX = re.compile(r'^[a-z2-7]+$')
DEFAULT_ZW_FILENAME_REGEX = re.compile(r"^onionshare_[a-z2-7]{6}.zip$")
RANDOM_STR_REGEX = re.compile(r"^[a-z2-7]+$")
def web_obj(common_obj, mode, num_files=0):
@ -53,12 +53,12 @@ def web_obj(common_obj, mode, num_files=0):
web.app.testing = True
# Share mode
if mode == 'share':
if mode == "share":
# Add files
files = []
for i in range(num_files):
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
tmp_file.write(b'*' * 1024)
tmp_file.write(b"*" * 1024)
files.append(tmp_file.name)
web.share_mode.set_file_info(files)
# Receive mode
@ -70,122 +70,130 @@ def web_obj(common_obj, mode, num_files=0):
class TestWeb:
def test_share_mode(self, common_obj):
web = web_obj(common_obj, 'share', 3)
assert web.mode is 'share'
web = web_obj(common_obj, "share", 3)
assert web.mode is "share"
with web.app.test_client() as c:
# Load / without auth
res = c.get('/')
res = c.get("/")
res.get_data()
assert res.status_code == 401
# Load / with invalid auth
res = c.get('/', headers=self._make_auth_headers('invalid'))
res = c.get("/", headers=self._make_auth_headers("invalid"))
res.get_data()
assert res.status_code == 401
# Load / with valid auth
res = c.get('/', headers=self._make_auth_headers(web.password))
res = c.get("/", headers=self._make_auth_headers(web.password))
res.get_data()
assert res.status_code == 200
# Download
res = c.get('/download', headers=self._make_auth_headers(web.password))
res = c.get("/download", headers=self._make_auth_headers(web.password))
res.get_data()
assert res.status_code == 200
assert res.mimetype == 'application/zip'
assert res.mimetype == "application/zip"
def test_share_mode_close_after_first_download_on(self, common_obj, temp_file_1024):
web = web_obj(common_obj, 'share', 3)
web = web_obj(common_obj, "share", 3)
web.stay_open = False
assert web.running == True
with web.app.test_client() as c:
# Download the first time
res = c.get('/download', headers=self._make_auth_headers(web.password))
res = c.get("/download", headers=self._make_auth_headers(web.password))
res.get_data()
assert res.status_code == 200
assert res.mimetype == 'application/zip'
assert res.mimetype == "application/zip"
assert web.running == False
def test_share_mode_close_after_first_download_off(self, common_obj, temp_file_1024):
web = web_obj(common_obj, 'share', 3)
def test_share_mode_close_after_first_download_off(
self, common_obj, temp_file_1024
):
web = web_obj(common_obj, "share", 3)
web.stay_open = True
assert web.running == True
with web.app.test_client() as c:
# Download the first time
res = c.get('/download', headers=self._make_auth_headers(web.password))
res = c.get("/download", headers=self._make_auth_headers(web.password))
res.get_data()
assert res.status_code == 200
assert res.mimetype == 'application/zip'
assert res.mimetype == "application/zip"
assert web.running == True
def test_receive_mode(self, common_obj):
web = web_obj(common_obj, 'receive')
assert web.mode is 'receive'
web = web_obj(common_obj, "receive")
assert web.mode is "receive"
with web.app.test_client() as c:
# Load / without auth
res = c.get('/')
res = c.get("/")
res.get_data()
assert res.status_code == 401
# Load / with invalid auth
res = c.get('/', headers=self._make_auth_headers('invalid'))
res = c.get("/", headers=self._make_auth_headers("invalid"))
res.get_data()
assert res.status_code == 401
# Load / with valid auth
res = c.get('/', headers=self._make_auth_headers(web.password))
res = c.get("/", headers=self._make_auth_headers(web.password))
res.get_data()
assert res.status_code == 200
def test_public_mode_on(self, common_obj):
web = web_obj(common_obj, 'receive')
common_obj.settings.set('public_mode', True)
web = web_obj(common_obj, "receive")
common_obj.settings.set("public_mode", True)
with web.app.test_client() as c:
# Loading / should work without auth
res = c.get('/')
res = c.get("/")
data1 = res.get_data()
assert res.status_code == 200
def test_public_mode_off(self, common_obj):
web = web_obj(common_obj, 'receive')
common_obj.settings.set('public_mode', False)
web = web_obj(common_obj, "receive")
common_obj.settings.set("public_mode", False)
with web.app.test_client() as c:
# Load / without auth
res = c.get('/')
res = c.get("/")
res.get_data()
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("{}/css/style.css".format(web.static_url_path))
res.get_data()
assert res.status_code == 200
# Load / with valid auth
res = c.get('/', headers=self._make_auth_headers(web.password))
res = c.get("/", headers=self._make_auth_headers(web.password))
res.get_data()
assert res.status_code == 200
def _make_auth_headers(self, password):
auth = base64.b64encode(b'onionshare:'+password.encode()).decode()
auth = base64.b64encode(b"onionshare:" + password.encode()).decode()
h = Headers()
h.add('Authorization', 'Basic ' + auth)
h.add("Authorization", "Basic " + auth)
return h
class TestZipWriterDefault:
@pytest.mark.parametrize('test_input', (
'onionshare_{}.zip'.format(''.join(
random.choice('abcdefghijklmnopqrstuvwxyz234567') for _ in range(6)
)) for _ in range(50)
))
@pytest.mark.parametrize(
"test_input",
(
"onionshare_{}.zip".format(
"".join(
random.choice("abcdefghijklmnopqrstuvwxyz234567") for _ in range(6)
)
)
for _ in range(50)
),
)
def test_default_zw_filename_regex(self, test_input):
assert bool(DEFAULT_ZW_FILENAME_REGEX.match(test_input))
@ -200,15 +208,14 @@ class TestZipWriterDefault:
assert default_zw.z._allowZip64 is True
def test_zipfile_mode(self, default_zw):
assert default_zw.z.mode == 'w'
assert default_zw.z.mode == "w"
def test_callback(self, default_zw):
assert default_zw.processed_size_callback(None) is None
def test_add_file(self, default_zw, temp_file_1024_delete):
default_zw.add_file(temp_file_1024_delete)
zipfile_info = default_zw.z.getinfo(
os.path.basename(temp_file_1024_delete))
zipfile_info = default_zw.z.getinfo(os.path.basename(temp_file_1024_delete))
assert zipfile_info.compress_type == zipfile.ZIP_DEFLATED
assert zipfile_info.file_size == 1024
@ -220,12 +227,15 @@ class TestZipWriterDefault:
class TestZipWriterCustom:
@pytest.mark.parametrize('test_input', (
Common.random_string(
random.randint(2, 50),
random.choice((None, random.randint(2, 50)))
) for _ in range(50)
))
@pytest.mark.parametrize(
"test_input",
(
Common.random_string(
random.randint(2, 50), random.choice((None, random.randint(2, 50)))
)
for _ in range(50)
),
)
def test_random_string_regex(self, test_input):
assert bool(RANDOM_STR_REGEX.match(test_input))
@ -233,4 +243,4 @@ class TestZipWriterCustom:
assert bool(RANDOM_STR_REGEX.match(custom_zw.zip_filename))
def test_custom_callback(self, custom_zw):
assert custom_zw.processed_size_callback(None) == 'custom_callback'
assert custom_zw.processed_size_callback(None) == "custom_callback"