mirror of
https://github.com/onionshare/onionshare.git
synced 2024-10-01 01:35:40 -04:00
Remove useless CLI stuff from GUI tests, and refactor so that qtapp is a singleton and all tests can be run at once
This commit is contained in:
parent
06eb12cf8f
commit
f08e6e9830
@ -1,14 +1,9 @@
|
||||
import sys
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import pytest
|
||||
from PySide2 import QtTest
|
||||
|
||||
from onionshare_cli import common, web, settings
|
||||
|
||||
|
||||
# Force tests to look for resources in the source code tree
|
||||
sys.onionshare_dev_mode = True
|
||||
@ -37,180 +32,13 @@ sys.path.insert(
|
||||
),
|
||||
)
|
||||
|
||||
# The temporary directory for CLI tests
|
||||
test_temp_dir = None
|
||||
# Create common and qtapp singletons
|
||||
from onionshare_cli.common import Common
|
||||
from onionshare import Application
|
||||
|
||||
common = Common(verbose=True)
|
||||
qtapp = Application(common)
|
||||
|
||||
def pytest_addoption(parser):
|
||||
parser.addoption(
|
||||
"--runtor", action="store_true", default=False, help="run tor tests"
|
||||
)
|
||||
|
||||
|
||||
def pytest_collection_modifyitems(config, items):
|
||||
if not config.getoption("--runtor"):
|
||||
# --runtor given in cli: do not skip tor tests
|
||||
skip_tor = pytest.mark.skip(reason="need --runtor option to run")
|
||||
for item in items:
|
||||
if "tor" in item.keywords:
|
||||
item.add_marker(skip_tor)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_dir():
|
||||
"""Creates a persistent temporary directory for the CLI tests to use"""
|
||||
global test_temp_dir
|
||||
if not test_temp_dir:
|
||||
test_temp_dir = tempfile.mkdtemp()
|
||||
return test_temp_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_dir_1024(temp_dir):
|
||||
"""Create a temporary directory that has a single file of a
|
||||
particular size (1024 bytes).
|
||||
"""
|
||||
|
||||
new_temp_dir = tempfile.mkdtemp(dir=temp_dir)
|
||||
tmp_file, tmp_file_path = tempfile.mkstemp(dir=new_temp_dir)
|
||||
with open(tmp_file, "wb") as f:
|
||||
f.write(b"*" * 1024)
|
||||
return new_temp_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_dir_1024_delete(temp_dir):
|
||||
"""Create a temporary directory that has a single file of a
|
||||
particular size (1024 bytes). The temporary directory (including
|
||||
the file inside) will be deleted after fixture usage.
|
||||
"""
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=temp_dir) as new_temp_dir:
|
||||
tmp_file, tmp_file_path = tempfile.mkstemp(dir=new_temp_dir)
|
||||
with open(tmp_file, "wb") as f:
|
||||
f.write(b"*" * 1024)
|
||||
yield new_temp_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_file_1024(temp_dir):
|
||||
"""Create a temporary file of a particular size (1024 bytes)."""
|
||||
|
||||
with tempfile.NamedTemporaryFile(delete=False, dir=temp_dir) as tmp_file:
|
||||
tmp_file.write(b"*" * 1024)
|
||||
return tmp_file.name
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_file_1024_delete(temp_dir):
|
||||
"""
|
||||
Create a temporary file of a particular size (1024 bytes).
|
||||
The temporary file will be deleted after fixture usage.
|
||||
"""
|
||||
|
||||
with tempfile.NamedTemporaryFile(dir=temp_dir, delete=False) as tmp_file:
|
||||
tmp_file.write(b"*" * 1024)
|
||||
tmp_file.flush()
|
||||
tmp_file.close()
|
||||
yield tmp_file.name
|
||||
|
||||
|
||||
@pytest.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",
|
||||
)
|
||||
yield zw
|
||||
zw.close()
|
||||
os.remove(zw.zip_filename)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def default_zw():
|
||||
zw = web.share_mode.ZipWriter(common.Common())
|
||||
yield zw
|
||||
zw.close()
|
||||
tmp_dir = os.path.dirname(zw.zip_filename)
|
||||
try:
|
||||
shutil.rmtree(tmp_dir, ignore_errors=True)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def locale_en(monkeypatch):
|
||||
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("en_US", "UTF-8"))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def locale_fr(monkeypatch):
|
||||
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("fr_FR", "UTF-8"))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def locale_invalid(monkeypatch):
|
||||
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("xx_XX", "UTF-8"))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def locale_ru(monkeypatch):
|
||||
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("ru_RU", "UTF-8"))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def platform_darwin(monkeypatch):
|
||||
monkeypatch.setattr("platform.system", lambda: "Darwin")
|
||||
|
||||
|
||||
@pytest.fixture # (scope="session")
|
||||
def platform_linux(monkeypatch):
|
||||
monkeypatch.setattr("platform.system", lambda: "Linux")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def platform_windows(monkeypatch):
|
||||
monkeypatch.setattr("platform.system", lambda: "Windows")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sys_argv_sys_prefix(monkeypatch):
|
||||
monkeypatch.setattr("sys.argv", [sys.prefix])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sys_frozen(monkeypatch):
|
||||
monkeypatch.setattr("sys.frozen", True, raising=False)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sys_meipass(monkeypatch):
|
||||
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)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def time_time_100(monkeypatch):
|
||||
monkeypatch.setattr("time.time", lambda: 100)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def time_strftime(monkeypatch):
|
||||
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"
|
||||
return settings.Settings(_common)
|
||||
# Attach them to sys, so GuiBaseTest can retrieve them
|
||||
sys.onionshare_common = common
|
||||
sys.onionshare_qtapp = qtapp
|
||||
|
@ -5,6 +5,7 @@ import shutil
|
||||
import tempfile
|
||||
import secrets
|
||||
import platform
|
||||
import sys
|
||||
|
||||
from PySide2 import QtCore, QtTest, QtWidgets
|
||||
|
||||
@ -21,12 +22,12 @@ from onionshare import strings
|
||||
class GuiBaseTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
common = Common(verbose=True)
|
||||
common = sys.onionshare_common
|
||||
qtapp = sys.onionshare_qtapp
|
||||
|
||||
# Delete any old test data that might exist
|
||||
shutil.rmtree(common.build_data_dir(), ignore_errors=True)
|
||||
|
||||
qtapp = Application(common)
|
||||
common.gui = GuiCommon(common, qtapp, local_only=True)
|
||||
cls.gui = MainWindow(common, filenames=None)
|
||||
cls.gui.qtapp = qtapp
|
||||
@ -294,23 +295,42 @@ class GuiBaseTest(unittest.TestCase):
|
||||
if not tab.settings.get("general", "public"):
|
||||
# Both the private key field and the toggle button should be seen
|
||||
self.assertTrue(tab.get_mode().server_status.private_key.isVisible())
|
||||
self.assertTrue(tab.get_mode().server_status.client_auth_toggle_button.isVisible())
|
||||
self.assertEqual(tab.get_mode().server_status.client_auth_toggle_button.text(), strings._("gui_reveal"))
|
||||
self.assertTrue(
|
||||
tab.get_mode().server_status.client_auth_toggle_button.isVisible()
|
||||
)
|
||||
self.assertEqual(
|
||||
tab.get_mode().server_status.client_auth_toggle_button.text(),
|
||||
strings._("gui_reveal"),
|
||||
)
|
||||
|
||||
# Test that the key is masked unless Reveal is clicked
|
||||
self.assertEqual(tab.get_mode().server_status.private_key.text(), "*" * len(tab.app.auth_string))
|
||||
self.assertEqual(
|
||||
tab.get_mode().server_status.private_key.text(),
|
||||
"*" * len(tab.app.auth_string),
|
||||
)
|
||||
|
||||
# Click reveal
|
||||
tab.get_mode().server_status.client_auth_toggle_button.click()
|
||||
|
||||
# The real private key should be revealed
|
||||
self.assertEqual(tab.get_mode().server_status.private_key.text(), tab.app.auth_string)
|
||||
self.assertEqual(tab.get_mode().server_status.client_auth_toggle_button.text(), strings._("gui_hide"))
|
||||
self.assertEqual(
|
||||
tab.get_mode().server_status.private_key.text(), tab.app.auth_string
|
||||
)
|
||||
self.assertEqual(
|
||||
tab.get_mode().server_status.client_auth_toggle_button.text(),
|
||||
strings._("gui_hide"),
|
||||
)
|
||||
|
||||
# Click hide, key should be masked again
|
||||
tab.get_mode().server_status.client_auth_toggle_button.click()
|
||||
self.assertEqual(tab.get_mode().server_status.private_key.text(), "*" * len(tab.app.auth_string))
|
||||
self.assertEqual(tab.get_mode().server_status.client_auth_toggle_button.text(), strings._("gui_reveal"))
|
||||
self.assertEqual(
|
||||
tab.get_mode().server_status.private_key.text(),
|
||||
"*" * len(tab.app.auth_string),
|
||||
)
|
||||
self.assertEqual(
|
||||
tab.get_mode().server_status.client_auth_toggle_button.text(),
|
||||
strings._("gui_reveal"),
|
||||
)
|
||||
else:
|
||||
self.assertFalse(tab.get_mode().server_status.private_key.isVisible())
|
||||
|
||||
@ -429,7 +449,6 @@ class GuiBaseTest(unittest.TestCase):
|
||||
tab.get_mode().server_status.copy_client_auth_button.isVisible()
|
||||
)
|
||||
|
||||
|
||||
def web_server_is_stopped(self, tab):
|
||||
"""Test that the web server also stopped"""
|
||||
QtTest.QTest.qWait(800, self.gui.qtapp)
|
||||
@ -515,7 +534,9 @@ class GuiBaseTest(unittest.TestCase):
|
||||
)
|
||||
tab.get_mode().server_status.copy_client_auth_button.click()
|
||||
clipboard = tab.common.gui.qtapp.clipboard()
|
||||
self.assertEqual(clipboard.text(), "E2GOT5LTUTP3OAMRCRXO4GSH6VKJEUOXZQUC336SRKAHTTT5OVSA")
|
||||
self.assertEqual(
|
||||
clipboard.text(), "E2GOT5LTUTP3OAMRCRXO4GSH6VKJEUOXZQUC336SRKAHTTT5OVSA"
|
||||
)
|
||||
|
||||
def clientauth_is_not_visible(self, tab):
|
||||
"""Test that the ClientAuth button is not visible"""
|
||||
@ -523,19 +544,19 @@ class GuiBaseTest(unittest.TestCase):
|
||||
tab.get_mode().server_status.copy_client_auth_button.isVisible()
|
||||
)
|
||||
|
||||
def hit_405(self, url, expected_resp, data = {}, methods = [] ):
|
||||
def hit_405(self, url, expected_resp, data={}, methods=[]):
|
||||
"""Test various HTTP methods and the response"""
|
||||
for method in methods:
|
||||
if method == "put":
|
||||
r = requests.put(url, data = data)
|
||||
r = requests.put(url, data=data)
|
||||
if method == "post":
|
||||
r = requests.post(url, data = data)
|
||||
r = requests.post(url, data=data)
|
||||
if method == "delete":
|
||||
r = requests.delete(url)
|
||||
if method == "options":
|
||||
r = requests.options(url)
|
||||
self.assertTrue(expected_resp in r.text)
|
||||
self.assertFalse('Werkzeug' in r.headers)
|
||||
self.assertFalse("Werkzeug" in r.headers)
|
||||
|
||||
# Grouped tests follow from here
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user