2018-02-10 15:57:05 -05:00
|
|
|
import sys
|
2017-07-08 00:51:09 -04:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-10-13 01:40:55 -04:00
|
|
|
from onionshare_cli import common, web
|
2017-07-08 00:51:09 -04:00
|
|
|
|
2021-04-29 20:13:05 -04:00
|
|
|
# Force tests to look for resources in the source code tree
|
|
|
|
sys.onionshare_dev_mode = True
|
|
|
|
|
|
|
|
# Let OnionShare know the tests are running, to avoid colliding with settings files
|
|
|
|
sys.onionshare_test_mode = True
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2019-11-10 16:55:15 -05:00
|
|
|
# The temporary directory for CLI tests
|
|
|
|
test_temp_dir = None
|
|
|
|
|
|
|
|
|
2018-10-12 19:35:09 -04:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption(
|
|
|
|
"--runtor", action="store_true", default=False, help="run tor tests"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_collection_modifyitems(config, items):
|
2019-01-16 00:52:42 -05:00
|
|
|
if not config.getoption("--runtor"):
|
2018-10-12 19:35:09 -04:00
|
|
|
# --runtor given in cli: do not skip tor tests
|
2019-01-16 00:52:42 -05:00
|
|
|
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)
|
|
|
|
|
2018-10-12 19:35:09 -04:00
|
|
|
|
2017-07-08 13:29:19 -04:00
|
|
|
@pytest.fixture
|
2019-11-10 16:55:15 -05:00
|
|
|
def temp_dir():
|
|
|
|
"""Creates a persistent temporary directory for the CLI tests to use"""
|
|
|
|
global test_temp_dir
|
|
|
|
if not test_temp_dir:
|
2021-12-02 00:01:32 -05:00
|
|
|
test_temp_dir = tempfile.TemporaryDirectory()
|
2019-11-10 16:55:15 -05:00
|
|
|
return test_temp_dir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def temp_dir_1024(temp_dir):
|
2021-04-29 20:13:05 -04:00
|
|
|
"""Create a temporary directory that has a single file of a
|
2017-07-08 01:16:37 -04:00
|
|
|
particular size (1024 bytes).
|
2017-07-08 00:51:09 -04:00
|
|
|
"""
|
2017-07-08 01:16:37 -04:00
|
|
|
|
2021-12-02 00:01:32 -05:00
|
|
|
new_temp_dir = tempfile.TemporaryDirectory(dir=temp_dir.name)
|
|
|
|
tmp_file = tempfile.NamedTemporaryFile(dir=new_temp_dir.name)
|
|
|
|
tmp_file.write(b"*" * 1024)
|
2019-11-10 16:55:15 -05:00
|
|
|
return new_temp_dir
|
2017-07-08 01:16:37 -04:00
|
|
|
|
|
|
|
|
2021-05-02 18:33:42 -04:00
|
|
|
@pytest.fixture
|
2019-11-10 16:55:15 -05:00
|
|
|
def temp_dir_1024_delete(temp_dir):
|
2021-04-29 20:13:05 -04:00
|
|
|
"""Create a temporary directory that has a single file of a
|
2017-07-08 01:16:37 -04:00
|
|
|
particular size (1024 bytes). The temporary directory (including
|
|
|
|
the file inside) will be deleted after fixture usage.
|
2017-07-08 00:51:09 -04:00
|
|
|
"""
|
|
|
|
|
2021-12-02 00:01:32 -05:00
|
|
|
with tempfile.TemporaryDirectory(dir=temp_dir.name) as new_temp_dir:
|
|
|
|
with open(os.path.join(new_temp_dir, "file"), "wb") as f:
|
2019-10-13 00:01:25 -04:00
|
|
|
f.write(b"*" * 1024)
|
2019-11-10 16:55:15 -05:00
|
|
|
yield new_temp_dir
|
2017-07-08 00:51:09 -04:00
|
|
|
|
|
|
|
|
2017-07-08 13:29:19 -04:00
|
|
|
@pytest.fixture
|
2019-11-10 16:55:15 -05:00
|
|
|
def temp_file_1024(temp_dir):
|
2021-04-29 20:13:05 -04:00
|
|
|
"""Create a temporary file of a particular size (1024 bytes)."""
|
2017-07-08 01:16:37 -04:00
|
|
|
|
2021-12-02 00:01:32 -05:00
|
|
|
filename = os.path.join(temp_dir.name, "file")
|
|
|
|
with open(filename, "wb") as f:
|
|
|
|
f.write(b"*" * 1024)
|
|
|
|
return filename
|
2017-07-08 01:16:37 -04:00
|
|
|
|
|
|
|
|
2021-05-02 18:33:42 -04:00
|
|
|
@pytest.fixture
|
2019-11-10 16:55:15 -05:00
|
|
|
def temp_file_1024_delete(temp_dir):
|
2017-07-08 00:51:09 -04:00
|
|
|
"""
|
|
|
|
Create a temporary file of a particular size (1024 bytes).
|
|
|
|
The temporary file will be deleted after fixture usage.
|
|
|
|
"""
|
|
|
|
|
2021-12-02 00:01:32 -05:00
|
|
|
with tempfile.NamedTemporaryFile(dir=temp_dir.name, delete=False) as tmp_file:
|
2019-10-13 00:01:25 -04:00
|
|
|
tmp_file.write(b"*" * 1024)
|
2017-07-08 00:51:09 -04:00
|
|
|
tmp_file.flush()
|
2020-08-21 17:38:25 -04:00
|
|
|
tmp_file.close()
|
2021-12-02 00:01:32 -05:00
|
|
|
yield tmp_file
|
2017-07-08 00:51:09 -04:00
|
|
|
|
|
|
|
|
2021-05-02 18:33:42 -04:00
|
|
|
@pytest.fixture(scope="session")
|
2017-07-08 00:51:09 -04:00
|
|
|
def custom_zw():
|
2018-09-21 02:45:13 -04:00
|
|
|
zw = web.share_mode.ZipWriter(
|
2018-03-13 05:22:26 -04:00
|
|
|
common.Common(),
|
|
|
|
zip_filename=common.Common.random_string(4, 6),
|
2019-10-13 00:01:25 -04:00
|
|
|
processed_size_callback=lambda _: "custom_callback",
|
2017-07-08 00:51:09 -04:00
|
|
|
)
|
|
|
|
yield zw
|
|
|
|
zw.close()
|
|
|
|
os.remove(zw.zip_filename)
|
|
|
|
|
|
|
|
|
2021-05-02 18:33:42 -04:00
|
|
|
@pytest.fixture(scope="session")
|
2017-07-08 00:51:09 -04:00
|
|
|
def default_zw():
|
2018-09-21 02:45:13 -04:00
|
|
|
zw = web.share_mode.ZipWriter(common.Common())
|
2017-07-08 00:51:09 -04:00
|
|
|
yield zw
|
|
|
|
zw.close()
|
|
|
|
tmp_dir = os.path.dirname(zw.zip_filename)
|
2019-11-10 16:55:15 -05:00
|
|
|
try:
|
|
|
|
shutil.rmtree(tmp_dir, ignore_errors=True)
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2019-11-10 16:55:15 -05:00
|
|
|
pass
|
2017-07-08 00:51:09 -04:00
|
|
|
|
|
|
|
|
2017-07-08 02:17:20 -04:00
|
|
|
@pytest.fixture
|
|
|
|
def locale_en(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("en_US", "UTF-8"))
|
2017-07-08 02:17:20 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def locale_fr(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("fr_FR", "UTF-8"))
|
2017-07-08 02:17:20 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def locale_invalid(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("xx_XX", "UTF-8"))
|
2017-07-08 02:17:20 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def locale_ru(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("locale.getdefaultlocale", lambda: ("ru_RU", "UTF-8"))
|
2017-07-08 02:17:20 -04:00
|
|
|
|
|
|
|
|
2017-07-08 00:51:09 -04:00
|
|
|
@pytest.fixture
|
|
|
|
def platform_darwin(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("platform.system", lambda: "Darwin")
|
2017-07-08 00:51:09 -04:00
|
|
|
|
|
|
|
|
2017-07-08 13:29:19 -04:00
|
|
|
@pytest.fixture # (scope="session")
|
2017-07-08 00:51:09 -04:00
|
|
|
def platform_linux(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("platform.system", lambda: "Linux")
|
2017-07-08 00:51:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def platform_windows(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("platform.system", lambda: "Windows")
|
2017-07-08 00:51:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def sys_argv_sys_prefix(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("sys.argv", [sys.prefix])
|
2017-07-08 00:51:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def sys_frozen(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("sys.frozen", True, raising=False)
|
2017-07-08 00:51:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def sys_meipass(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("sys._MEIPASS", os.path.expanduser("~"), raising=False)
|
2017-07-08 00:51:09 -04:00
|
|
|
|
|
|
|
|
2017-07-08 13:29:19 -04:00
|
|
|
@pytest.fixture # (scope="session")
|
2017-07-08 00:51:09 -04:00
|
|
|
def sys_onionshare_dev_mode(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("sys.onionshare_dev_mode", True, raising=False)
|
2017-07-08 00:51:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def time_time_100(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("time.time", lambda: 100)
|
2017-07-08 00:51:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def time_strftime(monkeypatch):
|
2019-10-13 00:01:25 -04:00
|
|
|
monkeypatch.setattr("time.strftime", lambda _: "Jun 06 2013 11:05:00")
|
|
|
|
|
2018-03-13 05:22:26 -04:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def common_obj():
|
2018-10-12 19:35:09 -04:00
|
|
|
return common.Common()
|