aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2024-05-04 17:29:55 +0200
committerDaniel Schadt <kingdread@gmx.de>2024-05-04 17:29:55 +0200
commitea655c63ed950a4a60f8bf65b54a73516abd6aba (patch)
tree6f107833d0784590bc78066fa5c9863c91055e44 /tests
parentc9cde87237eec537198c5d53347c81da7191d5c6 (diff)
parentc0b49a618d96f2088234dbb4c0ca8006475d432e (diff)
downloadfietsboek-ea655c63ed950a4a60f8bf65b54a73516abd6aba.tar.gz
fietsboek-ea655c63ed950a4a60f8bf65b54a73516abd6aba.tar.bz2
fietsboek-ea655c63ed950a4a60f8bf65b54a73516abd6aba.zip
Merge branch 'migration-bootstrap-test'
Diffstat (limited to 'tests')
-rw-r--r--tests/bootstrap/test_new_instance.py81
-rw-r--r--tests/conftest.py2
2 files changed, 83 insertions, 0 deletions
diff --git a/tests/bootstrap/test_new_instance.py b/tests/bootstrap/test_new_instance.py
new file mode 100644
index 0000000..dc3076e
--- /dev/null
+++ b/tests/bootstrap/test_new_instance.py
@@ -0,0 +1,81 @@
+"""Test to ensure that bootstrapping a new instance works with the fietsupdate
+script, as described in the documentation.
+"""
+
+import contextlib
+import logging
+import os
+import shutil
+import subprocess
+import venv
+from pathlib import Path
+
+LOGGER = logging.getLogger(__name__)
+REPO_BASE = Path(__file__).parent.parent.parent
+
+
+@contextlib.contextmanager
+def chdir(path: Path):
+ """Change path as context manager.
+
+ Changes back to the previous path on ``__exit__``.
+ """
+ cwd = Path.cwd()
+ os.chdir(path)
+ try:
+ yield
+ finally:
+ os.chdir(cwd)
+
+
+def install_fietsboek(venv_path: Path) -> Path:
+ """Creates a virtual env in the given path and installs fietsboek.
+
+ :param venv_path: Path in which the virtual environment should be created.
+ :return: The path to the ``bin`` directory in which all scripts reside.
+ """
+ venv.create(venv_path, with_pip=True)
+ subprocess.check_call([venv_path / "bin" / "pip", "install", str(REPO_BASE)])
+ return venv_path / "bin"
+
+
+def create_config(config_name: Path):
+ """Copies the testing.ini config and fills in the placeholders.
+
+ :param config_name: Path to the resulting config.
+ """
+ shutil.copy(REPO_BASE / "testing.ini", config_name)
+ config = config_name.read_text()
+ config = config.replace("# %% fietsboek.data_dir %%", "fietsboek.data_dir = %(here)s/data")
+ config_name.write_text(config)
+ Path("data").mkdir()
+
+
+def test_setup_via_fietsupdate(tmpdir):
+ with chdir(tmpdir):
+ # We create a new temporary virtual environment with a fresh install, just
+ # to be sure there's as little interference as possible.
+ LOGGER.info("Installing Fietsboek into clean env")
+ binaries_path = install_fietsboek(tmpdir / "venv")
+
+ LOGGER.info("Creating a test configuration")
+ create_config(Path("testing.ini"))
+
+ # Try to run the migrations
+ subprocess.check_call(
+ [binaries_path / "fietsupdate", "update", "-c", "testing.ini", "-f"]
+ )
+
+ # Also try to add an administrator
+ subprocess.check_call([
+ binaries_path / "fietsctl",
+ "user",
+ "add",
+ "-c", "testing.ini",
+ "--email", "foobar@example.com",
+ "--name", "Foo Bar",
+ "--password", "raboof",
+ "--admin",
+ ])
+
+ assert True
diff --git a/tests/conftest.py b/tests/conftest.py
index 94f795a..cd74b0b 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -75,6 +75,8 @@ def _cleanup_data(app_settings):
data_dir = Path(app_settings["fietsboek.data_dir"])
if (data_dir / "tracks").is_dir():
shutil.rmtree(data_dir / "tracks")
+ if (data_dir / "users").is_dir():
+ shutil.rmtree(data_dir / "users")
@pytest.fixture(scope='module')
def app(app_settings, dbengine, tmp_path_factory):