diff --git a/.gitignore b/.gitignore index 68bc17f..d3bc319 100644 --- a/.gitignore +++ b/.gitignore @@ -120,7 +120,6 @@ celerybeat.pid *.sage.py # Environments -.env .venv env/ venv/ @@ -158,3 +157,6 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +# Temp files +temp/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..62c327d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,12 @@ +{ + "python.testing.unittestArgs": [ + "-v", + "-s", + "./tests", + "-p", + "test_*.py" + ], + "python.envFile": "${workspaceFolder}/tests/e2e/.env", + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true +} \ No newline at end of file diff --git a/README.md b/README.md index 3ca8ffe..755b721 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ client.set_user_enabled(user.Id, enabled=True) from vaultwarden.clients.bitwarden import BitwardenAPIClient from vaultwarden.models.bitwarden import Organization, OrganizationCollection, get_organization -bitwarden_client = BitwardenAPIClient(url="https://vaultwarden.example.com", email="admin@example", password="admin_password", client_id="client_id", client_secret="client_secret") +bitwarden_client = BitwardenAPIClient(url="https://vaultwarden.example.com", email="admin@example", password="admin_password", client_id="client_id", client_secret="client_secret", device_id="my_test_device") org_uuid = "550e8400-e29b-41d4-a716-446655440000" @@ -135,10 +135,15 @@ You can now install the project and its dependencies using: pip install -e .[test] ``` ### Testing -To run the tests, use: +Configure the test by loading the environment variables +```bash +set -a; . tests/e2e/.env; set +a +``` +_this is proconfigrued for vscode python test extention_ +To run the tests, use: ```bash -bash tests/e2e/run_tests.sh +python -m unittests discover ``` ## License diff --git a/tests/e2e/.env b/tests/e2e/.env new file mode 100644 index 0000000..b6c105e --- /dev/null +++ b/tests/e2e/.env @@ -0,0 +1,11 @@ +VAULTWARDEN_VERSION="testing" +VAULTWARDEN_URL="http://localhost:8080" +VAULTWARDEN_ADMIN_TOKEN="admin" +VAULTWARDEN_INVITATIONS_ALLOWED="true" +BITWARDEN_URL="http://localhost:8080" +BITWARDEN_EMAIL="test-account@example.com" +BITWARDEN_PASSWORD="test-account" +BITWARDEN_CLIENT_ID="user.a8be340c-856b-481f-8183-2b7712995da2" +BITWARDEN_CLIENT_SECRET="ag66paVUq4h7tBLbCbJOY5tJkQvUuT" +BITWARDEN_TEST_ORGANIZATION="cda840d2-1de0-4f31-bd49-b30dacd7e8b0" +BITWARDEN_DEVICE_ID="e54ba5f5-7d58-4830-8f2b-99194c70c14f" \ No newline at end of file diff --git a/tests/e2e/compose.yaml b/tests/e2e/compose.yaml new file mode 100644 index 0000000..7c5bcf4 --- /dev/null +++ b/tests/e2e/compose.yaml @@ -0,0 +1,13 @@ +services: + vaultwarden: + image: vaultwarden/server:${VAULTWARDEN_VERSION} + container_name: python_vaultwarden_test + ports: + - "8080:80" + volumes: + - ./temp:/data + environment: + INVITATIONS_ALLOWED: ${VAULTWARDEN_INVITATIONS_ALLOWED} + I_REALLY_WANT_VOLATILE_STORAGE: "true" + ADMIN_TOKEN: ${VAULTWARDEN_ADMIN_TOKEN} + restart: unless-stopped \ No newline at end of file diff --git a/tests/e2e/docker_helper.py b/tests/e2e/docker_helper.py new file mode 100644 index 0000000..e859f8e --- /dev/null +++ b/tests/e2e/docker_helper.py @@ -0,0 +1,15 @@ +import os +import shutil +from time import sleep + +def start_docker(): + shutil.copytree("tests/fixtures/server", "tests/e2e/temp/", dirs_exist_ok=True) + os.system("docker compose -f tests/e2e/compose.yaml up -d") + sleep(1) + +def stop_docker(): + os.system("docker compose -f tests/e2e/compose.yaml down") + try: + shutil.rmtree("tests/e2e/temp") + except FileNotFoundError: + pass \ No newline at end of file diff --git a/tests/e2e/run_tests.sh b/tests/e2e/run_tests.sh deleted file mode 100755 index 4059ef8..0000000 --- a/tests/e2e/run_tests.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash - -if [[ -z "${VAULTWARDEN_VERSION}" ]]; then - VAULTWARDEN_VERSION="1.34.3" -fi - -temp_dir=$(mktemp -d) - -# Copy fixtures db to tmp -cp tests/fixtures/server/* $temp_dir - -# Start Vaultwarden docker -docker run -d --name vaultwarden -v $temp_dir:/data --env I_REALLY_WANT_VOLATILE_STORAGE=true --env ADMIN_TOKEN=admin --restart unless-stopped -p 80:80 vaultwarden/server:${VAULTWARDEN_VERSION} - -exit 0 - -# Wait for vaultwarden to start -sleep 3 - -# Set env variables -export VAULTWARDEN_URL="http://localhost:80" -export VAULTWARDEN_ADMIN_TOKEN="admin" -export BITWARDEN_URL="http://localhost:80" -export BITWARDEN_EMAIL="test-account@example.com" -export BITWARDEN_PASSWORD="test-account" -export BITWARDEN_CLIENT_ID="user.a8be340c-856b-481f-8183-2b7712995da2" -export BITWARDEN_CLIENT_SECRET="ag66paVUq4h7tBLbCbJOY5tJkQvUuT" -export BITWARDEN_TEST_ORGANIZATION="cda840d2-1de0-4f31-bd49-b30dacd7e8b0" -export BITWARDEN_DEVICE_ID="e54ba5f5-7d58-4830-8f2b-99194c70c14f" - -# Run tests -hatch run test:with-coverage - -# store the exit code -TEST_EXIT_CODE=$? - -# Stop and remove vaultwarden docker -docker stop vaultwarden -docker rm vaultwarden - -# Remove fixtures db from tmp -rm -rf $temp_dir - -# Exit with the test exit code -exit $TEST_EXIT_CODE \ No newline at end of file diff --git a/tests/e2e/test_bitwarden.py b/tests/e2e/test_bitwarden.py index 49d5ca0..37c28dd 100644 --- a/tests/e2e/test_bitwarden.py +++ b/tests/e2e/test_bitwarden.py @@ -4,6 +4,8 @@ from vaultwarden.clients.bitwarden import BitwardenAPIClient from vaultwarden.models.bitwarden import get_organization +from .docker_helper import start_docker, stop_docker + # Get Bitwarden credentials from environment variables url = os.environ.get("BITWARDEN_URL", None) email = os.environ.get("BITWARDEN_EMAIL", None) @@ -11,17 +13,21 @@ client_id = os.environ.get("BITWARDEN_CLIENT_ID", None) client_secret = os.environ.get("BITWARDEN_CLIENT_SECRET", None) device_id = os.environ.get("BITWARDEN_DEVICE_ID", None) -bitwarden = BitwardenAPIClient( - url, email, password, client_id, client_secret, device_id -) # Get test organization id from environment variables test_organization = os.environ.get("BITWARDEN_TEST_ORGANIZATION", None) class BitwardenBasic(unittest.TestCase): + def tearDownClass() -> None: + stop_docker() + def setUp(self) -> None: - self.organization = get_organization(bitwarden, test_organization) + start_docker() + self.bitwarden = BitwardenAPIClient( + url, email, password, client_id, client_secret, device_id + ) + self.organization = get_organization(self.bitwarden, test_organization) self.test_colls_names = self.organization.collections(as_dict=True) self.test_colls_ids = self.organization.collections() self.test_users = self.organization.users() @@ -39,6 +45,9 @@ def setUp(self) -> None: "test-collection-2" ).users() + def tearDown(self) -> None: + stop_docker() + def test_get_organization_users(self): self.assertEqual(len(self.test_users), 2) diff --git a/tests/e2e/test_vaultwarden.py b/tests/e2e/test_vaultwarden.py index ed6981d..b0b7b89 100644 --- a/tests/e2e/test_vaultwarden.py +++ b/tests/e2e/test_vaultwarden.py @@ -4,6 +4,8 @@ from vaultwarden.clients.vaultwarden import VaultwardenAdminClient +from .docker_helper import start_docker, stop_docker + # Get Vaultwarden Admin credentials from environment variables url = os.environ.get("VAULTWARDEN_URL", None) admin_token = os.environ.get("VAULTWARDEN_ADMIN_TOKEN", None) @@ -11,7 +13,14 @@ # TODO Add tests for VaultwardenAdminClient class VaultwardenAdminClientBasic(unittest.TestCase): + def tearDownClass() -> None: + stop_docker() + def setUp(self) -> None: + start_docker() self.vaultwarden = VaultwardenAdminClient( url=url, admin_secret_token=admin_token ) + + def tearDown(self) -> None: + stop_docker()