-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for keycloak, and a few more for the cli
- Loading branch information
Showing
5 changed files
with
187 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import os | ||
from typing import Generator | ||
|
||
import pytest | ||
from click.testing import CliRunner | ||
from dotenv import find_dotenv, load_dotenv, unset_key | ||
|
||
from esgf_generator.cli import esgf_delete, esgf_generator, validate_token | ||
|
||
ENV_FILE = find_dotenv() | ||
|
||
if ENV_FILE is None: | ||
raise Exception("No .env file found, please create one in the root directory") | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def clean_env() -> Generator[None, None, None]: | ||
unset_key(ENV_FILE, "TOKEN") | ||
load_dotenv(ENV_FILE) | ||
|
||
yield | ||
|
||
unset_key(ENV_FILE, "TOKEN") | ||
load_dotenv(ENV_FILE) | ||
|
||
|
||
@pytest.fixture | ||
def runner() -> CliRunner: | ||
return CliRunner() | ||
|
||
|
||
def test_invalid_credentials(runner: CliRunner) -> None: | ||
|
||
user_input = "invalid_user\ninvalid_user" | ||
|
||
result = runner.invoke( | ||
esgf_generator, ["1", "--node", "east", "--publish"], input=user_input | ||
) | ||
|
||
load_dotenv(ENV_FILE) | ||
token = os.getenv("TOKEN") | ||
|
||
assert "Authentication Failed" in result.output | ||
|
||
assert not token | ||
|
||
|
||
def test_validate_token(runner: CliRunner) -> None: | ||
|
||
user_input = "test_user\ntest_user" | ||
|
||
result = runner.invoke( | ||
esgf_generator, ["1", "--node", "east", "--publish"], input=user_input | ||
) | ||
|
||
assert result.exit_code == 0 | ||
|
||
assert validate_token() | ||
|
||
|
||
def test_invalid_token(runner: CliRunner, monkeypatch: pytest.MonkeyPatch) -> None: | ||
monkeypatch.setenv("TOKEN", "invalid_token") | ||
|
||
result = validate_token() | ||
|
||
assert not result | ||
|
||
|
||
def test_get_token(runner: CliRunner) -> None: | ||
|
||
user_input = "test_user\ntest_user" | ||
|
||
result = runner.invoke( | ||
esgf_generator, ["1", "--node", "east", "--publish"], input=user_input | ||
) | ||
|
||
load_dotenv(ENV_FILE) | ||
token = os.getenv("TOKEN") | ||
|
||
assert result.exit_code == 0 | ||
|
||
assert token is not None | ||
|
||
|
||
def test_user_scope(runner: CliRunner) -> None: | ||
|
||
user_input = "test_user\ntest_user" | ||
|
||
result = runner.invoke( | ||
esgf_delete, | ||
["collection_id", "item_id", "--node", "east", "--hard", "--publish"], | ||
input=user_input, | ||
) | ||
|
||
assert result.exit_code == 0 | ||
|
||
assert "Not enough permissions" in result.output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.