Skip to content

Commit

Permalink
Add role based rest and docstrings for all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Edbo849 committed Nov 12, 2024
1 parent 1938247 commit 5730c6a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
45 changes: 45 additions & 0 deletions esgf-generator/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def check_elasticsearch_index(expected_properties: dict[str, Union[str, bool]])


def test_add_new_item(runner: CliRunner) -> None:
"""
Test adding a new item using the esgf_generator CLI command.
"""
user_input = "test_user\ntest_user"

result = runner.invoke(
Expand All @@ -76,6 +79,9 @@ def test_add_new_item(runner: CliRunner) -> None:


def test_add_replica(runner: CliRunner) -> None:
"""
Test adding a replica using the esgf_replicate CLI command.
"""
user_input = "test_user\ntest_user"

result = runner.invoke(
Expand All @@ -95,6 +101,9 @@ def test_add_replica(runner: CliRunner) -> None:


def test_remove_replica(runner: CliRunner) -> None:
"""
Test removing a replica using the esgf_update CLI command.
"""
user_input = "test_user\ntest_user"

result = runner.invoke(
Expand All @@ -116,6 +125,9 @@ def test_remove_replica(runner: CliRunner) -> None:


def test_esgf_update_invalid_json(runner: CliRunner) -> None:
"""
Test updating an item with invalid JSON using the esgf_update CLI command.
"""
user_input = "test_user\ntest_user"

result = runner.invoke(
Expand All @@ -135,6 +147,9 @@ def test_esgf_update_invalid_json(runner: CliRunner) -> None:


def test_update_item(runner: CliRunner) -> None:
"""
Test updating an item using the esgf_update CLI command.
"""
user_input = "test_user\ntest_user"

result = runner.invoke(
Expand All @@ -155,6 +170,9 @@ def test_update_item(runner: CliRunner) -> None:


def test_retract_item(runner: CliRunner) -> None:
"""
Test retracting an item using the esgf_delete CLI command.
"""
user_input = "test_user\ntest_user"

result = runner.invoke(
Expand All @@ -175,6 +193,9 @@ def test_retract_item(runner: CliRunner) -> None:


def test_unretract_item(runner: CliRunner) -> None:
"""
Test unretracting an item using the esgf_update CLI command.
"""
user_input = "test_user\ntest_user"

result = runner.invoke(
Expand All @@ -195,7 +216,25 @@ def test_unretract_item(runner: CliRunner) -> None:
check_elasticsearch_index({"properties.retracted": False})


def test_role_based_access(runner: CliRunner) -> None:
"""
Test role-based access using the esgf_generator CLI command.
"""
user_input = "test_user\ntest_user"

result = runner.invoke(
esgf_delete,
[collection_id, item_id, "--node", "east", "--hard", "--publish"],
input=user_input,
)
if "Not enough permissions" not in result.output:
pytest.fail("Not enough permissions")


def test_remove_item(runner: CliRunner) -> None:
"""
Test removing an item using the esgf_delete CLI command.
"""
user_input = "test_admin\ntest_admin"

result = runner.invoke(
Expand All @@ -211,6 +250,9 @@ def test_remove_item(runner: CliRunner) -> None:


def test_delete_non_existent_item(runner: CliRunner) -> None:
"""
Test deleting a non-existent item using the esgf_delete CLI command.
"""
user_input = "test_admin\ntest_admin"

result = runner.invoke(
Expand All @@ -224,6 +266,9 @@ def test_delete_non_existent_item(runner: CliRunner) -> None:


def test_update_non_existent_item(runner: CliRunner) -> None:
"""
Test updating a non-existent item using the esgf_update CLI command.
"""
user_input = "test_user\ntest_user"

result = runner.invoke(
Expand Down
12 changes: 12 additions & 0 deletions esgf-generator/tests/test_keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def delete_generated_item(runner: CliRunner, collection_id: str, item_id: str) -


def test_invalid_credentials(runner: CliRunner) -> None:
"""
Test invalid keycloak credentials when trying to run a command.
"""
user_input = "invalid_user\ninvalid_user"

result = runner.invoke(
Expand All @@ -76,6 +79,9 @@ def test_invalid_credentials(runner: CliRunner) -> None:


def test_validate_token(runner: CliRunner) -> None:
"""
Test validating an access token.
"""
user_input = "test_admin\ntest_admin"

result = runner.invoke(
Expand All @@ -99,13 +105,19 @@ def test_validate_token(runner: CliRunner) -> None:


def test_invalid_token(runner: CliRunner, monkeypatch: pytest.MonkeyPatch) -> None:
"""
Test validating an invalid access token.
"""
monkeypatch.setenv("TOKEN", "invalid_token")

if validate_token():
pytest.fail("Token validation should have failed for invalid token")


def test_get_token(runner: CliRunner) -> None:
"""
Test retreiving a new access token.
"""
user_input = "test_admin\ntest_admin"

result = runner.invoke(
Expand Down
15 changes: 15 additions & 0 deletions esgf-generator/tests/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ def clean_env() -> Generator[None, None, None]:


def test_database_connection() -> None:
"""
Test the database connection by pinging the MongoDB server.
"""
try:
client.admin.command("ping")
except Exception as e:
pytest.fail(f"Database is not running: {e}")


def test_create_event_in_database(runner: CliRunner) -> None:
"""
Test a create event in the database using the esgf_generator CLI command.
"""
result = runner.invoke(
esgf_generator,
["1", "--node", "east", "--publish"],
Expand All @@ -67,6 +73,9 @@ def test_create_event_in_database(runner: CliRunner) -> None:


def test_update_event_in_database(runner: CliRunner) -> None:
"""
Test an update event in the database using the esgf_update CLI command.
"""
runner.invoke(
esgf_update,
["collection_id", "item_id", "--node", "east", "--publish"],
Expand All @@ -78,6 +87,9 @@ def test_update_event_in_database(runner: CliRunner) -> None:


def test_patch_event_in_database(runner: CliRunner) -> None:
"""
Test a patching event in the database using the esgf_replicate CLI command.
"""
runner.invoke(
esgf_replicate,
[
Expand All @@ -95,6 +107,9 @@ def test_patch_event_in_database(runner: CliRunner) -> None:


def test_delete_event_in_database(runner: CliRunner) -> None:
"""
Test a deleting event in the database using the esgf_delete CLI command.
"""
runner.invoke(
esgf_delete,
["collection_id", "item_id", "--node", "east", "--publish"],
Expand Down

0 comments on commit 5730c6a

Please sign in to comment.