diff --git a/esgf-consumer/esgf_consumer/__init__.py b/esgf-consumer/esgf_consumer/__init__.py index cf0db17..89a719f 100644 --- a/esgf-consumer/esgf_consumer/__init__.py +++ b/esgf-consumer/esgf_consumer/__init__.py @@ -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: diff --git a/esgf-generator/esgf_generator/cli.py b/esgf-generator/esgf_generator/cli.py index 7aa1b9f..dee0c23 100644 --- a/esgf-generator/esgf_generator/cli.py +++ b/esgf-generator/esgf_generator/cli.py @@ -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") @@ -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}"}, @@ -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( @@ -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}", diff --git a/esgf-generator/esgf_generator/test_cli.py b/esgf-generator/esgf_generator/test_cli.py index 0bd09fb..cb8aa44 100644 --- a/esgf-generator/esgf_generator/test_cli.py +++ b/esgf-generator/esgf_generator/test_cli.py @@ -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: @@ -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}"}) @@ -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"}) @@ -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"}) @@ -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}) @@ -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: @@ -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: @@ -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") diff --git a/esgf-generator/esgf_generator/test_keycloak.py b/esgf-generator/esgf_generator/test_keycloak.py index ad0d00d..6263a7f 100644 --- a/esgf-generator/esgf_generator/test_keycloak.py +++ b/esgf-generator/esgf_generator/test_keycloak.py @@ -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() @@ -30,7 +30,6 @@ def runner() -> CliRunner: def test_invalid_credentials(runner: CliRunner) -> None: - user_input = "invalid_user\ninvalid_user" result = runner.invoke( @@ -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( @@ -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")