Skip to content

Commit

Permalink
Quality updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Edbo849 committed Nov 7, 2024
1 parent 5ef779f commit 63ddb96
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 43 deletions.
2 changes: 1 addition & 1 deletion esgf-consumer/esgf_consumer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def consume(settings: Settings) -> None:
logger.critical("Producer started.")

logger.critical("Starting http client...")
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(timeout=5.0) as client:

logger.critical("http client started.")
try:
Expand Down
8 changes: 4 additions & 4 deletions esgf-generator/esgf_generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def authenticate() -> str:
"password": password,
}

response = httpx.post(url, data=data)
response = httpx.post(url, data=data, timeout=5.0)

if response.status_code == 200:
token = response.json().get("access_token")
Expand Down Expand Up @@ -142,7 +142,7 @@ def esgf_generator(
click.echo(
f"Sending {instance.properties.instance_id}, collection: {instance.collection} to ESGF node '{node}'"
)
with httpx.Client() as client:
with httpx.Client(timeout=5.0) as client:
result = client.post(
f"http://localhost:{NODE_PORTS[node]}/{instance.collection}/items",
headers={"Authorization": f"Bearer {token}"},
Expand Down Expand Up @@ -211,7 +211,7 @@ def esgf_update(
item = update_topic(item, item_id, collection_id)

if publish:
with httpx.Client() as client:
with httpx.Client(timeout=5) as client:
if partial_update_data:
click.echo()
click.echo(
Expand Down Expand Up @@ -280,7 +280,7 @@ def esgf_delete(
click.echo(f"Deleting item {item_id} in collection {collection_id}")

if publish:
with httpx.Client() as client:
with httpx.Client(timeout=5.0) as client:
if hard:
result = client.delete(
f"http://localhost:{NODE_PORTS[node]}/{collection_id}/items/{item_id}",
Expand Down
23 changes: 13 additions & 10 deletions esgf-generator/esgf_generator/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ def check_elasticsearch_index(expected_properties: dict[str, Any]) -> None:
current = source
for k in keys:
if k not in current:
raise KeyError(f"Key '{keys}' not found in the document")
pytest.fail(f"Key '{keys}' not found in the document")
current = current[k]
if current != value:
raise Exception(f"Expected {keys} to be {value}, but got {current}")
pytest.fail(f"Expected {keys} to be {value}, but got {current}")


def test_add_new_item(runner: CliRunner) -> None:
Expand All @@ -70,7 +70,7 @@ def test_add_new_item(runner: CliRunner) -> None:
time.sleep(15)

if result.exit_code != 0:
raise RuntimeError(f"Command failed with exit code {result.exit_code}")
pytest.fail(f"Command failed with exit code {result.exit_code}")
get_item_details(result)
check_elasticsearch_index({"properties.title": f"{item_id}"})

Expand All @@ -92,7 +92,7 @@ def test_add_replica(runner: CliRunner) -> None:
input=user_input,
)
if result.exit_code != 0:
raise RuntimeError(f"Command failed with exit code {result.exit_code}")
pytest.fail(f"Command failed with exit code {result.exit_code}")
check_elasticsearch_index({"properties.Replica": "Node 1"})


Expand All @@ -113,7 +113,7 @@ def test_update_item(runner: CliRunner) -> None:
input=user_input,
)
if result.exit_code != 0:
raise RuntimeError(f"Command failed with exit code {result.exit_code}")
pytest.fail(f"Command failed with exit code {result.exit_code}")
check_elasticsearch_index({"properties.description": "Test Description"})


Expand All @@ -133,7 +133,7 @@ def test_remove_replica(runner: CliRunner) -> None:
input=user_input,
)
if result.exit_code != 0:
raise RuntimeError(f"Command failed with exit code {result.exit_code}")
pytest.fail(f"Command failed with exit code {result.exit_code}")
check_elasticsearch_index({"properties.retracted": True})


Expand All @@ -146,10 +146,10 @@ def test_remove_item(runner: CliRunner) -> None:
input=user_input,
)
if result.exit_code != 0:
raise RuntimeError(f"Command failed with exit code {result.exit_code}")
pytest.fail(f"Command failed with exit code {result.exit_code}")
response = es.exists(index="item_{collection_id}-000001", id=item_id)
if response:
raise ValueError("Document still exists after deletion")
pytest.fail("Document still exists after deletion")


def test_delete_non_existent_item(runner: CliRunner) -> None:
Expand All @@ -161,7 +161,8 @@ def test_delete_non_existent_item(runner: CliRunner) -> None:
input=user_input,
)

assert "Cannot delete non-existent item" in result.output
if "Cannot delete non-existent item" not in result.output:
pytest.fail("Expected 'Cannot delete non-existent item' in output")


def test_update_non_existent_item(runner: CliRunner) -> None:
Expand All @@ -180,4 +181,6 @@ def test_update_non_existent_item(runner: CliRunner) -> None:
],
input=user_input,
)
assert "Cannot update non-existent item" in result.output

if "Cannot update non-existent item" not in result.output:
pytest.fail("Expected 'Cannot update non-existent item' in output")
43 changes: 15 additions & 28 deletions esgf-generator/esgf_generator/test_keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
from esgf_generator.cli import esgf_generator, validate_token

ENV_FILE = find_dotenv()

Expand All @@ -30,7 +30,6 @@ def runner() -> CliRunner:


def test_invalid_credentials(runner: CliRunner) -> None:

user_input = "invalid_user\ninvalid_user"

result = runner.invoke(
Expand All @@ -40,34 +39,35 @@ def test_invalid_credentials(runner: CliRunner) -> None:
load_dotenv(ENV_FILE)
token = os.getenv("TOKEN")

assert "Authentication Failed" in result.output
if "Authentication Failed" not in result.output:
pytest.fail("Expected 'Authentication Failed' in output")

assert not token
if token:
pytest.fail("Token should not be set")


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
if result.exit_code != 0:
pytest.fail(f"Expected exit code 0, got {result.exit_code}")

assert validate_token()
if not validate_token():
pytest.fail("Token validation failed")


def test_invalid_token(runner: CliRunner, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("TOKEN", "invalid_token")

result = validate_token()

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


def test_get_token(runner: CliRunner) -> None:

user_input = "test_user\ntest_user"

result = runner.invoke(
Expand All @@ -77,21 +77,8 @@ def test_get_token(runner: CliRunner) -> None:
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
if result.exit_code != 0:
pytest.fail(f"Expected exit code 0, got {result.exit_code}")

assert "Not enough permissions" in result.output
if not token:
pytest.fail("Token should be set")

0 comments on commit 63ddb96

Please sign in to comment.