From 6afe0cc521ee2551fe17c01247ea57d2fc53a299 Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Fri, 10 Mar 2023 11:05:22 -0500 Subject: [PATCH] added tests/ this runs off pytest currently we only tested the CLI commands --- .gitignore | 1 + requirements.txt | 10 +++++++++ sachet/server/commands.py | 11 +++------- sachet/server/users/manage.py | 11 +--------- tests/__init__.py | 0 tests/conftest.py | 34 ++++++++++++++++++++++++++++++ tests/test_cli.py | 39 +++++++++++++++++++++++++++++++++++ 7 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_cli.py diff --git a/.gitignore b/.gitignore index 00e5b19..74ddc72 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ __pycache__/ .env config.yml instance/ +.coverage diff --git a/requirements.txt b/requirements.txt index 9f5de63..b07d2ef 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,17 +1,27 @@ +attrs==22.2.0 bcrypt==4.0.1 click==8.1.3 +coverage==7.2.1 +exceptiongroup==1.1.0 Flask==2.2.3 Flask-Bcrypt==1.0.1 Flask-Cors==3.0.10 Flask-Script==2.0.6 Flask-SQLAlchemy==3.0.3 +Flask-Testing==0.8.1 greenlet==2.0.2 +iniconfig==2.0.0 itsdangerous==2.1.2 Jinja2==3.1.2 MarkupSafe==2.1.2 +packaging==23.0 +pluggy==1.0.0 PyJWT==2.6.0 +pytest==7.2.2 +pytest-cov==4.0.0 PyYAML==6.0 six==1.16.0 SQLAlchemy==2.0.5.post1 +tomli==2.0.1 typing_extensions==4.5.0 Werkzeug==2.2.3 diff --git a/sachet/server/commands.py b/sachet/server/commands.py index 873f06c..8462372 100644 --- a/sachet/server/commands.py +++ b/sachet/server/commands.py @@ -35,14 +35,9 @@ def create_user(admin, username, password): manage.create_user(admin, username, password) @user_cli.command("delete") -@click.option("--username", "selector_type", help="Delete by username.", flag_value="username", default=True) -@click.option("--id", "selector_type", help="Delete by ID.", flag_value="ID", default=True) -@click.argument("selector") +@click.argument("username") @click.option('--yes', is_flag=True, expose_value=False, prompt=f"Are you sure you want to delete this user?") -def delete_user(selector_type, selector): - if selector_type == "username": - manage.delete_user_by_username(selector) - elif selector_type == "ID": - manage.delete_user_by_id(selector) +def delete_user(username): + manage.delete_user_by_username(username) app.cli.add_command(user_cli) diff --git a/sachet/server/users/manage.py b/sachet/server/users/manage.py index 63272a8..622d845 100644 --- a/sachet/server/users/manage.py +++ b/sachet/server/users/manage.py @@ -18,7 +18,7 @@ def create_user(admin, username, password): db.session.add(user) db.session.commit() else: - raise KeyError(f"User '{username}' already exists (ID: {user.id})") + raise KeyError(f"User '{username}' already exists.") def delete_user_by_username(username): user = User.query.filter_by(username=username).first() @@ -28,12 +28,3 @@ def delete_user_by_username(username): else: db.session.delete(user) db.session.commit() - -def delete_user_by_id(id): - user = User.query.filter_by(id=id).first() - - if not user: - raise KeyError(f"User with ID {id} does not exist.") - else: - db.session.delete(user) - db.session.commit() diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..19496f5 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,34 @@ +import pytest +from click.testing import CliRunner + +import os +os.environ["APP_SETTINGS"] = "sachet.server.config.TestingConfig" + +from sachet.server import app, db + +@pytest.fixture +def flask_app(): + """Flask application with DB already set up and ready.""" + with app.test_client() as client: + with app.app_context(): + db.drop_all() + db.create_all() + db.session.commit() + yield app.test_client + db.session.remove() + db.drop_all() + + +@pytest.fixture +def flask_app_bare(): + """Flask application with empty DB.""" + with app.test_client() as client: + with app.app_context(): + yield client + db.drop_all() + + +@pytest.fixture +def cli(): + """click's testing fixture""" + return CliRunner() diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..4925bc9 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,39 @@ +import pytest +from sachet.server.commands import create_db, drop_db, create_user, delete_user +from sachet.server import app, db +from sqlalchemy import inspect + +from sachet.server.models import User + +def test_db(flask_app_bare, cli): + """Test the CLI's ability to create and drop the DB.""" + # make tables + result = cli.invoke(create_db) + assert result.exit_code == 0 + assert "users" in inspect(db.engine).get_table_names() + + # tear down + result = cli.invoke(drop_db, ["--yes"]) + assert result.exit_code == 0 + assert "users" not in inspect(db.engine).get_table_names() + + +def test_user(flask_app, cli): + """Test the CLI's ability to create then delete a user.""" + # create user + result = cli.invoke(create_user, ["--username", "jeff", "--password", "1234"]) + assert result.exit_code == 0 + assert User.query.filter_by(username="jeff").first() is not None + + # create duplicate user + result = cli.invoke(create_user, ["--username", "jeff", "--password", "1234"]) + assert isinstance(result.exception, KeyError) + + # delete user + result = cli.invoke(delete_user, ["--yes", "jeff"]) + assert result.exit_code == 0 + assert User.query.filter_by(username="jeff").first() is None + + # delete non-existent user + result = cli.invoke(delete_user, ["--yes", "jeff"]) + assert isinstance(result.exception, KeyError)