From d80fd036c04e8387b3df4b5a47784cbfe8e2a41b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Thu, 25 Jan 2024 16:28:44 +0000 Subject: [PATCH 001/110] Moving fixture to a fixture folder. --- tests/{test_post.json => fixtures/post.json} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{test_post.json => fixtures/post.json} (100%) diff --git a/tests/test_post.json b/tests/fixtures/post.json similarity index 100% rename from tests/test_post.json rename to tests/fixtures/post.json From a81de1d07ef0aba03e3f159814dc223f661e7fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Thu, 25 Jan 2024 16:29:03 +0000 Subject: [PATCH 002/110] Splitting test_commands in different test files. --- tests/unit/commands/test_about.py | 0 tests/unit/commands/test_account.py | 87 +++++++++++ tests/unit/commands/test_aggregate.py | 0 tests/unit/commands/test_domain.py | 0 tests/unit/commands/test_file.py | 33 +++++ tests/unit/commands/test_instance.py | 0 tests/unit/commands/test_message.py | 116 +++++++++++++++ tests/unit/commands/test_node.py | 0 tests/unit/commands/test_program.py | 0 tests/unit/commands/test_utils.py | 18 +++ tests/unit/test_commands.py | 205 -------------------------- 11 files changed, 254 insertions(+), 205 deletions(-) create mode 100644 tests/unit/commands/test_about.py create mode 100644 tests/unit/commands/test_account.py create mode 100644 tests/unit/commands/test_aggregate.py create mode 100644 tests/unit/commands/test_domain.py create mode 100644 tests/unit/commands/test_file.py create mode 100644 tests/unit/commands/test_instance.py create mode 100644 tests/unit/commands/test_message.py create mode 100644 tests/unit/commands/test_node.py create mode 100644 tests/unit/commands/test_program.py create mode 100644 tests/unit/commands/test_utils.py delete mode 100644 tests/unit/test_commands.py diff --git a/tests/unit/commands/test_about.py b/tests/unit/commands/test_about.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py new file mode 100644 index 00000000..b7d4ec6f --- /dev/null +++ b/tests/unit/commands/test_account.py @@ -0,0 +1,87 @@ +from pathlib import Path + +from aleph.sdk.chains.ethereum import ETHAccount +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def get_account(my_account_file: Path) -> ETHAccount: + with open(my_account_file, "rb") as fd: + private_key = fd.read() + return ETHAccount(private_key=private_key) + + +def get_test_message(account: ETHAccount): + return { + "chain": "ETH", + "sender": account.get_address(), + "type": "AGGREGATE", + "item_hash": "0x1234", + } + + +def test_account_create(account_file: Path): + old_key = account_file.read_bytes() + result = runner.invoke( + app, ["account", "create", "--replace", "--private-key-file", str(account_file)] + ) + assert result.exit_code == 0, result.stdout + new_key = account_file.read_bytes() + assert new_key != old_key + + +def test_account_address(account_file: Path): + result = runner.invoke( + app, ["account", "address", "--private-key-file", str(account_file)] + ) + assert result.exit_code == 0 + assert result.stdout.startswith("0x") + assert len(result.stdout.strip()) == 42 + + +def test_account_export_private_key(account_file: Path): + result = runner.invoke( + app, ["account", "export-private-key", "--private-key-file", str(account_file)] + ) + assert result.exit_code == 0 + assert result.stdout.startswith("0x") + assert len(result.stdout.strip()) == 66 + + +def test_account_path(): + result = runner.invoke(app, ["account", "path"]) + assert result.stdout.startswith("/") + + +def test_sign_raw(): + result = runner.invoke( + app, + [ + "account", + "sign-bytes", + "--message", + "some message", + ], + ) + + assert result.exit_code == 0 + assert "0x" in result.stdout + + +def test_sign_raw_stdin(): + message = "some message" + result = runner.invoke( + app, + [ + "account", + "sign-bytes", + ], + input=message, + ) + + assert result.exit_code == 0 + assert "0x" in result.stdout + diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py new file mode 100644 index 00000000..c8eae7eb --- /dev/null +++ b/tests/unit/commands/test_file.py @@ -0,0 +1,33 @@ +from tempfile import NamedTemporaryFile + +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def test_file_upload(): + # Test upload a file to aleph network by creating a file and upload it to an aleph node + with NamedTemporaryFile() as temp_file: + temp_file.write(b"Hello World \n") + result = runner.invoke( + app, + ["file", "upload", temp_file.name], + ) + assert result.exit_code == 0 + assert result.stdout is not None + + +def test_file_download(): + # Test download a file to aleph network + result = runner.invoke( + app, + [ + "file", + "download", + "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH", + ], # 5 bytes file + ) + assert result.exit_code == 0 + assert result.stdout is not None diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py new file mode 100644 index 00000000..17d68ba9 --- /dev/null +++ b/tests/unit/commands/test_message.py @@ -0,0 +1,116 @@ +import json +import os +from pathlib import Path + +from aleph.sdk.chains.ethereum import ETHAccount +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def get_account(my_account_file: Path) -> ETHAccount: + with open(my_account_file, "rb") as fd: + private_key = fd.read() + return ETHAccount(private_key=private_key) + + +def get_test_message(account: ETHAccount): + return { + "chain": "ETH", + "sender": account.get_address(), + "type": "AGGREGATE", + "item_hash": "0x1234", + } + + +def test_message_get(): + # Use subprocess to avoid border effects between tests caused by the initialisation + # of the aiohttp client session out of an async context in the SDK. This avoids + # a "no running event loop" error when running several tests back to back. + result = runner.invoke( + app, + [ + "message", + "get", + "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + ], + ) + assert result.exit_code == 0 + assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout + + +def test_message_find(): + result = runner.invoke( + app, + [ + "message", + "find", + "--pagination=1", + "--page=1", + "--start-date=1234", + "--chains=ETH", + "--hashes=bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + ], + ) + assert result.exit_code == 0 + assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout + assert ( + "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" + in result.stdout + ) + + +def test_post_message(account_file): + test_file_path = Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")).absolute().as_posix() + result = runner.invoke( + app, + [ + "message", + "post", + "--private-key-file", + str(account_file), + "--path", + str(test_file_path), + ], + ) + assert result.exit_code == 0 + assert "item_hash" in result.stdout + + +def test_sign_message(account_file): + account = get_account(account_file) + message = get_test_message(account) + result = runner.invoke( + app, + [ + "message", + "sign", + "--private-key-file", + str(account_file), + "--message", + json.dumps(message), + ], + ) + + assert result.exit_code == 0 + assert "signature" in result.stdout + + +def test_sign_message_stdin(account_file): + account = get_account(account_file) + message = get_test_message(account) + result = runner.invoke( + app, + [ + "message", + "sign", + "--private-key-file", + str(account_file), + ], + input=json.dumps(message), + ) + + assert result.exit_code == 0 + assert "signature" in result.stdout \ No newline at end of file diff --git a/tests/unit/commands/test_node.py b/tests/unit/commands/test_node.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_utils.py b/tests/unit/commands/test_utils.py new file mode 100644 index 00000000..0671bf24 --- /dev/null +++ b/tests/unit/commands/test_utils.py @@ -0,0 +1,18 @@ +from aleph_message.models import ( + AggregateMessage, + ForgetMessage, + MessageType, + PostMessage, + ProgramMessage, + StoreMessage, +) + +from aleph_client.utils import get_message_type_value + + +def test_get_message_type_value(): + assert get_message_type_value(PostMessage) == MessageType.post + assert get_message_type_value(AggregateMessage) == MessageType.aggregate + assert get_message_type_value(StoreMessage) == MessageType.store + assert get_message_type_value(ProgramMessage) == MessageType.program + assert get_message_type_value(ForgetMessage) == MessageType.forget diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py deleted file mode 100644 index 91ef8148..00000000 --- a/tests/unit/test_commands.py +++ /dev/null @@ -1,205 +0,0 @@ -import json -from pathlib import Path -from tempfile import NamedTemporaryFile - -from aleph.sdk.chains.ethereum import ETHAccount -from typer.testing import CliRunner - -from aleph_client.__main__ import app - -runner = CliRunner() - - -def get_account(my_account_file: Path) -> ETHAccount: - with open(my_account_file, "rb") as fd: - private_key = fd.read() - return ETHAccount(private_key=private_key) - - -def get_test_message(account: ETHAccount): - return { - "chain": "ETH", - "sender": account.get_address(), - "type": "AGGREGATE", - "item_hash": "0x1234", - } - - -def test_account_create(account_file: Path): - old_key = account_file.read_bytes() - result = runner.invoke( - app, ["account", "create", "--replace", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0, result.stdout - new_key = account_file.read_bytes() - assert new_key != old_key - - -def test_account_address(account_file: Path): - result = runner.invoke( - app, ["account", "address", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0 - assert result.stdout.startswith("0x") - assert len(result.stdout.strip()) == 42 - - -def test_account_export_private_key(account_file: Path): - result = runner.invoke( - app, ["account", "export-private-key", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0 - assert result.stdout.startswith("0x") - assert len(result.stdout.strip()) == 66 - - -def test_account_path(): - result = runner.invoke(app, ["account", "path"]) - assert result.stdout.startswith("/") - - -def test_message_get(): - # Use subprocess to avoid border effects between tests caused by the initialisation - # of the aiohttp client session out of an async context in the SDK. This avoids - # a "no running event loop" error when running several tests back to back. - result = runner.invoke( - app, - [ - "message", - "get", - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", - ], - ) - assert result.exit_code == 0 - assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - - -def test_message_find(): - result = runner.invoke( - app, - [ - "message", - "find", - "--pagination=1", - "--page=1", - "--start-date=1234", - "--chains=ETH", - "--hashes=bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", - ], - ) - assert result.exit_code == 0 - assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - assert ( - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" - in result.stdout - ) - - -def test_post_message(account_file): - test_file_path = Path(__file__).parent.parent / "test_post.json" - result = runner.invoke( - app, - [ - "message", - "post", - "--private-key-file", - str(account_file), - "--path", - str(test_file_path), - ], - ) - assert result.exit_code == 0 - assert "item_hash" in result.stdout - - -def test_sign_message(account_file): - account = get_account(account_file) - message = get_test_message(account) - result = runner.invoke( - app, - [ - "message", - "sign", - "--private-key-file", - str(account_file), - "--message", - json.dumps(message), - ], - ) - - assert result.exit_code == 0 - assert "signature" in result.stdout - - -def test_sign_message_stdin(account_file): - account = get_account(account_file) - message = get_test_message(account) - result = runner.invoke( - app, - [ - "message", - "sign", - "--private-key-file", - str(account_file), - ], - input=json.dumps(message), - ) - - assert result.exit_code == 0 - assert "signature" in result.stdout - - -def test_sign_raw(): - result = runner.invoke( - app, - [ - "account", - "sign-bytes", - "--message", - "some message", - ], - ) - - assert result.exit_code == 0 - assert "0x" in result.stdout - - -def test_sign_raw_stdin(): - message = "some message" - result = runner.invoke( - app, - [ - "account", - "sign-bytes", - ], - input=message, - ) - - assert result.exit_code == 0 - assert "0x" in result.stdout - - -def test_file_upload(): - # Test upload a file to aleph network by creating a file and upload it to an aleph node - with NamedTemporaryFile() as temp_file: - temp_file.write(b"Hello World \n") - result = runner.invoke( - app, - ["file", "upload", temp_file.name], - ) - assert result.exit_code == 0 - assert result.stdout is not None - - -def test_file_download(): - # Test download a file to aleph network - result = runner.invoke( - app, - [ - "file", - "download", - "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH", - ], # 5 bytes file - ) - assert result.exit_code == 0 - assert result.stdout is not None From 120e20b3b3caa3d6076bd3dce2411b4cbca74698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Thu, 25 Jan 2024 16:29:43 +0000 Subject: [PATCH 003/110] Improving test_utils.py test and applying simple best practices. --- tests/unit/conftest.py | 1 + tests/unit/test_utils.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index ad43487e..532a3ed9 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -24,4 +24,5 @@ def empty_account_file() -> Generator[Path, None, None]: def account_file(empty_account_file: Path) -> Path: private_key = generate_key() empty_account_file.write_bytes(private_key) + return empty_account_file diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 0671bf24..7813919c 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -5,6 +5,7 @@ PostMessage, ProgramMessage, StoreMessage, + InstanceMessage ) from aleph_client.utils import get_message_type_value @@ -15,4 +16,5 @@ def test_get_message_type_value(): assert get_message_type_value(AggregateMessage) == MessageType.aggregate assert get_message_type_value(StoreMessage) == MessageType.store assert get_message_type_value(ProgramMessage) == MessageType.program + assert get_message_type_value(InstanceMessage) == MessageType.instance assert get_message_type_value(ForgetMessage) == MessageType.forget From 7a39210757b2fa86cf165ddff9fcc6256e26c2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Thu, 25 Jan 2024 17:10:57 +0000 Subject: [PATCH 004/110] Improving test files and adding some more new tests. --- tests/unit/commands/test_about.py | 29 +++++ tests/unit/commands/test_account.py | 129 +++++++++++--------- tests/unit/commands/test_file.py | 44 +++---- tests/unit/commands/test_message.py | 180 +++++++++++++++------------- tests/unit/commands/test_root.py | 15 +++ tests/unit/commands/test_utils.py | 18 --- 6 files changed, 235 insertions(+), 180 deletions(-) create mode 100644 tests/unit/commands/test_root.py delete mode 100644 tests/unit/commands/test_utils.py diff --git a/tests/unit/commands/test_about.py b/tests/unit/commands/test_about.py index e69de29b..83041579 100644 --- a/tests/unit/commands/test_about.py +++ b/tests/unit/commands/test_about.py @@ -0,0 +1,29 @@ +import re + +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def test_about_help(): + result = runner.invoke( + app, ["about", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "version" in result.stdout + + +def test_about_version(): + result = runner.invoke( + app, ["about", "version"] + ) + + assert result.exit_code == 1, result.stdout + + pattern = r"Aleph CLI Version: \d+\.\d+\.\d+.*" + + assert re.match(pattern, result.stdout) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index b7d4ec6f..75dce95b 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -1,6 +1,6 @@ from pathlib import Path -from aleph.sdk.chains.ethereum import ETHAccount +import pytest from typer.testing import CliRunner from aleph_client.__main__ import app @@ -8,80 +8,95 @@ runner = CliRunner() -def get_account(my_account_file: Path) -> ETHAccount: - with open(my_account_file, "rb") as fd: - private_key = fd.read() - return ETHAccount(private_key=private_key) +def test_account_help(): + result = runner.invoke( + app, ["account", "--help"] + ) + assert result.exit_code == 0, result.stdout -def get_test_message(account: ETHAccount): - return { - "chain": "ETH", - "sender": account.get_address(), - "type": "AGGREGATE", - "item_hash": "0x1234", - } + assert "Sign a message using your private key." in result.stdout + + +def test_account_address(account_file: Path): + result = runner.invoke( + app, ["account", "address", "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0 + + assert result.stdout.startswith("0x") + + assert len(result.stdout.strip()) == 42 + + +@pytest.mark.skip(reason="Not implemented. It's failing the retrieve the balance for the address.") +def test_account_balance(account_file: Path): + result = runner.invoke( + app, ["account", "balance", "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0 def test_account_create(account_file: Path): - old_key = account_file.read_bytes() - result = runner.invoke( - app, ["account", "create", "--replace", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0, result.stdout - new_key = account_file.read_bytes() - assert new_key != old_key + old_key = account_file.read_bytes() + result = runner.invoke( + app, ["account", "create", "--replace", "--private-key-file", str(account_file)] + ) + assert result.exit_code == 0, result.stdout -def test_account_address(account_file: Path): - result = runner.invoke( - app, ["account", "address", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0 - assert result.stdout.startswith("0x") - assert len(result.stdout.strip()) == 42 + new_key = account_file.read_bytes() + + assert new_key != old_key def test_account_export_private_key(account_file: Path): - result = runner.invoke( - app, ["account", "export-private-key", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0 - assert result.stdout.startswith("0x") - assert len(result.stdout.strip()) == 66 + result = runner.invoke( + app, ["account", "export-private-key", "--private-key-file", str(account_file)] + ) + assert result.exit_code == 0 + + assert result.stdout.startswith("0x") + + assert len(result.stdout.strip()) == 66 def test_account_path(): - result = runner.invoke(app, ["account", "path"]) - assert result.stdout.startswith("/") + result = runner.invoke(app, ["account", "path"]) + + assert result.stdout.startswith("/") def test_sign_raw(): - result = runner.invoke( - app, - [ - "account", - "sign-bytes", - "--message", - "some message", - ], - ) + result = runner.invoke( + app, + [ + "account", + "sign-bytes", + "--message", + "some message", + ], + ) + + assert result.exit_code == 0 - assert result.exit_code == 0 - assert "0x" in result.stdout + assert "0x" in result.stdout def test_sign_raw_stdin(): - message = "some message" - result = runner.invoke( - app, - [ - "account", - "sign-bytes", - ], - input=message, - ) - - assert result.exit_code == 0 - assert "0x" in result.stdout + message = "some message" + result = runner.invoke( + app, + [ + "account", + "sign-bytes", + ], + input=message, + ) + + assert result.exit_code == 0 + + assert "0x" in result.stdout diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index c8eae7eb..a544fba9 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -8,26 +8,30 @@ def test_file_upload(): - # Test upload a file to aleph network by creating a file and upload it to an aleph node - with NamedTemporaryFile() as temp_file: - temp_file.write(b"Hello World \n") - result = runner.invoke( - app, - ["file", "upload", temp_file.name], - ) - assert result.exit_code == 0 - assert result.stdout is not None + # Test upload a file to aleph network by creating a file and upload it to an aleph node + with NamedTemporaryFile() as temp_file: + temp_file.write(b"Hello World \n") + result = runner.invoke( + app, + ["file", "upload", temp_file.name], + ) + + assert result.exit_code == 0 + + assert result.stdout is not None def test_file_download(): - # Test download a file to aleph network - result = runner.invoke( - app, - [ - "file", - "download", - "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH", - ], # 5 bytes file - ) - assert result.exit_code == 0 - assert result.stdout is not None + # Test download a file to aleph network + result = runner.invoke( + app, + [ + "file", + "download", + "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH", + ], # 5 bytes file + ) + + assert result.exit_code == 0 + + assert result.stdout is not None diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index 17d68ba9..a7663148 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -11,106 +11,116 @@ def get_account(my_account_file: Path) -> ETHAccount: - with open(my_account_file, "rb") as fd: - private_key = fd.read() - return ETHAccount(private_key=private_key) + with open(my_account_file, "rb") as fd: + private_key = fd.read() + + return ETHAccount(private_key=private_key) def get_test_message(account: ETHAccount): - return { - "chain": "ETH", - "sender": account.get_address(), - "type": "AGGREGATE", - "item_hash": "0x1234", - } + return { + "chain": "ETH", + "sender": account.get_address(), + "type": "AGGREGATE", + "item_hash": "0x1234", + } def test_message_get(): - # Use subprocess to avoid border effects between tests caused by the initialisation - # of the aiohttp client session out of an async context in the SDK. This avoids - # a "no running event loop" error when running several tests back to back. - result = runner.invoke( - app, - [ - "message", - "get", - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", - ], - ) - assert result.exit_code == 0 - assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout + # Use subprocess to avoid border effects between tests caused by the initialisation + # of the aiohttp client session out of an async context in the SDK. This avoids + # a "no running event loop" error when running several tests back to back. + result = runner.invoke( + app, + [ + "message", + "get", + "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + ], + ) + + assert result.exit_code == 0 + + assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout def test_message_find(): - result = runner.invoke( - app, - [ - "message", - "find", - "--pagination=1", - "--page=1", - "--start-date=1234", - "--chains=ETH", - "--hashes=bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", - ], - ) - assert result.exit_code == 0 - assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - assert ( - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" - in result.stdout - ) + result = runner.invoke( + app, + [ + "message", + "find", + "--pagination=1", + "--page=1", + "--start-date=1234", + "--chains=ETH", + "--hashes=bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + ], + ) + + assert result.exit_code == 0 + + assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout + + assert ( + "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" + in result.stdout + ) def test_post_message(account_file): - test_file_path = Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")).absolute().as_posix() - result = runner.invoke( - app, - [ - "message", - "post", - "--private-key-file", - str(account_file), - "--path", - str(test_file_path), - ], - ) - assert result.exit_code == 0 - assert "item_hash" in result.stdout + test_file_path = Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")).absolute().as_posix() + result = runner.invoke( + app, + [ + "message", + "post", + "--private-key-file", + str(account_file), + "--path", + str(test_file_path), + ], + ) + + assert result.exit_code == 0 + + assert "item_hash" in result.stdout def test_sign_message(account_file): - account = get_account(account_file) - message = get_test_message(account) - result = runner.invoke( - app, - [ - "message", - "sign", - "--private-key-file", - str(account_file), - "--message", - json.dumps(message), - ], - ) - - assert result.exit_code == 0 - assert "signature" in result.stdout + account = get_account(account_file) + message = get_test_message(account) + result = runner.invoke( + app, + [ + "message", + "sign", + "--private-key-file", + str(account_file), + "--message", + json.dumps(message), + ], + ) + + assert result.exit_code == 0 + + assert "signature" in result.stdout def test_sign_message_stdin(account_file): - account = get_account(account_file) - message = get_test_message(account) - result = runner.invoke( - app, - [ - "message", - "sign", - "--private-key-file", - str(account_file), - ], - input=json.dumps(message), - ) - - assert result.exit_code == 0 - assert "signature" in result.stdout \ No newline at end of file + account = get_account(account_file) + message = get_test_message(account) + result = runner.invoke( + app, + [ + "message", + "sign", + "--private-key-file", + str(account_file), + ], + input=json.dumps(message), + ) + + assert result.exit_code == 0 + + assert "signature" in result.stdout \ No newline at end of file diff --git a/tests/unit/commands/test_root.py b/tests/unit/commands/test_root.py new file mode 100644 index 00000000..07b37e05 --- /dev/null +++ b/tests/unit/commands/test_root.py @@ -0,0 +1,15 @@ +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def test_root_help(): + result = runner.invoke( + app, ["--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Upload and update programs on aleph.im VM" in result.stdout diff --git a/tests/unit/commands/test_utils.py b/tests/unit/commands/test_utils.py deleted file mode 100644 index 0671bf24..00000000 --- a/tests/unit/commands/test_utils.py +++ /dev/null @@ -1,18 +0,0 @@ -from aleph_message.models import ( - AggregateMessage, - ForgetMessage, - MessageType, - PostMessage, - ProgramMessage, - StoreMessage, -) - -from aleph_client.utils import get_message_type_value - - -def test_get_message_type_value(): - assert get_message_type_value(PostMessage) == MessageType.post - assert get_message_type_value(AggregateMessage) == MessageType.aggregate - assert get_message_type_value(StoreMessage) == MessageType.store - assert get_message_type_value(ProgramMessage) == MessageType.program - assert get_message_type_value(ForgetMessage) == MessageType.forget From 40f1ac835c66e18d176818bf11291f13796fa5b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 31 Jan 2024 20:02:57 +0000 Subject: [PATCH 005/110] Moving about tests. --- .gitignore | 1 + tests/unit/commands/test_about.py | 10 --------- tests/unit/commands/test_account.py | 10 --------- tests/unit/commands/test_help.py | 35 +++++++++++++++++++++++++++++ tests/unit/commands/test_root.py | 15 ------------- 5 files changed, 36 insertions(+), 35 deletions(-) create mode 100644 tests/unit/commands/test_help.py delete mode 100644 tests/unit/commands/test_root.py diff --git a/.gitignore b/.gitignore index c4734889..b7d34d83 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ __pycache__/* .pydevproject .settings .idea +*.iml tags # Package files diff --git a/tests/unit/commands/test_about.py b/tests/unit/commands/test_about.py index 83041579..176eab8d 100644 --- a/tests/unit/commands/test_about.py +++ b/tests/unit/commands/test_about.py @@ -7,16 +7,6 @@ runner = CliRunner() -def test_about_help(): - result = runner.invoke( - app, ["about", "--help"] - ) - - assert result.exit_code == 0, result.stdout - - assert "version" in result.stdout - - def test_about_version(): result = runner.invoke( app, ["about", "version"] diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index 75dce95b..a59d52c6 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -8,16 +8,6 @@ runner = CliRunner() -def test_account_help(): - result = runner.invoke( - app, ["account", "--help"] - ) - - assert result.exit_code == 0, result.stdout - - assert "Sign a message using your private key." in result.stdout - - def test_account_address(account_file: Path): result = runner.invoke( app, ["account", "address", "--private-key-file", str(account_file)] diff --git a/tests/unit/commands/test_help.py b/tests/unit/commands/test_help.py new file mode 100644 index 00000000..61da0d9a --- /dev/null +++ b/tests/unit/commands/test_help.py @@ -0,0 +1,35 @@ +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def test_root_help(): + result = runner.invoke( + app, ["--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Upload and update programs on aleph.im VM" in result.stdout + + +def test_about_help(): + result = runner.invoke( + app, ["about", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "version" in result.stdout + + +def test_account_help(): + result = runner.invoke( + app, ["account", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Sign a message using your private key." in result.stdout diff --git a/tests/unit/commands/test_root.py b/tests/unit/commands/test_root.py deleted file mode 100644 index 07b37e05..00000000 --- a/tests/unit/commands/test_root.py +++ /dev/null @@ -1,15 +0,0 @@ -from typer.testing import CliRunner - -from aleph_client.__main__ import app - -runner = CliRunner() - - -def test_root_help(): - result = runner.invoke( - app, ["--help"] - ) - - assert result.exit_code == 0, result.stdout - - assert "Upload and update programs on aleph.im VM" in result.stdout From c2443b6e9819b3cad3a462e748d232b048ea5c40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 31 Jan 2024 20:50:55 +0000 Subject: [PATCH 006/110] Adding unit tests for the domain command. --- tests/unit/commands/test_domain.py | 88 ++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index e69de29b..39379909 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -0,0 +1,88 @@ +import re +from pathlib import Path + +import pytest +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_add_ipfs(account_file: Path): + domain = "aleph.im" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + + result = runner.invoke( + app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "ipfs", "--item-hash", item_hash] + ) + + assert result.exit_code == 0, result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_add_program(account_file: Path): + domain = "aleph.im" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + + result = runner.invoke( + app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "program", "--item-hash", item_hash] + ) + + assert result.exit_code == 0, result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_add_instance(account_file: Path): + domain = "aleph.im" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + + result = runner.invoke( + app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "instance", "--item-hash", item_hash] + ) + + assert result.exit_code == 0, result.stdout + + +def test_domain_attach(account_file: Path): + domain = "aleph.im" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + + result = runner.invoke( + app, ["domain", "attach", domain, "--private-key-file", str(account_file), "--item-hash", item_hash] + ) + + assert result.exit_code == 0, result.stdout + + pattern = rf".*Attach resource to: {domain}.*" + + assert re.match(pattern, result.stdout) + + +def test_domain_detach(account_file: Path): + domain = "aleph.im" + + result = runner.invoke( + app, ["domain", "detach", domain, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + pattern = rf".*Detach resource of: {domain}.*" + + assert re.match(pattern, result.stdout) + + +def test_domain_info(account_file: Path): + domain = "aleph.im" + + result = runner.invoke( + app, ["domain", "info", domain, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + pattern = rf".*Domain: {domain} not configured.*" + + assert re.match(pattern, result.stdout) From 0bb4df02e366d14dbf41ff5928e5aded667f1245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 31 Jan 2024 21:11:46 +0000 Subject: [PATCH 007/110] Adding unit tests for the aggregate command. --- tests/unit/commands/test_aggregate.py | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index e69de29b..5d45418b 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -0,0 +1,48 @@ +from pathlib import Path + +import pytest +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +@pytest.mark.skip(reason="Not implemented.") +def test_aggregate_forget(account_file: Path): + key = "key" + channel = "channel" + + result = runner.invoke( + app, ["aggregate", "forget", key, "--channel", channel, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_aggregate_get(account_file: Path): + key = "key" + address = "address" + + result = runner.invoke( + app, ["aggregate", "get", key, "--address", address, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_aggregate_post(account_file: Path): + key = "key" + content = " {'c': 3, 'd': 4}" + address = "address" + channel = "channel" + inline = "no-inline" + sync = "no-sync" + + result = runner.invoke( + app, ["aggregate", "post", key, content, "--address", address, "--channel", channel, "--inline", inline, "--sync", sync, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout From c8266746ed9071f29fe6ecbd1fddacdd728e566d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Wed, 31 Jan 2024 19:21:35 -0300 Subject: [PATCH 008/110] Added a test for node compute --- tests/unit/commands/test_node.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/unit/commands/test_node.py b/tests/unit/commands/test_node.py index e69de29b..65904f4c 100644 --- a/tests/unit/commands/test_node.py +++ b/tests/unit/commands/test_node.py @@ -0,0 +1,20 @@ +import re + +from pathlib import Path + +import pytest +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + +def test_node_compute(): + result = runner.invoke( + app, ["node", "compute"] + ) + assert result.exit_code == 0 + pattern = r".*Compute Node Information.*" + assert re.match(pattern, result.stdout) + # pattern = r".*?([0-9]+\.[0-9]+%|100\.00%|0\.00%).*?([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}).*?([0-9]+\.[0-9]+%|100\.00%).*?([a-z]+).*?" + # assert len(re.findall(pattern, result.stdout, re.MULTILINE)) > 0 \ No newline at end of file From 97ab342f15db0e3799068c4a5e7385e5decaafbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 31 Jan 2024 22:30:01 +0000 Subject: [PATCH 009/110] Adding unit tests for the program command (WIP). --- tests/unit/commands/test_program.py | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index e69de29b..a40d863b 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -0,0 +1,44 @@ +import re +import tempfile +from pathlib import Path + +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def test_program_unpersist(account_file: Path): + item_hash = "098f6bcd4621d373cade4e832627b4f6" + + result = runner.invoke( + app, ["program", "unpersist", item_hash, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + +def test_program_update(account_file: Path): + item_hash = "098f6bcd4621d373cade4e832627b4f6" + path = tempfile.TemporaryFile() + + result = runner.invoke( + app, ["program", "update", item_hash, path, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + +def test_program_upload(account_file: Path): + path = tempfile.TemporaryFile() + entrypoint = "entrypoint" + channel = "channel" + memory = "" + + + result = runner.invoke( + app, ["program", "upload", item_hash, path, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout \ No newline at end of file From f15f6f2b71179b7cd20a2490c859ef4030e20886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Thu, 1 Feb 2024 01:50:36 -0300 Subject: [PATCH 010/110] Adding unit tests for the instance command. --- tests/unit/commands/test_instance.py | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index e69de29b..0319a2e4 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -0,0 +1,60 @@ +from typer.testing import CliRunner +from aleph_client.__main__ import app +import re +import pytest + +runner = CliRunner() +item_hash = None + + +@pytest.mark.skip(reason="Not implemented.") +def test_instance_create(): + global item_hash + + rootfs = "Ubuntu 22" + rootfs_name = "Ubuntu 22" + vcpus = 1 + memory = 256 + rootfs_size = 2000 + + result = runner.invoke( + app, [ + "instance", "create", + "--rootfs", rootfs, + "--rootfs-name", rootfs_name, + "--vcpus", vcpus, + "--memory", memory, + "--rootfs-size", rootfs_size + ] + ) + + assert result.exit_code == 0 + assert result.stdout + + item_hash_regex = r"\b0x[a-fA-F0-9]{40,42}\b" + item_hashes = re.findall(item_hash_regex, result.stdout) + + item_hash = item_hashes[0] if item_hashes else None + + +@pytest.mark.skip(reason="Not implemented.") +def test_instance_delete(): + result = runner.invoke( + app, [ + "instance", "create", + "--item_hash", item_hash, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + + +def test_instance_list(): + result = runner.invoke( + app, ["instance", "list"] + ) + + assert result.exit_code == 0 + assert result.stdout + assert "Item Hash Vcpus Memory Disk size IPv6 address" in result.stdout From 32489231c7413bdaff093d0e8f7b4102362623c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Thu, 1 Feb 2024 23:23:40 +0000 Subject: [PATCH 011/110] Simple change. --- tests/unit/commands/test_aggregate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index 5d45418b..b50ba547 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -35,7 +35,7 @@ def test_aggregate_get(account_file: Path): @pytest.mark.skip(reason="Not implemented.") def test_aggregate_post(account_file: Path): key = "key" - content = " {'c': 3, 'd': 4}" + content = "{'c': 3, 'd': 4}" address = "address" channel = "channel" inline = "no-inline" From 3f03b5058d66608f71033ed4eaab53cd0c1ec978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Tue, 6 Feb 2024 21:58:26 +0000 Subject: [PATCH 012/110] Improving unit tests. --- tests/unit/commands/test_account.py | 10 ++++++++-- tests/unit/commands/test_aggregate.py | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index a59d52c6..f3ddc0e8 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -9,8 +9,11 @@ def test_account_address(account_file: Path): + private_key = None + private_key_file = str(account_file) + result = runner.invoke( - app, ["account", "address", "--private-key-file", str(account_file)] + app, ["account", "address", "--private-key-file", private_key_file] ) assert result.exit_code == 0 @@ -22,8 +25,11 @@ def test_account_address(account_file: Path): @pytest.mark.skip(reason="Not implemented. It's failing the retrieve the balance for the address.") def test_account_balance(account_file: Path): + private_key = None + private_key_file = str(account_file) + result = runner.invoke( - app, ["account", "balance", "--private-key-file", str(account_file)] + app, ["account", "balance", "--private-key-file", private_key_file] ) assert result.exit_code == 0 diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index b50ba547..c81a2bd8 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -40,9 +40,10 @@ def test_aggregate_post(account_file: Path): channel = "channel" inline = "no-inline" sync = "no-sync" + debug = "no-debug" result = runner.invoke( - app, ["aggregate", "post", key, content, "--address", address, "--channel", channel, "--inline", inline, "--sync", sync, "--private-key-file", str(account_file)] + app, ["aggregate", "post", key, content, "--address", address, "--channel", channel, "--inline", inline, "--sync", sync, "--private-key-file", str(account_file), '--debug', debug] ) assert result.exit_code == 0, result.stdout From a5b31389fe99117422b064fbf6f3fb126669398c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Tue, 6 Feb 2024 19:22:23 -0300 Subject: [PATCH 013/110] Improving unittests at test_aggregate.py. --- tests/unit/commands/test_aggregate.py | 53 +++++++++++++++++++++------ 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index b50ba547..d502cc80 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -10,11 +10,21 @@ @pytest.mark.skip(reason="Not implemented.") def test_aggregate_forget(account_file: Path): - key = "key" - channel = "channel" + key = None + channel = None + private_key = None + private_key_file = str(account_file) + reason = None + debug = "--no-debug" result = runner.invoke( - app, ["aggregate", "forget", key, "--channel", channel, "--private-key-file", str(account_file)] + app, [ + "aggregate", "forget", key, + "--channel", channel, + "--reason", reason, + "--private-key-file", private_key_file, + debug + ] ) assert result.exit_code == 0, result.stdout @@ -22,11 +32,19 @@ def test_aggregate_forget(account_file: Path): @pytest.mark.skip(reason="Not implemented.") def test_aggregate_get(account_file: Path): - key = "key" - address = "address" + key = None + address = None + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" result = runner.invoke( - app, ["aggregate", "get", key, "--address", address, "--private-key-file", str(account_file)] + app, [ + "aggregate", "get", key, + "--address", address, + "--private-key-file", private_key_file, + debug + ] ) assert result.exit_code == 0, result.stdout @@ -34,15 +52,26 @@ def test_aggregate_get(account_file: Path): @pytest.mark.skip(reason="Not implemented.") def test_aggregate_post(account_file: Path): - key = "key" - content = "{'c': 3, 'd': 4}" - address = "address" + key = None + content = None + address = None + private_key = None + private_key_file = str(account_file) channel = "channel" - inline = "no-inline" - sync = "no-sync" + inline = "--no-inline" + sync = "--no-sync" + debug = "--no-debug" result = runner.invoke( - app, ["aggregate", "post", key, content, "--address", address, "--channel", channel, "--inline", inline, "--sync", sync, "--private-key-file", str(account_file)] + app, [ + "aggregate", "post", key, content, + "--address", address, + "--channel", channel, + "--private-key-file", private_key_file, + inline, + sync, + debug + ] ) assert result.exit_code == 0, result.stdout From 437ca61b2fdfdf6a84694ce2ed3d02915374d603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Tue, 6 Feb 2024 19:24:55 -0300 Subject: [PATCH 014/110] Improving unit test --- tests/unit/commands/test_about.py | 1 + tests/unit/commands/test_domain.py | 89 +++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/tests/unit/commands/test_about.py b/tests/unit/commands/test_about.py index 176eab8d..7cbc32f4 100644 --- a/tests/unit/commands/test_about.py +++ b/tests/unit/commands/test_about.py @@ -1,5 +1,6 @@ import re +import pytest from typer.testing import CliRunner from aleph_client.__main__ import app diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index 39379909..e8d274e1 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -9,25 +9,108 @@ runner = CliRunner() +@pytest.mark.skip(reason="Not implemented.") +def test_domain_add(account_file: Path): + fqdn = "aleph.im" + private_key = "private_key" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + target = "ipfs" # {ipfs|program|instance} + owner = "owner" + ask = "--ask" # {--ask|--no-ask} + + result = runner.invoke( + app, [ + "domain", "add", fqdn, + "--private-key", private_key, + "--private-key-file", str(account_file), + "--target", target, + "--item-hash", item_hash, + "--owner", owner, + ask + ] + ) + + assert result.exit_code == 0 + assert result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_attach(account_file: Path): + fqdn = "aleph.im" + private_key = "private_key" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + ask = "--ask" # {--ask|--no-ask} + + result = runner.invoke( + app, [ + "domain", "attach", fqdn, + "--private-key", private_key, + "--private-key-file", str(account_file), + "--item-hash", item_hash, + ask + ] + ) + + assert result.exit_code == 0 + assert result.stdout + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_detach(account_file: Path): + fqdn = "aleph.im" + private_key = "private_key" + ask = "--ask" # {--ask|--no-ask} + + result = runner.invoke( + app, [ + "domain", "detach", fqdn, + "--private-key", private_key, + "--private-key-file", str(account_file), + ask + ] + ) + + assert result.exit_code == 0 + assert result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_info(account_file: Path): + fqdn = "aleph.im" + private_key = "private_key" + + result = runner.invoke( + app, [ + "domain", "info", fqdn, + "--private-key", private_key, + "--private-key-file", str(account_file), + ] + ) + + assert result.exit_code == 0 + assert result.stdout + @pytest.mark.skip(reason="Not implemented.") def test_domain_add_ipfs(account_file: Path): domain = "aleph.im" item_hash = "098f6bcd4621d373cade4e832627b4f6" - + result = runner.invoke( app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "ipfs", "--item-hash", item_hash] ) assert result.exit_code == 0, result.stdout - @pytest.mark.skip(reason="Not implemented.") def test_domain_add_program(account_file: Path): domain = "aleph.im" item_hash = "098f6bcd4621d373cade4e832627b4f6" result = runner.invoke( - app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "program", "--item-hash", item_hash] + app, [ + "domain", "add", domain, + "--private-key-file", str(account_file), + "--target", "program", + "--item-hash", item_hash] ) assert result.exit_code == 0, result.stdout From 8e95d7cb6ab4600c1d104aa60f71ce56dabf4ea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Tue, 6 Feb 2024 22:26:22 +0000 Subject: [PATCH 015/110] Improving unit test files. --- tests/unit/commands/test_account.py | 63 ++++++++++++++++++++++++----- tests/unit/commands/test_file.py | 37 +++++++++++++++-- 2 files changed, 87 insertions(+), 13 deletions(-) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index f3ddc0e8..6563fac1 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -13,7 +13,11 @@ def test_account_address(account_file: Path): private_key_file = str(account_file) result = runner.invoke( - app, ["account", "address", "--private-key-file", private_key_file] + app, [ + "account", + "address", + "--private-key-file", private_key_file + ] ) assert result.exit_code == 0 @@ -25,20 +29,38 @@ def test_account_address(account_file: Path): @pytest.mark.skip(reason="Not implemented. It's failing the retrieve the balance for the address.") def test_account_balance(account_file: Path): + address = None private_key = None private_key_file = str(account_file) result = runner.invoke( - app, ["account", "balance", "--private-key-file", private_key_file] + app, [ + "account", + "balance", + "--address", address, + "--private-key-file", private_key_file + ] ) assert result.exit_code == 0 def test_account_create(account_file: Path): + private_key = None + private_key_file = str(account_file) + replace = "--replace" + debug = "--no-debug" + old_key = account_file.read_bytes() + result = runner.invoke( - app, ["account", "create", "--replace", "--private-key-file", str(account_file)] + app, [ + "account", + "create", + "--private-key-file", private_key_file, + replace, + debug + ] ) assert result.exit_code == 0, result.stdout @@ -49,8 +71,15 @@ def test_account_create(account_file: Path): def test_account_export_private_key(account_file: Path): + private_key = None + private_key_file = str(account_file) + result = runner.invoke( - app, ["account", "export-private-key", "--private-key-file", str(account_file)] + app, [ + "account", + "export-private-key", + "--private-key-file", private_key_file + ] ) assert result.exit_code == 0 @@ -60,19 +89,28 @@ def test_account_export_private_key(account_file: Path): def test_account_path(): - result = runner.invoke(app, ["account", "path"]) + result = runner.invoke(app, [ + "account", + "path" + ]) assert result.stdout.startswith("/") -def test_sign_raw(): +def test_sign_bytes_raw(account_file: Path): + message = "some message" + private_key = "" + private_key_file = str(account_file) + debug = "--no-debug" + result = runner.invoke( app, [ "account", "sign-bytes", - "--message", - "some message", + "--message", message, + "--private-key-file", private_key_file, + debug ], ) @@ -81,13 +119,19 @@ def test_sign_raw(): assert "0x" in result.stdout -def test_sign_raw_stdin(): +def test_sign_bytes_raw_stdin(account_file: Path): message = "some message" + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" + result = runner.invoke( app, [ "account", "sign-bytes", + "--private-key-file", private_key_file, + debug ], input=message, ) @@ -95,4 +139,3 @@ def test_sign_raw_stdin(): assert result.exit_code == 0 assert "0x" in result.stdout - diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index a544fba9..95ab6ac2 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -1,3 +1,4 @@ +from pathlib import Path from tempfile import NamedTemporaryFile from typer.testing import CliRunner @@ -7,13 +8,31 @@ runner = CliRunner() -def test_file_upload(): +def test_file_upload(account_file: Path): + path = None + channel = None + private_key = None + private_key_file = str(account_file) + ref = None + debug = "--no-debug" + # Test upload a file to aleph network by creating a file and upload it to an aleph node with NamedTemporaryFile() as temp_file: temp_file.write(b"Hello World \n") + + path = temp_file.name + result = runner.invoke( app, - ["file", "upload", temp_file.name], + [ + "file", + "upload", + path, + "--channel", channel, + "--private-key-file", private_key_file, + ref, + debug + ], ) assert result.exit_code == 0 @@ -22,13 +41,25 @@ def test_file_upload(): def test_file_download(): + hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" + use_ipfs = "--no-use-ipfs" + output_path = "." + file_name = None + file_extension = None + debug = "--no-debug" + # Test download a file to aleph network result = runner.invoke( app, [ "file", "download", - "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH", + hash, + use_ipfs, + "--output-path", output_path, + "--file-name", file_name, + "--file-extension", file_extension, + debug ], # 5 bytes file ) From c1c857fec8b7441caa5a638afe8b46f2abee17b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 20:20:14 +0000 Subject: [PATCH 016/110] Updating unit tests from domain command. --- tests/unit/commands/test_domain.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index e8d274e1..1ad8583b 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -12,7 +12,8 @@ @pytest.mark.skip(reason="Not implemented.") def test_domain_add(account_file: Path): fqdn = "aleph.im" - private_key = "private_key" + private_key = None + private_key_file = str(account_file) item_hash = "098f6bcd4621d373cade4e832627b4f6" target = "ipfs" # {ipfs|program|instance} owner = "owner" @@ -21,8 +22,7 @@ def test_domain_add(account_file: Path): result = runner.invoke( app, [ "domain", "add", fqdn, - "--private-key", private_key, - "--private-key-file", str(account_file), + "--private-key-file", private_key_file, "--target", target, "--item-hash", item_hash, "--owner", owner, From 1351412ccd3006f519d9f234deb46263406a77be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 17:24:56 -0300 Subject: [PATCH 017/110] Improving unittests at test_help.py. --- tests/unit/commands/test_help.py | 70 ++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/unit/commands/test_help.py b/tests/unit/commands/test_help.py index 61da0d9a..adbdf19e 100644 --- a/tests/unit/commands/test_help.py +++ b/tests/unit/commands/test_help.py @@ -33,3 +33,73 @@ def test_account_help(): assert result.exit_code == 0, result.stdout assert "Sign a message using your private key." in result.stdout + + +def test_aggregate_help(): + result = runner.invoke( + app, ["aggregate", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Manage aggregate messages on aleph.im" in result.stdout + + +def test_domain_help(): + result = runner.invoke( + app, ["domain", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Manage custom Domain (dns) on aleph.im" in result.stdout + + +def test_file_help(): + result = runner.invoke( + app, ["file", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "File uploading and pinning on IPFS and aleph.im" in result.stdout + + +def test_instance_help(): + result = runner.invoke( + app, ["instance", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Manage instances (VMs) on aleph.im network" in result.stdout + + +def test_message_help(): + result = runner.invoke( + app, ["message", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Post, amend, watch and forget messages on aleph.im" in result.stdout + + +def test_node_help(): + result = runner.invoke( + app, ["node", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Get node info on aleph.im network" in result.stdout + + +def test_program_help(): + result = runner.invoke( + app, ["program", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Upload and update programs on aleph.im VM" in result.stdout From b4d19a7d9d9d8c117c9e6bd270f3ba69cfd23aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 20:33:35 +0000 Subject: [PATCH 018/110] Adding unit tests for the file command. --- tests/unit/commands/test_file.py | 104 +++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index 95ab6ac2..ede38566 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -66,3 +66,107 @@ def test_file_download(): assert result.exit_code == 0 assert result.stdout is not None + + +def test_file_forget(account_file: Path): + item_hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" + reason = "reason" + channel = "" + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" + + result = runner.invoke( + app, + [ + "file", + "forget", + item_hash, + reason, + "--channel", channel, + "--private-key-file", private_key_file, + debug + ], + ) + + assert result.exit_code == 0 + + assert result.stdout is not None + + +def test_file_forget(account_file: Path): + item_hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" + reason = "reason" + channel = "" + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" + + result = runner.invoke( + app, + [ + "file", + "forget", + item_hash, + reason, + "--channel", channel, + "--private-key-file", private_key_file, + debug + ], + ) + + assert result.exit_code == 0 + + assert result.stdout is not None + + +def test_file_list(account_file: Path): + address = None + private_key = None + private_key_file = str(account_file) + pagination = 100 + page = 1 + sort_order = -1 + json = "--no-json" + + result = runner.invoke( + app, + [ + "file", + "list", + "--address", address, + "--private-key-file", private_key_file, + "--pagination", pagination, + "--page", page, + "--sort-order", sort_order, + "--json", json + ], # 5 bytes file + ) + + assert result.exit_code == 0 + + assert result.stdout is not None + + +def test_file_pin(account_file: Path): + item_hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" + channel = None + private_key = None + private_key_file = str(account_file) + ref = None + debug = "--no-debug" + + result = runner.invoke( + app, + [ + "file", + "pin", + item_hash, + "--channel", channel, + "--private-key-file", private_key_file, + "--ref", ref, + debug + ], + ) + + assert result.exit_code == 0 From 9f9ab142c357c71e6d57b0900a8277c1ec23420d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Wed, 7 Feb 2024 17:35:47 -0300 Subject: [PATCH 019/110] Updating unit tests from instance command. --- tests/unit/commands/test_instance.py | 84 ++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index 0319a2e4..14db8997 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -6,6 +6,90 @@ runner = CliRunner() item_hash = None +@pytest.mark.skip(reason="Not implemented.") +def test_instance_create(account_file: Path): + channel = "channel" + memory = "memory" + vcpus = "vcpus" + timeout_seconds = "timeout_seconds" + private_key = None + private_key_file = str(account_file) + ssh_pubkey_file = "ssh_pubkey_file" + print_messages = "--print-messages" #[--print-messages|--no-print-messages] + rootfs = "rootfs" + rootfs_name = "rootfs_name" + rootfs_size = "rootfs_size" + debug = "--debug" # [--debug|--no-debug] + persistent_volume = "persistent_volume" + ephemeral_volume = "ephemeral_volume" + immutable_volume = "immutable_volume" + + result = runner.invoke( + app, [ + "instance", "create", + "--channel", channel, + "--memory", memory, + "--vcpus", vcpus, + "--timeout-seconds", timeout_seconds, + "--private-key-file", private_key_file, + "--ssh-pubkey-file", ssh_pubkey_file, + print_messages, + "--rootfs", rootfs, + "--rootfs-name", rootfs_name, + "--rootfs-size", rootfs_size, + debug, + "--persistent-volume", persistent_volume, + "--ephemeral-volume", ephemeral_volume, + "--imutable-volume", immutable_volume + ] + ) + + assert result.exit_code == 0 + assert result.stdout + +@pytest.mark.skip(reason="Not implemented.") +def test_instance_delete(account_file: Path): + item_hash = "item_hash" + reason = "reason" + private_key = None + private_key_file = str(account_file) + print_messages = "--print-messages" #[--print-messages|--no-print-messages] + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "instance", "delete", + item_hash, + "--reason", reason, + "--private-key-file", private_key_file, + print_messages, + debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + +@pytest.mark.skip(reason="Not implemented.") +def test_instance_list(account_file: Path): + address = "address" + private_key = None + private_key_file = str(account_file) + json = "--json" # [--json|--no-json] + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "instance", "list", + "--address", address + "--private-key-file", private_key_file, + json, + debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout @pytest.mark.skip(reason="Not implemented.") def test_instance_create(): From 3ebbd3259eb879ce1da433daa7bb4ff90fba370c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Wed, 7 Feb 2024 17:39:35 -0300 Subject: [PATCH 020/110] Updating unit tests from instance command. --- tests/unit/commands/test_instance.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index 14db8997..68bec088 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -1,4 +1,5 @@ from typer.testing import CliRunner +from pathlib import Path from aleph_client.__main__ import app import re import pytest @@ -81,7 +82,7 @@ def test_instance_list(account_file: Path): result = runner.invoke( app, [ "instance", "list", - "--address", address + "--address", address, "--private-key-file", private_key_file, json, debug, From bc19380774ee7c8c39d4765c6b2cb6a9b05d9d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 20:39:39 +0000 Subject: [PATCH 021/110] Adding unit tests for the node command. --- tests/unit/commands/test_node.py | 42 ++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/tests/unit/commands/test_node.py b/tests/unit/commands/test_node.py index 65904f4c..334be36d 100644 --- a/tests/unit/commands/test_node.py +++ b/tests/unit/commands/test_node.py @@ -1,20 +1,52 @@ import re -from pathlib import Path - -import pytest from typer.testing import CliRunner from aleph_client.__main__ import app runner = CliRunner() + def test_node_compute(): + json = "--no-json" + active = "--no-active" + address = None + debug = "--no-debug" + result = runner.invoke( - app, ["node", "compute"] + app, [ + "node", + "compute", + json, + active, + "--address", address, + debug + ] ) + assert result.exit_code == 0 + pattern = r".*Compute Node Information.*" assert re.match(pattern, result.stdout) # pattern = r".*?([0-9]+\.[0-9]+%|100\.00%|0\.00%).*?([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}).*?([0-9]+\.[0-9]+%|100\.00%).*?([a-z]+).*?" - # assert len(re.findall(pattern, result.stdout, re.MULTILINE)) > 0 \ No newline at end of file + # assert len(re.findall(pattern, result.stdout, re.MULTILINE)) > 0 + + +def test_node_core(): + json = "--no-json" + active = "--no-active" + address = None + debug = "--no-debug" + + result = runner.invoke( + app, [ + "node", + "core", + json, + active, + "--address", address, + debug + ] + ) + + assert result.exit_code == 0 From 3bfed9b23845e9946ab35c5873f3f840af05dcbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 20:56:46 +0000 Subject: [PATCH 022/110] Adding unit tests for the help command. --- tests/unit/commands/test_help.py | 373 ++++++++++++++++++++++++++++++- 1 file changed, 363 insertions(+), 10 deletions(-) diff --git a/tests/unit/commands/test_help.py b/tests/unit/commands/test_help.py index adbdf19e..40a38f14 100644 --- a/tests/unit/commands/test_help.py +++ b/tests/unit/commands/test_help.py @@ -7,7 +7,9 @@ def test_root_help(): result = runner.invoke( - app, ["--help"] + app, [ + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -17,7 +19,10 @@ def test_root_help(): def test_about_help(): result = runner.invoke( - app, ["about", "--help"] + app, [ + "about", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -25,9 +30,24 @@ def test_about_help(): assert "version" in result.stdout +def test_about_version_help(): + result = runner.invoke( + app, [ + "about", + "version" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_account_help(): result = runner.invoke( - app, ["account", "--help"] + app, [ + "account", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -35,9 +55,96 @@ def test_account_help(): assert "Sign a message using your private key." in result.stdout +def test_account_address_help(): + result = runner.invoke( + app, [ + "account", + "address" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_balance_help(): + result = runner.invoke( + app, [ + "account", + "address" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_balance_help(): + result = runner.invoke( + app, [ + "account", + "balance" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_create_help(): + result = runner.invoke( + app, [ + "account", + "create" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_export_private_key_help(): + result = runner.invoke( + app, [ + "account", + "export-private-key" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_path_help(): + result = runner.invoke( + app, [ + "account", + "path" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_sign_bytes(): + result = runner.invoke( + app, [ + "account", + "sign-bytes" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_aggregate_help(): result = runner.invoke( - app, ["aggregate", "--help"] + app, [ + "aggregate", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -45,9 +152,48 @@ def test_aggregate_help(): assert "Manage aggregate messages on aleph.im" in result.stdout +def test_aggregate_forget_help(): + result = runner.invoke( + app, [ + "aggregate", + "forget" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_aggregate_get_help(): + result = runner.invoke( + app, [ + "aggregate", + "get" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_aggregate_post_help(): + result = runner.invoke( + app, [ + "aggregate", + "post" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_domain_help(): result = runner.invoke( - app, ["domain", "--help"] + app, [ + "domain", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -55,9 +201,60 @@ def test_domain_help(): assert "Manage custom Domain (dns) on aleph.im" in result.stdout +def test_domain_add_help(): + result = runner.invoke( + app, [ + "domain", + "add" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_domain_attach_help(): + result = runner.invoke( + app, [ + "domain", + "attach" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_domain_detach_help(): + result = runner.invoke( + app, [ + "domain", + "detach" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_domain_info_help(): + result = runner.invoke( + app, [ + "domain", + "info" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_file_help(): result = runner.invoke( - app, ["file", "--help"] + app, [ + "file", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -65,9 +262,72 @@ def test_file_help(): assert "File uploading and pinning on IPFS and aleph.im" in result.stdout +def test_file_download_help(): + result = runner.invoke( + app, [ + "file", + "download" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_file_forget_help(): + result = runner.invoke( + app, [ + "file", + "forget" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_file_list_help(): + result = runner.invoke( + app, [ + "file", + "list" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_file_pin_help(): + result = runner.invoke( + app, [ + "file", + "pin" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_file_upload_help(): + result = runner.invoke( + app, [ + "file", + "upload" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_instance_help(): result = runner.invoke( - app, ["instance", "--help"] + app, [ + "instance", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -75,9 +335,48 @@ def test_instance_help(): assert "Manage instances (VMs) on aleph.im network" in result.stdout +def test_instance_create_help(): + result = runner.invoke( + app, [ + "instance", + "create" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_instance_delete_help(): + result = runner.invoke( + app, [ + "instance", + "delete" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_instance_list_help(): + result = runner.invoke( + app, [ + "instance", + "list" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_message_help(): result = runner.invoke( - app, ["message", "--help"] + app, [ + "message", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -85,9 +384,60 @@ def test_message_help(): assert "Post, amend, watch and forget messages on aleph.im" in result.stdout +def test_message_amend_help(): + result = runner.invoke( + app, [ + "message", + "amend", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_find_help(): + result = runner.invoke( + app, [ + "message", + "find", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_find_help(): + result = runner.invoke( + app, [ + "message", + "find", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_forget_help(): + result = runner.invoke( + app, [ + "message", + "forget", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_node_help(): result = runner.invoke( - app, ["node", "--help"] + app, [ + "node", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -97,7 +447,10 @@ def test_node_help(): def test_program_help(): result = runner.invoke( - app, ["program", "--help"] + app, [ + "program", + "--help" + ] ) assert result.exit_code == 0, result.stdout From cdee5448c0f887baf678ddd6dec46b713620a7a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 17:57:15 -0300 Subject: [PATCH 023/110] Improving unittests at test_message.py (wip). --- tests/unit/commands/test_message.py | 101 +++++++++++++++++++++------- 1 file changed, 77 insertions(+), 24 deletions(-) diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index a7663148..40cb6d25 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -1,5 +1,6 @@ import json import os +import pytest from pathlib import Path from aleph.sdk.chains.ethereum import ETHAccount @@ -26,35 +27,60 @@ def get_test_message(account: ETHAccount): } -def test_message_get(): - # Use subprocess to avoid border effects between tests caused by the initialisation - # of the aiohttp client session out of an async context in the SDK. This avoids - # a "no running event loop" error when running several tests back to back. +@pytest.mark.skip(reason="Not implemented.") +def test_message_amend(account_file: Path): + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" + result = runner.invoke( app, [ "message", - "get", - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + "amend", + "--private-key-file", private_key_file, + debug ], ) assert result.exit_code == 0 - assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - def test_message_find(): + pagination = 1 + page = 1 + message_types = None + content_types = None + content_keys = None + refs = None + addresses = None + tags = None + hashes = "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" + channels = None + chains = "ETH" + start_date = 1234 + end_date = None + ignore_invalid_messages = "--no-ignore-invalid-messages" + result = runner.invoke( app, [ "message", "find", - "--pagination=1", - "--page=1", - "--start-date=1234", - "--chains=ETH", - "--hashes=bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + "--pagination", pagination, + "--page", page, + "--message-types", message_types, + "--content-types", content_types, + "--content-keys", content_keys, + "--refs", refs, + "--addresses", addresses, + "--tags", tags, + "--hashes", hashes, + "--channels", channels, + "--chains", chains, + "--start-date", start_date, + "--end-date", end_date, + "--ignore-invalid-messages", ignore_invalid_messages ], ) @@ -62,23 +88,50 @@ def test_message_find(): assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - assert ( - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" - in result.stdout + assert ("bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" in result.stdout) + + +def test_message_get(): + # Use subprocess to avoid border effects between tests caused by the initialisation + # of the aiohttp client session out of an async context in the SDK. This avoids + # a "no running event loop" error when running several tests back to back. + + item_hash = "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" + + result = runner.invoke( + app, + [ + "message", + "get", + item_hash, + ], ) + assert result.exit_code == 0 + + assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout + + +def test_message_post(account_file): + test_file_path = Path(os.path.join( + Path(__file__).parent.parent.parent, "fixtures", "post.json") + ).absolute().as_posix() + + path = str(test_file_path), + type = "test" + ref = None + channel = None + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" -def test_post_message(account_file): - test_file_path = Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")).absolute().as_posix() result = runner.invoke( app, [ "message", "post", - "--private-key-file", - str(account_file), - "--path", - str(test_file_path), + "--private-key-file", private_key_file, + "--path", path ], ) @@ -87,7 +140,7 @@ def test_post_message(account_file): assert "item_hash" in result.stdout -def test_sign_message(account_file): +def test_message_sign(account_file): account = get_account(account_file) message = get_test_message(account) result = runner.invoke( @@ -107,7 +160,7 @@ def test_sign_message(account_file): assert "signature" in result.stdout -def test_sign_message_stdin(account_file): +def test_message_sign_stdin(account_file): account = get_account(account_file) message = get_test_message(account) result = runner.invoke( From 3888b878a82bec7df446d36374e0628de519adee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Wed, 7 Feb 2024 17:56:48 -0300 Subject: [PATCH 024/110] Updating unit tests from program command. --- tests/unit/commands/test_program.py | 73 +++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index a40d863b..2bfaeb0f 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -5,10 +5,83 @@ from typer.testing import CliRunner from aleph_client.__main__ import app +import pytest runner = CliRunner() +@pytest.mark.skip(reason="Not implemented.") +def test_program_unpersist(account_file: Path): + item_hash = "item_hash" + private_key = None + private_key_file = str(account_file) + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "program", "unpersist", item_hash, + "--private-key-file", private_key_file, + debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + +@pytest.mark.skip(reason="Not implemented.") +def test_program_upload(account_file: Path): + path = "path" + entrypoint = "entrypoint" + channel = "channel" + memory = "memory" + vcpus = "vcpus" + timeout_seconds = "tomeout_seconds" + private_key = None + private_key_file = str(account_file) + print_messages = "--print-messages" # [--print-message|--no-print-message] + print_code_message = "--print-code-message" # [--print-code-message|--no-print-code-message] + print_program_message = "--print-program-message" # [--print-program-message|--no-print-program-message] + runtime = "runtime" + beta = "--beta" # [--beta|--no-beta] + debug = "--debug" # [--debug|--no-debug] + persistent = "--persistent" # [--persistent|--no-persistent] + persistent_volume = "persistent_volume" + + result = runner.invoke( + app, [ + "program", "update", path, entrypoint, + + + "--private-key-file", private_key_file, + print_message, + debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + +@pytest.mark.skip(reason="Not implemented.") +def test_program_update(account_file: Path): + item_hash = "item_hash" + path = "path" + private_key = None + private_key_file = str(account_file) + print_message = "--print-message" # [--print-message|--no-print-message] + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "program", "update", item_hash, path, + "--private-key-file", private_key_file, + print_message, + debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + def test_program_unpersist(account_file: Path): item_hash = "098f6bcd4621d373cade4e832627b4f6" From e1c58157c5320805edc0d61b4ae3cedb394ca85c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Wed, 21 Feb 2024 18:09:41 -0300 Subject: [PATCH 025/110] Added tests on test_program.py --- tests/unit/commands/test_program.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 2bfaeb0f..1139c334 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -46,15 +46,28 @@ def test_program_upload(account_file: Path): debug = "--debug" # [--debug|--no-debug] persistent = "--persistent" # [--persistent|--no-persistent] persistent_volume = "persistent_volume" + ephemeral_volume = "ephemeral_volume" + immutable_volume = "immutable_volume" + result = runner.invoke( app, [ "program", "update", path, entrypoint, - - + "--channel", channel, + "--memory", memory, + "--vcpus", vcpus, + "--timeout-seconds", timeout_seconds, "--private-key-file", private_key_file, - print_message, + print_messages, + print_code_message, + print_program_message, + "--runtime", runtime, + beta, debug, + persistent, + "--persistent-volume", persistent_volume, + "--ephemeral-volume", ephemeral_volume, + "--immutable-volume", immutable_volume ] ) From abadc5346c6a5a14068091479cbcb574cf46d78b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 21 Feb 2024 21:14:56 +0000 Subject: [PATCH 026/110] Improving and fixing unit tests. --- tests/unit/commands/test_help.py | 160 +++++++++++++++++++++++-------- 1 file changed, 122 insertions(+), 38 deletions(-) diff --git a/tests/unit/commands/test_help.py b/tests/unit/commands/test_help.py index 40a38f14..2b8eb459 100644 --- a/tests/unit/commands/test_help.py +++ b/tests/unit/commands/test_help.py @@ -34,7 +34,7 @@ def test_about_version_help(): result = runner.invoke( app, [ "about", - "version" + "version", "--help" ] ) @@ -59,7 +59,7 @@ def test_account_address_help(): result = runner.invoke( app, [ "account", - "address" + "address", "--help" ] ) @@ -71,19 +71,7 @@ def test_account_balance_help(): result = runner.invoke( app, [ "account", - "address" - "--help" - ] - ) - - assert result.exit_code == 0, result.stdout - - -def test_account_balance_help(): - result = runner.invoke( - app, [ - "account", - "balance" + "balance", "--help" ] ) @@ -95,7 +83,7 @@ def test_account_create_help(): result = runner.invoke( app, [ "account", - "create" + "create", "--help" ] ) @@ -107,7 +95,7 @@ def test_account_export_private_key_help(): result = runner.invoke( app, [ "account", - "export-private-key" + "export-private-key", "--help" ] ) @@ -119,7 +107,7 @@ def test_account_path_help(): result = runner.invoke( app, [ "account", - "path" + "path", "--help" ] ) @@ -131,7 +119,7 @@ def test_account_sign_bytes(): result = runner.invoke( app, [ "account", - "sign-bytes" + "sign-bytes", "--help" ] ) @@ -156,7 +144,7 @@ def test_aggregate_forget_help(): result = runner.invoke( app, [ "aggregate", - "forget" + "forget", "--help" ] ) @@ -168,7 +156,7 @@ def test_aggregate_get_help(): result = runner.invoke( app, [ "aggregate", - "get" + "get", "--help" ] ) @@ -180,7 +168,7 @@ def test_aggregate_post_help(): result = runner.invoke( app, [ "aggregate", - "post" + "post", "--help" ] ) @@ -205,7 +193,7 @@ def test_domain_add_help(): result = runner.invoke( app, [ "domain", - "add" + "add", "--help" ] ) @@ -217,7 +205,7 @@ def test_domain_attach_help(): result = runner.invoke( app, [ "domain", - "attach" + "attach", "--help" ] ) @@ -229,7 +217,7 @@ def test_domain_detach_help(): result = runner.invoke( app, [ "domain", - "detach" + "detach", "--help" ] ) @@ -241,7 +229,7 @@ def test_domain_info_help(): result = runner.invoke( app, [ "domain", - "info" + "info", "--help" ] ) @@ -266,7 +254,7 @@ def test_file_download_help(): result = runner.invoke( app, [ "file", - "download" + "download", "--help" ] ) @@ -278,7 +266,7 @@ def test_file_forget_help(): result = runner.invoke( app, [ "file", - "forget" + "forget", "--help" ] ) @@ -290,7 +278,7 @@ def test_file_list_help(): result = runner.invoke( app, [ "file", - "list" + "list", "--help" ] ) @@ -302,7 +290,7 @@ def test_file_pin_help(): result = runner.invoke( app, [ "file", - "pin" + "pin", "--help" ] ) @@ -314,7 +302,7 @@ def test_file_upload_help(): result = runner.invoke( app, [ "file", - "upload" + "upload", "--help" ] ) @@ -339,7 +327,7 @@ def test_instance_create_help(): result = runner.invoke( app, [ "instance", - "create" + "create", "--help" ] ) @@ -351,7 +339,7 @@ def test_instance_delete_help(): result = runner.invoke( app, [ "instance", - "delete" + "delete", "--help" ] ) @@ -363,7 +351,7 @@ def test_instance_list_help(): result = runner.invoke( app, [ "instance", - "list" + "list", "--help" ] ) @@ -408,11 +396,11 @@ def test_message_find_help(): assert result.exit_code == 0, result.stdout -def test_message_find_help(): +def test_message_forget_help(): result = runner.invoke( app, [ "message", - "find", + "forget", "--help" ] ) @@ -420,11 +408,47 @@ def test_message_find_help(): assert result.exit_code == 0, result.stdout -def test_message_forget_help(): +def test_message_get_help(): result = runner.invoke( app, [ "message", - "forget", + "get", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_post_help(): + result = runner.invoke( + app, [ + "message", + "post", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_sign_help(): + result = runner.invoke( + app, [ + "message", + "sign", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_watch_help(): + result = runner.invoke( + app, [ + "message", + "watch", "--help" ] ) @@ -445,6 +469,30 @@ def test_node_help(): assert "Get node info on aleph.im network" in result.stdout +def test_node_compute_help(): + result = runner.invoke( + app, [ + "node", + "compute", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_node_core_help(): + result = runner.invoke( + app, [ + "node", + "core", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_program_help(): result = runner.invoke( app, [ @@ -456,3 +504,39 @@ def test_program_help(): assert result.exit_code == 0, result.stdout assert "Upload and update programs on aleph.im VM" in result.stdout + + +def test_program_unpersist_help(): + result = runner.invoke( + app, [ + "program", + "unpersist", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_program_update_help(): + result = runner.invoke( + app, [ + "program", + "update", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_program_upload_help(): + result = runner.invoke( + app, [ + "program", + "upload", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout From 2045fd955e6a5bdceeb8370fdea1320817132d04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 21 Feb 2024 21:19:57 +0000 Subject: [PATCH 027/110] Enabling unit test. --- tests/unit/commands/test_account.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index 6563fac1..7054662d 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -27,7 +27,6 @@ def test_account_address(account_file: Path): assert len(result.stdout.strip()) == 42 -@pytest.mark.skip(reason="Not implemented. It's failing the retrieve the balance for the address.") def test_account_balance(account_file: Path): address = None private_key = None From af991d66c30dc670641f8dfa5012a54593a812b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Mon, 26 Feb 2024 22:26:32 +0000 Subject: [PATCH 028/110] Fixing and improving account unit tests. --- tests/unit/commands/test_account.py | 37 +++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index 7054662d..bd440f3b 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -1,6 +1,5 @@ +import re from pathlib import Path - -import pytest from typer.testing import CliRunner from aleph_client.__main__ import app @@ -9,7 +8,7 @@ def test_account_address(account_file: Path): - private_key = None + # private_key = None private_key_file = str(account_file) result = runner.invoke( @@ -22,14 +21,16 @@ def test_account_address(account_file: Path): assert result.exit_code == 0 - assert result.stdout.startswith("0x") + pattern = r"0x.*" + assert re.match(pattern, result.stdout) assert len(result.stdout.strip()) == 42 +# TODO Verify if the output message ""Failed to retrieve balance..." is ok!!! def test_account_balance(account_file: Path): address = None - private_key = None + # private_key = None private_key_file = str(account_file) result = runner.invoke( @@ -41,11 +42,14 @@ def test_account_balance(account_file: Path): ] ) + pattern = r"Failed to retrieve balance for address 0x.*\. Status code: 404" + assert re.match(pattern, result.stdout) + assert result.exit_code == 0 def test_account_create(account_file: Path): - private_key = None + # private_key = None private_key_file = str(account_file) replace = "--replace" debug = "--no-debug" @@ -66,11 +70,14 @@ def test_account_create(account_file: Path): new_key = account_file.read_bytes() + pattern = r"Private key stored in .*" + assert re.match(pattern, result.stdout) + assert new_key != old_key def test_account_export_private_key(account_file: Path): - private_key = None + # private_key = None private_key_file = str(account_file) result = runner.invoke( @@ -84,6 +91,9 @@ def test_account_export_private_key(account_file: Path): assert result.stdout.startswith("0x") + pattern = r"0x.*" + assert re.match(pattern, result.stdout) + assert len(result.stdout.strip()) == 66 @@ -93,12 +103,13 @@ def test_account_path(): "path" ]) - assert result.stdout.startswith("/") + pattern = r".*.aleph-im/private-keys/ethereum\.key" + assert re.match(pattern, result.stdout) def test_sign_bytes_raw(account_file: Path): message = "some message" - private_key = "" + # private_key = None private_key_file = str(account_file) debug = "--no-debug" @@ -115,12 +126,13 @@ def test_sign_bytes_raw(account_file: Path): assert result.exit_code == 0 - assert "0x" in result.stdout + pattern = r"0x.*" + assert re.match(pattern, result.stdout) def test_sign_bytes_raw_stdin(account_file: Path): message = "some message" - private_key = None + # private_key = None private_key_file = str(account_file) debug = "--no-debug" @@ -137,4 +149,5 @@ def test_sign_bytes_raw_stdin(account_file: Path): assert result.exit_code == 0 - assert "0x" in result.stdout + pattern = r"0x.*" + assert re.match(pattern, result.stdout) From 5510fe9a48400fbebcf5035d650749e286133cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Mon, 26 Feb 2024 22:33:29 +0000 Subject: [PATCH 029/110] Fixing and improving aggregate unit tests. --- tests/unit/commands/test_aggregate.py | 42 +++++++++++++-------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index d502cc80..948a49e6 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -8,21 +8,26 @@ runner = CliRunner() -@pytest.mark.skip(reason="Not implemented.") -def test_aggregate_forget(account_file: Path): - key = None - channel = None - private_key = None +# TODO Stopped here!!! +def test_aggregate_post(account_file: Path): + key = "key" + content = """{"c": 3, "d": "test"}""" + address = None + # private_key = None private_key_file = str(account_file) - reason = None + channel = "channel" + inline = "--no-inline" + sync = "--no-sync" debug = "--no-debug" result = runner.invoke( app, [ - "aggregate", "forget", key, + "aggregate", "post", key, content, + "--address", address, "--channel", channel, - "--reason", reason, "--private-key-file", private_key_file, + inline, + sync, debug ] ) @@ -30,11 +35,10 @@ def test_aggregate_forget(account_file: Path): assert result.exit_code == 0, result.stdout -@pytest.mark.skip(reason="Not implemented.") def test_aggregate_get(account_file: Path): key = None address = None - private_key = None + # private_key = None private_key_file = str(account_file) debug = "--no-debug" @@ -50,26 +54,20 @@ def test_aggregate_get(account_file: Path): assert result.exit_code == 0, result.stdout -@pytest.mark.skip(reason="Not implemented.") -def test_aggregate_post(account_file: Path): +def test_aggregate_forget(account_file: Path): key = None - content = None - address = None - private_key = None + channel = None + # private_key = None private_key_file = str(account_file) - channel = "channel" - inline = "--no-inline" - sync = "--no-sync" + reason = None debug = "--no-debug" result = runner.invoke( app, [ - "aggregate", "post", key, content, - "--address", address, + "aggregate", "forget", key, "--channel", channel, + "--reason", reason, "--private-key-file", private_key_file, - inline, - sync, debug ] ) From 74fd6581f05a133bddc0dbf3a7ee479df75c6dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 26 Feb 2024 19:34:58 -0300 Subject: [PATCH 030/110] aleph test upload program passing --- tests/fixtures/example_program_upload.zip | Bin 0 -> 373 bytes tests/unit/commands/test_program.py | 29 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/example_program_upload.zip diff --git a/tests/fixtures/example_program_upload.zip b/tests/fixtures/example_program_upload.zip new file mode 100644 index 0000000000000000000000000000000000000000..9321b4dd6d03cdb0955641ce5ca0c6cacf243308 GIT binary patch literal 373 zcmWIWW@Zs#U|`^2*fuFN;*!>P(d|H910w^22!jkmYDHphK~8FXT4HfYVnJrSpWoAYR>=tK5 zCX`Y9@~f4{%X)L&wHh2v(f6vqf7;u$(y6lI^}7shf1Qd?Nhe{O zZhvNUsP2Cq>1N1~)3kM$bZ5w-q!Yf4IVJN9C%P1JahqM*blC5$Z{#Gez}Sw{F<L+SMjZ?E3`GWKX|j^ zi+Syu`k5bC0=yZS Date: Mon, 26 Feb 2024 19:41:47 -0300 Subject: [PATCH 031/110] =?UTF-8?q?Structuring=20tests=20for=20=E2=80=98me?= =?UTF-8?q?ssages=E2=80=99=20command.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/commands/test_message.py | 85 +++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 17 deletions(-) diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index 40cb6d25..560d3b5e 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -29,8 +29,8 @@ def get_test_message(account: ETHAccount): @pytest.mark.skip(reason="Not implemented.") def test_message_amend(account_file: Path): - private_key = None - private_key_file = str(account_file) + # private_key = None + private_key_file = get_account(account_file) debug = "--no-debug" result = runner.invoke( @@ -46,6 +46,7 @@ def test_message_amend(account_file: Path): assert result.exit_code == 0 +@pytest.mark.skip(reason="Not implemented.") def test_message_find(): pagination = 1 page = 1 @@ -91,6 +92,29 @@ def test_message_find(): assert ("bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" in result.stdout) +@pytest.mark.skip(reason="Not implemented.") +def test_message_forget(account_file: Path): + reason = None + channel = None + # private_key = None + private_key_file = get_account(account_file) + debug = "--no-debug" + + result = runner.invoke( + app, + [ + "message", + "forget", + "--reason", reason, + "--channel", channel, + "--private-key-file", private_key_file, + debug, + ], + ) + + assert result.exit_code == 0 + + def test_message_get(): # Use subprocess to avoid border effects between tests caused by the initialisation # of the aiohttp client session out of an async context in the SDK. This avoids @@ -118,11 +142,11 @@ def test_message_post(account_file): ).absolute().as_posix() path = str(test_file_path), - type = "test" + message_type = "test" ref = None channel = None - private_key = None - private_key_file = str(account_file) + # private_key = None + private_key_file = get_account(account_file) debug = "--no-debug" result = runner.invoke( @@ -130,8 +154,12 @@ def test_message_post(account_file): [ "message", "post", + "--path", path, + "--type", message_type, + "--ref", ref, + "--channel", channel, "--private-key-file", private_key_file, - "--path", path + debug ], ) @@ -141,17 +169,19 @@ def test_message_post(account_file): def test_message_sign(account_file): - account = get_account(account_file) - message = get_test_message(account) + # private_key = None + private_key_file = get_account(account_file) + message = get_test_message(private_key_file) + debug = "--no-debug" + result = runner.invoke( app, [ "message", "sign", - "--private-key-file", - str(account_file), - "--message", - json.dumps(message), + "--message", json.dumps(message), + "--private-key-file", private_key_file, + debug ], ) @@ -161,19 +191,40 @@ def test_message_sign(account_file): def test_message_sign_stdin(account_file): - account = get_account(account_file) - message = get_test_message(account) + # private_key = None + private_key_file = get_account(account_file) + message = get_test_message(private_key_file) + debug = "--no-debug" + result = runner.invoke( app, [ "message", "sign", - "--private-key-file", - str(account_file), + "--message", message, + "--private-key-file", private_key_file, + debug ], input=json.dumps(message), ) assert result.exit_code == 0 - assert "signature" in result.stdout \ No newline at end of file + assert "signature" in result.stdout + + +def test_message_watch(account_file: Path): + indent = None + debug = "--no-debug" + + result = runner.invoke( + app, + [ + "message", + "watch", + "--indent", indent, + debug + ], + ) + + assert result.exit_code == 0 From 270ae078c2418c85a3cdf1bd67c62b7cde957999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Fri, 1 Mar 2024 17:07:42 -0300 Subject: [PATCH 032/110] =?UTF-8?q?Restructuring=20tests=20for=20=E2=80=98?= =?UTF-8?q?domain=E2=80=99=20command.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/commands/test_domain.py | 141 ++++++++++++----------------- 1 file changed, 59 insertions(+), 82 deletions(-) diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index 1ad8583b..1825bc5e 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -10,14 +10,14 @@ @pytest.mark.skip(reason="Not implemented.") -def test_domain_add(account_file: Path): +def test_domain_add_ipfs(account_file: Path): fqdn = "aleph.im" - private_key = None + # private_key = None private_key_file = str(account_file) + target = "ipfs" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - target = "ipfs" # {ipfs|program|instance} owner = "owner" - ask = "--ask" # {--ask|--no-ask} + ask = "--ask" # {--ask|--no-ask} result = runner.invoke( app, [ @@ -27,26 +27,31 @@ def test_domain_add(account_file: Path): "--item-hash", item_hash, "--owner", owner, ask - ] + ] ) assert result.exit_code == 0 assert result.stdout +# noinspection DuplicatedCode @pytest.mark.skip(reason="Not implemented.") -def test_domain_attach(account_file: Path): - fqdn = "aleph.im" - private_key = "private_key" +def test_domain_add_program(account_file: Path): + fqdn = "aleph.im" # domain + # private_key = None + private_key_file = str(account_file) + target = "program" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - ask = "--ask" # {--ask|--no-ask} + owner = "owner" + ask = "--ask" # {--ask|--no-ask} result = runner.invoke( app, [ - "domain", "attach", fqdn, - "--private-key", private_key, - "--private-key-file", str(account_file), + "domain", "add", fqdn, + "--private-key-file", private_key_file, + "--target", target, "--item-hash", item_hash, + "--owner", owner, ask ] ) @@ -54,17 +59,25 @@ def test_domain_attach(account_file: Path): assert result.exit_code == 0 assert result.stdout + +# noinspection DuplicatedCode @pytest.mark.skip(reason="Not implemented.") -def test_domain_detach(account_file: Path): - fqdn = "aleph.im" - private_key = "private_key" - ask = "--ask" # {--ask|--no-ask} +def test_domain_add_instance(account_file: Path): + fqdn = "aleph.im" # domain + # private_key = None + private_key_file = str(account_file) + target = "instance" # {ipfs|program|instance} + item_hash = "098f6bcd4621d373cade4e832627b4f6" + owner = "owner" + ask = "--ask" # {--ask|--no-ask} result = runner.invoke( app, [ - "domain", "detach", fqdn, - "--private-key", private_key, - "--private-key-file", str(account_file), + "domain", "add", fqdn, + "--private-key-file", private_key_file, + "--target", target, + "--item-hash", item_hash, + "--owner", owner, ask ] ) @@ -74,98 +87,62 @@ def test_domain_detach(account_file: Path): @pytest.mark.skip(reason="Not implemented.") -def test_domain_info(account_file: Path): - fqdn = "aleph.im" - private_key = "private_key" - - result = runner.invoke( - app, [ - "domain", "info", fqdn, - "--private-key", private_key, - "--private-key-file", str(account_file), - ] - ) - - assert result.exit_code == 0 - assert result.stdout - -@pytest.mark.skip(reason="Not implemented.") -def test_domain_add_ipfs(account_file: Path): - domain = "aleph.im" - item_hash = "098f6bcd4621d373cade4e832627b4f6" - - result = runner.invoke( - app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "ipfs", "--item-hash", item_hash] - ) - - assert result.exit_code == 0, result.stdout - -@pytest.mark.skip(reason="Not implemented.") -def test_domain_add_program(account_file: Path): - domain = "aleph.im" - item_hash = "098f6bcd4621d373cade4e832627b4f6" - - result = runner.invoke( - app, [ - "domain", "add", domain, - "--private-key-file", str(account_file), - "--target", "program", - "--item-hash", item_hash] - ) - - assert result.exit_code == 0, result.stdout - - -@pytest.mark.skip(reason="Not implemented.") -def test_domain_add_instance(account_file: Path): - domain = "aleph.im" - item_hash = "098f6bcd4621d373cade4e832627b4f6" - - result = runner.invoke( - app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "instance", "--item-hash", item_hash] - ) - - assert result.exit_code == 0, result.stdout - - def test_domain_attach(account_file: Path): - domain = "aleph.im" + fqdn = "aleph.im" # domain + # private_key = None + private_key_file = str(account_file) item_hash = "098f6bcd4621d373cade4e832627b4f6" result = runner.invoke( - app, ["domain", "attach", domain, "--private-key-file", str(account_file), "--item-hash", item_hash] + app, [ + "domain", "attach", fqdn, + "--private-key-file", private_key_file, + "--item-hash", item_hash, + ] ) assert result.exit_code == 0, result.stdout - pattern = rf".*Attach resource to: {domain}.*" + pattern = rf".*Attach resource to: {fqdn}.*" assert re.match(pattern, result.stdout) +@pytest.mark.skip(reason="Not implemented.") def test_domain_detach(account_file: Path): - domain = "aleph.im" + fqdn = "aleph.im" # domain + # private_key = None + private_key_file = str(account_file) result = runner.invoke( - app, ["domain", "detach", domain, "--private-key-file", str(account_file)] + app, [ + "domain", "detach", fqdn, + "--private-key-file", private_key_file, + ] ) assert result.exit_code == 0, result.stdout - pattern = rf".*Detach resource of: {domain}.*" + pattern = rf".*Detach resource of: {fqdn}.*" assert re.match(pattern, result.stdout) +@pytest.mark.skip(reason="Not implemented.") def test_domain_info(account_file: Path): - domain = "aleph.im" + fqdn = "aleph.im" # domain + # private_key = None + private_key_file = str(account_file) result = runner.invoke( - app, ["domain", "info", domain, "--private-key-file", str(account_file)] + app, [ + "domain", "info", fqdn, + "--private-key-file", private_key_file, + ] ) assert result.exit_code == 0, result.stdout - pattern = rf".*Domain: {domain} not configured.*" + pattern = rf".*Domain: {fqdn} not configured.*" assert re.match(pattern, result.stdout) From 999e6055894b51af18cf131bb107468c9393aaa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Fri, 1 Mar 2024 20:08:01 +0000 Subject: [PATCH 033/110] Adding new test aggreate comparing the full output. --- tests/unit/commands/test_about.py | 2 -- tests/unit/commands/test_aggregate.py | 41 +++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/tests/unit/commands/test_about.py b/tests/unit/commands/test_about.py index 7cbc32f4..ccab0d55 100644 --- a/tests/unit/commands/test_about.py +++ b/tests/unit/commands/test_about.py @@ -1,6 +1,4 @@ import re - -import pytest from typer.testing import CliRunner from aleph_client.__main__ import app diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index 948a49e6..38bade41 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -1,3 +1,7 @@ +import textwrap + +import re + from pathlib import Path import pytest @@ -8,16 +12,15 @@ runner = CliRunner() -# TODO Stopped here!!! def test_aggregate_post(account_file: Path): key = "key" content = """{"c": 3, "d": "test"}""" address = None - # private_key = None - private_key_file = str(account_file) channel = "channel" inline = "--no-inline" sync = "--no-sync" + # private_key = None + private_key_file = str(account_file) debug = "--no-debug" result = runner.invoke( @@ -34,6 +37,38 @@ def test_aggregate_post(account_file: Path): assert result.exit_code == 0, result.stdout + pattern = textwrap.dedent( + '''\ + \{ + "chain": "ETH", + "sender": ".*", + "type": "AGGREGATE", + "channel": "channel", + "confirmations": null, + "confirmed": null, + "signature": ".*", + "size": null, + "time": [0-9]+\.[0-9]+, + "item_type": "storage", + "item_content": null, + "hash_type": null, + "item_hash": ".*", + "content": \{ + "address": ".*", + "time": [0-9]+\.[0-9]+, + "key": "key", + "content": \{ + "c": 3, + "d": "test" + \} + \}, + "forgotten_by": null + \} + ''' + ) + + assert re.fullmatch(pattern, result.stdout) + def test_aggregate_get(account_file: Path): key = None From dddc37aeb15308f057b2c1ec38abbf4b6ca0176d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Fri, 1 Mar 2024 20:19:22 +0000 Subject: [PATCH 034/110] Working with the aggregate tests. --- tests/unit/commands/test_aggregate.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index 38bade41..7e9ccafc 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -1,10 +1,6 @@ -import textwrap - import re - +import textwrap from pathlib import Path - -import pytest from typer.testing import CliRunner from aleph_client.__main__ import app @@ -70,8 +66,9 @@ def test_aggregate_post(account_file: Path): assert re.fullmatch(pattern, result.stdout) +# TODO Stopped here!!! def test_aggregate_get(account_file: Path): - key = None + key = "key" address = None # private_key = None private_key_file = str(account_file) @@ -86,6 +83,10 @@ def test_aggregate_get(account_file: Path): ] ) + print("exit_code:") + print(result.exit_code) + print("stdout:") + assert result.exit_code == 0, result.stdout From 2898693ff7140282b4264cbcfd722f8f3325d00f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Fri, 1 Mar 2024 17:24:17 -0300 Subject: [PATCH 035/110] Using fixture to get the item_hash --- tests/unit/commands/test_program.py | 125 +++++++++++++++------------- 1 file changed, 66 insertions(+), 59 deletions(-) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 88b05058..412999f8 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -11,28 +11,12 @@ runner = CliRunner() -@pytest.mark.skip(reason="Not implemented.") -def test_program_unpersist(account_file: Path): - item_hash = "item_hash" - private_key = None - private_key_file = str(account_file) - debug = "--debug" # [--debug|--no-debug] - - result = runner.invoke( - app, [ - "program", "unpersist", item_hash, - "--private-key-file", private_key_file, - debug, - ] - ) - - assert result.exit_code == 0 - assert result.stdout - -@pytest.mark.skip(reason="Not implemented.") def test_program_upload(account_file: Path): - path = "path" - entrypoint = "entrypoint" + path = Path(os.path.join( + Path(__file__).parent.parent.parent, "fixtures", "example_program_upload.zip") + ).absolute().as_posix() + + entrypoint = "__init__:app" channel = "channel" memory = "memory" vcpus = "vcpus" @@ -42,7 +26,7 @@ def test_program_upload(account_file: Path): print_messages = "--print-messages" # [--print-message|--no-print-message] print_code_message = "--print-code-message" # [--print-code-message|--no-print-code-message] print_program_message = "--print-program-message" # [--print-program-message|--no-print-program-message] - runtime = "runtime" + runtime = "f873715dc2feec3833074bd4b8745363a0e0093746b987b4c8191268883b2463" beta = "--beta" # [--beta|--no-beta] debug = "--debug" # [--debug|--no-debug] persistent = "--persistent" # [--persistent|--no-persistent] @@ -54,30 +38,57 @@ def test_program_upload(account_file: Path): result = runner.invoke( app, [ "program", "upload", path, entrypoint, - "--channel", channel, - "--memory", memory, - "--vcpus", vcpus, - "--timeout-seconds", timeout_seconds, - "--private-key-file", private_key_file, - print_messages, - print_code_message, - print_program_message, + # "--channel", channel, + # "--memory", memory, + # "--vcpus", vcpus, + # "--timeout-seconds", timeout_seconds, + # "--private-key-file", private_key_file, + # print_messages, + # print_code_message, + # print_program_message, "--runtime", runtime, - beta, - debug, - persistent, - "--persistent-volume", persistent_volume, - "--ephemeral-volume", ephemeral_volume, - "--immutable-volume", immutable_volume + # beta, + # debug, + # persistent, + # "--persistent-volume", persistent_volume, + # "--ephemeral-volume", ephemeral_volume, + # "--immutable-volume", immutable_volume ] ) + pattern = r"Your program has been uploaded on aleph.im" + assert re.match(pattern, result.stdout) assert result.exit_code == 0 assert result.stdout + padrao = r'https://aleph\.sh/vm/(\w+)' + correspondencias = re.findall(padrao, result.stdout) + if correspondencias: + item_hash = correspondencias[0] + + + +def test_program_update(account_file: Path): + item_hash = "item_hash" + path = "path" + private_key = None + private_key_file = str(account_file) + print_message = "--print-message" # [--print-message|--no-print-message] + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "program", "update", item_hash, path, + "--private-key-file", private_key_file, + print_message, + debug, + ] + ) + assert result.exit_code == 0 + assert result.stdout -def test_program_upload_01(account_file: Path): - #path = Path("./../../fixtures/example_program_upload.zip").absolute() +@pytest.fixture +def item_hash_upload(account_file: Path): path = Path(os.path.join( Path(__file__).parent.parent.parent, "fixtures", "example_program_upload.zip") ).absolute().as_posix() @@ -87,41 +98,36 @@ def test_program_upload_01(account_file: Path): result = runner.invoke( app, [ - "program", "upload", path, entrypoint, - "--runtime", runtime, + "program", "upload", path, entrypoint, "--runtime", runtime ] ) - print("##########################") - - print(result.stdout) - - - assert result.exit_code == 0 - assert result.stdout + pattern = r'https://aleph\.sh/vm/(\w+)' + matchings = re.findall(pattern, result.stdout) + if matchings: + item_hash = matchings[0] + return item_hash -@pytest.mark.skip(reason="Not implemented.") -def test_program_update(account_file: Path): - item_hash = "item_hash" - path = "path" +def test_program_unpersist(account_file: Path, item_hash_upload): + item_hash = item_hash_upload private_key = None private_key_file = str(account_file) - print_message = "--print-message" # [--print-message|--no-print-message] - debug = "--debug" # [--debug|--no-debug] + # debug = "--debug" # [--debug|--no-debug] result = runner.invoke( app, [ - "program", "update", item_hash, path, + "program", "unpersist", item_hash, "--private-key-file", private_key_file, - print_message, - debug, + # debug, ] ) assert result.exit_code == 0 assert result.stdout -def test_program_unpersist(account_file: Path): + print(result.stdout) + +def test_program_unpersist_x(account_file: Path): item_hash = "098f6bcd4621d373cade4e832627b4f6" result = runner.invoke( @@ -131,7 +137,7 @@ def test_program_unpersist(account_file: Path): assert result.exit_code == 0, result.stdout -def test_program_update(account_file: Path): +def test_program_update_x(account_file: Path): item_hash = "098f6bcd4621d373cade4e832627b4f6" path = tempfile.TemporaryFile() @@ -142,7 +148,8 @@ def test_program_update(account_file: Path): assert result.exit_code == 0, result.stdout -def test_program_upload(account_file: Path): +@pytest.mark.skip(reason="Not implemented.") +def test_program_upload_x(account_file: Path): item_hash = "item_hash" path = tempfile.TemporaryFile() entrypoint = "entrypoint" From 8a2b7725572b7fc92bbbcd84a4392251dc3324b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Fri, 1 Mar 2024 17:39:05 -0300 Subject: [PATCH 036/110] Working on tests for the "domain" command (wip). --- tests/unit/commands/test_domain.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index 1825bc5e..01f64326 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -16,8 +16,7 @@ def test_domain_add_ipfs(account_file: Path): private_key_file = str(account_file) target = "ipfs" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - owner = "owner" - ask = "--ask" # {--ask|--no-ask} + owner = None result = runner.invoke( app, [ @@ -26,7 +25,6 @@ def test_domain_add_ipfs(account_file: Path): "--target", target, "--item-hash", item_hash, "--owner", owner, - ask ] ) @@ -42,8 +40,7 @@ def test_domain_add_program(account_file: Path): private_key_file = str(account_file) target = "program" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - owner = "owner" - ask = "--ask" # {--ask|--no-ask} + owner = None result = runner.invoke( app, [ @@ -52,7 +49,6 @@ def test_domain_add_program(account_file: Path): "--target", target, "--item-hash", item_hash, "--owner", owner, - ask ] ) @@ -68,8 +64,7 @@ def test_domain_add_instance(account_file: Path): private_key_file = str(account_file) target = "instance" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - owner = "owner" - ask = "--ask" # {--ask|--no-ask} + owner = None result = runner.invoke( app, [ @@ -78,7 +73,6 @@ def test_domain_add_instance(account_file: Path): "--target", target, "--item-hash", item_hash, "--owner", owner, - ask ] ) @@ -128,7 +122,6 @@ def test_domain_detach(account_file: Path): assert re.match(pattern, result.stdout) -@pytest.mark.skip(reason="Not implemented.") def test_domain_info(account_file: Path): fqdn = "aleph.im" # domain # private_key = None From aedf474dc2efb9d2a94d242a8154fa994f4648ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 16:50:08 -0300 Subject: [PATCH 037/110] Fixed a bug on calling a asybc function withou await, working on program update command test --- src/aleph_client/commands/program.py | 4 +-- tests/unit/commands/test_program.py | 50 +++++++++++++--------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/aleph_client/commands/program.py b/src/aleph_client/commands/program.py index ee680c54..fd0aff72 100644 --- a/src/aleph_client/commands/program.py +++ b/src/aleph_client/commands/program.py @@ -199,11 +199,11 @@ async def update( async with AuthenticatedAlephHttpClient( account=account, api_server=sdk_settings.API_HOST ) as client: - program_message: ProgramMessage = client.get_message( + program_message: ProgramMessage = await client.get_message( item_hash=item_hash, message_type=ProgramMessage ) code_ref = program_message.content.code.ref - code_message: StoreMessage = client.get_message( + code_message: StoreMessage = await client.get_message( item_hash=code_ref, message_type=StoreMessage ) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 412999f8..8838088b 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -58,34 +58,7 @@ def test_program_upload(account_file: Path): pattern = r"Your program has been uploaded on aleph.im" assert re.match(pattern, result.stdout) - assert result.exit_code == 0 - assert result.stdout - padrao = r'https://aleph\.sh/vm/(\w+)' - correspondencias = re.findall(padrao, result.stdout) - if correspondencias: - item_hash = correspondencias[0] - - - -def test_program_update(account_file: Path): - item_hash = "item_hash" - path = "path" - private_key = None - private_key_file = str(account_file) - print_message = "--print-message" # [--print-message|--no-print-message] - debug = "--debug" # [--debug|--no-debug] - result = runner.invoke( - app, [ - "program", "update", item_hash, path, - "--private-key-file", private_key_file, - print_message, - debug, - ] - ) - - assert result.exit_code == 0 - assert result.stdout @pytest.fixture def item_hash_upload(account_file: Path): @@ -108,6 +81,29 @@ def item_hash_upload(account_file: Path): item_hash = matchings[0] return item_hash + +def test_program_update(account_file: Path, item_hash_upload): + item_hash = item_hash_upload + path = Path(os.path.join( + Path(__file__).parent.parent.parent, "fixtures", "example_program_upload.zip") + ).absolute().as_posix() + private_key = None + private_key_file = str(account_file) + # print_message = "--print-message" # [--print-message|--no-print-message] + # debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "program", "update", item_hash, path, + "--private-key-file", private_key_file + #print_message, + #debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + def test_program_unpersist(account_file: Path, item_hash_upload): item_hash = item_hash_upload private_key = None From 3eeedd1fde180ddf39895a3593bfeedf6dd981c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 17:09:42 -0300 Subject: [PATCH 038/110] Fixed file upload test --- tests/fixtures/anything.txt | 1 + tests/unit/commands/test_file.py | 51 ++++++++++++++++---------------- 2 files changed, 26 insertions(+), 26 deletions(-) create mode 100644 tests/fixtures/anything.txt diff --git a/tests/fixtures/anything.txt b/tests/fixtures/anything.txt new file mode 100644 index 00000000..be066f98 --- /dev/null +++ b/tests/fixtures/anything.txt @@ -0,0 +1 @@ +it is just an empty file for the tests \ No newline at end of file diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index ede38566..d8f605d7 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -1,4 +1,5 @@ from pathlib import Path +import os from tempfile import NamedTemporaryFile from typer.testing import CliRunner @@ -9,35 +10,33 @@ def test_file_upload(account_file: Path): - path = None - channel = None - private_key = None + path = Path(os.path.join( + Path(__file__).parent.parent.parent, "fixtures", "anything.txt") + ).absolute().as_posix() + # channel = None + # private_key = None private_key_file = str(account_file) - ref = None - debug = "--no-debug" + # ref = None + # debug = "--no-debug" # Test upload a file to aleph network by creating a file and upload it to an aleph node - with NamedTemporaryFile() as temp_file: - temp_file.write(b"Hello World \n") - - path = temp_file.name - - result = runner.invoke( - app, - [ - "file", - "upload", - path, - "--channel", channel, - "--private-key-file", private_key_file, - ref, - debug - ], - ) - - assert result.exit_code == 0 - - assert result.stdout is not None + result = runner.invoke( + app, + [ + "file", + "upload", + path, + # "--channel", channel, + "--private-key-file", private_key_file, + # ref, + # debug + ], + ) + + print(result.stdout) + assert result.exit_code == 0 + + assert result.stdout is not None def test_file_download(): From cd72f3cae9307bbccf643b66cef1d0963702b8a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 17:12:30 -0300 Subject: [PATCH 039/110] Cleaning up some tests --- tests/unit/commands/test_program.py | 38 +---------------------------- 1 file changed, 1 insertion(+), 37 deletions(-) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 8838088b..7129c3be 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -121,40 +121,4 @@ def test_program_unpersist(account_file: Path, item_hash_upload): assert result.exit_code == 0 assert result.stdout - print(result.stdout) - -def test_program_unpersist_x(account_file: Path): - item_hash = "098f6bcd4621d373cade4e832627b4f6" - - result = runner.invoke( - app, ["program", "unpersist", item_hash, "--private-key-file", str(account_file)] - ) - - assert result.exit_code == 0, result.stdout - - -def test_program_update_x(account_file: Path): - item_hash = "098f6bcd4621d373cade4e832627b4f6" - path = tempfile.TemporaryFile() - - result = runner.invoke( - app, ["program", "update", item_hash, path, "--private-key-file", str(account_file)] - ) - - assert result.exit_code == 0, result.stdout - - -@pytest.mark.skip(reason="Not implemented.") -def test_program_upload_x(account_file: Path): - item_hash = "item_hash" - path = tempfile.TemporaryFile() - entrypoint = "entrypoint" - channel = "channel" - memory = "" - - - result = runner.invoke( - app, ["program", "upload", item_hash, path, "--private-key-file", str(account_file)] - ) - - assert result.exit_code == 0, result.stdout \ No newline at end of file + print(result.stdout) \ No newline at end of file From a6356bc0afd4ede643eb8460eade99676a9b0067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 17:13:57 -0300 Subject: [PATCH 040/110] Cleaning up some tests --- tests/unit/commands/test_program.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 7129c3be..2b4e17ac 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -119,6 +119,4 @@ def test_program_unpersist(account_file: Path, item_hash_upload): ) assert result.exit_code == 0 - assert result.stdout - - print(result.stdout) \ No newline at end of file + assert result.stdout \ No newline at end of file From 9f40c8b5d417c59085f932bb0044291333728164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 17:15:56 -0300 Subject: [PATCH 041/110] Cleaning up some tests --- tests/unit/commands/test_file.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index d8f605d7..fad11e82 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -33,10 +33,8 @@ def test_file_upload(account_file: Path): ], ) - print(result.stdout) assert result.exit_code == 0 - - assert result.stdout is not None + assert result.stdout def test_file_download(): From 5c8418963e342d192c2384ce91996578b0a51838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 17:25:26 -0300 Subject: [PATCH 042/110] Removing some warnings --- tests/unit/commands/test_file.py | 27 ----------------- tests/unit/commands/test_program.py | 46 +++++++++++++++-------------- 2 files changed, 24 insertions(+), 49 deletions(-) diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index fad11e82..dc7ab0e7 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -1,6 +1,5 @@ from pathlib import Path import os -from tempfile import NamedTemporaryFile from typer.testing import CliRunner @@ -91,32 +90,6 @@ def test_file_forget(account_file: Path): assert result.stdout is not None -def test_file_forget(account_file: Path): - item_hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" - reason = "reason" - channel = "" - private_key = None - private_key_file = str(account_file) - debug = "--no-debug" - - result = runner.invoke( - app, - [ - "file", - "forget", - item_hash, - reason, - "--channel", channel, - "--private-key-file", private_key_file, - debug - ], - ) - - assert result.exit_code == 0 - - assert result.stdout is not None - - def test_file_list(account_file: Path): address = None private_key = None diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 2b4e17ac..0de315f1 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -17,22 +17,22 @@ def test_program_upload(account_file: Path): ).absolute().as_posix() entrypoint = "__init__:app" - channel = "channel" - memory = "memory" - vcpus = "vcpus" - timeout_seconds = "tomeout_seconds" - private_key = None - private_key_file = str(account_file) - print_messages = "--print-messages" # [--print-message|--no-print-message] - print_code_message = "--print-code-message" # [--print-code-message|--no-print-code-message] - print_program_message = "--print-program-message" # [--print-program-message|--no-print-program-message] + # channel = "channel" + # memory = "memory" + # vcpus = "vcpus" + # timeout_seconds = "timeout_seconds" + # private_key = None + # private_key_file = str(account_file) + # print_messages = "--print-messages" # [--print-message|--no-print-message] + # print_code_message = "--print-code-message" # [--print-code-message|--no-print-code-message] + # print_program_message = "--print-program-message" # [--print-program-message|--no-print-program-message] runtime = "f873715dc2feec3833074bd4b8745363a0e0093746b987b4c8191268883b2463" - beta = "--beta" # [--beta|--no-beta] - debug = "--debug" # [--debug|--no-debug] - persistent = "--persistent" # [--persistent|--no-persistent] - persistent_volume = "persistent_volume" - ephemeral_volume = "ephemeral_volume" - immutable_volume = "immutable_volume" + # beta = "--beta" # [--beta|--no-beta] + # debug = "--debug" # [--debug|--no-debug] + # persistent = "--persistent" # [--persistent|--no-persistent] + # persistent_volume = "persistent_volume" + # ephemeral_volume = "ephemeral_volume" + # immutable_volume = "immutable_volume" result = runner.invoke( @@ -76,9 +76,9 @@ def item_hash_upload(account_file: Path): ) pattern = r'https://aleph\.sh/vm/(\w+)' - matchings = re.findall(pattern, result.stdout) - if matchings: - item_hash = matchings[0] + match = re.findall(pattern, result.stdout) + if match: + item_hash = match[0] return item_hash @@ -87,7 +87,7 @@ def test_program_update(account_file: Path, item_hash_upload): path = Path(os.path.join( Path(__file__).parent.parent.parent, "fixtures", "example_program_upload.zip") ).absolute().as_posix() - private_key = None + # private_key = None private_key_file = str(account_file) # print_message = "--print-message" # [--print-message|--no-print-message] # debug = "--debug" # [--debug|--no-debug] @@ -96,14 +96,15 @@ def test_program_update(account_file: Path, item_hash_upload): app, [ "program", "update", item_hash, path, "--private-key-file", private_key_file - #print_message, - #debug, + # print_message, + # debug, ] ) assert result.exit_code == 0 assert result.stdout + def test_program_unpersist(account_file: Path, item_hash_upload): item_hash = item_hash_upload private_key = None @@ -119,4 +120,5 @@ def test_program_unpersist(account_file: Path, item_hash_upload): ) assert result.exit_code == 0 - assert result.stdout \ No newline at end of file + assert result.stdout + \ No newline at end of file From e4c6eaf5ed5d586606f3b92106f47c8d4e52c0c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Mon, 11 Mar 2024 20:41:12 +0000 Subject: [PATCH 043/110] Possible fix to the aggregate command (needs to be reviewed). --- src/aleph_client/commands/aggregate.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/aleph_client/commands/aggregate.py b/src/aleph_client/commands/aggregate.py index efe149e8..87d91e41 100644 --- a/src/aleph_client/commands/aggregate.py +++ b/src/aleph_client/commands/aggregate.py @@ -45,13 +45,14 @@ async def forget( message_response = await client.get_messages( message_filter=MessageFilter( addresses=[account.get_address()], - message_types=[MessageType.aggregate.value], + message_types=[MessageType.aggregate], content_keys=[key], ) ) - hash_list = [message["item_hash"] for message in message_response.messages] - await client.forget(hashes=hash_list, reason=reason, channel=channel) + hash_list = [message.item_hash for message in message_response.messages] + + typer.echo(await client.forget(hashes=hash_list, reason=reason, channel=channel)) @app.command() From a7e6b2648a0bbc170e3dfc59d316e7b40b0a0b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Mon, 11 Mar 2024 20:41:53 +0000 Subject: [PATCH 044/110] Fixing test_aggregate.py unit tests. Some aspects needs review. --- tests/unit/commands/test_aggregate.py | 49 +++++++++++++++++++-------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index 7e9ccafc..aa4d1f2f 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -1,3 +1,6 @@ +import asyncio +import json +import pytest import re import textwrap from pathlib import Path @@ -8,7 +11,8 @@ runner = CliRunner() -def test_aggregate_post(account_file: Path): +@pytest.fixture +def fixture_aggregate_post(account_file: Path): key = "key" content = """{"c": 3, "d": "test"}""" address = None @@ -31,6 +35,12 @@ def test_aggregate_post(account_file: Path): ] ) + return result + + +def test_aggregate_post(fixture_aggregate_post): + result = fixture_aggregate_post + assert result.exit_code == 0, result.stdout pattern = textwrap.dedent( @@ -66,8 +76,10 @@ def test_aggregate_post(account_file: Path): assert re.fullmatch(pattern, result.stdout) -# TODO Stopped here!!! -def test_aggregate_get(account_file: Path): +def test_aggregate_get(account_file: Path, fixture_aggregate_post): + # TODO This delay is being necessary for the post to be available. Verify!!! + asyncio.run(asyncio.sleep(1)) + key = "key" address = None # private_key = None @@ -83,29 +95,38 @@ def test_aggregate_get(account_file: Path): ] ) - print("exit_code:") - print(result.exit_code) - print("stdout:") - assert result.exit_code == 0, result.stdout + expected_content = """{"c": 3, "d": "test"}""" + + assert json.dumps(json.loads(expected_content), separators=(',', ':')) == \ + json.dumps(json.loads(result.stdout), separators=(',', ':')) -def test_aggregate_forget(account_file: Path): - key = None - channel = None + +def test_aggregate_forget(account_file: Path, fixture_aggregate_post): + # TODO This delay is being necessary for the post to be available. Verify!!! + asyncio.run(asyncio.sleep(1)) + + hash = json.loads(fixture_aggregate_post.stdout)["item_hash"] + + key = hash + # channel = None # private_key = None private_key_file = str(account_file) - reason = None - debug = "--no-debug" + # reason = None + debug = "--debug" result = runner.invoke( app, [ "aggregate", "forget", key, - "--channel", channel, - "--reason", reason, + # "--channel", channel, + # "--reason", reason, "--private-key-file", private_key_file, debug ] ) assert result.exit_code == 0, result.stdout + + pattern = r".*forgotten_by=.*, Date: Mon, 11 Mar 2024 17:55:20 -0300 Subject: [PATCH 045/110] test message post is passing --- tests/unit/commands/test_message.py | 36 ++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index 560d3b5e..55a865d5 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -137,17 +137,16 @@ def test_message_get(): def test_message_post(account_file): - test_file_path = Path(os.path.join( + path = Path(os.path.join( Path(__file__).parent.parent.parent, "fixtures", "post.json") ).absolute().as_posix() - path = str(test_file_path), - message_type = "test" - ref = None - channel = None + message_type = "POST" + # ref = None + # channel = None # private_key = None - private_key_file = get_account(account_file) - debug = "--no-debug" + # private_key_file = get_account(account_file) + # debug = "--no-debug" result = runner.invoke( app, @@ -156,32 +155,33 @@ def test_message_post(account_file): "post", "--path", path, "--type", message_type, - "--ref", ref, - "--channel", channel, - "--private-key-file", private_key_file, - debug + # "--ref", ref, + # "--channel", channel, + # "--private-key-file", private_key_file, + # debug ], ) assert result.exit_code == 0 + assert result.stdout - assert "item_hash" in result.stdout - -def test_message_sign(account_file): +def test_message_sign(account_file): # TODO !!! # private_key = None private_key_file = get_account(account_file) - message = get_test_message(private_key_file) - debug = "--no-debug" + # message = get_test_message(private_key_file) + message = "A message to be sign" + # debug = "--no-debug" result = runner.invoke( app, [ "message", "sign", - "--message", json.dumps(message), + # "--message", json.dumps(message), + "--message", message, "--private-key-file", private_key_file, - debug + # debug ], ) From bfdba9ea382db876a29459dd9d417bfb305cfead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Mon, 11 Mar 2024 20:58:13 +0000 Subject: [PATCH 046/110] Improving and fixing test_node.py unit tests. --- tests/unit/commands/test_node.py | 102 +++++++++++++++++++------------ 1 file changed, 63 insertions(+), 39 deletions(-) diff --git a/tests/unit/commands/test_node.py b/tests/unit/commands/test_node.py index 334be36d..8ed4869b 100644 --- a/tests/unit/commands/test_node.py +++ b/tests/unit/commands/test_node.py @@ -1,3 +1,5 @@ +import textwrap + import re from typer.testing import CliRunner @@ -8,45 +10,67 @@ def test_node_compute(): - json = "--no-json" - active = "--no-active" - address = None - debug = "--no-debug" - - result = runner.invoke( - app, [ - "node", - "compute", - json, - active, - "--address", address, - debug - ] - ) - - assert result.exit_code == 0 - - pattern = r".*Compute Node Information.*" - assert re.match(pattern, result.stdout) - # pattern = r".*?([0-9]+\.[0-9]+%|100\.00%|0\.00%).*?([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}).*?([0-9]+\.[0-9]+%|100\.00%).*?([a-z]+).*?" - # assert len(re.findall(pattern, result.stdout, re.MULTILINE)) > 0 + json = "--no-json" + active = "--no-active" + address = None + debug = "--no-debug" + + result = runner.invoke( + app, [ + "node", + "compute", + json, + active, + "--address", address, + debug + ] + ) + + assert result.exit_code == 0 + + pattern = textwrap.dedent( + '''\ + .*Compute Node Information.* + .* + .* Score ┃ Name ┃ Creation Time ┃ Decentralization ┃ Status.* + .* + │ [0-9]+\.[0-9]+% │ .* │ [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} │ .*[0-9]+\.[0-9]+% │ .* │ + [\s\S]* + ''' + ) + + assert re.fullmatch(pattern, result.stdout) +# TODO Stopped here!!! def test_node_core(): - json = "--no-json" - active = "--no-active" - address = None - debug = "--no-debug" - - result = runner.invoke( - app, [ - "node", - "core", - json, - active, - "--address", address, - debug - ] - ) - - assert result.exit_code == 0 + json = "--no-json" + active = "--no-active" + address = None + debug = "--no-debug" + + result = runner.invoke( + app, [ + "node", + "core", + json, + active, + "--address", address, + debug + ] + ) + + assert result.exit_code == 0 + + pattern = textwrap.dedent( + '''\ + .*Core Channel Node Information.* + .* + .* Score ┃ Name ┃ Staked ┃ Linked ┃ Creation Time ┃ Status + .* + │ [0-9]+\.[0-9]+% │ .* │ .* │ [0-9]+ │ [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} │ .* │ + [\s\S]* + ''' + ) + + assert re.fullmatch(pattern, result.stdout) From 73958d3e5112f6e3fd2c59cf2fe61c787bf7e5b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 20 Mar 2024 16:24:54 +0000 Subject: [PATCH 047/110] Adding example ssh keys. --- tests/fixtures/example_ssh_key | 7 +++++++ tests/fixtures/example_ssh_key.pub | 1 + 2 files changed, 8 insertions(+) create mode 100644 tests/fixtures/example_ssh_key create mode 100644 tests/fixtures/example_ssh_key.pub diff --git a/tests/fixtures/example_ssh_key b/tests/fixtures/example_ssh_key new file mode 100644 index 00000000..aa33e1bc --- /dev/null +++ b/tests/fixtures/example_ssh_key @@ -0,0 +1,7 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW +QyNTUxOQAAACCVA+x8tYGrdR6pOyWKWHAefzT2lfhn4kGfenF97hiBewAAAJiRRWNykUVj +cgAAAAtzc2gtZWQyNTUxOQAAACCVA+x8tYGrdR6pOyWKWHAefzT2lfhn4kGfenF97hiBew +AAAEDBOi9c3d1WmYdQfL25G9BDm7oXElKz1egMs6ySvRJLr5UD7Hy1gat1Hqk7JYpYcB5/ +NPaV+GfiQZ96cX3uGIF7AAAAEWFkbWluQGV4YW1wbGUuY29tAQIDBA== +-----END OPENSSH PRIVATE KEY----- diff --git a/tests/fixtures/example_ssh_key.pub b/tests/fixtures/example_ssh_key.pub new file mode 100644 index 00000000..1f924323 --- /dev/null +++ b/tests/fixtures/example_ssh_key.pub @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJUD7Hy1gat1Hqk7JYpYcB5/NPaV+GfiQZ96cX3uGIF7 admin@example.com From 7bae3a693a2d66b26ef43fd56b0eac70b70a30f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 20 Mar 2024 16:25:18 +0000 Subject: [PATCH 048/110] Adding new fixture. --- tests/unit/conftest.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 532a3ed9..b23428f0 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -6,12 +6,14 @@ Read more about conftest.py under: https://pytest.org/latest/plugins.html """ +import os + from pathlib import Path from tempfile import NamedTemporaryFile from typing import Generator import pytest -from aleph.sdk.chains.common import generate_key +from aleph.sdk.chains.common import generate_key, get_public_key @pytest.fixture @@ -26,3 +28,19 @@ def account_file(empty_account_file: Path) -> Path: empty_account_file.write_bytes(private_key) return empty_account_file + + +@pytest.fixture +def ssh_keys_files(empty_account_file: Path) -> dict[str, Path]: + private_key_file = Path(os.path.join( + Path(__file__).parent.parent, "fixtures", "example_ssh_key") + ).absolute() + + public_key_file = Path(os.path.join( + Path(__file__).parent.parent, "fixtures", "example_ssh_key.pub") + ).absolute() + + return { + "private_key": private_key_file, + "public_key": public_key_file + } From 613b52a55dba5b2a65096ecac738dc24650b9626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 20 Mar 2024 16:26:06 +0000 Subject: [PATCH 049/110] Working with the instance command unit tests. --- tests/unit/commands/test_instance.py | 146 ++++++++++++++------------- 1 file changed, 77 insertions(+), 69 deletions(-) diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index 68bec088..723a4022 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -7,23 +7,24 @@ runner = CliRunner() item_hash = None -@pytest.mark.skip(reason="Not implemented.") -def test_instance_create(account_file: Path): + +# TODO Stopped here!!! +def test_instance_create(account_file: Path, ssh_keys_files: dict[str, Path]): channel = "channel" - memory = "memory" - vcpus = "vcpus" - timeout_seconds = "timeout_seconds" - private_key = None + memory = 256 + vcpus = 1 + timeout_seconds = 30.0 + # private_key = None private_key_file = str(account_file) - ssh_pubkey_file = "ssh_pubkey_file" - print_messages = "--print-messages" #[--print-messages|--no-print-messages] - rootfs = "rootfs" - rootfs_name = "rootfs_name" - rootfs_size = "rootfs_size" - debug = "--debug" # [--debug|--no-debug] - persistent_volume = "persistent_volume" - ephemeral_volume = "ephemeral_volume" - immutable_volume = "immutable_volume" + ssh_pubkey_file = str(ssh_keys_files["public_key"]) + print_messages = "--no-print-messages" + rootfs = "Ubuntu 22" + rootfs_name = "main-rootfs" + rootfs_size = 2000 + debug = "--no-debug" + persistent_volume = None + ephemeral_volume = None + immutable_volume = None result = runner.invoke( app, [ @@ -41,14 +42,22 @@ def test_instance_create(account_file: Path): debug, "--persistent-volume", persistent_volume, "--ephemeral-volume", ephemeral_volume, - "--imutable-volume", immutable_volume + "--immutable-volume", immutable_volume ] ) + print("result.exit_code:") + print(result.exit_code) + print("result.stdout:") + print(result.stdout) + # from tests.unit.test_utils import dump + # print("dump:") + # print(dump(result)) + assert result.exit_code == 0 - assert result.stdout + # assert result.stdout + -@pytest.mark.skip(reason="Not implemented.") def test_instance_delete(account_file: Path): item_hash = "item_hash" reason = "reason" @@ -71,7 +80,7 @@ def test_instance_delete(account_file: Path): assert result.exit_code == 0 assert result.stdout -@pytest.mark.skip(reason="Not implemented.") + def test_instance_list(account_file: Path): address = "address" private_key = None @@ -92,54 +101,53 @@ def test_instance_list(account_file: Path): assert result.exit_code == 0 assert result.stdout -@pytest.mark.skip(reason="Not implemented.") -def test_instance_create(): - global item_hash - - rootfs = "Ubuntu 22" - rootfs_name = "Ubuntu 22" - vcpus = 1 - memory = 256 - rootfs_size = 2000 - - result = runner.invoke( - app, [ - "instance", "create", - "--rootfs", rootfs, - "--rootfs-name", rootfs_name, - "--vcpus", vcpus, - "--memory", memory, - "--rootfs-size", rootfs_size - ] - ) - - assert result.exit_code == 0 - assert result.stdout - - item_hash_regex = r"\b0x[a-fA-F0-9]{40,42}\b" - item_hashes = re.findall(item_hash_regex, result.stdout) - - item_hash = item_hashes[0] if item_hashes else None - - -@pytest.mark.skip(reason="Not implemented.") -def test_instance_delete(): - result = runner.invoke( - app, [ - "instance", "create", - "--item_hash", item_hash, - ] - ) - - assert result.exit_code == 0 - assert result.stdout - - -def test_instance_list(): - result = runner.invoke( - app, ["instance", "list"] - ) - assert result.exit_code == 0 - assert result.stdout - assert "Item Hash Vcpus Memory Disk size IPv6 address" in result.stdout +# def test_instance_create(): +# global item_hash +# +# rootfs = "Ubuntu 22" +# rootfs_name = "Ubuntu 22" +# vcpus = 1 +# memory = 256 +# rootfs_size = 2000 +# +# result = runner.invoke( +# app, [ +# "instance", "create", +# "--rootfs", rootfs, +# "--rootfs-name", rootfs_name, +# "--vcpus", vcpus, +# "--memory", memory, +# "--rootfs-size", rootfs_size +# ] +# ) +# +# assert result.exit_code == 0 +# assert result.stdout +# +# item_hash_regex = r"\b0x[a-fA-F0-9]{40,42}\b" +# item_hashes = re.findall(item_hash_regex, result.stdout) +# +# item_hash = item_hashes[0] if item_hashes else None +# +# +# def test_instance_delete(): +# result = runner.invoke( +# app, [ +# "instance", "create", +# "--item_hash", item_hash, +# ] +# ) +# +# assert result.exit_code == 0 +# assert result.stdout +# +# +# def test_instance_list(): +# result = runner.invoke( +# app, ["instance", "list"] +# ) +# +# assert result.exit_code == 0 +# assert result.stdout +# assert "Item Hash Vcpus Memory Disk size IPv6 address" in result.stdout From a0fcaf5b26c868f1d200b3285e9d94547fec2b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Tue, 9 Apr 2024 21:23:42 -0300 Subject: [PATCH 050/110] Adding a dump function to the utils files. --- tests/unit/test_utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 7813919c..d713be9b 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,3 +1,6 @@ +import jsonpickle +from typing import Any, Dict + from aleph_message.models import ( AggregateMessage, ForgetMessage, @@ -18,3 +21,16 @@ def test_get_message_type_value(): assert get_message_type_value(ProgramMessage) == MessageType.program assert get_message_type_value(InstanceMessage) == MessageType.instance assert get_message_type_value(ForgetMessage) == MessageType.forget + + +def dump(target: Any): + try: + if isinstance(target, str): + return target + + if isinstance(target, Dict): + return str(target) + + return jsonpickle.encode(target, unpicklable=True, indent=2) + except (Exception,): + return target From 2208dc9369c8b3605bd724f61cd26d48fed3564a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Thu, 25 Jan 2024 16:28:44 +0000 Subject: [PATCH 051/110] Moving fixture to a fixture folder. --- tests/{test_post.json => fixtures/post.json} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{test_post.json => fixtures/post.json} (100%) diff --git a/tests/test_post.json b/tests/fixtures/post.json similarity index 100% rename from tests/test_post.json rename to tests/fixtures/post.json From 383d069f886e54ebc947155b35e6db88980454da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Thu, 25 Jan 2024 16:29:03 +0000 Subject: [PATCH 052/110] Splitting test_commands in different test files. --- tests/unit/commands/test_about.py | 0 tests/unit/commands/test_account.py | 87 +++++++++++ tests/unit/commands/test_aggregate.py | 0 tests/unit/commands/test_domain.py | 0 tests/unit/commands/test_file.py | 33 +++++ tests/unit/commands/test_instance.py | 0 tests/unit/commands/test_message.py | 116 +++++++++++++++ tests/unit/commands/test_node.py | 0 tests/unit/commands/test_program.py | 0 tests/unit/commands/test_utils.py | 18 +++ tests/unit/test_commands.py | 205 -------------------------- 11 files changed, 254 insertions(+), 205 deletions(-) create mode 100644 tests/unit/commands/test_about.py create mode 100644 tests/unit/commands/test_account.py create mode 100644 tests/unit/commands/test_aggregate.py create mode 100644 tests/unit/commands/test_domain.py create mode 100644 tests/unit/commands/test_file.py create mode 100644 tests/unit/commands/test_instance.py create mode 100644 tests/unit/commands/test_message.py create mode 100644 tests/unit/commands/test_node.py create mode 100644 tests/unit/commands/test_program.py create mode 100644 tests/unit/commands/test_utils.py delete mode 100644 tests/unit/test_commands.py diff --git a/tests/unit/commands/test_about.py b/tests/unit/commands/test_about.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py new file mode 100644 index 00000000..b7d4ec6f --- /dev/null +++ b/tests/unit/commands/test_account.py @@ -0,0 +1,87 @@ +from pathlib import Path + +from aleph.sdk.chains.ethereum import ETHAccount +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def get_account(my_account_file: Path) -> ETHAccount: + with open(my_account_file, "rb") as fd: + private_key = fd.read() + return ETHAccount(private_key=private_key) + + +def get_test_message(account: ETHAccount): + return { + "chain": "ETH", + "sender": account.get_address(), + "type": "AGGREGATE", + "item_hash": "0x1234", + } + + +def test_account_create(account_file: Path): + old_key = account_file.read_bytes() + result = runner.invoke( + app, ["account", "create", "--replace", "--private-key-file", str(account_file)] + ) + assert result.exit_code == 0, result.stdout + new_key = account_file.read_bytes() + assert new_key != old_key + + +def test_account_address(account_file: Path): + result = runner.invoke( + app, ["account", "address", "--private-key-file", str(account_file)] + ) + assert result.exit_code == 0 + assert result.stdout.startswith("0x") + assert len(result.stdout.strip()) == 42 + + +def test_account_export_private_key(account_file: Path): + result = runner.invoke( + app, ["account", "export-private-key", "--private-key-file", str(account_file)] + ) + assert result.exit_code == 0 + assert result.stdout.startswith("0x") + assert len(result.stdout.strip()) == 66 + + +def test_account_path(): + result = runner.invoke(app, ["account", "path"]) + assert result.stdout.startswith("/") + + +def test_sign_raw(): + result = runner.invoke( + app, + [ + "account", + "sign-bytes", + "--message", + "some message", + ], + ) + + assert result.exit_code == 0 + assert "0x" in result.stdout + + +def test_sign_raw_stdin(): + message = "some message" + result = runner.invoke( + app, + [ + "account", + "sign-bytes", + ], + input=message, + ) + + assert result.exit_code == 0 + assert "0x" in result.stdout + diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py new file mode 100644 index 00000000..c8eae7eb --- /dev/null +++ b/tests/unit/commands/test_file.py @@ -0,0 +1,33 @@ +from tempfile import NamedTemporaryFile + +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def test_file_upload(): + # Test upload a file to aleph network by creating a file and upload it to an aleph node + with NamedTemporaryFile() as temp_file: + temp_file.write(b"Hello World \n") + result = runner.invoke( + app, + ["file", "upload", temp_file.name], + ) + assert result.exit_code == 0 + assert result.stdout is not None + + +def test_file_download(): + # Test download a file to aleph network + result = runner.invoke( + app, + [ + "file", + "download", + "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH", + ], # 5 bytes file + ) + assert result.exit_code == 0 + assert result.stdout is not None diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py new file mode 100644 index 00000000..17d68ba9 --- /dev/null +++ b/tests/unit/commands/test_message.py @@ -0,0 +1,116 @@ +import json +import os +from pathlib import Path + +from aleph.sdk.chains.ethereum import ETHAccount +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def get_account(my_account_file: Path) -> ETHAccount: + with open(my_account_file, "rb") as fd: + private_key = fd.read() + return ETHAccount(private_key=private_key) + + +def get_test_message(account: ETHAccount): + return { + "chain": "ETH", + "sender": account.get_address(), + "type": "AGGREGATE", + "item_hash": "0x1234", + } + + +def test_message_get(): + # Use subprocess to avoid border effects between tests caused by the initialisation + # of the aiohttp client session out of an async context in the SDK. This avoids + # a "no running event loop" error when running several tests back to back. + result = runner.invoke( + app, + [ + "message", + "get", + "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + ], + ) + assert result.exit_code == 0 + assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout + + +def test_message_find(): + result = runner.invoke( + app, + [ + "message", + "find", + "--pagination=1", + "--page=1", + "--start-date=1234", + "--chains=ETH", + "--hashes=bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + ], + ) + assert result.exit_code == 0 + assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout + assert ( + "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" + in result.stdout + ) + + +def test_post_message(account_file): + test_file_path = Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")).absolute().as_posix() + result = runner.invoke( + app, + [ + "message", + "post", + "--private-key-file", + str(account_file), + "--path", + str(test_file_path), + ], + ) + assert result.exit_code == 0 + assert "item_hash" in result.stdout + + +def test_sign_message(account_file): + account = get_account(account_file) + message = get_test_message(account) + result = runner.invoke( + app, + [ + "message", + "sign", + "--private-key-file", + str(account_file), + "--message", + json.dumps(message), + ], + ) + + assert result.exit_code == 0 + assert "signature" in result.stdout + + +def test_sign_message_stdin(account_file): + account = get_account(account_file) + message = get_test_message(account) + result = runner.invoke( + app, + [ + "message", + "sign", + "--private-key-file", + str(account_file), + ], + input=json.dumps(message), + ) + + assert result.exit_code == 0 + assert "signature" in result.stdout \ No newline at end of file diff --git a/tests/unit/commands/test_node.py b/tests/unit/commands/test_node.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/commands/test_utils.py b/tests/unit/commands/test_utils.py new file mode 100644 index 00000000..0671bf24 --- /dev/null +++ b/tests/unit/commands/test_utils.py @@ -0,0 +1,18 @@ +from aleph_message.models import ( + AggregateMessage, + ForgetMessage, + MessageType, + PostMessage, + ProgramMessage, + StoreMessage, +) + +from aleph_client.utils import get_message_type_value + + +def test_get_message_type_value(): + assert get_message_type_value(PostMessage) == MessageType.post + assert get_message_type_value(AggregateMessage) == MessageType.aggregate + assert get_message_type_value(StoreMessage) == MessageType.store + assert get_message_type_value(ProgramMessage) == MessageType.program + assert get_message_type_value(ForgetMessage) == MessageType.forget diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py deleted file mode 100644 index 91ef8148..00000000 --- a/tests/unit/test_commands.py +++ /dev/null @@ -1,205 +0,0 @@ -import json -from pathlib import Path -from tempfile import NamedTemporaryFile - -from aleph.sdk.chains.ethereum import ETHAccount -from typer.testing import CliRunner - -from aleph_client.__main__ import app - -runner = CliRunner() - - -def get_account(my_account_file: Path) -> ETHAccount: - with open(my_account_file, "rb") as fd: - private_key = fd.read() - return ETHAccount(private_key=private_key) - - -def get_test_message(account: ETHAccount): - return { - "chain": "ETH", - "sender": account.get_address(), - "type": "AGGREGATE", - "item_hash": "0x1234", - } - - -def test_account_create(account_file: Path): - old_key = account_file.read_bytes() - result = runner.invoke( - app, ["account", "create", "--replace", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0, result.stdout - new_key = account_file.read_bytes() - assert new_key != old_key - - -def test_account_address(account_file: Path): - result = runner.invoke( - app, ["account", "address", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0 - assert result.stdout.startswith("0x") - assert len(result.stdout.strip()) == 42 - - -def test_account_export_private_key(account_file: Path): - result = runner.invoke( - app, ["account", "export-private-key", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0 - assert result.stdout.startswith("0x") - assert len(result.stdout.strip()) == 66 - - -def test_account_path(): - result = runner.invoke(app, ["account", "path"]) - assert result.stdout.startswith("/") - - -def test_message_get(): - # Use subprocess to avoid border effects between tests caused by the initialisation - # of the aiohttp client session out of an async context in the SDK. This avoids - # a "no running event loop" error when running several tests back to back. - result = runner.invoke( - app, - [ - "message", - "get", - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", - ], - ) - assert result.exit_code == 0 - assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - - -def test_message_find(): - result = runner.invoke( - app, - [ - "message", - "find", - "--pagination=1", - "--page=1", - "--start-date=1234", - "--chains=ETH", - "--hashes=bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", - ], - ) - assert result.exit_code == 0 - assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - assert ( - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" - in result.stdout - ) - - -def test_post_message(account_file): - test_file_path = Path(__file__).parent.parent / "test_post.json" - result = runner.invoke( - app, - [ - "message", - "post", - "--private-key-file", - str(account_file), - "--path", - str(test_file_path), - ], - ) - assert result.exit_code == 0 - assert "item_hash" in result.stdout - - -def test_sign_message(account_file): - account = get_account(account_file) - message = get_test_message(account) - result = runner.invoke( - app, - [ - "message", - "sign", - "--private-key-file", - str(account_file), - "--message", - json.dumps(message), - ], - ) - - assert result.exit_code == 0 - assert "signature" in result.stdout - - -def test_sign_message_stdin(account_file): - account = get_account(account_file) - message = get_test_message(account) - result = runner.invoke( - app, - [ - "message", - "sign", - "--private-key-file", - str(account_file), - ], - input=json.dumps(message), - ) - - assert result.exit_code == 0 - assert "signature" in result.stdout - - -def test_sign_raw(): - result = runner.invoke( - app, - [ - "account", - "sign-bytes", - "--message", - "some message", - ], - ) - - assert result.exit_code == 0 - assert "0x" in result.stdout - - -def test_sign_raw_stdin(): - message = "some message" - result = runner.invoke( - app, - [ - "account", - "sign-bytes", - ], - input=message, - ) - - assert result.exit_code == 0 - assert "0x" in result.stdout - - -def test_file_upload(): - # Test upload a file to aleph network by creating a file and upload it to an aleph node - with NamedTemporaryFile() as temp_file: - temp_file.write(b"Hello World \n") - result = runner.invoke( - app, - ["file", "upload", temp_file.name], - ) - assert result.exit_code == 0 - assert result.stdout is not None - - -def test_file_download(): - # Test download a file to aleph network - result = runner.invoke( - app, - [ - "file", - "download", - "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH", - ], # 5 bytes file - ) - assert result.exit_code == 0 - assert result.stdout is not None From 6b1969ebfbdbf727e10701a92eb022ddfafe6dfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Thu, 25 Jan 2024 16:29:43 +0000 Subject: [PATCH 053/110] Improving test_utils.py test and applying simple best practices. --- tests/unit/conftest.py | 1 + tests/unit/test_utils.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index ad43487e..532a3ed9 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -24,4 +24,5 @@ def empty_account_file() -> Generator[Path, None, None]: def account_file(empty_account_file: Path) -> Path: private_key = generate_key() empty_account_file.write_bytes(private_key) + return empty_account_file diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 0671bf24..7813919c 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -5,6 +5,7 @@ PostMessage, ProgramMessage, StoreMessage, + InstanceMessage ) from aleph_client.utils import get_message_type_value @@ -15,4 +16,5 @@ def test_get_message_type_value(): assert get_message_type_value(AggregateMessage) == MessageType.aggregate assert get_message_type_value(StoreMessage) == MessageType.store assert get_message_type_value(ProgramMessage) == MessageType.program + assert get_message_type_value(InstanceMessage) == MessageType.instance assert get_message_type_value(ForgetMessage) == MessageType.forget From 511fe1a4881c29dbefa00137cb90df705432357c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Thu, 25 Jan 2024 17:10:57 +0000 Subject: [PATCH 054/110] Improving test files and adding some more new tests. --- tests/unit/commands/test_about.py | 29 +++++ tests/unit/commands/test_account.py | 129 +++++++++++--------- tests/unit/commands/test_file.py | 44 +++---- tests/unit/commands/test_message.py | 180 +++++++++++++++------------- tests/unit/commands/test_root.py | 15 +++ tests/unit/commands/test_utils.py | 18 --- 6 files changed, 235 insertions(+), 180 deletions(-) create mode 100644 tests/unit/commands/test_root.py delete mode 100644 tests/unit/commands/test_utils.py diff --git a/tests/unit/commands/test_about.py b/tests/unit/commands/test_about.py index e69de29b..83041579 100644 --- a/tests/unit/commands/test_about.py +++ b/tests/unit/commands/test_about.py @@ -0,0 +1,29 @@ +import re + +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def test_about_help(): + result = runner.invoke( + app, ["about", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "version" in result.stdout + + +def test_about_version(): + result = runner.invoke( + app, ["about", "version"] + ) + + assert result.exit_code == 1, result.stdout + + pattern = r"Aleph CLI Version: \d+\.\d+\.\d+.*" + + assert re.match(pattern, result.stdout) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index b7d4ec6f..75dce95b 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -1,6 +1,6 @@ from pathlib import Path -from aleph.sdk.chains.ethereum import ETHAccount +import pytest from typer.testing import CliRunner from aleph_client.__main__ import app @@ -8,80 +8,95 @@ runner = CliRunner() -def get_account(my_account_file: Path) -> ETHAccount: - with open(my_account_file, "rb") as fd: - private_key = fd.read() - return ETHAccount(private_key=private_key) +def test_account_help(): + result = runner.invoke( + app, ["account", "--help"] + ) + assert result.exit_code == 0, result.stdout -def get_test_message(account: ETHAccount): - return { - "chain": "ETH", - "sender": account.get_address(), - "type": "AGGREGATE", - "item_hash": "0x1234", - } + assert "Sign a message using your private key." in result.stdout + + +def test_account_address(account_file: Path): + result = runner.invoke( + app, ["account", "address", "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0 + + assert result.stdout.startswith("0x") + + assert len(result.stdout.strip()) == 42 + + +@pytest.mark.skip(reason="Not implemented. It's failing the retrieve the balance for the address.") +def test_account_balance(account_file: Path): + result = runner.invoke( + app, ["account", "balance", "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0 def test_account_create(account_file: Path): - old_key = account_file.read_bytes() - result = runner.invoke( - app, ["account", "create", "--replace", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0, result.stdout - new_key = account_file.read_bytes() - assert new_key != old_key + old_key = account_file.read_bytes() + result = runner.invoke( + app, ["account", "create", "--replace", "--private-key-file", str(account_file)] + ) + assert result.exit_code == 0, result.stdout -def test_account_address(account_file: Path): - result = runner.invoke( - app, ["account", "address", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0 - assert result.stdout.startswith("0x") - assert len(result.stdout.strip()) == 42 + new_key = account_file.read_bytes() + + assert new_key != old_key def test_account_export_private_key(account_file: Path): - result = runner.invoke( - app, ["account", "export-private-key", "--private-key-file", str(account_file)] - ) - assert result.exit_code == 0 - assert result.stdout.startswith("0x") - assert len(result.stdout.strip()) == 66 + result = runner.invoke( + app, ["account", "export-private-key", "--private-key-file", str(account_file)] + ) + assert result.exit_code == 0 + + assert result.stdout.startswith("0x") + + assert len(result.stdout.strip()) == 66 def test_account_path(): - result = runner.invoke(app, ["account", "path"]) - assert result.stdout.startswith("/") + result = runner.invoke(app, ["account", "path"]) + + assert result.stdout.startswith("/") def test_sign_raw(): - result = runner.invoke( - app, - [ - "account", - "sign-bytes", - "--message", - "some message", - ], - ) + result = runner.invoke( + app, + [ + "account", + "sign-bytes", + "--message", + "some message", + ], + ) + + assert result.exit_code == 0 - assert result.exit_code == 0 - assert "0x" in result.stdout + assert "0x" in result.stdout def test_sign_raw_stdin(): - message = "some message" - result = runner.invoke( - app, - [ - "account", - "sign-bytes", - ], - input=message, - ) - - assert result.exit_code == 0 - assert "0x" in result.stdout + message = "some message" + result = runner.invoke( + app, + [ + "account", + "sign-bytes", + ], + input=message, + ) + + assert result.exit_code == 0 + + assert "0x" in result.stdout diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index c8eae7eb..a544fba9 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -8,26 +8,30 @@ def test_file_upload(): - # Test upload a file to aleph network by creating a file and upload it to an aleph node - with NamedTemporaryFile() as temp_file: - temp_file.write(b"Hello World \n") - result = runner.invoke( - app, - ["file", "upload", temp_file.name], - ) - assert result.exit_code == 0 - assert result.stdout is not None + # Test upload a file to aleph network by creating a file and upload it to an aleph node + with NamedTemporaryFile() as temp_file: + temp_file.write(b"Hello World \n") + result = runner.invoke( + app, + ["file", "upload", temp_file.name], + ) + + assert result.exit_code == 0 + + assert result.stdout is not None def test_file_download(): - # Test download a file to aleph network - result = runner.invoke( - app, - [ - "file", - "download", - "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH", - ], # 5 bytes file - ) - assert result.exit_code == 0 - assert result.stdout is not None + # Test download a file to aleph network + result = runner.invoke( + app, + [ + "file", + "download", + "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH", + ], # 5 bytes file + ) + + assert result.exit_code == 0 + + assert result.stdout is not None diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index 17d68ba9..a7663148 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -11,106 +11,116 @@ def get_account(my_account_file: Path) -> ETHAccount: - with open(my_account_file, "rb") as fd: - private_key = fd.read() - return ETHAccount(private_key=private_key) + with open(my_account_file, "rb") as fd: + private_key = fd.read() + + return ETHAccount(private_key=private_key) def get_test_message(account: ETHAccount): - return { - "chain": "ETH", - "sender": account.get_address(), - "type": "AGGREGATE", - "item_hash": "0x1234", - } + return { + "chain": "ETH", + "sender": account.get_address(), + "type": "AGGREGATE", + "item_hash": "0x1234", + } def test_message_get(): - # Use subprocess to avoid border effects between tests caused by the initialisation - # of the aiohttp client session out of an async context in the SDK. This avoids - # a "no running event loop" error when running several tests back to back. - result = runner.invoke( - app, - [ - "message", - "get", - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", - ], - ) - assert result.exit_code == 0 - assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout + # Use subprocess to avoid border effects between tests caused by the initialisation + # of the aiohttp client session out of an async context in the SDK. This avoids + # a "no running event loop" error when running several tests back to back. + result = runner.invoke( + app, + [ + "message", + "get", + "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + ], + ) + + assert result.exit_code == 0 + + assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout def test_message_find(): - result = runner.invoke( - app, - [ - "message", - "find", - "--pagination=1", - "--page=1", - "--start-date=1234", - "--chains=ETH", - "--hashes=bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", - ], - ) - assert result.exit_code == 0 - assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - assert ( - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" - in result.stdout - ) + result = runner.invoke( + app, + [ + "message", + "find", + "--pagination=1", + "--page=1", + "--start-date=1234", + "--chains=ETH", + "--hashes=bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + ], + ) + + assert result.exit_code == 0 + + assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout + + assert ( + "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" + in result.stdout + ) def test_post_message(account_file): - test_file_path = Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")).absolute().as_posix() - result = runner.invoke( - app, - [ - "message", - "post", - "--private-key-file", - str(account_file), - "--path", - str(test_file_path), - ], - ) - assert result.exit_code == 0 - assert "item_hash" in result.stdout + test_file_path = Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")).absolute().as_posix() + result = runner.invoke( + app, + [ + "message", + "post", + "--private-key-file", + str(account_file), + "--path", + str(test_file_path), + ], + ) + + assert result.exit_code == 0 + + assert "item_hash" in result.stdout def test_sign_message(account_file): - account = get_account(account_file) - message = get_test_message(account) - result = runner.invoke( - app, - [ - "message", - "sign", - "--private-key-file", - str(account_file), - "--message", - json.dumps(message), - ], - ) - - assert result.exit_code == 0 - assert "signature" in result.stdout + account = get_account(account_file) + message = get_test_message(account) + result = runner.invoke( + app, + [ + "message", + "sign", + "--private-key-file", + str(account_file), + "--message", + json.dumps(message), + ], + ) + + assert result.exit_code == 0 + + assert "signature" in result.stdout def test_sign_message_stdin(account_file): - account = get_account(account_file) - message = get_test_message(account) - result = runner.invoke( - app, - [ - "message", - "sign", - "--private-key-file", - str(account_file), - ], - input=json.dumps(message), - ) - - assert result.exit_code == 0 - assert "signature" in result.stdout \ No newline at end of file + account = get_account(account_file) + message = get_test_message(account) + result = runner.invoke( + app, + [ + "message", + "sign", + "--private-key-file", + str(account_file), + ], + input=json.dumps(message), + ) + + assert result.exit_code == 0 + + assert "signature" in result.stdout \ No newline at end of file diff --git a/tests/unit/commands/test_root.py b/tests/unit/commands/test_root.py new file mode 100644 index 00000000..07b37e05 --- /dev/null +++ b/tests/unit/commands/test_root.py @@ -0,0 +1,15 @@ +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def test_root_help(): + result = runner.invoke( + app, ["--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Upload and update programs on aleph.im VM" in result.stdout diff --git a/tests/unit/commands/test_utils.py b/tests/unit/commands/test_utils.py deleted file mode 100644 index 0671bf24..00000000 --- a/tests/unit/commands/test_utils.py +++ /dev/null @@ -1,18 +0,0 @@ -from aleph_message.models import ( - AggregateMessage, - ForgetMessage, - MessageType, - PostMessage, - ProgramMessage, - StoreMessage, -) - -from aleph_client.utils import get_message_type_value - - -def test_get_message_type_value(): - assert get_message_type_value(PostMessage) == MessageType.post - assert get_message_type_value(AggregateMessage) == MessageType.aggregate - assert get_message_type_value(StoreMessage) == MessageType.store - assert get_message_type_value(ProgramMessage) == MessageType.program - assert get_message_type_value(ForgetMessage) == MessageType.forget From d0877861964209fc6e3fecc1f9e90f2f0fc01be4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 31 Jan 2024 20:02:57 +0000 Subject: [PATCH 055/110] Moving about tests. --- .gitignore | 1 + tests/unit/commands/test_about.py | 10 --------- tests/unit/commands/test_account.py | 10 --------- tests/unit/commands/test_help.py | 35 +++++++++++++++++++++++++++++ tests/unit/commands/test_root.py | 15 ------------- 5 files changed, 36 insertions(+), 35 deletions(-) create mode 100644 tests/unit/commands/test_help.py delete mode 100644 tests/unit/commands/test_root.py diff --git a/.gitignore b/.gitignore index c4734889..b7d34d83 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ __pycache__/* .pydevproject .settings .idea +*.iml tags # Package files diff --git a/tests/unit/commands/test_about.py b/tests/unit/commands/test_about.py index 83041579..176eab8d 100644 --- a/tests/unit/commands/test_about.py +++ b/tests/unit/commands/test_about.py @@ -7,16 +7,6 @@ runner = CliRunner() -def test_about_help(): - result = runner.invoke( - app, ["about", "--help"] - ) - - assert result.exit_code == 0, result.stdout - - assert "version" in result.stdout - - def test_about_version(): result = runner.invoke( app, ["about", "version"] diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index 75dce95b..a59d52c6 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -8,16 +8,6 @@ runner = CliRunner() -def test_account_help(): - result = runner.invoke( - app, ["account", "--help"] - ) - - assert result.exit_code == 0, result.stdout - - assert "Sign a message using your private key." in result.stdout - - def test_account_address(account_file: Path): result = runner.invoke( app, ["account", "address", "--private-key-file", str(account_file)] diff --git a/tests/unit/commands/test_help.py b/tests/unit/commands/test_help.py new file mode 100644 index 00000000..61da0d9a --- /dev/null +++ b/tests/unit/commands/test_help.py @@ -0,0 +1,35 @@ +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def test_root_help(): + result = runner.invoke( + app, ["--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Upload and update programs on aleph.im VM" in result.stdout + + +def test_about_help(): + result = runner.invoke( + app, ["about", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "version" in result.stdout + + +def test_account_help(): + result = runner.invoke( + app, ["account", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Sign a message using your private key." in result.stdout diff --git a/tests/unit/commands/test_root.py b/tests/unit/commands/test_root.py deleted file mode 100644 index 07b37e05..00000000 --- a/tests/unit/commands/test_root.py +++ /dev/null @@ -1,15 +0,0 @@ -from typer.testing import CliRunner - -from aleph_client.__main__ import app - -runner = CliRunner() - - -def test_root_help(): - result = runner.invoke( - app, ["--help"] - ) - - assert result.exit_code == 0, result.stdout - - assert "Upload and update programs on aleph.im VM" in result.stdout From a4b87dc836f256d36ec345cf9c123e44df844787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 31 Jan 2024 20:50:55 +0000 Subject: [PATCH 056/110] Adding unit tests for the domain command. --- tests/unit/commands/test_domain.py | 88 ++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index e69de29b..39379909 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -0,0 +1,88 @@ +import re +from pathlib import Path + +import pytest +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_add_ipfs(account_file: Path): + domain = "aleph.im" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + + result = runner.invoke( + app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "ipfs", "--item-hash", item_hash] + ) + + assert result.exit_code == 0, result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_add_program(account_file: Path): + domain = "aleph.im" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + + result = runner.invoke( + app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "program", "--item-hash", item_hash] + ) + + assert result.exit_code == 0, result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_add_instance(account_file: Path): + domain = "aleph.im" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + + result = runner.invoke( + app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "instance", "--item-hash", item_hash] + ) + + assert result.exit_code == 0, result.stdout + + +def test_domain_attach(account_file: Path): + domain = "aleph.im" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + + result = runner.invoke( + app, ["domain", "attach", domain, "--private-key-file", str(account_file), "--item-hash", item_hash] + ) + + assert result.exit_code == 0, result.stdout + + pattern = rf".*Attach resource to: {domain}.*" + + assert re.match(pattern, result.stdout) + + +def test_domain_detach(account_file: Path): + domain = "aleph.im" + + result = runner.invoke( + app, ["domain", "detach", domain, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + pattern = rf".*Detach resource of: {domain}.*" + + assert re.match(pattern, result.stdout) + + +def test_domain_info(account_file: Path): + domain = "aleph.im" + + result = runner.invoke( + app, ["domain", "info", domain, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + pattern = rf".*Domain: {domain} not configured.*" + + assert re.match(pattern, result.stdout) From e56facbbcfb2e999ccc5f33bd536fc7cd1cac7f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 31 Jan 2024 21:11:46 +0000 Subject: [PATCH 057/110] Adding unit tests for the aggregate command. --- tests/unit/commands/test_aggregate.py | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index e69de29b..5d45418b 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -0,0 +1,48 @@ +from pathlib import Path + +import pytest +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +@pytest.mark.skip(reason="Not implemented.") +def test_aggregate_forget(account_file: Path): + key = "key" + channel = "channel" + + result = runner.invoke( + app, ["aggregate", "forget", key, "--channel", channel, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_aggregate_get(account_file: Path): + key = "key" + address = "address" + + result = runner.invoke( + app, ["aggregate", "get", key, "--address", address, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_aggregate_post(account_file: Path): + key = "key" + content = " {'c': 3, 'd': 4}" + address = "address" + channel = "channel" + inline = "no-inline" + sync = "no-sync" + + result = runner.invoke( + app, ["aggregate", "post", key, content, "--address", address, "--channel", channel, "--inline", inline, "--sync", sync, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout From 2417b4c5fa4f5cc7f2fda1aec9352f60d07e7f6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Wed, 31 Jan 2024 19:21:35 -0300 Subject: [PATCH 058/110] Added a test for node compute --- tests/unit/commands/test_node.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/unit/commands/test_node.py b/tests/unit/commands/test_node.py index e69de29b..65904f4c 100644 --- a/tests/unit/commands/test_node.py +++ b/tests/unit/commands/test_node.py @@ -0,0 +1,20 @@ +import re + +from pathlib import Path + +import pytest +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + +def test_node_compute(): + result = runner.invoke( + app, ["node", "compute"] + ) + assert result.exit_code == 0 + pattern = r".*Compute Node Information.*" + assert re.match(pattern, result.stdout) + # pattern = r".*?([0-9]+\.[0-9]+%|100\.00%|0\.00%).*?([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}).*?([0-9]+\.[0-9]+%|100\.00%).*?([a-z]+).*?" + # assert len(re.findall(pattern, result.stdout, re.MULTILINE)) > 0 \ No newline at end of file From 191835e94196fac87ae6c435ae7cf75d1691db9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 31 Jan 2024 22:30:01 +0000 Subject: [PATCH 059/110] Adding unit tests for the program command (WIP). --- tests/unit/commands/test_program.py | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index e69de29b..a40d863b 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -0,0 +1,44 @@ +import re +import tempfile +from pathlib import Path + +from typer.testing import CliRunner + +from aleph_client.__main__ import app + +runner = CliRunner() + + +def test_program_unpersist(account_file: Path): + item_hash = "098f6bcd4621d373cade4e832627b4f6" + + result = runner.invoke( + app, ["program", "unpersist", item_hash, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + +def test_program_update(account_file: Path): + item_hash = "098f6bcd4621d373cade4e832627b4f6" + path = tempfile.TemporaryFile() + + result = runner.invoke( + app, ["program", "update", item_hash, path, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout + + +def test_program_upload(account_file: Path): + path = tempfile.TemporaryFile() + entrypoint = "entrypoint" + channel = "channel" + memory = "" + + + result = runner.invoke( + app, ["program", "upload", item_hash, path, "--private-key-file", str(account_file)] + ) + + assert result.exit_code == 0, result.stdout \ No newline at end of file From 54e04bca0a801c61ab22eb83ee81f17aaa3d662c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Thu, 1 Feb 2024 01:50:36 -0300 Subject: [PATCH 060/110] Adding unit tests for the instance command. --- tests/unit/commands/test_instance.py | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index e69de29b..0319a2e4 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -0,0 +1,60 @@ +from typer.testing import CliRunner +from aleph_client.__main__ import app +import re +import pytest + +runner = CliRunner() +item_hash = None + + +@pytest.mark.skip(reason="Not implemented.") +def test_instance_create(): + global item_hash + + rootfs = "Ubuntu 22" + rootfs_name = "Ubuntu 22" + vcpus = 1 + memory = 256 + rootfs_size = 2000 + + result = runner.invoke( + app, [ + "instance", "create", + "--rootfs", rootfs, + "--rootfs-name", rootfs_name, + "--vcpus", vcpus, + "--memory", memory, + "--rootfs-size", rootfs_size + ] + ) + + assert result.exit_code == 0 + assert result.stdout + + item_hash_regex = r"\b0x[a-fA-F0-9]{40,42}\b" + item_hashes = re.findall(item_hash_regex, result.stdout) + + item_hash = item_hashes[0] if item_hashes else None + + +@pytest.mark.skip(reason="Not implemented.") +def test_instance_delete(): + result = runner.invoke( + app, [ + "instance", "create", + "--item_hash", item_hash, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + + +def test_instance_list(): + result = runner.invoke( + app, ["instance", "list"] + ) + + assert result.exit_code == 0 + assert result.stdout + assert "Item Hash Vcpus Memory Disk size IPv6 address" in result.stdout From 6a6609939a32cf37f0dd2d65d411c5dc748a031b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Thu, 1 Feb 2024 23:23:40 +0000 Subject: [PATCH 061/110] Simple change. --- tests/unit/commands/test_aggregate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index 5d45418b..b50ba547 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -35,7 +35,7 @@ def test_aggregate_get(account_file: Path): @pytest.mark.skip(reason="Not implemented.") def test_aggregate_post(account_file: Path): key = "key" - content = " {'c': 3, 'd': 4}" + content = "{'c': 3, 'd': 4}" address = "address" channel = "channel" inline = "no-inline" From 84c61628d5b841b09682eb0f94827d9dc8cfbca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Tue, 6 Feb 2024 21:58:26 +0000 Subject: [PATCH 062/110] Improving unit tests. --- tests/unit/commands/test_account.py | 10 ++++++++-- tests/unit/commands/test_aggregate.py | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index a59d52c6..f3ddc0e8 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -9,8 +9,11 @@ def test_account_address(account_file: Path): + private_key = None + private_key_file = str(account_file) + result = runner.invoke( - app, ["account", "address", "--private-key-file", str(account_file)] + app, ["account", "address", "--private-key-file", private_key_file] ) assert result.exit_code == 0 @@ -22,8 +25,11 @@ def test_account_address(account_file: Path): @pytest.mark.skip(reason="Not implemented. It's failing the retrieve the balance for the address.") def test_account_balance(account_file: Path): + private_key = None + private_key_file = str(account_file) + result = runner.invoke( - app, ["account", "balance", "--private-key-file", str(account_file)] + app, ["account", "balance", "--private-key-file", private_key_file] ) assert result.exit_code == 0 diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index b50ba547..c81a2bd8 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -40,9 +40,10 @@ def test_aggregate_post(account_file: Path): channel = "channel" inline = "no-inline" sync = "no-sync" + debug = "no-debug" result = runner.invoke( - app, ["aggregate", "post", key, content, "--address", address, "--channel", channel, "--inline", inline, "--sync", sync, "--private-key-file", str(account_file)] + app, ["aggregate", "post", key, content, "--address", address, "--channel", channel, "--inline", inline, "--sync", sync, "--private-key-file", str(account_file), '--debug', debug] ) assert result.exit_code == 0, result.stdout From 3f17b90f26b819864a97a49c05d0df2eb83c195c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Tue, 6 Feb 2024 19:22:23 -0300 Subject: [PATCH 063/110] Improving unittests at test_aggregate.py. --- tests/unit/commands/test_aggregate.py | 54 ++++++++++++++++++++------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index c81a2bd8..d502cc80 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -10,11 +10,21 @@ @pytest.mark.skip(reason="Not implemented.") def test_aggregate_forget(account_file: Path): - key = "key" - channel = "channel" + key = None + channel = None + private_key = None + private_key_file = str(account_file) + reason = None + debug = "--no-debug" result = runner.invoke( - app, ["aggregate", "forget", key, "--channel", channel, "--private-key-file", str(account_file)] + app, [ + "aggregate", "forget", key, + "--channel", channel, + "--reason", reason, + "--private-key-file", private_key_file, + debug + ] ) assert result.exit_code == 0, result.stdout @@ -22,11 +32,19 @@ def test_aggregate_forget(account_file: Path): @pytest.mark.skip(reason="Not implemented.") def test_aggregate_get(account_file: Path): - key = "key" - address = "address" + key = None + address = None + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" result = runner.invoke( - app, ["aggregate", "get", key, "--address", address, "--private-key-file", str(account_file)] + app, [ + "aggregate", "get", key, + "--address", address, + "--private-key-file", private_key_file, + debug + ] ) assert result.exit_code == 0, result.stdout @@ -34,16 +52,26 @@ def test_aggregate_get(account_file: Path): @pytest.mark.skip(reason="Not implemented.") def test_aggregate_post(account_file: Path): - key = "key" - content = "{'c': 3, 'd': 4}" - address = "address" + key = None + content = None + address = None + private_key = None + private_key_file = str(account_file) channel = "channel" - inline = "no-inline" - sync = "no-sync" - debug = "no-debug" + inline = "--no-inline" + sync = "--no-sync" + debug = "--no-debug" result = runner.invoke( - app, ["aggregate", "post", key, content, "--address", address, "--channel", channel, "--inline", inline, "--sync", sync, "--private-key-file", str(account_file), '--debug', debug] + app, [ + "aggregate", "post", key, content, + "--address", address, + "--channel", channel, + "--private-key-file", private_key_file, + inline, + sync, + debug + ] ) assert result.exit_code == 0, result.stdout From aaab1c265990dd557fced4850a93a02e863b0ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Tue, 6 Feb 2024 19:24:55 -0300 Subject: [PATCH 064/110] Improving unit test --- tests/unit/commands/test_about.py | 1 + tests/unit/commands/test_domain.py | 89 +++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/tests/unit/commands/test_about.py b/tests/unit/commands/test_about.py index 176eab8d..7cbc32f4 100644 --- a/tests/unit/commands/test_about.py +++ b/tests/unit/commands/test_about.py @@ -1,5 +1,6 @@ import re +import pytest from typer.testing import CliRunner from aleph_client.__main__ import app diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index 39379909..e8d274e1 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -9,25 +9,108 @@ runner = CliRunner() +@pytest.mark.skip(reason="Not implemented.") +def test_domain_add(account_file: Path): + fqdn = "aleph.im" + private_key = "private_key" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + target = "ipfs" # {ipfs|program|instance} + owner = "owner" + ask = "--ask" # {--ask|--no-ask} + + result = runner.invoke( + app, [ + "domain", "add", fqdn, + "--private-key", private_key, + "--private-key-file", str(account_file), + "--target", target, + "--item-hash", item_hash, + "--owner", owner, + ask + ] + ) + + assert result.exit_code == 0 + assert result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_attach(account_file: Path): + fqdn = "aleph.im" + private_key = "private_key" + item_hash = "098f6bcd4621d373cade4e832627b4f6" + ask = "--ask" # {--ask|--no-ask} + + result = runner.invoke( + app, [ + "domain", "attach", fqdn, + "--private-key", private_key, + "--private-key-file", str(account_file), + "--item-hash", item_hash, + ask + ] + ) + + assert result.exit_code == 0 + assert result.stdout + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_detach(account_file: Path): + fqdn = "aleph.im" + private_key = "private_key" + ask = "--ask" # {--ask|--no-ask} + + result = runner.invoke( + app, [ + "domain", "detach", fqdn, + "--private-key", private_key, + "--private-key-file", str(account_file), + ask + ] + ) + + assert result.exit_code == 0 + assert result.stdout + + +@pytest.mark.skip(reason="Not implemented.") +def test_domain_info(account_file: Path): + fqdn = "aleph.im" + private_key = "private_key" + + result = runner.invoke( + app, [ + "domain", "info", fqdn, + "--private-key", private_key, + "--private-key-file", str(account_file), + ] + ) + + assert result.exit_code == 0 + assert result.stdout + @pytest.mark.skip(reason="Not implemented.") def test_domain_add_ipfs(account_file: Path): domain = "aleph.im" item_hash = "098f6bcd4621d373cade4e832627b4f6" - + result = runner.invoke( app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "ipfs", "--item-hash", item_hash] ) assert result.exit_code == 0, result.stdout - @pytest.mark.skip(reason="Not implemented.") def test_domain_add_program(account_file: Path): domain = "aleph.im" item_hash = "098f6bcd4621d373cade4e832627b4f6" result = runner.invoke( - app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "program", "--item-hash", item_hash] + app, [ + "domain", "add", domain, + "--private-key-file", str(account_file), + "--target", "program", + "--item-hash", item_hash] ) assert result.exit_code == 0, result.stdout From 73112bb875e69658cccee619f2d00feee5459ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Tue, 6 Feb 2024 22:26:22 +0000 Subject: [PATCH 065/110] Improving unit test files. --- tests/unit/commands/test_account.py | 63 ++++++++++++++++++++++++----- tests/unit/commands/test_file.py | 37 +++++++++++++++-- 2 files changed, 87 insertions(+), 13 deletions(-) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index f3ddc0e8..6563fac1 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -13,7 +13,11 @@ def test_account_address(account_file: Path): private_key_file = str(account_file) result = runner.invoke( - app, ["account", "address", "--private-key-file", private_key_file] + app, [ + "account", + "address", + "--private-key-file", private_key_file + ] ) assert result.exit_code == 0 @@ -25,20 +29,38 @@ def test_account_address(account_file: Path): @pytest.mark.skip(reason="Not implemented. It's failing the retrieve the balance for the address.") def test_account_balance(account_file: Path): + address = None private_key = None private_key_file = str(account_file) result = runner.invoke( - app, ["account", "balance", "--private-key-file", private_key_file] + app, [ + "account", + "balance", + "--address", address, + "--private-key-file", private_key_file + ] ) assert result.exit_code == 0 def test_account_create(account_file: Path): + private_key = None + private_key_file = str(account_file) + replace = "--replace" + debug = "--no-debug" + old_key = account_file.read_bytes() + result = runner.invoke( - app, ["account", "create", "--replace", "--private-key-file", str(account_file)] + app, [ + "account", + "create", + "--private-key-file", private_key_file, + replace, + debug + ] ) assert result.exit_code == 0, result.stdout @@ -49,8 +71,15 @@ def test_account_create(account_file: Path): def test_account_export_private_key(account_file: Path): + private_key = None + private_key_file = str(account_file) + result = runner.invoke( - app, ["account", "export-private-key", "--private-key-file", str(account_file)] + app, [ + "account", + "export-private-key", + "--private-key-file", private_key_file + ] ) assert result.exit_code == 0 @@ -60,19 +89,28 @@ def test_account_export_private_key(account_file: Path): def test_account_path(): - result = runner.invoke(app, ["account", "path"]) + result = runner.invoke(app, [ + "account", + "path" + ]) assert result.stdout.startswith("/") -def test_sign_raw(): +def test_sign_bytes_raw(account_file: Path): + message = "some message" + private_key = "" + private_key_file = str(account_file) + debug = "--no-debug" + result = runner.invoke( app, [ "account", "sign-bytes", - "--message", - "some message", + "--message", message, + "--private-key-file", private_key_file, + debug ], ) @@ -81,13 +119,19 @@ def test_sign_raw(): assert "0x" in result.stdout -def test_sign_raw_stdin(): +def test_sign_bytes_raw_stdin(account_file: Path): message = "some message" + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" + result = runner.invoke( app, [ "account", "sign-bytes", + "--private-key-file", private_key_file, + debug ], input=message, ) @@ -95,4 +139,3 @@ def test_sign_raw_stdin(): assert result.exit_code == 0 assert "0x" in result.stdout - diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index a544fba9..95ab6ac2 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -1,3 +1,4 @@ +from pathlib import Path from tempfile import NamedTemporaryFile from typer.testing import CliRunner @@ -7,13 +8,31 @@ runner = CliRunner() -def test_file_upload(): +def test_file_upload(account_file: Path): + path = None + channel = None + private_key = None + private_key_file = str(account_file) + ref = None + debug = "--no-debug" + # Test upload a file to aleph network by creating a file and upload it to an aleph node with NamedTemporaryFile() as temp_file: temp_file.write(b"Hello World \n") + + path = temp_file.name + result = runner.invoke( app, - ["file", "upload", temp_file.name], + [ + "file", + "upload", + path, + "--channel", channel, + "--private-key-file", private_key_file, + ref, + debug + ], ) assert result.exit_code == 0 @@ -22,13 +41,25 @@ def test_file_upload(): def test_file_download(): + hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" + use_ipfs = "--no-use-ipfs" + output_path = "." + file_name = None + file_extension = None + debug = "--no-debug" + # Test download a file to aleph network result = runner.invoke( app, [ "file", "download", - "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH", + hash, + use_ipfs, + "--output-path", output_path, + "--file-name", file_name, + "--file-extension", file_extension, + debug ], # 5 bytes file ) From 921d95cb33bc233d8ddc4de36b27754f678e72b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 20:20:14 +0000 Subject: [PATCH 066/110] Updating unit tests from domain command. --- tests/unit/commands/test_domain.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index e8d274e1..1ad8583b 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -12,7 +12,8 @@ @pytest.mark.skip(reason="Not implemented.") def test_domain_add(account_file: Path): fqdn = "aleph.im" - private_key = "private_key" + private_key = None + private_key_file = str(account_file) item_hash = "098f6bcd4621d373cade4e832627b4f6" target = "ipfs" # {ipfs|program|instance} owner = "owner" @@ -21,8 +22,7 @@ def test_domain_add(account_file: Path): result = runner.invoke( app, [ "domain", "add", fqdn, - "--private-key", private_key, - "--private-key-file", str(account_file), + "--private-key-file", private_key_file, "--target", target, "--item-hash", item_hash, "--owner", owner, From 6f06f0fcf52286aa50d6e154023c2a65aa2a57a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 17:24:56 -0300 Subject: [PATCH 067/110] Improving unittests at test_help.py. --- tests/unit/commands/test_help.py | 70 ++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/unit/commands/test_help.py b/tests/unit/commands/test_help.py index 61da0d9a..adbdf19e 100644 --- a/tests/unit/commands/test_help.py +++ b/tests/unit/commands/test_help.py @@ -33,3 +33,73 @@ def test_account_help(): assert result.exit_code == 0, result.stdout assert "Sign a message using your private key." in result.stdout + + +def test_aggregate_help(): + result = runner.invoke( + app, ["aggregate", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Manage aggregate messages on aleph.im" in result.stdout + + +def test_domain_help(): + result = runner.invoke( + app, ["domain", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Manage custom Domain (dns) on aleph.im" in result.stdout + + +def test_file_help(): + result = runner.invoke( + app, ["file", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "File uploading and pinning on IPFS and aleph.im" in result.stdout + + +def test_instance_help(): + result = runner.invoke( + app, ["instance", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Manage instances (VMs) on aleph.im network" in result.stdout + + +def test_message_help(): + result = runner.invoke( + app, ["message", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Post, amend, watch and forget messages on aleph.im" in result.stdout + + +def test_node_help(): + result = runner.invoke( + app, ["node", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Get node info on aleph.im network" in result.stdout + + +def test_program_help(): + result = runner.invoke( + app, ["program", "--help"] + ) + + assert result.exit_code == 0, result.stdout + + assert "Upload and update programs on aleph.im VM" in result.stdout From 66d094910729cdb5bb5e484a22727938ad227510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 20:33:35 +0000 Subject: [PATCH 068/110] Adding unit tests for the file command. --- tests/unit/commands/test_file.py | 104 +++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index 95ab6ac2..ede38566 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -66,3 +66,107 @@ def test_file_download(): assert result.exit_code == 0 assert result.stdout is not None + + +def test_file_forget(account_file: Path): + item_hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" + reason = "reason" + channel = "" + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" + + result = runner.invoke( + app, + [ + "file", + "forget", + item_hash, + reason, + "--channel", channel, + "--private-key-file", private_key_file, + debug + ], + ) + + assert result.exit_code == 0 + + assert result.stdout is not None + + +def test_file_forget(account_file: Path): + item_hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" + reason = "reason" + channel = "" + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" + + result = runner.invoke( + app, + [ + "file", + "forget", + item_hash, + reason, + "--channel", channel, + "--private-key-file", private_key_file, + debug + ], + ) + + assert result.exit_code == 0 + + assert result.stdout is not None + + +def test_file_list(account_file: Path): + address = None + private_key = None + private_key_file = str(account_file) + pagination = 100 + page = 1 + sort_order = -1 + json = "--no-json" + + result = runner.invoke( + app, + [ + "file", + "list", + "--address", address, + "--private-key-file", private_key_file, + "--pagination", pagination, + "--page", page, + "--sort-order", sort_order, + "--json", json + ], # 5 bytes file + ) + + assert result.exit_code == 0 + + assert result.stdout is not None + + +def test_file_pin(account_file: Path): + item_hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" + channel = None + private_key = None + private_key_file = str(account_file) + ref = None + debug = "--no-debug" + + result = runner.invoke( + app, + [ + "file", + "pin", + item_hash, + "--channel", channel, + "--private-key-file", private_key_file, + "--ref", ref, + debug + ], + ) + + assert result.exit_code == 0 From 25c98b9be6ad7fbc2dccd5960cde607dff2b46e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 20:39:39 +0000 Subject: [PATCH 069/110] Adding unit tests for the node command. --- tests/unit/commands/test_node.py | 42 ++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/tests/unit/commands/test_node.py b/tests/unit/commands/test_node.py index 65904f4c..334be36d 100644 --- a/tests/unit/commands/test_node.py +++ b/tests/unit/commands/test_node.py @@ -1,20 +1,52 @@ import re -from pathlib import Path - -import pytest from typer.testing import CliRunner from aleph_client.__main__ import app runner = CliRunner() + def test_node_compute(): + json = "--no-json" + active = "--no-active" + address = None + debug = "--no-debug" + result = runner.invoke( - app, ["node", "compute"] + app, [ + "node", + "compute", + json, + active, + "--address", address, + debug + ] ) + assert result.exit_code == 0 + pattern = r".*Compute Node Information.*" assert re.match(pattern, result.stdout) # pattern = r".*?([0-9]+\.[0-9]+%|100\.00%|0\.00%).*?([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}).*?([0-9]+\.[0-9]+%|100\.00%).*?([a-z]+).*?" - # assert len(re.findall(pattern, result.stdout, re.MULTILINE)) > 0 \ No newline at end of file + # assert len(re.findall(pattern, result.stdout, re.MULTILINE)) > 0 + + +def test_node_core(): + json = "--no-json" + active = "--no-active" + address = None + debug = "--no-debug" + + result = runner.invoke( + app, [ + "node", + "core", + json, + active, + "--address", address, + debug + ] + ) + + assert result.exit_code == 0 From c34a803416a76a3c0f1bdab857c8ce06717bec4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Wed, 7 Feb 2024 17:35:47 -0300 Subject: [PATCH 070/110] Updating unit tests from instance command. --- tests/unit/commands/test_instance.py | 84 ++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index 0319a2e4..14db8997 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -6,6 +6,90 @@ runner = CliRunner() item_hash = None +@pytest.mark.skip(reason="Not implemented.") +def test_instance_create(account_file: Path): + channel = "channel" + memory = "memory" + vcpus = "vcpus" + timeout_seconds = "timeout_seconds" + private_key = None + private_key_file = str(account_file) + ssh_pubkey_file = "ssh_pubkey_file" + print_messages = "--print-messages" #[--print-messages|--no-print-messages] + rootfs = "rootfs" + rootfs_name = "rootfs_name" + rootfs_size = "rootfs_size" + debug = "--debug" # [--debug|--no-debug] + persistent_volume = "persistent_volume" + ephemeral_volume = "ephemeral_volume" + immutable_volume = "immutable_volume" + + result = runner.invoke( + app, [ + "instance", "create", + "--channel", channel, + "--memory", memory, + "--vcpus", vcpus, + "--timeout-seconds", timeout_seconds, + "--private-key-file", private_key_file, + "--ssh-pubkey-file", ssh_pubkey_file, + print_messages, + "--rootfs", rootfs, + "--rootfs-name", rootfs_name, + "--rootfs-size", rootfs_size, + debug, + "--persistent-volume", persistent_volume, + "--ephemeral-volume", ephemeral_volume, + "--imutable-volume", immutable_volume + ] + ) + + assert result.exit_code == 0 + assert result.stdout + +@pytest.mark.skip(reason="Not implemented.") +def test_instance_delete(account_file: Path): + item_hash = "item_hash" + reason = "reason" + private_key = None + private_key_file = str(account_file) + print_messages = "--print-messages" #[--print-messages|--no-print-messages] + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "instance", "delete", + item_hash, + "--reason", reason, + "--private-key-file", private_key_file, + print_messages, + debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + +@pytest.mark.skip(reason="Not implemented.") +def test_instance_list(account_file: Path): + address = "address" + private_key = None + private_key_file = str(account_file) + json = "--json" # [--json|--no-json] + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "instance", "list", + "--address", address + "--private-key-file", private_key_file, + json, + debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout @pytest.mark.skip(reason="Not implemented.") def test_instance_create(): From ed64d9fab24bd8e55095cfb902ca5df82b28dde2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Wed, 7 Feb 2024 17:39:35 -0300 Subject: [PATCH 071/110] Updating unit tests from instance command. --- tests/unit/commands/test_instance.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index 14db8997..68bec088 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -1,4 +1,5 @@ from typer.testing import CliRunner +from pathlib import Path from aleph_client.__main__ import app import re import pytest @@ -81,7 +82,7 @@ def test_instance_list(account_file: Path): result = runner.invoke( app, [ "instance", "list", - "--address", address + "--address", address, "--private-key-file", private_key_file, json, debug, From 82ed6212684f6b87c52c1fbe0060efb3a19ef8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 20:56:46 +0000 Subject: [PATCH 072/110] Adding unit tests for the help command. --- tests/unit/commands/test_help.py | 373 ++++++++++++++++++++++++++++++- 1 file changed, 363 insertions(+), 10 deletions(-) diff --git a/tests/unit/commands/test_help.py b/tests/unit/commands/test_help.py index adbdf19e..40a38f14 100644 --- a/tests/unit/commands/test_help.py +++ b/tests/unit/commands/test_help.py @@ -7,7 +7,9 @@ def test_root_help(): result = runner.invoke( - app, ["--help"] + app, [ + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -17,7 +19,10 @@ def test_root_help(): def test_about_help(): result = runner.invoke( - app, ["about", "--help"] + app, [ + "about", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -25,9 +30,24 @@ def test_about_help(): assert "version" in result.stdout +def test_about_version_help(): + result = runner.invoke( + app, [ + "about", + "version" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_account_help(): result = runner.invoke( - app, ["account", "--help"] + app, [ + "account", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -35,9 +55,96 @@ def test_account_help(): assert "Sign a message using your private key." in result.stdout +def test_account_address_help(): + result = runner.invoke( + app, [ + "account", + "address" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_balance_help(): + result = runner.invoke( + app, [ + "account", + "address" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_balance_help(): + result = runner.invoke( + app, [ + "account", + "balance" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_create_help(): + result = runner.invoke( + app, [ + "account", + "create" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_export_private_key_help(): + result = runner.invoke( + app, [ + "account", + "export-private-key" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_path_help(): + result = runner.invoke( + app, [ + "account", + "path" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_account_sign_bytes(): + result = runner.invoke( + app, [ + "account", + "sign-bytes" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_aggregate_help(): result = runner.invoke( - app, ["aggregate", "--help"] + app, [ + "aggregate", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -45,9 +152,48 @@ def test_aggregate_help(): assert "Manage aggregate messages on aleph.im" in result.stdout +def test_aggregate_forget_help(): + result = runner.invoke( + app, [ + "aggregate", + "forget" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_aggregate_get_help(): + result = runner.invoke( + app, [ + "aggregate", + "get" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_aggregate_post_help(): + result = runner.invoke( + app, [ + "aggregate", + "post" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_domain_help(): result = runner.invoke( - app, ["domain", "--help"] + app, [ + "domain", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -55,9 +201,60 @@ def test_domain_help(): assert "Manage custom Domain (dns) on aleph.im" in result.stdout +def test_domain_add_help(): + result = runner.invoke( + app, [ + "domain", + "add" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_domain_attach_help(): + result = runner.invoke( + app, [ + "domain", + "attach" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_domain_detach_help(): + result = runner.invoke( + app, [ + "domain", + "detach" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_domain_info_help(): + result = runner.invoke( + app, [ + "domain", + "info" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_file_help(): result = runner.invoke( - app, ["file", "--help"] + app, [ + "file", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -65,9 +262,72 @@ def test_file_help(): assert "File uploading and pinning on IPFS and aleph.im" in result.stdout +def test_file_download_help(): + result = runner.invoke( + app, [ + "file", + "download" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_file_forget_help(): + result = runner.invoke( + app, [ + "file", + "forget" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_file_list_help(): + result = runner.invoke( + app, [ + "file", + "list" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_file_pin_help(): + result = runner.invoke( + app, [ + "file", + "pin" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_file_upload_help(): + result = runner.invoke( + app, [ + "file", + "upload" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_instance_help(): result = runner.invoke( - app, ["instance", "--help"] + app, [ + "instance", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -75,9 +335,48 @@ def test_instance_help(): assert "Manage instances (VMs) on aleph.im network" in result.stdout +def test_instance_create_help(): + result = runner.invoke( + app, [ + "instance", + "create" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_instance_delete_help(): + result = runner.invoke( + app, [ + "instance", + "delete" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_instance_list_help(): + result = runner.invoke( + app, [ + "instance", + "list" + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_message_help(): result = runner.invoke( - app, ["message", "--help"] + app, [ + "message", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -85,9 +384,60 @@ def test_message_help(): assert "Post, amend, watch and forget messages on aleph.im" in result.stdout +def test_message_amend_help(): + result = runner.invoke( + app, [ + "message", + "amend", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_find_help(): + result = runner.invoke( + app, [ + "message", + "find", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_find_help(): + result = runner.invoke( + app, [ + "message", + "find", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_forget_help(): + result = runner.invoke( + app, [ + "message", + "forget", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_node_help(): result = runner.invoke( - app, ["node", "--help"] + app, [ + "node", + "--help" + ] ) assert result.exit_code == 0, result.stdout @@ -97,7 +447,10 @@ def test_node_help(): def test_program_help(): result = runner.invoke( - app, ["program", "--help"] + app, [ + "program", + "--help" + ] ) assert result.exit_code == 0, result.stdout From 75965823fa9ee281c0d6cf420f555e2e3e3373a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Wed, 7 Feb 2024 17:57:15 -0300 Subject: [PATCH 073/110] Improving unittests at test_message.py (wip). --- tests/unit/commands/test_message.py | 101 +++++++++++++++++++++------- 1 file changed, 77 insertions(+), 24 deletions(-) diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index a7663148..40cb6d25 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -1,5 +1,6 @@ import json import os +import pytest from pathlib import Path from aleph.sdk.chains.ethereum import ETHAccount @@ -26,35 +27,60 @@ def get_test_message(account: ETHAccount): } -def test_message_get(): - # Use subprocess to avoid border effects between tests caused by the initialisation - # of the aiohttp client session out of an async context in the SDK. This avoids - # a "no running event loop" error when running several tests back to back. +@pytest.mark.skip(reason="Not implemented.") +def test_message_amend(account_file: Path): + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" + result = runner.invoke( app, [ "message", - "get", - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + "amend", + "--private-key-file", private_key_file, + debug ], ) assert result.exit_code == 0 - assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - def test_message_find(): + pagination = 1 + page = 1 + message_types = None + content_types = None + content_keys = None + refs = None + addresses = None + tags = None + hashes = "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" + channels = None + chains = "ETH" + start_date = 1234 + end_date = None + ignore_invalid_messages = "--no-ignore-invalid-messages" + result = runner.invoke( app, [ "message", "find", - "--pagination=1", - "--page=1", - "--start-date=1234", - "--chains=ETH", - "--hashes=bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4", + "--pagination", pagination, + "--page", page, + "--message-types", message_types, + "--content-types", content_types, + "--content-keys", content_keys, + "--refs", refs, + "--addresses", addresses, + "--tags", tags, + "--hashes", hashes, + "--channels", channels, + "--chains", chains, + "--start-date", start_date, + "--end-date", end_date, + "--ignore-invalid-messages", ignore_invalid_messages ], ) @@ -62,23 +88,50 @@ def test_message_find(): assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - assert ( - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" - in result.stdout + assert ("bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" in result.stdout) + + +def test_message_get(): + # Use subprocess to avoid border effects between tests caused by the initialisation + # of the aiohttp client session out of an async context in the SDK. This avoids + # a "no running event loop" error when running several tests back to back. + + item_hash = "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" + + result = runner.invoke( + app, + [ + "message", + "get", + item_hash, + ], ) + assert result.exit_code == 0 + + assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout + + +def test_message_post(account_file): + test_file_path = Path(os.path.join( + Path(__file__).parent.parent.parent, "fixtures", "post.json") + ).absolute().as_posix() + + path = str(test_file_path), + type = "test" + ref = None + channel = None + private_key = None + private_key_file = str(account_file) + debug = "--no-debug" -def test_post_message(account_file): - test_file_path = Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")).absolute().as_posix() result = runner.invoke( app, [ "message", "post", - "--private-key-file", - str(account_file), - "--path", - str(test_file_path), + "--private-key-file", private_key_file, + "--path", path ], ) @@ -87,7 +140,7 @@ def test_post_message(account_file): assert "item_hash" in result.stdout -def test_sign_message(account_file): +def test_message_sign(account_file): account = get_account(account_file) message = get_test_message(account) result = runner.invoke( @@ -107,7 +160,7 @@ def test_sign_message(account_file): assert "signature" in result.stdout -def test_sign_message_stdin(account_file): +def test_message_sign_stdin(account_file): account = get_account(account_file) message = get_test_message(account) result = runner.invoke( From fb21fd32c4d39fbc4ef866846aa22d364b3bdd95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Wed, 7 Feb 2024 17:56:48 -0300 Subject: [PATCH 074/110] Updating unit tests from program command. --- tests/unit/commands/test_program.py | 73 +++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index a40d863b..2bfaeb0f 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -5,10 +5,83 @@ from typer.testing import CliRunner from aleph_client.__main__ import app +import pytest runner = CliRunner() +@pytest.mark.skip(reason="Not implemented.") +def test_program_unpersist(account_file: Path): + item_hash = "item_hash" + private_key = None + private_key_file = str(account_file) + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "program", "unpersist", item_hash, + "--private-key-file", private_key_file, + debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + +@pytest.mark.skip(reason="Not implemented.") +def test_program_upload(account_file: Path): + path = "path" + entrypoint = "entrypoint" + channel = "channel" + memory = "memory" + vcpus = "vcpus" + timeout_seconds = "tomeout_seconds" + private_key = None + private_key_file = str(account_file) + print_messages = "--print-messages" # [--print-message|--no-print-message] + print_code_message = "--print-code-message" # [--print-code-message|--no-print-code-message] + print_program_message = "--print-program-message" # [--print-program-message|--no-print-program-message] + runtime = "runtime" + beta = "--beta" # [--beta|--no-beta] + debug = "--debug" # [--debug|--no-debug] + persistent = "--persistent" # [--persistent|--no-persistent] + persistent_volume = "persistent_volume" + + result = runner.invoke( + app, [ + "program", "update", path, entrypoint, + + + "--private-key-file", private_key_file, + print_message, + debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + +@pytest.mark.skip(reason="Not implemented.") +def test_program_update(account_file: Path): + item_hash = "item_hash" + path = "path" + private_key = None + private_key_file = str(account_file) + print_message = "--print-message" # [--print-message|--no-print-message] + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "program", "update", item_hash, path, + "--private-key-file", private_key_file, + print_message, + debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + def test_program_unpersist(account_file: Path): item_hash = "098f6bcd4621d373cade4e832627b4f6" From 103f474a9d3f0ed95b8e2ce9805a06cf752e17f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Wed, 21 Feb 2024 18:09:41 -0300 Subject: [PATCH 075/110] Added tests on test_program.py --- tests/unit/commands/test_program.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 2bfaeb0f..1139c334 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -46,15 +46,28 @@ def test_program_upload(account_file: Path): debug = "--debug" # [--debug|--no-debug] persistent = "--persistent" # [--persistent|--no-persistent] persistent_volume = "persistent_volume" + ephemeral_volume = "ephemeral_volume" + immutable_volume = "immutable_volume" + result = runner.invoke( app, [ "program", "update", path, entrypoint, - - + "--channel", channel, + "--memory", memory, + "--vcpus", vcpus, + "--timeout-seconds", timeout_seconds, "--private-key-file", private_key_file, - print_message, + print_messages, + print_code_message, + print_program_message, + "--runtime", runtime, + beta, debug, + persistent, + "--persistent-volume", persistent_volume, + "--ephemeral-volume", ephemeral_volume, + "--immutable-volume", immutable_volume ] ) From 7764296fa4a4c0a7452737cf712056bc4647ff69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 21 Feb 2024 21:14:56 +0000 Subject: [PATCH 076/110] Improving and fixing unit tests. --- tests/unit/commands/test_help.py | 160 +++++++++++++++++++++++-------- 1 file changed, 122 insertions(+), 38 deletions(-) diff --git a/tests/unit/commands/test_help.py b/tests/unit/commands/test_help.py index 40a38f14..2b8eb459 100644 --- a/tests/unit/commands/test_help.py +++ b/tests/unit/commands/test_help.py @@ -34,7 +34,7 @@ def test_about_version_help(): result = runner.invoke( app, [ "about", - "version" + "version", "--help" ] ) @@ -59,7 +59,7 @@ def test_account_address_help(): result = runner.invoke( app, [ "account", - "address" + "address", "--help" ] ) @@ -71,19 +71,7 @@ def test_account_balance_help(): result = runner.invoke( app, [ "account", - "address" - "--help" - ] - ) - - assert result.exit_code == 0, result.stdout - - -def test_account_balance_help(): - result = runner.invoke( - app, [ - "account", - "balance" + "balance", "--help" ] ) @@ -95,7 +83,7 @@ def test_account_create_help(): result = runner.invoke( app, [ "account", - "create" + "create", "--help" ] ) @@ -107,7 +95,7 @@ def test_account_export_private_key_help(): result = runner.invoke( app, [ "account", - "export-private-key" + "export-private-key", "--help" ] ) @@ -119,7 +107,7 @@ def test_account_path_help(): result = runner.invoke( app, [ "account", - "path" + "path", "--help" ] ) @@ -131,7 +119,7 @@ def test_account_sign_bytes(): result = runner.invoke( app, [ "account", - "sign-bytes" + "sign-bytes", "--help" ] ) @@ -156,7 +144,7 @@ def test_aggregate_forget_help(): result = runner.invoke( app, [ "aggregate", - "forget" + "forget", "--help" ] ) @@ -168,7 +156,7 @@ def test_aggregate_get_help(): result = runner.invoke( app, [ "aggregate", - "get" + "get", "--help" ] ) @@ -180,7 +168,7 @@ def test_aggregate_post_help(): result = runner.invoke( app, [ "aggregate", - "post" + "post", "--help" ] ) @@ -205,7 +193,7 @@ def test_domain_add_help(): result = runner.invoke( app, [ "domain", - "add" + "add", "--help" ] ) @@ -217,7 +205,7 @@ def test_domain_attach_help(): result = runner.invoke( app, [ "domain", - "attach" + "attach", "--help" ] ) @@ -229,7 +217,7 @@ def test_domain_detach_help(): result = runner.invoke( app, [ "domain", - "detach" + "detach", "--help" ] ) @@ -241,7 +229,7 @@ def test_domain_info_help(): result = runner.invoke( app, [ "domain", - "info" + "info", "--help" ] ) @@ -266,7 +254,7 @@ def test_file_download_help(): result = runner.invoke( app, [ "file", - "download" + "download", "--help" ] ) @@ -278,7 +266,7 @@ def test_file_forget_help(): result = runner.invoke( app, [ "file", - "forget" + "forget", "--help" ] ) @@ -290,7 +278,7 @@ def test_file_list_help(): result = runner.invoke( app, [ "file", - "list" + "list", "--help" ] ) @@ -302,7 +290,7 @@ def test_file_pin_help(): result = runner.invoke( app, [ "file", - "pin" + "pin", "--help" ] ) @@ -314,7 +302,7 @@ def test_file_upload_help(): result = runner.invoke( app, [ "file", - "upload" + "upload", "--help" ] ) @@ -339,7 +327,7 @@ def test_instance_create_help(): result = runner.invoke( app, [ "instance", - "create" + "create", "--help" ] ) @@ -351,7 +339,7 @@ def test_instance_delete_help(): result = runner.invoke( app, [ "instance", - "delete" + "delete", "--help" ] ) @@ -363,7 +351,7 @@ def test_instance_list_help(): result = runner.invoke( app, [ "instance", - "list" + "list", "--help" ] ) @@ -408,11 +396,11 @@ def test_message_find_help(): assert result.exit_code == 0, result.stdout -def test_message_find_help(): +def test_message_forget_help(): result = runner.invoke( app, [ "message", - "find", + "forget", "--help" ] ) @@ -420,11 +408,47 @@ def test_message_find_help(): assert result.exit_code == 0, result.stdout -def test_message_forget_help(): +def test_message_get_help(): result = runner.invoke( app, [ "message", - "forget", + "get", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_post_help(): + result = runner.invoke( + app, [ + "message", + "post", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_sign_help(): + result = runner.invoke( + app, [ + "message", + "sign", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_message_watch_help(): + result = runner.invoke( + app, [ + "message", + "watch", "--help" ] ) @@ -445,6 +469,30 @@ def test_node_help(): assert "Get node info on aleph.im network" in result.stdout +def test_node_compute_help(): + result = runner.invoke( + app, [ + "node", + "compute", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_node_core_help(): + result = runner.invoke( + app, [ + "node", + "core", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + def test_program_help(): result = runner.invoke( app, [ @@ -456,3 +504,39 @@ def test_program_help(): assert result.exit_code == 0, result.stdout assert "Upload and update programs on aleph.im VM" in result.stdout + + +def test_program_unpersist_help(): + result = runner.invoke( + app, [ + "program", + "unpersist", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_program_update_help(): + result = runner.invoke( + app, [ + "program", + "update", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout + + +def test_program_upload_help(): + result = runner.invoke( + app, [ + "program", + "upload", + "--help" + ] + ) + + assert result.exit_code == 0, result.stdout From 35741ff1606ba66b9841bb4cc9b0933fb6939245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 21 Feb 2024 21:19:57 +0000 Subject: [PATCH 077/110] Enabling unit test. --- tests/unit/commands/test_account.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index 6563fac1..7054662d 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -27,7 +27,6 @@ def test_account_address(account_file: Path): assert len(result.stdout.strip()) == 42 -@pytest.mark.skip(reason="Not implemented. It's failing the retrieve the balance for the address.") def test_account_balance(account_file: Path): address = None private_key = None From 43f89fc71172ba3aa4e1e1f9e4651814d4d59a54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Mon, 26 Feb 2024 22:26:32 +0000 Subject: [PATCH 078/110] Fixing and improving account unit tests. --- tests/unit/commands/test_account.py | 37 +++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index 7054662d..bd440f3b 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -1,6 +1,5 @@ +import re from pathlib import Path - -import pytest from typer.testing import CliRunner from aleph_client.__main__ import app @@ -9,7 +8,7 @@ def test_account_address(account_file: Path): - private_key = None + # private_key = None private_key_file = str(account_file) result = runner.invoke( @@ -22,14 +21,16 @@ def test_account_address(account_file: Path): assert result.exit_code == 0 - assert result.stdout.startswith("0x") + pattern = r"0x.*" + assert re.match(pattern, result.stdout) assert len(result.stdout.strip()) == 42 +# TODO Verify if the output message ""Failed to retrieve balance..." is ok!!! def test_account_balance(account_file: Path): address = None - private_key = None + # private_key = None private_key_file = str(account_file) result = runner.invoke( @@ -41,11 +42,14 @@ def test_account_balance(account_file: Path): ] ) + pattern = r"Failed to retrieve balance for address 0x.*\. Status code: 404" + assert re.match(pattern, result.stdout) + assert result.exit_code == 0 def test_account_create(account_file: Path): - private_key = None + # private_key = None private_key_file = str(account_file) replace = "--replace" debug = "--no-debug" @@ -66,11 +70,14 @@ def test_account_create(account_file: Path): new_key = account_file.read_bytes() + pattern = r"Private key stored in .*" + assert re.match(pattern, result.stdout) + assert new_key != old_key def test_account_export_private_key(account_file: Path): - private_key = None + # private_key = None private_key_file = str(account_file) result = runner.invoke( @@ -84,6 +91,9 @@ def test_account_export_private_key(account_file: Path): assert result.stdout.startswith("0x") + pattern = r"0x.*" + assert re.match(pattern, result.stdout) + assert len(result.stdout.strip()) == 66 @@ -93,12 +103,13 @@ def test_account_path(): "path" ]) - assert result.stdout.startswith("/") + pattern = r".*.aleph-im/private-keys/ethereum\.key" + assert re.match(pattern, result.stdout) def test_sign_bytes_raw(account_file: Path): message = "some message" - private_key = "" + # private_key = None private_key_file = str(account_file) debug = "--no-debug" @@ -115,12 +126,13 @@ def test_sign_bytes_raw(account_file: Path): assert result.exit_code == 0 - assert "0x" in result.stdout + pattern = r"0x.*" + assert re.match(pattern, result.stdout) def test_sign_bytes_raw_stdin(account_file: Path): message = "some message" - private_key = None + # private_key = None private_key_file = str(account_file) debug = "--no-debug" @@ -137,4 +149,5 @@ def test_sign_bytes_raw_stdin(account_file: Path): assert result.exit_code == 0 - assert "0x" in result.stdout + pattern = r"0x.*" + assert re.match(pattern, result.stdout) From d377560ce2cd90cf31c24d8b0b51d2e1efa23598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Mon, 26 Feb 2024 22:33:29 +0000 Subject: [PATCH 079/110] Fixing and improving aggregate unit tests. --- tests/unit/commands/test_aggregate.py | 42 +++++++++++++-------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index d502cc80..948a49e6 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -8,21 +8,26 @@ runner = CliRunner() -@pytest.mark.skip(reason="Not implemented.") -def test_aggregate_forget(account_file: Path): - key = None - channel = None - private_key = None +# TODO Stopped here!!! +def test_aggregate_post(account_file: Path): + key = "key" + content = """{"c": 3, "d": "test"}""" + address = None + # private_key = None private_key_file = str(account_file) - reason = None + channel = "channel" + inline = "--no-inline" + sync = "--no-sync" debug = "--no-debug" result = runner.invoke( app, [ - "aggregate", "forget", key, + "aggregate", "post", key, content, + "--address", address, "--channel", channel, - "--reason", reason, "--private-key-file", private_key_file, + inline, + sync, debug ] ) @@ -30,11 +35,10 @@ def test_aggregate_forget(account_file: Path): assert result.exit_code == 0, result.stdout -@pytest.mark.skip(reason="Not implemented.") def test_aggregate_get(account_file: Path): key = None address = None - private_key = None + # private_key = None private_key_file = str(account_file) debug = "--no-debug" @@ -50,26 +54,20 @@ def test_aggregate_get(account_file: Path): assert result.exit_code == 0, result.stdout -@pytest.mark.skip(reason="Not implemented.") -def test_aggregate_post(account_file: Path): +def test_aggregate_forget(account_file: Path): key = None - content = None - address = None - private_key = None + channel = None + # private_key = None private_key_file = str(account_file) - channel = "channel" - inline = "--no-inline" - sync = "--no-sync" + reason = None debug = "--no-debug" result = runner.invoke( app, [ - "aggregate", "post", key, content, - "--address", address, + "aggregate", "forget", key, "--channel", channel, + "--reason", reason, "--private-key-file", private_key_file, - inline, - sync, debug ] ) From 74fdf6081b5aa74dd83af1628174a938a11e1008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 26 Feb 2024 19:34:58 -0300 Subject: [PATCH 080/110] aleph test upload program passing --- tests/fixtures/example_program_upload.zip | Bin 0 -> 373 bytes tests/unit/commands/test_program.py | 29 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/example_program_upload.zip diff --git a/tests/fixtures/example_program_upload.zip b/tests/fixtures/example_program_upload.zip new file mode 100644 index 0000000000000000000000000000000000000000..9321b4dd6d03cdb0955641ce5ca0c6cacf243308 GIT binary patch literal 373 zcmWIWW@Zs#U|`^2*fuFN;*!>P(d|H910w^22!jkmYDHphK~8FXT4HfYVnJrSpWoAYR>=tK5 zCX`Y9@~f4{%X)L&wHh2v(f6vqf7;u$(y6lI^}7shf1Qd?Nhe{O zZhvNUsP2Cq>1N1~)3kM$bZ5w-q!Yf4IVJN9C%P1JahqM*blC5$Z{#Gez}Sw{F<L+SMjZ?E3`GWKX|j^ zi+Syu`k5bC0=yZS Date: Mon, 26 Feb 2024 19:41:47 -0300 Subject: [PATCH 081/110] =?UTF-8?q?Structuring=20tests=20for=20=E2=80=98me?= =?UTF-8?q?ssages=E2=80=99=20command.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/commands/test_message.py | 85 +++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 17 deletions(-) diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index 40cb6d25..560d3b5e 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -29,8 +29,8 @@ def get_test_message(account: ETHAccount): @pytest.mark.skip(reason="Not implemented.") def test_message_amend(account_file: Path): - private_key = None - private_key_file = str(account_file) + # private_key = None + private_key_file = get_account(account_file) debug = "--no-debug" result = runner.invoke( @@ -46,6 +46,7 @@ def test_message_amend(account_file: Path): assert result.exit_code == 0 +@pytest.mark.skip(reason="Not implemented.") def test_message_find(): pagination = 1 page = 1 @@ -91,6 +92,29 @@ def test_message_find(): assert ("bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" in result.stdout) +@pytest.mark.skip(reason="Not implemented.") +def test_message_forget(account_file: Path): + reason = None + channel = None + # private_key = None + private_key_file = get_account(account_file) + debug = "--no-debug" + + result = runner.invoke( + app, + [ + "message", + "forget", + "--reason", reason, + "--channel", channel, + "--private-key-file", private_key_file, + debug, + ], + ) + + assert result.exit_code == 0 + + def test_message_get(): # Use subprocess to avoid border effects between tests caused by the initialisation # of the aiohttp client session out of an async context in the SDK. This avoids @@ -118,11 +142,11 @@ def test_message_post(account_file): ).absolute().as_posix() path = str(test_file_path), - type = "test" + message_type = "test" ref = None channel = None - private_key = None - private_key_file = str(account_file) + # private_key = None + private_key_file = get_account(account_file) debug = "--no-debug" result = runner.invoke( @@ -130,8 +154,12 @@ def test_message_post(account_file): [ "message", "post", + "--path", path, + "--type", message_type, + "--ref", ref, + "--channel", channel, "--private-key-file", private_key_file, - "--path", path + debug ], ) @@ -141,17 +169,19 @@ def test_message_post(account_file): def test_message_sign(account_file): - account = get_account(account_file) - message = get_test_message(account) + # private_key = None + private_key_file = get_account(account_file) + message = get_test_message(private_key_file) + debug = "--no-debug" + result = runner.invoke( app, [ "message", "sign", - "--private-key-file", - str(account_file), - "--message", - json.dumps(message), + "--message", json.dumps(message), + "--private-key-file", private_key_file, + debug ], ) @@ -161,19 +191,40 @@ def test_message_sign(account_file): def test_message_sign_stdin(account_file): - account = get_account(account_file) - message = get_test_message(account) + # private_key = None + private_key_file = get_account(account_file) + message = get_test_message(private_key_file) + debug = "--no-debug" + result = runner.invoke( app, [ "message", "sign", - "--private-key-file", - str(account_file), + "--message", message, + "--private-key-file", private_key_file, + debug ], input=json.dumps(message), ) assert result.exit_code == 0 - assert "signature" in result.stdout \ No newline at end of file + assert "signature" in result.stdout + + +def test_message_watch(account_file: Path): + indent = None + debug = "--no-debug" + + result = runner.invoke( + app, + [ + "message", + "watch", + "--indent", indent, + debug + ], + ) + + assert result.exit_code == 0 From c628cf5396f699154e5b241e23dd7cdde712d295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Fri, 1 Mar 2024 17:07:42 -0300 Subject: [PATCH 082/110] =?UTF-8?q?Restructuring=20tests=20for=20=E2=80=98?= =?UTF-8?q?domain=E2=80=99=20command.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/commands/test_domain.py | 141 ++++++++++++----------------- 1 file changed, 59 insertions(+), 82 deletions(-) diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index 1ad8583b..1825bc5e 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -10,14 +10,14 @@ @pytest.mark.skip(reason="Not implemented.") -def test_domain_add(account_file: Path): +def test_domain_add_ipfs(account_file: Path): fqdn = "aleph.im" - private_key = None + # private_key = None private_key_file = str(account_file) + target = "ipfs" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - target = "ipfs" # {ipfs|program|instance} owner = "owner" - ask = "--ask" # {--ask|--no-ask} + ask = "--ask" # {--ask|--no-ask} result = runner.invoke( app, [ @@ -27,26 +27,31 @@ def test_domain_add(account_file: Path): "--item-hash", item_hash, "--owner", owner, ask - ] + ] ) assert result.exit_code == 0 assert result.stdout +# noinspection DuplicatedCode @pytest.mark.skip(reason="Not implemented.") -def test_domain_attach(account_file: Path): - fqdn = "aleph.im" - private_key = "private_key" +def test_domain_add_program(account_file: Path): + fqdn = "aleph.im" # domain + # private_key = None + private_key_file = str(account_file) + target = "program" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - ask = "--ask" # {--ask|--no-ask} + owner = "owner" + ask = "--ask" # {--ask|--no-ask} result = runner.invoke( app, [ - "domain", "attach", fqdn, - "--private-key", private_key, - "--private-key-file", str(account_file), + "domain", "add", fqdn, + "--private-key-file", private_key_file, + "--target", target, "--item-hash", item_hash, + "--owner", owner, ask ] ) @@ -54,17 +59,25 @@ def test_domain_attach(account_file: Path): assert result.exit_code == 0 assert result.stdout + +# noinspection DuplicatedCode @pytest.mark.skip(reason="Not implemented.") -def test_domain_detach(account_file: Path): - fqdn = "aleph.im" - private_key = "private_key" - ask = "--ask" # {--ask|--no-ask} +def test_domain_add_instance(account_file: Path): + fqdn = "aleph.im" # domain + # private_key = None + private_key_file = str(account_file) + target = "instance" # {ipfs|program|instance} + item_hash = "098f6bcd4621d373cade4e832627b4f6" + owner = "owner" + ask = "--ask" # {--ask|--no-ask} result = runner.invoke( app, [ - "domain", "detach", fqdn, - "--private-key", private_key, - "--private-key-file", str(account_file), + "domain", "add", fqdn, + "--private-key-file", private_key_file, + "--target", target, + "--item-hash", item_hash, + "--owner", owner, ask ] ) @@ -74,98 +87,62 @@ def test_domain_detach(account_file: Path): @pytest.mark.skip(reason="Not implemented.") -def test_domain_info(account_file: Path): - fqdn = "aleph.im" - private_key = "private_key" - - result = runner.invoke( - app, [ - "domain", "info", fqdn, - "--private-key", private_key, - "--private-key-file", str(account_file), - ] - ) - - assert result.exit_code == 0 - assert result.stdout - -@pytest.mark.skip(reason="Not implemented.") -def test_domain_add_ipfs(account_file: Path): - domain = "aleph.im" - item_hash = "098f6bcd4621d373cade4e832627b4f6" - - result = runner.invoke( - app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "ipfs", "--item-hash", item_hash] - ) - - assert result.exit_code == 0, result.stdout - -@pytest.mark.skip(reason="Not implemented.") -def test_domain_add_program(account_file: Path): - domain = "aleph.im" - item_hash = "098f6bcd4621d373cade4e832627b4f6" - - result = runner.invoke( - app, [ - "domain", "add", domain, - "--private-key-file", str(account_file), - "--target", "program", - "--item-hash", item_hash] - ) - - assert result.exit_code == 0, result.stdout - - -@pytest.mark.skip(reason="Not implemented.") -def test_domain_add_instance(account_file: Path): - domain = "aleph.im" - item_hash = "098f6bcd4621d373cade4e832627b4f6" - - result = runner.invoke( - app, ["domain", "add", domain, "--private-key-file", str(account_file), "--target", "instance", "--item-hash", item_hash] - ) - - assert result.exit_code == 0, result.stdout - - def test_domain_attach(account_file: Path): - domain = "aleph.im" + fqdn = "aleph.im" # domain + # private_key = None + private_key_file = str(account_file) item_hash = "098f6bcd4621d373cade4e832627b4f6" result = runner.invoke( - app, ["domain", "attach", domain, "--private-key-file", str(account_file), "--item-hash", item_hash] + app, [ + "domain", "attach", fqdn, + "--private-key-file", private_key_file, + "--item-hash", item_hash, + ] ) assert result.exit_code == 0, result.stdout - pattern = rf".*Attach resource to: {domain}.*" + pattern = rf".*Attach resource to: {fqdn}.*" assert re.match(pattern, result.stdout) +@pytest.mark.skip(reason="Not implemented.") def test_domain_detach(account_file: Path): - domain = "aleph.im" + fqdn = "aleph.im" # domain + # private_key = None + private_key_file = str(account_file) result = runner.invoke( - app, ["domain", "detach", domain, "--private-key-file", str(account_file)] + app, [ + "domain", "detach", fqdn, + "--private-key-file", private_key_file, + ] ) assert result.exit_code == 0, result.stdout - pattern = rf".*Detach resource of: {domain}.*" + pattern = rf".*Detach resource of: {fqdn}.*" assert re.match(pattern, result.stdout) +@pytest.mark.skip(reason="Not implemented.") def test_domain_info(account_file: Path): - domain = "aleph.im" + fqdn = "aleph.im" # domain + # private_key = None + private_key_file = str(account_file) result = runner.invoke( - app, ["domain", "info", domain, "--private-key-file", str(account_file)] + app, [ + "domain", "info", fqdn, + "--private-key-file", private_key_file, + ] ) assert result.exit_code == 0, result.stdout - pattern = rf".*Domain: {domain} not configured.*" + pattern = rf".*Domain: {fqdn} not configured.*" assert re.match(pattern, result.stdout) From c174c9dcd109796c7412f401e8ddf5a6f946692d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Fri, 1 Mar 2024 20:08:01 +0000 Subject: [PATCH 083/110] Adding new test aggreate comparing the full output. --- tests/unit/commands/test_about.py | 2 -- tests/unit/commands/test_aggregate.py | 41 +++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/tests/unit/commands/test_about.py b/tests/unit/commands/test_about.py index 7cbc32f4..ccab0d55 100644 --- a/tests/unit/commands/test_about.py +++ b/tests/unit/commands/test_about.py @@ -1,6 +1,4 @@ import re - -import pytest from typer.testing import CliRunner from aleph_client.__main__ import app diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index 948a49e6..38bade41 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -1,3 +1,7 @@ +import textwrap + +import re + from pathlib import Path import pytest @@ -8,16 +12,15 @@ runner = CliRunner() -# TODO Stopped here!!! def test_aggregate_post(account_file: Path): key = "key" content = """{"c": 3, "d": "test"}""" address = None - # private_key = None - private_key_file = str(account_file) channel = "channel" inline = "--no-inline" sync = "--no-sync" + # private_key = None + private_key_file = str(account_file) debug = "--no-debug" result = runner.invoke( @@ -34,6 +37,38 @@ def test_aggregate_post(account_file: Path): assert result.exit_code == 0, result.stdout + pattern = textwrap.dedent( + '''\ + \{ + "chain": "ETH", + "sender": ".*", + "type": "AGGREGATE", + "channel": "channel", + "confirmations": null, + "confirmed": null, + "signature": ".*", + "size": null, + "time": [0-9]+\.[0-9]+, + "item_type": "storage", + "item_content": null, + "hash_type": null, + "item_hash": ".*", + "content": \{ + "address": ".*", + "time": [0-9]+\.[0-9]+, + "key": "key", + "content": \{ + "c": 3, + "d": "test" + \} + \}, + "forgotten_by": null + \} + ''' + ) + + assert re.fullmatch(pattern, result.stdout) + def test_aggregate_get(account_file: Path): key = None From 2fb4849ed23756f505911acb78d5d18858ad7efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Fri, 1 Mar 2024 20:19:22 +0000 Subject: [PATCH 084/110] Working with the aggregate tests. --- tests/unit/commands/test_aggregate.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index 38bade41..7e9ccafc 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -1,10 +1,6 @@ -import textwrap - import re - +import textwrap from pathlib import Path - -import pytest from typer.testing import CliRunner from aleph_client.__main__ import app @@ -70,8 +66,9 @@ def test_aggregate_post(account_file: Path): assert re.fullmatch(pattern, result.stdout) +# TODO Stopped here!!! def test_aggregate_get(account_file: Path): - key = None + key = "key" address = None # private_key = None private_key_file = str(account_file) @@ -86,6 +83,10 @@ def test_aggregate_get(account_file: Path): ] ) + print("exit_code:") + print(result.exit_code) + print("stdout:") + assert result.exit_code == 0, result.stdout From 372332a75bafac78f1b9f9ecb90941d8f49e2810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Fri, 1 Mar 2024 17:24:17 -0300 Subject: [PATCH 085/110] Using fixture to get the item_hash --- tests/unit/commands/test_program.py | 125 +++++++++++++++------------- 1 file changed, 66 insertions(+), 59 deletions(-) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 88b05058..412999f8 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -11,28 +11,12 @@ runner = CliRunner() -@pytest.mark.skip(reason="Not implemented.") -def test_program_unpersist(account_file: Path): - item_hash = "item_hash" - private_key = None - private_key_file = str(account_file) - debug = "--debug" # [--debug|--no-debug] - - result = runner.invoke( - app, [ - "program", "unpersist", item_hash, - "--private-key-file", private_key_file, - debug, - ] - ) - - assert result.exit_code == 0 - assert result.stdout - -@pytest.mark.skip(reason="Not implemented.") def test_program_upload(account_file: Path): - path = "path" - entrypoint = "entrypoint" + path = Path(os.path.join( + Path(__file__).parent.parent.parent, "fixtures", "example_program_upload.zip") + ).absolute().as_posix() + + entrypoint = "__init__:app" channel = "channel" memory = "memory" vcpus = "vcpus" @@ -42,7 +26,7 @@ def test_program_upload(account_file: Path): print_messages = "--print-messages" # [--print-message|--no-print-message] print_code_message = "--print-code-message" # [--print-code-message|--no-print-code-message] print_program_message = "--print-program-message" # [--print-program-message|--no-print-program-message] - runtime = "runtime" + runtime = "f873715dc2feec3833074bd4b8745363a0e0093746b987b4c8191268883b2463" beta = "--beta" # [--beta|--no-beta] debug = "--debug" # [--debug|--no-debug] persistent = "--persistent" # [--persistent|--no-persistent] @@ -54,30 +38,57 @@ def test_program_upload(account_file: Path): result = runner.invoke( app, [ "program", "upload", path, entrypoint, - "--channel", channel, - "--memory", memory, - "--vcpus", vcpus, - "--timeout-seconds", timeout_seconds, - "--private-key-file", private_key_file, - print_messages, - print_code_message, - print_program_message, + # "--channel", channel, + # "--memory", memory, + # "--vcpus", vcpus, + # "--timeout-seconds", timeout_seconds, + # "--private-key-file", private_key_file, + # print_messages, + # print_code_message, + # print_program_message, "--runtime", runtime, - beta, - debug, - persistent, - "--persistent-volume", persistent_volume, - "--ephemeral-volume", ephemeral_volume, - "--immutable-volume", immutable_volume + # beta, + # debug, + # persistent, + # "--persistent-volume", persistent_volume, + # "--ephemeral-volume", ephemeral_volume, + # "--immutable-volume", immutable_volume ] ) + pattern = r"Your program has been uploaded on aleph.im" + assert re.match(pattern, result.stdout) assert result.exit_code == 0 assert result.stdout + padrao = r'https://aleph\.sh/vm/(\w+)' + correspondencias = re.findall(padrao, result.stdout) + if correspondencias: + item_hash = correspondencias[0] + + + +def test_program_update(account_file: Path): + item_hash = "item_hash" + path = "path" + private_key = None + private_key_file = str(account_file) + print_message = "--print-message" # [--print-message|--no-print-message] + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "program", "update", item_hash, path, + "--private-key-file", private_key_file, + print_message, + debug, + ] + ) + assert result.exit_code == 0 + assert result.stdout -def test_program_upload_01(account_file: Path): - #path = Path("./../../fixtures/example_program_upload.zip").absolute() +@pytest.fixture +def item_hash_upload(account_file: Path): path = Path(os.path.join( Path(__file__).parent.parent.parent, "fixtures", "example_program_upload.zip") ).absolute().as_posix() @@ -87,41 +98,36 @@ def test_program_upload_01(account_file: Path): result = runner.invoke( app, [ - "program", "upload", path, entrypoint, - "--runtime", runtime, + "program", "upload", path, entrypoint, "--runtime", runtime ] ) - print("##########################") - - print(result.stdout) - - - assert result.exit_code == 0 - assert result.stdout + pattern = r'https://aleph\.sh/vm/(\w+)' + matchings = re.findall(pattern, result.stdout) + if matchings: + item_hash = matchings[0] + return item_hash -@pytest.mark.skip(reason="Not implemented.") -def test_program_update(account_file: Path): - item_hash = "item_hash" - path = "path" +def test_program_unpersist(account_file: Path, item_hash_upload): + item_hash = item_hash_upload private_key = None private_key_file = str(account_file) - print_message = "--print-message" # [--print-message|--no-print-message] - debug = "--debug" # [--debug|--no-debug] + # debug = "--debug" # [--debug|--no-debug] result = runner.invoke( app, [ - "program", "update", item_hash, path, + "program", "unpersist", item_hash, "--private-key-file", private_key_file, - print_message, - debug, + # debug, ] ) assert result.exit_code == 0 assert result.stdout -def test_program_unpersist(account_file: Path): + print(result.stdout) + +def test_program_unpersist_x(account_file: Path): item_hash = "098f6bcd4621d373cade4e832627b4f6" result = runner.invoke( @@ -131,7 +137,7 @@ def test_program_unpersist(account_file: Path): assert result.exit_code == 0, result.stdout -def test_program_update(account_file: Path): +def test_program_update_x(account_file: Path): item_hash = "098f6bcd4621d373cade4e832627b4f6" path = tempfile.TemporaryFile() @@ -142,7 +148,8 @@ def test_program_update(account_file: Path): assert result.exit_code == 0, result.stdout -def test_program_upload(account_file: Path): +@pytest.mark.skip(reason="Not implemented.") +def test_program_upload_x(account_file: Path): item_hash = "item_hash" path = tempfile.TemporaryFile() entrypoint = "entrypoint" From 4d487e9c0d5cc68f9c9364cfff9a22506ac716d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darley=20Ara=C3=BAjo=20Silva?= Date: Fri, 1 Mar 2024 17:39:05 -0300 Subject: [PATCH 086/110] Working on tests for the "domain" command (wip). --- tests/unit/commands/test_domain.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index 1825bc5e..01f64326 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -16,8 +16,7 @@ def test_domain_add_ipfs(account_file: Path): private_key_file = str(account_file) target = "ipfs" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - owner = "owner" - ask = "--ask" # {--ask|--no-ask} + owner = None result = runner.invoke( app, [ @@ -26,7 +25,6 @@ def test_domain_add_ipfs(account_file: Path): "--target", target, "--item-hash", item_hash, "--owner", owner, - ask ] ) @@ -42,8 +40,7 @@ def test_domain_add_program(account_file: Path): private_key_file = str(account_file) target = "program" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - owner = "owner" - ask = "--ask" # {--ask|--no-ask} + owner = None result = runner.invoke( app, [ @@ -52,7 +49,6 @@ def test_domain_add_program(account_file: Path): "--target", target, "--item-hash", item_hash, "--owner", owner, - ask ] ) @@ -68,8 +64,7 @@ def test_domain_add_instance(account_file: Path): private_key_file = str(account_file) target = "instance" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - owner = "owner" - ask = "--ask" # {--ask|--no-ask} + owner = None result = runner.invoke( app, [ @@ -78,7 +73,6 @@ def test_domain_add_instance(account_file: Path): "--target", target, "--item-hash", item_hash, "--owner", owner, - ask ] ) @@ -128,7 +122,6 @@ def test_domain_detach(account_file: Path): assert re.match(pattern, result.stdout) -@pytest.mark.skip(reason="Not implemented.") def test_domain_info(account_file: Path): fqdn = "aleph.im" # domain # private_key = None From 6b00a4691724b77dbd2a37cce3d6dc7266f79dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 16:50:08 -0300 Subject: [PATCH 087/110] Fixed a bug on calling a asybc function withou await, working on program update command test --- src/aleph_client/commands/program.py | 4 +-- tests/unit/commands/test_program.py | 50 +++++++++++++--------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/aleph_client/commands/program.py b/src/aleph_client/commands/program.py index ee680c54..fd0aff72 100644 --- a/src/aleph_client/commands/program.py +++ b/src/aleph_client/commands/program.py @@ -199,11 +199,11 @@ async def update( async with AuthenticatedAlephHttpClient( account=account, api_server=sdk_settings.API_HOST ) as client: - program_message: ProgramMessage = client.get_message( + program_message: ProgramMessage = await client.get_message( item_hash=item_hash, message_type=ProgramMessage ) code_ref = program_message.content.code.ref - code_message: StoreMessage = client.get_message( + code_message: StoreMessage = await client.get_message( item_hash=code_ref, message_type=StoreMessage ) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 412999f8..8838088b 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -58,34 +58,7 @@ def test_program_upload(account_file: Path): pattern = r"Your program has been uploaded on aleph.im" assert re.match(pattern, result.stdout) - assert result.exit_code == 0 - assert result.stdout - padrao = r'https://aleph\.sh/vm/(\w+)' - correspondencias = re.findall(padrao, result.stdout) - if correspondencias: - item_hash = correspondencias[0] - - - -def test_program_update(account_file: Path): - item_hash = "item_hash" - path = "path" - private_key = None - private_key_file = str(account_file) - print_message = "--print-message" # [--print-message|--no-print-message] - debug = "--debug" # [--debug|--no-debug] - result = runner.invoke( - app, [ - "program", "update", item_hash, path, - "--private-key-file", private_key_file, - print_message, - debug, - ] - ) - - assert result.exit_code == 0 - assert result.stdout @pytest.fixture def item_hash_upload(account_file: Path): @@ -108,6 +81,29 @@ def item_hash_upload(account_file: Path): item_hash = matchings[0] return item_hash + +def test_program_update(account_file: Path, item_hash_upload): + item_hash = item_hash_upload + path = Path(os.path.join( + Path(__file__).parent.parent.parent, "fixtures", "example_program_upload.zip") + ).absolute().as_posix() + private_key = None + private_key_file = str(account_file) + # print_message = "--print-message" # [--print-message|--no-print-message] + # debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, [ + "program", "update", item_hash, path, + "--private-key-file", private_key_file + #print_message, + #debug, + ] + ) + + assert result.exit_code == 0 + assert result.stdout + def test_program_unpersist(account_file: Path, item_hash_upload): item_hash = item_hash_upload private_key = None From 8507bb030721344aea7514aae09f967df6ba8282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 17:09:42 -0300 Subject: [PATCH 088/110] Fixed file upload test --- tests/fixtures/anything.txt | 1 + tests/unit/commands/test_file.py | 51 ++++++++++++++++---------------- 2 files changed, 26 insertions(+), 26 deletions(-) create mode 100644 tests/fixtures/anything.txt diff --git a/tests/fixtures/anything.txt b/tests/fixtures/anything.txt new file mode 100644 index 00000000..be066f98 --- /dev/null +++ b/tests/fixtures/anything.txt @@ -0,0 +1 @@ +it is just an empty file for the tests \ No newline at end of file diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index ede38566..d8f605d7 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -1,4 +1,5 @@ from pathlib import Path +import os from tempfile import NamedTemporaryFile from typer.testing import CliRunner @@ -9,35 +10,33 @@ def test_file_upload(account_file: Path): - path = None - channel = None - private_key = None + path = Path(os.path.join( + Path(__file__).parent.parent.parent, "fixtures", "anything.txt") + ).absolute().as_posix() + # channel = None + # private_key = None private_key_file = str(account_file) - ref = None - debug = "--no-debug" + # ref = None + # debug = "--no-debug" # Test upload a file to aleph network by creating a file and upload it to an aleph node - with NamedTemporaryFile() as temp_file: - temp_file.write(b"Hello World \n") - - path = temp_file.name - - result = runner.invoke( - app, - [ - "file", - "upload", - path, - "--channel", channel, - "--private-key-file", private_key_file, - ref, - debug - ], - ) - - assert result.exit_code == 0 - - assert result.stdout is not None + result = runner.invoke( + app, + [ + "file", + "upload", + path, + # "--channel", channel, + "--private-key-file", private_key_file, + # ref, + # debug + ], + ) + + print(result.stdout) + assert result.exit_code == 0 + + assert result.stdout is not None def test_file_download(): From 399530faa96526c8e2e4e5ea4bb47886b724ab1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 17:12:30 -0300 Subject: [PATCH 089/110] Cleaning up some tests --- tests/unit/commands/test_program.py | 38 +---------------------------- 1 file changed, 1 insertion(+), 37 deletions(-) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 8838088b..7129c3be 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -121,40 +121,4 @@ def test_program_unpersist(account_file: Path, item_hash_upload): assert result.exit_code == 0 assert result.stdout - print(result.stdout) - -def test_program_unpersist_x(account_file: Path): - item_hash = "098f6bcd4621d373cade4e832627b4f6" - - result = runner.invoke( - app, ["program", "unpersist", item_hash, "--private-key-file", str(account_file)] - ) - - assert result.exit_code == 0, result.stdout - - -def test_program_update_x(account_file: Path): - item_hash = "098f6bcd4621d373cade4e832627b4f6" - path = tempfile.TemporaryFile() - - result = runner.invoke( - app, ["program", "update", item_hash, path, "--private-key-file", str(account_file)] - ) - - assert result.exit_code == 0, result.stdout - - -@pytest.mark.skip(reason="Not implemented.") -def test_program_upload_x(account_file: Path): - item_hash = "item_hash" - path = tempfile.TemporaryFile() - entrypoint = "entrypoint" - channel = "channel" - memory = "" - - - result = runner.invoke( - app, ["program", "upload", item_hash, path, "--private-key-file", str(account_file)] - ) - - assert result.exit_code == 0, result.stdout \ No newline at end of file + print(result.stdout) \ No newline at end of file From 8d92a3377d9904d77735f2bf1903f1a5a6d1aa79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 17:13:57 -0300 Subject: [PATCH 090/110] Cleaning up some tests --- tests/unit/commands/test_program.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 7129c3be..2b4e17ac 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -119,6 +119,4 @@ def test_program_unpersist(account_file: Path, item_hash_upload): ) assert result.exit_code == 0 - assert result.stdout - - print(result.stdout) \ No newline at end of file + assert result.stdout \ No newline at end of file From 85658f8da666db791a20239f905ff7c15e63631d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 17:15:56 -0300 Subject: [PATCH 091/110] Cleaning up some tests --- tests/unit/commands/test_file.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index d8f605d7..fad11e82 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -33,10 +33,8 @@ def test_file_upload(account_file: Path): ], ) - print(result.stdout) assert result.exit_code == 0 - - assert result.stdout is not None + assert result.stdout def test_file_download(): From 3bf9a5b897553dc62157612360bcc1ed5117f113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Mon, 11 Mar 2024 17:25:26 -0300 Subject: [PATCH 092/110] Removing some warnings --- tests/unit/commands/test_file.py | 27 ----------------- tests/unit/commands/test_program.py | 46 +++++++++++++++-------------- 2 files changed, 24 insertions(+), 49 deletions(-) diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index fad11e82..dc7ab0e7 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -1,6 +1,5 @@ from pathlib import Path import os -from tempfile import NamedTemporaryFile from typer.testing import CliRunner @@ -91,32 +90,6 @@ def test_file_forget(account_file: Path): assert result.stdout is not None -def test_file_forget(account_file: Path): - item_hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" - reason = "reason" - channel = "" - private_key = None - private_key_file = str(account_file) - debug = "--no-debug" - - result = runner.invoke( - app, - [ - "file", - "forget", - item_hash, - reason, - "--channel", channel, - "--private-key-file", private_key_file, - debug - ], - ) - - assert result.exit_code == 0 - - assert result.stdout is not None - - def test_file_list(account_file: Path): address = None private_key = None diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 2b4e17ac..0de315f1 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -17,22 +17,22 @@ def test_program_upload(account_file: Path): ).absolute().as_posix() entrypoint = "__init__:app" - channel = "channel" - memory = "memory" - vcpus = "vcpus" - timeout_seconds = "tomeout_seconds" - private_key = None - private_key_file = str(account_file) - print_messages = "--print-messages" # [--print-message|--no-print-message] - print_code_message = "--print-code-message" # [--print-code-message|--no-print-code-message] - print_program_message = "--print-program-message" # [--print-program-message|--no-print-program-message] + # channel = "channel" + # memory = "memory" + # vcpus = "vcpus" + # timeout_seconds = "timeout_seconds" + # private_key = None + # private_key_file = str(account_file) + # print_messages = "--print-messages" # [--print-message|--no-print-message] + # print_code_message = "--print-code-message" # [--print-code-message|--no-print-code-message] + # print_program_message = "--print-program-message" # [--print-program-message|--no-print-program-message] runtime = "f873715dc2feec3833074bd4b8745363a0e0093746b987b4c8191268883b2463" - beta = "--beta" # [--beta|--no-beta] - debug = "--debug" # [--debug|--no-debug] - persistent = "--persistent" # [--persistent|--no-persistent] - persistent_volume = "persistent_volume" - ephemeral_volume = "ephemeral_volume" - immutable_volume = "immutable_volume" + # beta = "--beta" # [--beta|--no-beta] + # debug = "--debug" # [--debug|--no-debug] + # persistent = "--persistent" # [--persistent|--no-persistent] + # persistent_volume = "persistent_volume" + # ephemeral_volume = "ephemeral_volume" + # immutable_volume = "immutable_volume" result = runner.invoke( @@ -76,9 +76,9 @@ def item_hash_upload(account_file: Path): ) pattern = r'https://aleph\.sh/vm/(\w+)' - matchings = re.findall(pattern, result.stdout) - if matchings: - item_hash = matchings[0] + match = re.findall(pattern, result.stdout) + if match: + item_hash = match[0] return item_hash @@ -87,7 +87,7 @@ def test_program_update(account_file: Path, item_hash_upload): path = Path(os.path.join( Path(__file__).parent.parent.parent, "fixtures", "example_program_upload.zip") ).absolute().as_posix() - private_key = None + # private_key = None private_key_file = str(account_file) # print_message = "--print-message" # [--print-message|--no-print-message] # debug = "--debug" # [--debug|--no-debug] @@ -96,14 +96,15 @@ def test_program_update(account_file: Path, item_hash_upload): app, [ "program", "update", item_hash, path, "--private-key-file", private_key_file - #print_message, - #debug, + # print_message, + # debug, ] ) assert result.exit_code == 0 assert result.stdout + def test_program_unpersist(account_file: Path, item_hash_upload): item_hash = item_hash_upload private_key = None @@ -119,4 +120,5 @@ def test_program_unpersist(account_file: Path, item_hash_upload): ) assert result.exit_code == 0 - assert result.stdout \ No newline at end of file + assert result.stdout + \ No newline at end of file From 650e04a863bf4947deaceb9a706e66563f968649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Mon, 11 Mar 2024 20:41:12 +0000 Subject: [PATCH 093/110] Possible fix to the aggregate command (needs to be reviewed). --- src/aleph_client/commands/aggregate.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/aleph_client/commands/aggregate.py b/src/aleph_client/commands/aggregate.py index efe149e8..87d91e41 100644 --- a/src/aleph_client/commands/aggregate.py +++ b/src/aleph_client/commands/aggregate.py @@ -45,13 +45,14 @@ async def forget( message_response = await client.get_messages( message_filter=MessageFilter( addresses=[account.get_address()], - message_types=[MessageType.aggregate.value], + message_types=[MessageType.aggregate], content_keys=[key], ) ) - hash_list = [message["item_hash"] for message in message_response.messages] - await client.forget(hashes=hash_list, reason=reason, channel=channel) + hash_list = [message.item_hash for message in message_response.messages] + + typer.echo(await client.forget(hashes=hash_list, reason=reason, channel=channel)) @app.command() From eda641868d48c621cf72c1912c5bb3acc1d83266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Mon, 11 Mar 2024 20:41:53 +0000 Subject: [PATCH 094/110] Fixing test_aggregate.py unit tests. Some aspects needs review. --- tests/unit/commands/test_aggregate.py | 49 +++++++++++++++++++-------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index 7e9ccafc..aa4d1f2f 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -1,3 +1,6 @@ +import asyncio +import json +import pytest import re import textwrap from pathlib import Path @@ -8,7 +11,8 @@ runner = CliRunner() -def test_aggregate_post(account_file: Path): +@pytest.fixture +def fixture_aggregate_post(account_file: Path): key = "key" content = """{"c": 3, "d": "test"}""" address = None @@ -31,6 +35,12 @@ def test_aggregate_post(account_file: Path): ] ) + return result + + +def test_aggregate_post(fixture_aggregate_post): + result = fixture_aggregate_post + assert result.exit_code == 0, result.stdout pattern = textwrap.dedent( @@ -66,8 +76,10 @@ def test_aggregate_post(account_file: Path): assert re.fullmatch(pattern, result.stdout) -# TODO Stopped here!!! -def test_aggregate_get(account_file: Path): +def test_aggregate_get(account_file: Path, fixture_aggregate_post): + # TODO This delay is being necessary for the post to be available. Verify!!! + asyncio.run(asyncio.sleep(1)) + key = "key" address = None # private_key = None @@ -83,29 +95,38 @@ def test_aggregate_get(account_file: Path): ] ) - print("exit_code:") - print(result.exit_code) - print("stdout:") - assert result.exit_code == 0, result.stdout + expected_content = """{"c": 3, "d": "test"}""" + + assert json.dumps(json.loads(expected_content), separators=(',', ':')) == \ + json.dumps(json.loads(result.stdout), separators=(',', ':')) -def test_aggregate_forget(account_file: Path): - key = None - channel = None + +def test_aggregate_forget(account_file: Path, fixture_aggregate_post): + # TODO This delay is being necessary for the post to be available. Verify!!! + asyncio.run(asyncio.sleep(1)) + + hash = json.loads(fixture_aggregate_post.stdout)["item_hash"] + + key = hash + # channel = None # private_key = None private_key_file = str(account_file) - reason = None - debug = "--no-debug" + # reason = None + debug = "--debug" result = runner.invoke( app, [ "aggregate", "forget", key, - "--channel", channel, - "--reason", reason, + # "--channel", channel, + # "--reason", reason, "--private-key-file", private_key_file, debug ] ) assert result.exit_code == 0, result.stdout + + pattern = r".*forgotten_by=.*, Date: Mon, 11 Mar 2024 17:55:20 -0300 Subject: [PATCH 095/110] test message post is passing --- tests/unit/commands/test_message.py | 36 ++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index 560d3b5e..55a865d5 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -137,17 +137,16 @@ def test_message_get(): def test_message_post(account_file): - test_file_path = Path(os.path.join( + path = Path(os.path.join( Path(__file__).parent.parent.parent, "fixtures", "post.json") ).absolute().as_posix() - path = str(test_file_path), - message_type = "test" - ref = None - channel = None + message_type = "POST" + # ref = None + # channel = None # private_key = None - private_key_file = get_account(account_file) - debug = "--no-debug" + # private_key_file = get_account(account_file) + # debug = "--no-debug" result = runner.invoke( app, @@ -156,32 +155,33 @@ def test_message_post(account_file): "post", "--path", path, "--type", message_type, - "--ref", ref, - "--channel", channel, - "--private-key-file", private_key_file, - debug + # "--ref", ref, + # "--channel", channel, + # "--private-key-file", private_key_file, + # debug ], ) assert result.exit_code == 0 + assert result.stdout - assert "item_hash" in result.stdout - -def test_message_sign(account_file): +def test_message_sign(account_file): # TODO !!! # private_key = None private_key_file = get_account(account_file) - message = get_test_message(private_key_file) - debug = "--no-debug" + # message = get_test_message(private_key_file) + message = "A message to be sign" + # debug = "--no-debug" result = runner.invoke( app, [ "message", "sign", - "--message", json.dumps(message), + # "--message", json.dumps(message), + "--message", message, "--private-key-file", private_key_file, - debug + # debug ], ) From a1fb18eb6ee19cf395cc1cb8fa122e7dc224af4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Mon, 11 Mar 2024 20:58:13 +0000 Subject: [PATCH 096/110] Improving and fixing test_node.py unit tests. --- tests/unit/commands/test_node.py | 102 +++++++++++++++++++------------ 1 file changed, 63 insertions(+), 39 deletions(-) diff --git a/tests/unit/commands/test_node.py b/tests/unit/commands/test_node.py index 334be36d..8ed4869b 100644 --- a/tests/unit/commands/test_node.py +++ b/tests/unit/commands/test_node.py @@ -1,3 +1,5 @@ +import textwrap + import re from typer.testing import CliRunner @@ -8,45 +10,67 @@ def test_node_compute(): - json = "--no-json" - active = "--no-active" - address = None - debug = "--no-debug" - - result = runner.invoke( - app, [ - "node", - "compute", - json, - active, - "--address", address, - debug - ] - ) - - assert result.exit_code == 0 - - pattern = r".*Compute Node Information.*" - assert re.match(pattern, result.stdout) - # pattern = r".*?([0-9]+\.[0-9]+%|100\.00%|0\.00%).*?([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}).*?([0-9]+\.[0-9]+%|100\.00%).*?([a-z]+).*?" - # assert len(re.findall(pattern, result.stdout, re.MULTILINE)) > 0 + json = "--no-json" + active = "--no-active" + address = None + debug = "--no-debug" + + result = runner.invoke( + app, [ + "node", + "compute", + json, + active, + "--address", address, + debug + ] + ) + + assert result.exit_code == 0 + + pattern = textwrap.dedent( + '''\ + .*Compute Node Information.* + .* + .* Score ┃ Name ┃ Creation Time ┃ Decentralization ┃ Status.* + .* + │ [0-9]+\.[0-9]+% │ .* │ [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} │ .*[0-9]+\.[0-9]+% │ .* │ + [\s\S]* + ''' + ) + + assert re.fullmatch(pattern, result.stdout) +# TODO Stopped here!!! def test_node_core(): - json = "--no-json" - active = "--no-active" - address = None - debug = "--no-debug" - - result = runner.invoke( - app, [ - "node", - "core", - json, - active, - "--address", address, - debug - ] - ) - - assert result.exit_code == 0 + json = "--no-json" + active = "--no-active" + address = None + debug = "--no-debug" + + result = runner.invoke( + app, [ + "node", + "core", + json, + active, + "--address", address, + debug + ] + ) + + assert result.exit_code == 0 + + pattern = textwrap.dedent( + '''\ + .*Core Channel Node Information.* + .* + .* Score ┃ Name ┃ Staked ┃ Linked ┃ Creation Time ┃ Status + .* + │ [0-9]+\.[0-9]+% │ .* │ .* │ [0-9]+ │ [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} │ .* │ + [\s\S]* + ''' + ) + + assert re.fullmatch(pattern, result.stdout) From dcc69c857cc9c6fa125229a2adc5ac2f31bd998f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 20 Mar 2024 16:24:54 +0000 Subject: [PATCH 097/110] Adding example ssh keys. --- tests/fixtures/example_ssh_key | 7 +++++++ tests/fixtures/example_ssh_key.pub | 1 + 2 files changed, 8 insertions(+) create mode 100644 tests/fixtures/example_ssh_key create mode 100644 tests/fixtures/example_ssh_key.pub diff --git a/tests/fixtures/example_ssh_key b/tests/fixtures/example_ssh_key new file mode 100644 index 00000000..aa33e1bc --- /dev/null +++ b/tests/fixtures/example_ssh_key @@ -0,0 +1,7 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW +QyNTUxOQAAACCVA+x8tYGrdR6pOyWKWHAefzT2lfhn4kGfenF97hiBewAAAJiRRWNykUVj +cgAAAAtzc2gtZWQyNTUxOQAAACCVA+x8tYGrdR6pOyWKWHAefzT2lfhn4kGfenF97hiBew +AAAEDBOi9c3d1WmYdQfL25G9BDm7oXElKz1egMs6ySvRJLr5UD7Hy1gat1Hqk7JYpYcB5/ +NPaV+GfiQZ96cX3uGIF7AAAAEWFkbWluQGV4YW1wbGUuY29tAQIDBA== +-----END OPENSSH PRIVATE KEY----- diff --git a/tests/fixtures/example_ssh_key.pub b/tests/fixtures/example_ssh_key.pub new file mode 100644 index 00000000..1f924323 --- /dev/null +++ b/tests/fixtures/example_ssh_key.pub @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJUD7Hy1gat1Hqk7JYpYcB5/NPaV+GfiQZ96cX3uGIF7 admin@example.com From 4c1236b926f73f4fbd20578e1a363c9a3d709ff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 20 Mar 2024 16:25:18 +0000 Subject: [PATCH 098/110] Adding new fixture. --- tests/unit/conftest.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 532a3ed9..b23428f0 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -6,12 +6,14 @@ Read more about conftest.py under: https://pytest.org/latest/plugins.html """ +import os + from pathlib import Path from tempfile import NamedTemporaryFile from typing import Generator import pytest -from aleph.sdk.chains.common import generate_key +from aleph.sdk.chains.common import generate_key, get_public_key @pytest.fixture @@ -26,3 +28,19 @@ def account_file(empty_account_file: Path) -> Path: empty_account_file.write_bytes(private_key) return empty_account_file + + +@pytest.fixture +def ssh_keys_files(empty_account_file: Path) -> dict[str, Path]: + private_key_file = Path(os.path.join( + Path(__file__).parent.parent, "fixtures", "example_ssh_key") + ).absolute() + + public_key_file = Path(os.path.join( + Path(__file__).parent.parent, "fixtures", "example_ssh_key.pub") + ).absolute() + + return { + "private_key": private_key_file, + "public_key": public_key_file + } From 43e5bee6748be2df360c5880e08f0e5ff2c84e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20Ara=C3=BAjo=20Silva?= Date: Wed, 20 Mar 2024 16:26:06 +0000 Subject: [PATCH 099/110] Working with the instance command unit tests. --- tests/unit/commands/test_instance.py | 146 ++++++++++++++------------- 1 file changed, 77 insertions(+), 69 deletions(-) diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index 68bec088..723a4022 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -7,23 +7,24 @@ runner = CliRunner() item_hash = None -@pytest.mark.skip(reason="Not implemented.") -def test_instance_create(account_file: Path): + +# TODO Stopped here!!! +def test_instance_create(account_file: Path, ssh_keys_files: dict[str, Path]): channel = "channel" - memory = "memory" - vcpus = "vcpus" - timeout_seconds = "timeout_seconds" - private_key = None + memory = 256 + vcpus = 1 + timeout_seconds = 30.0 + # private_key = None private_key_file = str(account_file) - ssh_pubkey_file = "ssh_pubkey_file" - print_messages = "--print-messages" #[--print-messages|--no-print-messages] - rootfs = "rootfs" - rootfs_name = "rootfs_name" - rootfs_size = "rootfs_size" - debug = "--debug" # [--debug|--no-debug] - persistent_volume = "persistent_volume" - ephemeral_volume = "ephemeral_volume" - immutable_volume = "immutable_volume" + ssh_pubkey_file = str(ssh_keys_files["public_key"]) + print_messages = "--no-print-messages" + rootfs = "Ubuntu 22" + rootfs_name = "main-rootfs" + rootfs_size = 2000 + debug = "--no-debug" + persistent_volume = None + ephemeral_volume = None + immutable_volume = None result = runner.invoke( app, [ @@ -41,14 +42,22 @@ def test_instance_create(account_file: Path): debug, "--persistent-volume", persistent_volume, "--ephemeral-volume", ephemeral_volume, - "--imutable-volume", immutable_volume + "--immutable-volume", immutable_volume ] ) + print("result.exit_code:") + print(result.exit_code) + print("result.stdout:") + print(result.stdout) + # from tests.unit.test_utils import dump + # print("dump:") + # print(dump(result)) + assert result.exit_code == 0 - assert result.stdout + # assert result.stdout + -@pytest.mark.skip(reason="Not implemented.") def test_instance_delete(account_file: Path): item_hash = "item_hash" reason = "reason" @@ -71,7 +80,7 @@ def test_instance_delete(account_file: Path): assert result.exit_code == 0 assert result.stdout -@pytest.mark.skip(reason="Not implemented.") + def test_instance_list(account_file: Path): address = "address" private_key = None @@ -92,54 +101,53 @@ def test_instance_list(account_file: Path): assert result.exit_code == 0 assert result.stdout -@pytest.mark.skip(reason="Not implemented.") -def test_instance_create(): - global item_hash - - rootfs = "Ubuntu 22" - rootfs_name = "Ubuntu 22" - vcpus = 1 - memory = 256 - rootfs_size = 2000 - - result = runner.invoke( - app, [ - "instance", "create", - "--rootfs", rootfs, - "--rootfs-name", rootfs_name, - "--vcpus", vcpus, - "--memory", memory, - "--rootfs-size", rootfs_size - ] - ) - - assert result.exit_code == 0 - assert result.stdout - - item_hash_regex = r"\b0x[a-fA-F0-9]{40,42}\b" - item_hashes = re.findall(item_hash_regex, result.stdout) - - item_hash = item_hashes[0] if item_hashes else None - - -@pytest.mark.skip(reason="Not implemented.") -def test_instance_delete(): - result = runner.invoke( - app, [ - "instance", "create", - "--item_hash", item_hash, - ] - ) - - assert result.exit_code == 0 - assert result.stdout - - -def test_instance_list(): - result = runner.invoke( - app, ["instance", "list"] - ) - assert result.exit_code == 0 - assert result.stdout - assert "Item Hash Vcpus Memory Disk size IPv6 address" in result.stdout +# def test_instance_create(): +# global item_hash +# +# rootfs = "Ubuntu 22" +# rootfs_name = "Ubuntu 22" +# vcpus = 1 +# memory = 256 +# rootfs_size = 2000 +# +# result = runner.invoke( +# app, [ +# "instance", "create", +# "--rootfs", rootfs, +# "--rootfs-name", rootfs_name, +# "--vcpus", vcpus, +# "--memory", memory, +# "--rootfs-size", rootfs_size +# ] +# ) +# +# assert result.exit_code == 0 +# assert result.stdout +# +# item_hash_regex = r"\b0x[a-fA-F0-9]{40,42}\b" +# item_hashes = re.findall(item_hash_regex, result.stdout) +# +# item_hash = item_hashes[0] if item_hashes else None +# +# +# def test_instance_delete(): +# result = runner.invoke( +# app, [ +# "instance", "create", +# "--item_hash", item_hash, +# ] +# ) +# +# assert result.exit_code == 0 +# assert result.stdout +# +# +# def test_instance_list(): +# result = runner.invoke( +# app, ["instance", "list"] +# ) +# +# assert result.exit_code == 0 +# assert result.stdout +# assert "Item Hash Vcpus Memory Disk size IPv6 address" in result.stdout From a42783598fa7cf41c155024e1a79c9eb4751c533 Mon Sep 17 00:00:00 2001 From: mhh Date: Mon, 29 Apr 2024 09:48:12 +0200 Subject: [PATCH 100/110] WIP instance tests --- tests/unit/commands/test_instance.py | 12 +----------- tests/unit/test_utils.py | 16 ---------------- 2 files changed, 1 insertion(+), 27 deletions(-) diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index 723a4022..0fbd3c92 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -1,25 +1,21 @@ from typer.testing import CliRunner from pathlib import Path from aleph_client.__main__ import app -import re -import pytest runner = CliRunner() item_hash = None -# TODO Stopped here!!! +# TODO: Add non-interactive flag def test_instance_create(account_file: Path, ssh_keys_files: dict[str, Path]): channel = "channel" memory = 256 vcpus = 1 timeout_seconds = 30.0 - # private_key = None private_key_file = str(account_file) ssh_pubkey_file = str(ssh_keys_files["public_key"]) print_messages = "--no-print-messages" rootfs = "Ubuntu 22" - rootfs_name = "main-rootfs" rootfs_size = 2000 debug = "--no-debug" persistent_volume = None @@ -37,7 +33,6 @@ def test_instance_create(account_file: Path, ssh_keys_files: dict[str, Path]): "--ssh-pubkey-file", ssh_pubkey_file, print_messages, "--rootfs", rootfs, - "--rootfs-name", rootfs_name, "--rootfs-size", rootfs_size, debug, "--persistent-volume", persistent_volume, @@ -50,9 +45,6 @@ def test_instance_create(account_file: Path, ssh_keys_files: dict[str, Path]): print(result.exit_code) print("result.stdout:") print(result.stdout) - # from tests.unit.test_utils import dump - # print("dump:") - # print(dump(result)) assert result.exit_code == 0 # assert result.stdout @@ -61,7 +53,6 @@ def test_instance_create(account_file: Path, ssh_keys_files: dict[str, Path]): def test_instance_delete(account_file: Path): item_hash = "item_hash" reason = "reason" - private_key = None private_key_file = str(account_file) print_messages = "--print-messages" #[--print-messages|--no-print-messages] debug = "--debug" # [--debug|--no-debug] @@ -83,7 +74,6 @@ def test_instance_delete(account_file: Path): def test_instance_list(account_file: Path): address = "address" - private_key = None private_key_file = str(account_file) json = "--json" # [--json|--no-json] debug = "--debug" # [--debug|--no-debug] diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index d713be9b..7813919c 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,6 +1,3 @@ -import jsonpickle -from typing import Any, Dict - from aleph_message.models import ( AggregateMessage, ForgetMessage, @@ -21,16 +18,3 @@ def test_get_message_type_value(): assert get_message_type_value(ProgramMessage) == MessageType.program assert get_message_type_value(InstanceMessage) == MessageType.instance assert get_message_type_value(ForgetMessage) == MessageType.forget - - -def dump(target: Any): - try: - if isinstance(target, str): - return target - - if isinstance(target, Dict): - return str(target) - - return jsonpickle.encode(target, unpicklable=True, indent=2) - except (Exception,): - return target From 5c5758480a33ff5503ead1f8d98f7b797ff16136 Mon Sep 17 00:00:00 2001 From: mhh Date: Mon, 29 Apr 2024 15:58:22 +0200 Subject: [PATCH 101/110] Fix tests, add specific exit codes and exit message function --- src/aleph_client/__init__.py | 4 +- src/aleph_client/__main__.py | 14 +- src/aleph_client/commands/about.py | 2 +- src/aleph_client/commands/account.py | 28 +- src/aleph_client/commands/aggregate.py | 15 +- src/aleph_client/commands/domain.py | 29 ++- src/aleph_client/commands/files.py | 5 +- src/aleph_client/commands/instance.py | 79 +++--- src/aleph_client/commands/message.py | 23 +- src/aleph_client/commands/node.py | 9 +- src/aleph_client/commands/program.py | 52 ++-- src/aleph_client/commands/utils.py | 37 ++- src/aleph_client/exit_codes.py | 39 +++ src/aleph_client/utils.py | 16 +- tests/unit/commands/test_about.py | 7 +- tests/unit/commands/test_account.py | 53 ++-- tests/unit/commands/test_aggregate.py | 105 ++++---- tests/unit/commands/test_domain.py | 102 +++++--- tests/unit/commands/test_file.py | 81 +++--- tests/unit/commands/test_help.py | 341 ++++--------------------- tests/unit/commands/test_instance.py | 217 +++++++--------- tests/unit/commands/test_message.py | 131 ++++------ tests/unit/commands/test_node.py | 69 +---- tests/unit/commands/test_program.py | 105 +++++--- tests/unit/conftest.py | 16 +- tests/unit/test_utils.py | 12 +- 26 files changed, 669 insertions(+), 922 deletions(-) create mode 100644 src/aleph_client/exit_codes.py diff --git a/src/aleph_client/__init__.py b/src/aleph_client/__init__.py index 3b86c105..bca833ab 100644 --- a/src/aleph_client/__init__.py +++ b/src/aleph_client/__init__.py @@ -15,4 +15,6 @@ def __getattr__(name): if name in moved_types: - raise ImportError(f"The 'aleph_client.{name}' type is deprecated and has been removed from aleph_client. Please use `aleph.sdk.{name}` instead.") + raise ImportError( + f"The 'aleph_client.{name}' type is deprecated and has been removed from aleph_client. Please use `aleph.sdk.{name}` instead." + ) diff --git a/src/aleph_client/__main__.py b/src/aleph_client/__main__.py index e399f3c3..d4a48727 100644 --- a/src/aleph_client/__main__.py +++ b/src/aleph_client/__main__.py @@ -2,20 +2,10 @@ Aleph Client command-line interface. """ +from aleph_client.commands import (about, account, aggregate, domain, files, + instance, message, node, program) from aleph_client.utils import AsyncTyper -from aleph_client.commands import ( - about, - account, - aggregate, - domain, - files, - instance, - message, - node, - program, -) - app = AsyncTyper(no_args_is_help=True) app.add_typer(account.app, name="account", help="Manage account") diff --git a/src/aleph_client/commands/about.py b/src/aleph_client/commands/about.py index 72f959d5..c658e57e 100644 --- a/src/aleph_client/commands/about.py +++ b/src/aleph_client/commands/about.py @@ -14,7 +14,7 @@ def get_version(value: bool): __version__ = get_distribution(dist_name).version finally: typer.echo(f"Aleph CLI Version: {__version__}") - raise typer.Exit(1) + raise typer.Exit() @app.command() diff --git a/src/aleph_client/commands/account.py b/src/aleph_client/commands/account.py index ddb6c221..17d2084e 100644 --- a/src/aleph_client/commands/account.py +++ b/src/aleph_client/commands/account.py @@ -2,6 +2,7 @@ import base64 import json import logging +import os import sys from pathlib import Path from typing import Optional @@ -14,10 +15,10 @@ from aleph.sdk.conf import settings as sdk_settings from aleph.sdk.types import AccountFromPrivateKey from aleph.sdk.utils import extended_json_encoder -from typer.colors import RED from aleph_client.commands import help_strings from aleph_client.commands.utils import setup_logging +from aleph_client.exit_codes import exit_with_error_message from aleph_client.utils import AsyncTyper logger = logging.getLogger(__name__) @@ -45,9 +46,9 @@ def create( ) if private_key_file.exists() and not replace: - typer.secho(f"Error: key already exists: '{private_key_file}'", fg=RED) - - raise typer.Exit(1) + exit_with_error_message( + os.EX_CANTCREAT, f"key already exists: '{private_key_file}'" + ) private_key_bytes: bytes if private_key is not None: @@ -58,12 +59,11 @@ def create( private_key_bytes = generate_key() if not private_key_bytes: - typer.secho("An unexpected error occurred!", fg=RED) - raise typer.Exit(2) + exit_with_error_message(os.EX_DATAERR, "Failed to load private key") private_key_file.parent.mkdir(parents=True, exist_ok=True) private_key_file.write_bytes(private_key_bytes) - typer.secho(f"Private key stored in {private_key_file}", fg=RED) + typer.echo(f"Private key stored in {private_key_file}") @app.command() @@ -82,8 +82,7 @@ def address( if private_key is not None: private_key_file = None elif private_key_file and not private_key_file.exists(): - typer.secho("No private key available", fg=RED) - raise typer.Exit(code=1) + exit_with_error_message(os.EX_NOINPUT, "No private key provided") account: AccountFromPrivateKey = _load_account(private_key, private_key_file) typer.echo(account.get_address()) @@ -105,15 +104,16 @@ def export_private_key( if private_key is not None: private_key_file = None elif private_key_file and not private_key_file.exists(): - typer.secho("No private key available", fg=RED) - raise typer.Exit(code=1) + exit_with_error_message(os.EX_NOINPUT, "No private key provided") account: AccountFromPrivateKey = _load_account(private_key, private_key_file) if hasattr(account, "private_key"): private_key_hex: str = base64.b16encode(account.private_key).decode().lower() typer.echo(f"0x{private_key_hex}") else: - typer.secho(f"Private key cannot be read for {account}", fg=RED) + exit_with_error_message( + os.EX_DATAERR, f"Private key cannot be read for {account}" + ) @app.command() @@ -170,7 +170,9 @@ async def balance( response = await session.get(uri) if response.status == 200: balance_data = await response.json() - formatted_balance_data = json.dumps(balance_data, indent=4, default=extended_json_encoder) + formatted_balance_data = json.dumps( + balance_data, indent=4, default=extended_json_encoder + ) typer.echo(formatted_balance_data) else: typer.echo( diff --git a/src/aleph_client/commands/aggregate.py b/src/aleph_client/commands/aggregate.py index 6813e50d..3a119e11 100644 --- a/src/aleph_client/commands/aggregate.py +++ b/src/aleph_client/commands/aggregate.py @@ -1,4 +1,5 @@ import json +import os from pathlib import Path from typing import Optional @@ -13,6 +14,7 @@ from aleph_client.commands import help_strings from aleph_client.commands.utils import setup_logging +from aleph_client.exit_codes import exit_with_error_message from aleph_client.utils import AsyncTyper app = AsyncTyper(no_args_is_help=True) @@ -52,7 +54,9 @@ async def forget( hash_list = [message.item_hash for message in message_response.messages] - typer.echo(await client.forget(hashes=hash_list, reason=reason, channel=channel)) + typer.echo( + await client.forget(hashes=hash_list, reason=reason, channel=channel) + ) @app.command() @@ -82,8 +86,9 @@ async def post( try: content_dict = json.loads(content) except json.JSONDecodeError: - typer.echo("Invalid JSON for content. Please provide valid JSON.") - raise typer.Exit(1) + exit_with_error_message( + os.EX_DATAERR, "Invalid JSON for content. Please provide valid JSON." + ) async with AuthenticatedAlephHttpClient( account=account, api_server=sdk_settings.API_HOST @@ -96,7 +101,9 @@ async def post( inline=inline, address=address, ) - log_message = json.dumps(message.dict(), indent=4, default=extended_json_encoder) + log_message = json.dumps( + message.dict(), indent=4, default=extended_json_encoder + ) typer.echo(log_message) diff --git a/src/aleph_client/commands/domain.py b/src/aleph_client/commands/domain.py index 560f5458..f9031b25 100644 --- a/src/aleph_client/commands/domain.py +++ b/src/aleph_client/commands/domain.py @@ -6,13 +6,8 @@ from aleph.sdk.account import _load_account from aleph.sdk.client import AlephHttpClient, AuthenticatedAlephHttpClient from aleph.sdk.conf import settings as sdk_settings -from aleph.sdk.domain import ( - DomainValidator, - Hostname, - TargetType, - get_target_type, - hostname_from_url, -) +from aleph.sdk.domain import (DomainValidator, Hostname, TargetType, + get_target_type, hostname_from_url) from aleph.sdk.exceptions import DomainConfigurationError from aleph.sdk.query.filters import MessageFilter from aleph.sdk.types import AccountFromPrivateKey @@ -74,7 +69,9 @@ async def attach_resource( item_hash = Prompt.ask("Enter Hash reference of the resource to attach") while not spa: - is_spa: str = Prompt.ask("It is an SPA application?", choices=["y", "n"], default="n") + is_spa: str = Prompt.ask( + "It is an SPA application?", choices=["y", "n"], default="n" + ) spa = True if is_spa == "y" else False table = Table(title=f"Attach resource to: {fqdn}") @@ -281,7 +278,11 @@ async def attach( account: AccountFromPrivateKey = _load_account(private_key, private_key_file) await attach_resource( - account, Hostname(fqdn), item_hash, interactive=False if (not ask) else None, spa=True if spa else None + account, + Hostname(fqdn), + item_hash, + interactive=False if (not ask) else None, + spa=True if spa else None, ) raise typer.Exit() @@ -339,14 +340,20 @@ async def info( is_spa = "" if resource_type == TargetType.IPFS: final_resource = "" - is_spa = "True" if "spa" in domain_info["info"] and domain_info["info"]["spa"] == "1" else "False" + is_spa = ( + "True" + if "spa" in domain_info["info"] and domain_info["info"]["spa"] == "1" + else "False" + ) elif resource_type == TargetType.PROGRAM: final_resource = domain_info["info"]["message_id"] if resource_type == TargetType.INSTANCE: ips = await domain_validator.get_ipv6_addresses(Hostname(fqdn)) final_resource = ",".join([str(ip) for ip in ips]) - table.add_row(resource_type, domain_info["info"]["message_id"], final_resource, is_spa) + table.add_row( + resource_type, domain_info["info"]["message_id"], final_resource, is_spa + ) console.print(table) raise typer.Exit() diff --git a/src/aleph_client/commands/files.py b/src/aleph_client/commands/files.py index a698989e..4355fc1b 100644 --- a/src/aleph_client/commands/files.py +++ b/src/aleph_client/commands/files.py @@ -1,5 +1,6 @@ import json as json_lib import logging +import os from datetime import datetime from pathlib import Path from typing import Optional @@ -19,6 +20,7 @@ from aleph_client.commands import help_strings from aleph_client.commands.utils import setup_logging +from aleph_client.exit_codes import exit_with_error_message from aleph_client.utils import AsyncTyper logger = logging.getLogger(__name__) @@ -82,8 +84,7 @@ async def upload( account=account, api_server=sdk_settings.API_HOST ) as client: if not path.is_file(): - typer.echo(f"Error: File not found: '{path}'") - raise typer.Exit(code=1) + exit_with_error_message(os.EX_NOINPUT, f"No such file or directory: {path}") with open(path, "rb") as fd: logger.debug("Reading file") diff --git a/src/aleph_client/commands/instance.py b/src/aleph_client/commands/instance.py index 84fdb755..35595bd8 100644 --- a/src/aleph_client/commands/instance.py +++ b/src/aleph_client/commands/instance.py @@ -1,5 +1,6 @@ import asyncio import logging +import os from pathlib import Path from typing import List, Optional, Tuple, Union @@ -8,14 +9,12 @@ from aleph.sdk import AlephHttpClient, AuthenticatedAlephHttpClient from aleph.sdk.account import _load_account from aleph.sdk.conf import settings as sdk_settings -from aleph.sdk.exceptions import ( - ForgottenMessageError, - InsufficientFundsError, - MessageNotFoundError, -) +from aleph.sdk.exceptions import (ForgottenMessageError, + InsufficientFundsError, MessageNotFoundError) from aleph.sdk.query.filters import MessageFilter from aleph.sdk.types import AccountFromPrivateKey, StorageEnum -from aleph_message.models import InstanceMessage, ItemHash, MessageType, StoreMessage +from aleph_message.models import (InstanceMessage, ItemHash, MessageType, + StoreMessage) from aleph_message.models.execution.environment import HypervisorType from rich import box from rich.console import Console @@ -23,13 +22,12 @@ from rich.table import Table from aleph_client.commands import help_strings -from aleph_client.commands.utils import ( - get_or_prompt_volumes, - setup_logging, - validated_int_prompt, - validated_prompt, -) +from aleph_client.commands.utils import (get_or_prompt_volumes, + is_environment_interactive, + setup_logging, validated_int_prompt, + validated_prompt) from aleph_client.conf import settings +from aleph_client.exit_codes import EX_NOFUNDS, exit_with_error_message from aleph_client.utils import AsyncTyper logger = logging.getLogger(__name__) @@ -127,15 +125,16 @@ def validate_ssh_pubkey_file(file: Union[str, Path]) -> Path: HypervisorType.qemu: "qemu", } - rootfs = Prompt.ask( - f"Do you want to use a custom rootfs or one of the following prebuilt ones?", - default=rootfs, - choices=[*os_map.values(), "custom"], - ) + if is_environment_interactive(): + rootfs = Prompt.ask( + "Do you want to use a custom rootfs or one of the following prebuilt ones?", + default=rootfs, + choices=[*os_map.values(), "custom"], + ) if rootfs == "custom": rootfs = validated_prompt( - f"Enter the item hash of the rootfs to use for your instance", + "Enter the item hash of the rootfs to use for your instance", lambda x: len(x) == 64, ) else: @@ -146,28 +145,33 @@ def validate_ssh_pubkey_file(file: Union[str, Path]) -> Path: item_hash=rootfs, message_type=StoreMessage ) if not rootfs_message: - typer.echo("Given rootfs volume does not exist on aleph.im") - raise typer.Exit(code=1) + exit_with_error_message( + os.EX_NOINPUT, f"Given rootfs {rootfs} does not exist on aleph.im" + ) if rootfs_size is None and rootfs_message.content.size: rootfs_size = rootfs_message.content.size vcpus = validated_int_prompt( - f"Number of virtual cpus to allocate", vcpus, min_value=1, max_value=4 + "Number of virtual cpus to allocate", vcpus, min_value=1, max_value=4 ) memory = validated_int_prompt( - f"Maximum memory allocation on vm in MiB", memory, min_value=2000, max_value=8000 + "Maximum memory allocation on vm in MiB", + memory, + min_value=2000, + max_value=8000, ) rootfs_size = validated_int_prompt( - f"Disk size in MiB", rootfs_size, min_value=20000, max_value=100000 + "Disk size in MiB", rootfs_size, min_value=20000, max_value=100000 ) - hypervisor = Prompt.ask( - f"Which hypervisor you want to use?", - default=hypervisor, - choices=[*hv_map.values()], - ) + if is_environment_interactive(): + hypervisor = Prompt.ask( + "Which hypervisor you want to use?", + default=hypervisor, + choices=[*hv_map.values()], + ) volumes = get_or_prompt_volumes( persistent_volume=persistent_volume, @@ -193,11 +197,11 @@ def validate_ssh_pubkey_file(file: Union[str, Path]) -> Path: hypervisor=hypervisor, ) except InsufficientFundsError as e: - typer.echo( + exit_with_error_message( + EX_NOFUNDS, f"Instance creation failed due to insufficient funds.\n" - f"{account.get_address()} on {account.CHAIN} has {e.available_funds} ALEPH but needs {e.required_funds} ALEPH." + f"{account.get_address()} on {account.CHAIN} has {e.available_funds} ALEPH but needs {e.required_funds} ALEPH.", ) - raise typer.Exit(code=1) if print_messages: typer.echo(f"{message.json(indent=4)}") @@ -238,14 +242,13 @@ async def delete( item_hash=item_hash, message_type=InstanceMessage ) except MessageNotFoundError: - typer.echo("Instance does not exist") - raise typer.Exit(code=1) + exit_with_error_message(os.EX_NOINPUT, "Instance not found") except ForgottenMessageError: - typer.echo("Instance already forgotten") - raise typer.Exit(code=1) + exit_with_error_message(os.EX_NOINPUT, "Instance already forgotten") if existing_message.sender != account.get_address(): - typer.echo("You are not the owner of this instance") - raise typer.Exit(code=1) + exit_with_error_message( + os.EX_NOPERM, "You are not the owner of this instance" + ) message, status = await client.forget(hashes=[item_hash], reason=reason) if print_message: @@ -326,7 +329,7 @@ async def list( ) if not resp: typer.echo("No instances found") - raise typer.Exit(code=1) + raise typer.Exit() if json: typer.echo(resp.json(indent=4)) else: diff --git a/src/aleph_client/commands/message.py b/src/aleph_client/commands/message.py index f4b52898..a8187a42 100644 --- a/src/aleph_client/commands/message.py +++ b/src/aleph_client/commands/message.py @@ -16,16 +16,15 @@ from aleph.sdk.query.responses import MessagesResponse from aleph.sdk.types import AccountFromPrivateKey, StorageEnum from aleph.sdk.utils import extended_json_encoder +from aleph_message.models import (AlephMessage, ItemHash, MessageType, + ProgramMessage) + from aleph_client.commands import help_strings -from aleph_client.commands.utils import ( - colorful_json, - colorful_message_json, - input_multiline, - setup_logging, - str_to_datetime, -) +from aleph_client.commands.utils import (colorful_json, colorful_message_json, + input_multiline, setup_logging, + str_to_datetime) +from aleph_client.exit_codes import exit_with_error_message from aleph_client.utils import AsyncTyper -from aleph_message.models import AlephMessage, ItemHash, MessageType, ProgramMessage app = AsyncTyper(no_args_is_help=True) @@ -124,8 +123,7 @@ async def post( if path: if not path.is_file(): - typer.echo(f"Error: File not found: '{path}'") - raise typer.Exit(code=1) + exit_with_error_message(os.EX_NOINPUT, f"File not found: '{path}'") file_size = os.path.getsize(path) storage_engine = ( @@ -145,8 +143,9 @@ async def post( try: content = json.loads(content_raw) except json.decoder.JSONDecodeError: - typer.echo("Not valid JSON") - raise typer.Exit(code=2) + exit_with_error_message( + os.EX_DATAERR, "Invalid JSON for content. Please provide valid JSON." + ) async with AuthenticatedAlephHttpClient( account=account, api_server=sdk_settings.API_HOST diff --git a/src/aleph_client/commands/node.py b/src/aleph_client/commands/node.py index 817b7806..a3f6b1fd 100644 --- a/src/aleph_client/commands/node.py +++ b/src/aleph_client/commands/node.py @@ -1,9 +1,10 @@ import datetime import json as json_lib import logging +import os import re import unicodedata -from typing import Dict, List, Optional +from typing import Optional import aiohttp import typer @@ -13,6 +14,7 @@ from rich.table import Table from aleph_client.commands.utils import setup_logging +from aleph_client.exit_codes import exit_with_error_message from aleph_client.utils import AsyncTyper logger = logging.getLogger(__name__) @@ -35,8 +37,9 @@ async def _fetch_nodes() -> NodeInfo: async with aiohttp.ClientSession() as session: async with session.get(node_link) as resp: if resp.status != 200: - logger.error("Unable to fetch node information") - raise typer.Exit(1) + exit_with_error_message( + os.EX_TEMPFAIL, "Unable to fetch node information" + ) data = await resp.json() return NodeInfo(**data) diff --git a/src/aleph_client/commands/program.py b/src/aleph_client/commands/program.py index 0e1f06c7..32b00daf 100644 --- a/src/aleph_client/commands/program.py +++ b/src/aleph_client/commands/program.py @@ -1,5 +1,6 @@ import json import logging +import os from base64 import b16decode, b32encode from pathlib import Path from typing import Dict, List, Optional @@ -11,23 +12,17 @@ from aleph.sdk.conf import settings as sdk_settings from aleph.sdk.query.filters import MessageFilter from aleph.sdk.types import AccountFromPrivateKey, StorageEnum +from aleph_message.models import (ItemHash, MessagesResponse, ProgramContent, + ProgramMessage, StoreMessage) +from aleph_message.status import MessageStatus + from aleph_client.commands import help_strings -from aleph_client.commands.utils import ( - get_or_prompt_volumes, - input_multiline, - setup_logging, - yes_no_input, -) +from aleph_client.commands.utils import (get_or_prompt_volumes, + input_multiline, setup_logging, + yes_no_input) from aleph_client.conf import settings +from aleph_client.exit_codes import exit_with_error_message from aleph_client.utils import AsyncTyper, create_archive -from aleph_message.models import ( - ItemHash, - MessagesResponse, - ProgramContent, - ProgramMessage, - StoreMessage, -) -from aleph_message.status import MessageStatus logger = logging.getLogger(__name__) app = AsyncTyper(no_args_is_help=True) @@ -87,11 +82,9 @@ async def upload( try: path_object, encoding = create_archive(path) except BadZipFile: - typer.echo("Invalid zip archive") - raise typer.Exit(3) + exit_with_error_message(os.EX_DATAERR, f"Invalid zip archive: {path}") except FileNotFoundError: - typer.echo("No such file or directory") - raise typer.Exit(4) + exit_with_error_message(os.EX_NOINPUT, f"No such file or directory: {path}") account: AccountFromPrivateKey = _load_account(private_key, private_key_file) @@ -113,8 +106,9 @@ async def upload( try: subscriptions = json.loads(content_raw) except json.decoder.JSONDecodeError: - typer.echo("Not valid JSON") - raise typer.Exit(code=2) + exit_with_error_message( + os.EX_DATAERR, "Invalid JSON for content. Please provide valid JSON." + ) else: subscriptions = None @@ -210,18 +204,16 @@ async def update( try: path, encoding = create_archive(path) except BadZipFile: - typer.echo("Invalid zip archive") - raise typer.Exit(3) + exit_with_error_message(os.EX_DATAERR, f"Invalid zip archive: {path}") except FileNotFoundError: - typer.echo("No such file or directory") - raise typer.Exit(4) + exit_with_error_message(os.EX_NOINPUT, f"No such file or directory: {path}") if encoding != program_message.content.code.encoding: - logger.error( + exit_with_error_message( + os.EX_DATAERR, f"Code must be encoded with the same encoding as the previous version " - f"('{encoding}' vs '{program_message.content.code.encoding}'" + f"('{encoding}' vs '{program_message.content.code.encoding}'", ) - raise typer.Exit(1) # Upload the source code with open(path, "rb") as fd: @@ -231,7 +223,7 @@ async def update( logger.debug("Uploading file") message, status = client.create_store( file_content=file_content, - storage_engine=code_message.content.item_type, + storage_engine=StorageEnum(code_message.content.item_type), channel=code_message.channel, guess_mime_type=True, ref=code_message.item_hash, @@ -258,9 +250,7 @@ async def unpersist( account=account, api_server=sdk_settings.API_HOST ) as client: existing: MessagesResponse = await client.get_messages( - message_filter=MessageFilter( - hashes=[item_hash] - ) + message_filter=MessageFilter(hashes=[item_hash]) ) message: ProgramMessage = existing.messages[0] content: ProgramContent = message.content.copy() diff --git a/src/aleph_client/commands/utils.py b/src/aleph_client/commands/utils.py index 7c38082d..fab88910 100644 --- a/src/aleph_client/commands/utils.py +++ b/src/aleph_client/commands/utils.py @@ -29,6 +29,8 @@ def colorful_message_json(message: GenericMessage): def input_multiline() -> str: """Prompt the user for a multiline input.""" + if not is_environment_interactive(): + raise ValueError("Non-interactive mode does not support multiline input.") echo("Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it.") contents = "" while True: @@ -46,10 +48,14 @@ def setup_logging(debug: bool = False): def yes_no_input(text: str, default: bool) -> bool: + if not is_environment_interactive(): + return default return Prompt.ask(text, choices=["y", "n"], default=default) == "y" def prompt_for_volumes(): + if not is_environment_interactive(): + return [] while yes_no_input("Add volume ?", default=False): mount = Prompt.ask("Mount path: ") comment = Prompt.ask("Comment: ") @@ -143,17 +149,21 @@ def validated_prompt( validator: Callable[[str], Any], default: Optional[str] = None, ) -> str: + if not is_environment_interactive(): + if default is not None: + return default + raise ValueError("Non-interactive mode requires a default value.") while True: try: value = Prompt.ask( prompt, default=default, ) + if value is None: + raise PromptError except PromptError: echo(f"Invalid input: {value}\nTry again.") continue - if value is None and default is not None: - return default if validator(str(value)): return str(value) echo(f"Invalid input: {value}\nTry again.") @@ -165,26 +175,27 @@ def validated_int_prompt( min_value: Optional[int] = None, max_value: Optional[int] = None, ) -> int: + if not is_environment_interactive(): + if default is not None: + return default + raise ValueError("Non-interactive mode requires a default value.") while True: try: value = IntPrompt.ask( prompt + f" [min: {min_value or '-'}, max: {max_value or '-'}]", default=default, ) + if ( + value is None + or min_value is not None + and value < min_value + or max_value is not None + and value > max_value + ): + raise PromptError except PromptError: echo(f"Invalid input: {value}\nTry again.") continue - if value is None: - if default is not None: - return default - else: - value = 0 - if min_value is not None and value < min_value: - echo(f"Invalid input: {value}\nTry again.") - continue - if max_value is not None and value > max_value: - echo(f"Invalid input: {value}\nTry again.") - continue return value diff --git a/src/aleph_client/exit_codes.py b/src/aleph_client/exit_codes.py new file mode 100644 index 00000000..c2b7e525 --- /dev/null +++ b/src/aleph_client/exit_codes.py @@ -0,0 +1,39 @@ +import os +from typing import Optional + +import typer + +# Aleph client exit codes +EX_NOFUNDS = 99 + +# Exit code to error message mapping +EXIT_CODE_MESSAGES = { + os.EX_USAGE: "command line usage error", # starts at 64 + os.EX_DATAERR: "data format error", + os.EX_NOINPUT: "cannot open input", + os.EX_NOUSER: "addressee unknown", + os.EX_NOHOST: "host name unknown", + os.EX_UNAVAILABLE: "service unavailable", + os.EX_SOFTWARE: "internal software error", + os.EX_OSERR: "system error (e.g., can't fork)", + os.EX_OSFILE: "critical OS file missing", + os.EX_CANTCREAT: "can't create (user) output file", + os.EX_IOERR: "input/output error", + os.EX_TEMPFAIL: "temp failure; user is invited to retry", + os.EX_PROTOCOL: "remote error in protocol", + os.EX_NOPERM: "permission denied", + os.EX_CONFIG: "configuration error", # ends at 78 + EX_NOFUNDS: "insufficient funds", +} + + +def exit_with_error_message(exit_code: int, message: Optional[str] = None) -> None: + """ + Exit the program with the given exit code and print the corresponding error message. + """ + error_message = EXIT_CODE_MESSAGES.get(exit_code, "unknown error") + if message: + typer.echo(f"{error_message}: {message}") + else: + typer.echo(error_message) + typer.Exit(exit_code) diff --git a/src/aleph_client/utils.py b/src/aleph_client/utils.py index 5d367f7c..9f6dbf20 100644 --- a/src/aleph_client/utils.py +++ b/src/aleph_client/utils.py @@ -2,6 +2,8 @@ import inspect import logging import os +import signal +import sys from functools import partial, wraps from pathlib import Path from shutil import make_archive @@ -67,13 +69,25 @@ def get_message_type_value(message_type: Type[GenericMessage]) -> MessageType: class AsyncTyper(typer.Typer): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def sigint_handler(signum, frame): + sys.exit(130) + + signal.signal(signal.SIGINT, sigint_handler) + @staticmethod def maybe_run_async(decorator, f): if inspect.iscoroutinefunction(f): @wraps(f) def runner(*args, **kwargs): - return asyncio.run(f(*args, **kwargs)) + try: + return asyncio.run(f(*args, **kwargs)) + except asyncio.CancelledError: + print("Operation cancelled, exiting...") + sys.exit(130) decorator(runner) else: diff --git a/tests/unit/commands/test_about.py b/tests/unit/commands/test_about.py index ccab0d55..2e517680 100644 --- a/tests/unit/commands/test_about.py +++ b/tests/unit/commands/test_about.py @@ -1,4 +1,5 @@ import re + from typer.testing import CliRunner from aleph_client.__main__ import app @@ -7,11 +8,9 @@ def test_about_version(): - result = runner.invoke( - app, ["about", "version"] - ) + result = runner.invoke(app, ["about", "version"]) - assert result.exit_code == 1, result.stdout + assert result.exit_code == 0, result.stdout pattern = r"Aleph CLI Version: \d+\.\d+\.\d+.*" diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index bd440f3b..7061150c 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -1,5 +1,6 @@ import re from pathlib import Path + from typer.testing import CliRunner from aleph_client.__main__ import app @@ -12,11 +13,7 @@ def test_account_address(account_file: Path): private_key_file = str(account_file) result = runner.invoke( - app, [ - "account", - "address", - "--private-key-file", private_key_file - ] + app, ["account", "address", "--private-key-file", private_key_file] ) assert result.exit_code == 0 @@ -34,12 +31,15 @@ def test_account_balance(account_file: Path): private_key_file = str(account_file) result = runner.invoke( - app, [ + app, + [ "account", "balance", - "--address", address, - "--private-key-file", private_key_file - ] + "--address", + address, + "--private-key-file", + private_key_file, + ], ) pattern = r"Failed to retrieve balance for address 0x.*\. Status code: 404" @@ -57,13 +57,8 @@ def test_account_create(account_file: Path): old_key = account_file.read_bytes() result = runner.invoke( - app, [ - "account", - "create", - "--private-key-file", private_key_file, - replace, - debug - ] + app, + ["account", "create", "--private-key-file", private_key_file, replace, debug], ) assert result.exit_code == 0, result.stdout @@ -81,11 +76,7 @@ def test_account_export_private_key(account_file: Path): private_key_file = str(account_file) result = runner.invoke( - app, [ - "account", - "export-private-key", - "--private-key-file", private_key_file - ] + app, ["account", "export-private-key", "--private-key-file", private_key_file] ) assert result.exit_code == 0 @@ -98,10 +89,7 @@ def test_account_export_private_key(account_file: Path): def test_account_path(): - result = runner.invoke(app, [ - "account", - "path" - ]) + result = runner.invoke(app, ["account", "path"]) pattern = r".*.aleph-im/private-keys/ethereum\.key" assert re.match(pattern, result.stdout) @@ -118,9 +106,11 @@ def test_sign_bytes_raw(account_file: Path): [ "account", "sign-bytes", - "--message", message, - "--private-key-file", private_key_file, - debug + "--message", + message, + "--private-key-file", + private_key_file, + debug, ], ) @@ -138,12 +128,7 @@ def test_sign_bytes_raw_stdin(account_file: Path): result = runner.invoke( app, - [ - "account", - "sign-bytes", - "--private-key-file", private_key_file, - debug - ], + ["account", "sign-bytes", "--private-key-file", private_key_file, debug], input=message, ) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index aa4d1f2f..cafb0ca3 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -1,9 +1,10 @@ import asyncio import json -import pytest import re import textwrap from pathlib import Path + +import pytest from typer.testing import CliRunner from aleph_client.__main__ import app @@ -16,25 +17,33 @@ def fixture_aggregate_post(account_file: Path): key = "key" content = """{"c": 3, "d": "test"}""" address = None - channel = "channel" + channel = "TEST" inline = "--no-inline" sync = "--no-sync" - # private_key = None private_key_file = str(account_file) debug = "--no-debug" result = runner.invoke( - app, [ - "aggregate", "post", key, content, - "--address", address, - "--channel", channel, - "--private-key-file", private_key_file, + app, + [ + "aggregate", + "post", + key, + content, + "--address", + address, + "--channel", + channel, + "--private-key-file", + private_key_file, inline, sync, - debug - ] + debug, + ], ) + asyncio.run(asyncio.sleep(1)) + return result @@ -44,86 +53,90 @@ def test_aggregate_post(fixture_aggregate_post): assert result.exit_code == 0, result.stdout pattern = textwrap.dedent( - '''\ - \{ + """\ + \\{ "chain": "ETH", "sender": ".*", "type": "AGGREGATE", - "channel": "channel", + "channel": "TEST", "confirmations": null, "confirmed": null, "signature": ".*", "size": null, - "time": [0-9]+\.[0-9]+, + "time": [0-9]+\\.[0-9]+, "item_type": "storage", "item_content": null, "hash_type": null, "item_hash": ".*", - "content": \{ + "content": \\{ "address": ".*", - "time": [0-9]+\.[0-9]+, + "time": [0-9]+\\.[0-9]+, "key": "key", - "content": \{ + "content": \\{ "c": 3, "d": "test" - \} - \}, + \\} + \\}, "forgotten_by": null - \} - ''' + \\} + """ ) assert re.fullmatch(pattern, result.stdout) def test_aggregate_get(account_file: Path, fixture_aggregate_post): - # TODO This delay is being necessary for the post to be available. Verify!!! - asyncio.run(asyncio.sleep(1)) - key = "key" address = None - # private_key = None private_key_file = str(account_file) debug = "--no-debug" result = runner.invoke( - app, [ - "aggregate", "get", key, - "--address", address, - "--private-key-file", private_key_file, - debug - ] + app, + [ + "aggregate", + "get", + key, + "--address", + address, + "--private-key-file", + private_key_file, + debug, + ], ) assert result.exit_code == 0, result.stdout expected_content = """{"c": 3, "d": "test"}""" - assert json.dumps(json.loads(expected_content), separators=(',', ':')) == \ - json.dumps(json.loads(result.stdout), separators=(',', ':')) + assert json.dumps( + json.loads(expected_content), separators=(",", ":") + ) == json.dumps(json.loads(result.stdout), separators=(",", ":")) def test_aggregate_forget(account_file: Path, fixture_aggregate_post): - # TODO This delay is being necessary for the post to be available. Verify!!! - asyncio.run(asyncio.sleep(1)) - hash = json.loads(fixture_aggregate_post.stdout)["item_hash"] key = hash - # channel = None - # private_key = None + channel = "TEST" private_key_file = str(account_file) - # reason = None + reason = "Testing reasons." debug = "--debug" result = runner.invoke( - app, [ - "aggregate", "forget", key, - # "--channel", channel, - # "--reason", reason, - "--private-key-file", private_key_file, - debug - ] + app, + [ + "aggregate", + "forget", + key, + "--channel", + channel, + "--reason", + reason, + "--private-key-file", + private_key_file, + debug, + ], ) assert result.exit_code == 0, result.stdout diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index 01f64326..7bf37a49 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -19,13 +19,20 @@ def test_domain_add_ipfs(account_file: Path): owner = None result = runner.invoke( - app, [ - "domain", "add", fqdn, - "--private-key-file", private_key_file, - "--target", target, - "--item-hash", item_hash, - "--owner", owner, - ] + app, + [ + "domain", + "add", + fqdn, + "--private-key-file", + private_key_file, + "--target", + target, + "--item-hash", + item_hash, + "--owner", + owner, + ], ) assert result.exit_code == 0 @@ -43,13 +50,20 @@ def test_domain_add_program(account_file: Path): owner = None result = runner.invoke( - app, [ - "domain", "add", fqdn, - "--private-key-file", private_key_file, - "--target", target, - "--item-hash", item_hash, - "--owner", owner, - ] + app, + [ + "domain", + "add", + fqdn, + "--private-key-file", + private_key_file, + "--target", + target, + "--item-hash", + item_hash, + "--owner", + owner, + ], ) assert result.exit_code == 0 @@ -67,13 +81,20 @@ def test_domain_add_instance(account_file: Path): owner = None result = runner.invoke( - app, [ - "domain", "add", fqdn, - "--private-key-file", private_key_file, - "--target", target, - "--item-hash", item_hash, - "--owner", owner, - ] + app, + [ + "domain", + "add", + fqdn, + "--private-key-file", + private_key_file, + "--target", + target, + "--item-hash", + item_hash, + "--owner", + owner, + ], ) assert result.exit_code == 0 @@ -88,11 +109,16 @@ def test_domain_attach(account_file: Path): item_hash = "098f6bcd4621d373cade4e832627b4f6" result = runner.invoke( - app, [ - "domain", "attach", fqdn, - "--private-key-file", private_key_file, - "--item-hash", item_hash, - ] + app, + [ + "domain", + "attach", + fqdn, + "--private-key-file", + private_key_file, + "--item-hash", + item_hash, + ], ) assert result.exit_code == 0, result.stdout @@ -109,10 +135,14 @@ def test_domain_detach(account_file: Path): private_key_file = str(account_file) result = runner.invoke( - app, [ - "domain", "detach", fqdn, - "--private-key-file", private_key_file, - ] + app, + [ + "domain", + "detach", + fqdn, + "--private-key-file", + private_key_file, + ], ) assert result.exit_code == 0, result.stdout @@ -128,10 +158,14 @@ def test_domain_info(account_file: Path): private_key_file = str(account_file) result = runner.invoke( - app, [ - "domain", "info", fqdn, - "--private-key-file", private_key_file, - ] + app, + [ + "domain", + "info", + fqdn, + "--private-key-file", + private_key_file, + ], ) assert result.exit_code == 0, result.stdout diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index dc7ab0e7..13ca7c56 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -1,5 +1,5 @@ -from pathlib import Path import os +from pathlib import Path from typer.testing import CliRunner @@ -9,26 +9,25 @@ def test_file_upload(account_file: Path): - path = Path(os.path.join( - Path(__file__).parent.parent.parent, "fixtures", "anything.txt") - ).absolute().as_posix() - # channel = None - # private_key = None + path = ( + Path( + os.path.join( + Path(__file__).parent.parent.parent, "fixtures", "anything.txt" + ) + ) + .absolute() + .as_posix() + ) private_key_file = str(account_file) - # ref = None - # debug = "--no-debug" - # Test upload a file to aleph network by creating a file and upload it to an aleph node result = runner.invoke( app, [ "file", "upload", path, - # "--channel", channel, - "--private-key-file", private_key_file, - # ref, - # debug + "--private-key-file", + private_key_file, ], ) @@ -44,7 +43,6 @@ def test_file_download(): file_extension = None debug = "--no-debug" - # Test download a file to aleph network result = runner.invoke( app, [ @@ -52,10 +50,13 @@ def test_file_download(): "download", hash, use_ipfs, - "--output-path", output_path, - "--file-name", file_name, - "--file-extension", file_extension, - debug + "--output-path", + output_path, + "--file-name", + file_name, + "--file-extension", + file_extension, + debug, ], # 5 bytes file ) @@ -67,8 +68,7 @@ def test_file_download(): def test_file_forget(account_file: Path): item_hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" reason = "reason" - channel = "" - private_key = None + channel = "TEST" private_key_file = str(account_file) debug = "--no-debug" @@ -79,9 +79,11 @@ def test_file_forget(account_file: Path): "forget", item_hash, reason, - "--channel", channel, - "--private-key-file", private_key_file, - debug + "--channel", + channel, + "--private-key-file", + private_key_file, + debug, ], ) @@ -91,8 +93,6 @@ def test_file_forget(account_file: Path): def test_file_list(account_file: Path): - address = None - private_key = None private_key_file = str(account_file) pagination = 100 page = 1 @@ -104,13 +104,17 @@ def test_file_list(account_file: Path): [ "file", "list", - "--address", address, - "--private-key-file", private_key_file, - "--pagination", pagination, - "--page", page, - "--sort-order", sort_order, - "--json", json - ], # 5 bytes file + "--private-key-file", + private_key_file, + "--pagination", + pagination, + "--page", + page, + "--sort-order", + sort_order, + "--json", + json, + ], ) assert result.exit_code == 0 @@ -120,11 +124,8 @@ def test_file_list(account_file: Path): def test_file_pin(account_file: Path): item_hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" - channel = None - private_key = None + channel = "TEST" private_key_file = str(account_file) - ref = None - debug = "--no-debug" result = runner.invoke( app, @@ -132,10 +133,10 @@ def test_file_pin(account_file: Path): "file", "pin", item_hash, - "--channel", channel, - "--private-key-file", private_key_file, - "--ref", ref, - debug + "--channel", + channel, + "--private-key-file", + private_key_file, ], ) diff --git a/tests/unit/commands/test_help.py b/tests/unit/commands/test_help.py index 2b8eb459..3421c530 100644 --- a/tests/unit/commands/test_help.py +++ b/tests/unit/commands/test_help.py @@ -6,11 +6,7 @@ def test_root_help(): - result = runner.invoke( - app, [ - "--help" - ] - ) + result = runner.invoke(app, ["--help"]) assert result.exit_code == 0, result.stdout @@ -18,12 +14,7 @@ def test_root_help(): def test_about_help(): - result = runner.invoke( - app, [ - "about", - "--help" - ] - ) + result = runner.invoke(app, ["about", "--help"]) assert result.exit_code == 0, result.stdout @@ -31,24 +22,13 @@ def test_about_help(): def test_about_version_help(): - result = runner.invoke( - app, [ - "about", - "version", - "--help" - ] - ) + result = runner.invoke(app, ["about", "version", "--help"]) assert result.exit_code == 0, result.stdout def test_account_help(): - result = runner.invoke( - app, [ - "account", - "--help" - ] - ) + result = runner.invoke(app, ["account", "--help"]) assert result.exit_code == 0, result.stdout @@ -56,84 +36,43 @@ def test_account_help(): def test_account_address_help(): - result = runner.invoke( - app, [ - "account", - "address", - "--help" - ] - ) + result = runner.invoke(app, ["account", "address", "--help"]) assert result.exit_code == 0, result.stdout def test_account_balance_help(): - result = runner.invoke( - app, [ - "account", - "balance", - "--help" - ] - ) + result = runner.invoke(app, ["account", "balance", "--help"]) assert result.exit_code == 0, result.stdout def test_account_create_help(): - result = runner.invoke( - app, [ - "account", - "create", - "--help" - ] - ) + result = runner.invoke(app, ["account", "create", "--help"]) assert result.exit_code == 0, result.stdout def test_account_export_private_key_help(): - result = runner.invoke( - app, [ - "account", - "export-private-key", - "--help" - ] - ) + result = runner.invoke(app, ["account", "export-private-key", "--help"]) assert result.exit_code == 0, result.stdout def test_account_path_help(): - result = runner.invoke( - app, [ - "account", - "path", - "--help" - ] - ) + result = runner.invoke(app, ["account", "path", "--help"]) assert result.exit_code == 0, result.stdout def test_account_sign_bytes(): - result = runner.invoke( - app, [ - "account", - "sign-bytes", - "--help" - ] - ) + result = runner.invoke(app, ["account", "sign-bytes", "--help"]) assert result.exit_code == 0, result.stdout def test_aggregate_help(): - result = runner.invoke( - app, [ - "aggregate", - "--help" - ] - ) + result = runner.invoke(app, ["aggregate", "--help"]) assert result.exit_code == 0, result.stdout @@ -141,48 +80,25 @@ def test_aggregate_help(): def test_aggregate_forget_help(): - result = runner.invoke( - app, [ - "aggregate", - "forget", - "--help" - ] - ) + result = runner.invoke(app, ["aggregate", "forget", "--help"]) assert result.exit_code == 0, result.stdout def test_aggregate_get_help(): - result = runner.invoke( - app, [ - "aggregate", - "get", - "--help" - ] - ) + result = runner.invoke(app, ["aggregate", "get", "--help"]) assert result.exit_code == 0, result.stdout def test_aggregate_post_help(): - result = runner.invoke( - app, [ - "aggregate", - "post", - "--help" - ] - ) + result = runner.invoke(app, ["aggregate", "post", "--help"]) assert result.exit_code == 0, result.stdout def test_domain_help(): - result = runner.invoke( - app, [ - "domain", - "--help" - ] - ) + result = runner.invoke(app, ["domain", "--help"]) assert result.exit_code == 0, result.stdout @@ -190,60 +106,31 @@ def test_domain_help(): def test_domain_add_help(): - result = runner.invoke( - app, [ - "domain", - "add", - "--help" - ] - ) + result = runner.invoke(app, ["domain", "add", "--help"]) assert result.exit_code == 0, result.stdout def test_domain_attach_help(): - result = runner.invoke( - app, [ - "domain", - "attach", - "--help" - ] - ) + result = runner.invoke(app, ["domain", "attach", "--help"]) assert result.exit_code == 0, result.stdout def test_domain_detach_help(): - result = runner.invoke( - app, [ - "domain", - "detach", - "--help" - ] - ) + result = runner.invoke(app, ["domain", "detach", "--help"]) assert result.exit_code == 0, result.stdout def test_domain_info_help(): - result = runner.invoke( - app, [ - "domain", - "info", - "--help" - ] - ) + result = runner.invoke(app, ["domain", "info", "--help"]) assert result.exit_code == 0, result.stdout def test_file_help(): - result = runner.invoke( - app, [ - "file", - "--help" - ] - ) + result = runner.invoke(app, ["file", "--help"]) assert result.exit_code == 0, result.stdout @@ -251,72 +138,37 @@ def test_file_help(): def test_file_download_help(): - result = runner.invoke( - app, [ - "file", - "download", - "--help" - ] - ) + result = runner.invoke(app, ["file", "download", "--help"]) assert result.exit_code == 0, result.stdout def test_file_forget_help(): - result = runner.invoke( - app, [ - "file", - "forget", - "--help" - ] - ) + result = runner.invoke(app, ["file", "forget", "--help"]) assert result.exit_code == 0, result.stdout def test_file_list_help(): - result = runner.invoke( - app, [ - "file", - "list", - "--help" - ] - ) + result = runner.invoke(app, ["file", "list", "--help"]) assert result.exit_code == 0, result.stdout def test_file_pin_help(): - result = runner.invoke( - app, [ - "file", - "pin", - "--help" - ] - ) + result = runner.invoke(app, ["file", "pin", "--help"]) assert result.exit_code == 0, result.stdout def test_file_upload_help(): - result = runner.invoke( - app, [ - "file", - "upload", - "--help" - ] - ) + result = runner.invoke(app, ["file", "upload", "--help"]) assert result.exit_code == 0, result.stdout def test_instance_help(): - result = runner.invoke( - app, [ - "instance", - "--help" - ] - ) + result = runner.invoke(app, ["instance", "--help"]) assert result.exit_code == 0, result.stdout @@ -324,48 +176,25 @@ def test_instance_help(): def test_instance_create_help(): - result = runner.invoke( - app, [ - "instance", - "create", - "--help" - ] - ) + result = runner.invoke(app, ["instance", "create", "--help"]) assert result.exit_code == 0, result.stdout def test_instance_delete_help(): - result = runner.invoke( - app, [ - "instance", - "delete", - "--help" - ] - ) + result = runner.invoke(app, ["instance", "delete", "--help"]) assert result.exit_code == 0, result.stdout def test_instance_list_help(): - result = runner.invoke( - app, [ - "instance", - "list", - "--help" - ] - ) + result = runner.invoke(app, ["instance", "list", "--help"]) assert result.exit_code == 0, result.stdout def test_message_help(): - result = runner.invoke( - app, [ - "message", - "--help" - ] - ) + result = runner.invoke(app, ["message", "--help"]) assert result.exit_code == 0, result.stdout @@ -373,96 +202,49 @@ def test_message_help(): def test_message_amend_help(): - result = runner.invoke( - app, [ - "message", - "amend", - "--help" - ] - ) + result = runner.invoke(app, ["message", "amend", "--help"]) assert result.exit_code == 0, result.stdout def test_message_find_help(): - result = runner.invoke( - app, [ - "message", - "find", - "--help" - ] - ) + result = runner.invoke(app, ["message", "find", "--help"]) assert result.exit_code == 0, result.stdout def test_message_forget_help(): - result = runner.invoke( - app, [ - "message", - "forget", - "--help" - ] - ) + result = runner.invoke(app, ["message", "forget", "--help"]) assert result.exit_code == 0, result.stdout def test_message_get_help(): - result = runner.invoke( - app, [ - "message", - "get", - "--help" - ] - ) + result = runner.invoke(app, ["message", "get", "--help"]) assert result.exit_code == 0, result.stdout def test_message_post_help(): - result = runner.invoke( - app, [ - "message", - "post", - "--help" - ] - ) + result = runner.invoke(app, ["message", "post", "--help"]) assert result.exit_code == 0, result.stdout def test_message_sign_help(): - result = runner.invoke( - app, [ - "message", - "sign", - "--help" - ] - ) + result = runner.invoke(app, ["message", "sign", "--help"]) assert result.exit_code == 0, result.stdout def test_message_watch_help(): - result = runner.invoke( - app, [ - "message", - "watch", - "--help" - ] - ) + result = runner.invoke(app, ["message", "watch", "--help"]) assert result.exit_code == 0, result.stdout def test_node_help(): - result = runner.invoke( - app, [ - "node", - "--help" - ] - ) + result = runner.invoke(app, ["node", "--help"]) assert result.exit_code == 0, result.stdout @@ -470,36 +252,19 @@ def test_node_help(): def test_node_compute_help(): - result = runner.invoke( - app, [ - "node", - "compute", - "--help" - ] - ) + result = runner.invoke(app, ["node", "compute", "--help"]) assert result.exit_code == 0, result.stdout def test_node_core_help(): - result = runner.invoke( - app, [ - "node", - "core", - "--help" - ] - ) + result = runner.invoke(app, ["node", "core", "--help"]) assert result.exit_code == 0, result.stdout def test_program_help(): - result = runner.invoke( - app, [ - "program", - "--help" - ] - ) + result = runner.invoke(app, ["program", "--help"]) assert result.exit_code == 0, result.stdout @@ -507,36 +272,18 @@ def test_program_help(): def test_program_unpersist_help(): - result = runner.invoke( - app, [ - "program", - "unpersist", - "--help" - ] - ) + result = runner.invoke(app, ["program", "unpersist", "--help"]) assert result.exit_code == 0, result.stdout def test_program_update_help(): - result = runner.invoke( - app, [ - "program", - "update", - "--help" - ] - ) + result = runner.invoke(app, ["program", "update", "--help"]) assert result.exit_code == 0, result.stdout def test_program_upload_help(): - result = runner.invoke( - app, [ - "program", - "upload", - "--help" - ] - ) + result = runner.invoke(app, ["program", "upload", "--help"]) assert result.exit_code == 0, result.stdout diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index 0fbd3c92..34775f78 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -1,143 +1,100 @@ -from typer.testing import CliRunner +import os from pathlib import Path +from typing import Dict + +from typer.testing import CliRunner + from aleph_client.__main__ import app runner = CliRunner() item_hash = None -# TODO: Add non-interactive flag -def test_instance_create(account_file: Path, ssh_keys_files: dict[str, Path]): - channel = "channel" - memory = 256 - vcpus = 1 - timeout_seconds = 30.0 - private_key_file = str(account_file) - ssh_pubkey_file = str(ssh_keys_files["public_key"]) - print_messages = "--no-print-messages" - rootfs = "Ubuntu 22" - rootfs_size = 2000 - debug = "--no-debug" - persistent_volume = None - ephemeral_volume = None - immutable_volume = None - - result = runner.invoke( - app, [ - "instance", "create", - "--channel", channel, - "--memory", memory, - "--vcpus", vcpus, - "--timeout-seconds", timeout_seconds, - "--private-key-file", private_key_file, - "--ssh-pubkey-file", ssh_pubkey_file, - print_messages, - "--rootfs", rootfs, - "--rootfs-size", rootfs_size, - debug, - "--persistent-volume", persistent_volume, - "--ephemeral-volume", ephemeral_volume, - "--immutable-volume", immutable_volume - ] - ) - - print("result.exit_code:") - print(result.exit_code) - print("result.stdout:") - print(result.stdout) - - assert result.exit_code == 0 - # assert result.stdout +def test_instance_create(account_file: Path, ssh_keys_files: Dict[str, Path]): + channel = "TEST" + memory = 256 + vcpus = 1 + timeout_seconds = 30.0 + private_key_file = str(account_file) + ssh_pubkey_file = str(ssh_keys_files["public_key"]) + print_messages = "--no-print-messages" + rootfs = "Ubuntu 22" + rootfs_size = 2000 + + result = runner.invoke( + app, + [ + "instance", + "create", + "--channel", + channel, + "--memory", + memory, + "--vcpus", + vcpus, + "--timeout-seconds", + timeout_seconds, + "--private-key-file", + private_key_file, + "--ssh-pubkey-file", + ssh_pubkey_file, + print_messages, + "--rootfs", + rootfs, + "--rootfs-size", + rootfs_size, + "--debug", + ], + ) + + assert result.exit_code == 0 def test_instance_delete(account_file: Path): - item_hash = "item_hash" - reason = "reason" - private_key_file = str(account_file) - print_messages = "--print-messages" #[--print-messages|--no-print-messages] - debug = "--debug" # [--debug|--no-debug] - - result = runner.invoke( - app, [ - "instance", "delete", - item_hash, - "--reason", reason, - "--private-key-file", private_key_file, - print_messages, - debug, - ] - ) - - assert result.exit_code == 0 - assert result.stdout + item_hash = "item_hash" + reason = "reason" + private_key_file = str(account_file) + print_messages = "--print-messages" # [--print-messages|--no-print-messages] + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, + [ + "instance", + "delete", + item_hash, + "--reason", + reason, + "--private-key-file", + private_key_file, + print_messages, + debug, + ], + ) + + assert result.exit_code == os.EX_NOINPUT + assert result.stdout def test_instance_list(account_file: Path): - address = "address" - private_key_file = str(account_file) - json = "--json" # [--json|--no-json] - debug = "--debug" # [--debug|--no-debug] - - result = runner.invoke( - app, [ - "instance", "list", - "--address", address, - "--private-key-file", private_key_file, - json, - debug, - ] - ) - - assert result.exit_code == 0 - assert result.stdout - - -# def test_instance_create(): -# global item_hash -# -# rootfs = "Ubuntu 22" -# rootfs_name = "Ubuntu 22" -# vcpus = 1 -# memory = 256 -# rootfs_size = 2000 -# -# result = runner.invoke( -# app, [ -# "instance", "create", -# "--rootfs", rootfs, -# "--rootfs-name", rootfs_name, -# "--vcpus", vcpus, -# "--memory", memory, -# "--rootfs-size", rootfs_size -# ] -# ) -# -# assert result.exit_code == 0 -# assert result.stdout -# -# item_hash_regex = r"\b0x[a-fA-F0-9]{40,42}\b" -# item_hashes = re.findall(item_hash_regex, result.stdout) -# -# item_hash = item_hashes[0] if item_hashes else None -# -# -# def test_instance_delete(): -# result = runner.invoke( -# app, [ -# "instance", "create", -# "--item_hash", item_hash, -# ] -# ) -# -# assert result.exit_code == 0 -# assert result.stdout -# -# -# def test_instance_list(): -# result = runner.invoke( -# app, ["instance", "list"] -# ) -# -# assert result.exit_code == 0 -# assert result.stdout -# assert "Item Hash Vcpus Memory Disk size IPv6 address" in result.stdout + address = "address" + private_key_file = str(account_file) + json = "--json" # [--json|--no-json] + debug = "--debug" # [--debug|--no-debug] + + result = runner.invoke( + app, + [ + "instance", + "list", + "--address", + address, + "--private-key-file", + private_key_file, + json, + debug, + ], + ) + + assert result.exit_code == 0 + assert result.stdout diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index 55a865d5..6b0f4157 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -1,8 +1,11 @@ import json import os -import pytest +import signal +import subprocess +import time from pathlib import Path +import pytest from aleph.sdk.chains.ethereum import ETHAccount from typer.testing import CliRunner @@ -35,12 +38,7 @@ def test_message_amend(account_file: Path): result = runner.invoke( app, - [ - "message", - "amend", - "--private-key-file", private_key_file, - debug - ], + ["message", "amend", "--private-key-file", private_key_file, debug], ) assert result.exit_code == 0 @@ -50,17 +48,9 @@ def test_message_amend(account_file: Path): def test_message_find(): pagination = 1 page = 1 - message_types = None - content_types = None - content_keys = None - refs = None - addresses = None - tags = None hashes = "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" - channels = None chains = "ETH" start_date = 1234 - end_date = None ignore_invalid_messages = "--no-ignore-invalid-messages" result = runner.invoke( @@ -68,20 +58,18 @@ def test_message_find(): [ "message", "find", - "--pagination", pagination, - "--page", page, - "--message-types", message_types, - "--content-types", content_types, - "--content-keys", content_keys, - "--refs", refs, - "--addresses", addresses, - "--tags", tags, - "--hashes", hashes, - "--channels", channels, - "--chains", chains, - "--start-date", start_date, - "--end-date", end_date, - "--ignore-invalid-messages", ignore_invalid_messages + "--pagination", + pagination, + "--page", + page, + "--hashes", + hashes, + "--chains", + chains, + "--start-date", + start_date, + "--ignore-invalid-messages", + ignore_invalid_messages, ], ) @@ -89,14 +77,14 @@ def test_message_find(): assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - assert ("bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" in result.stdout) + assert ( + "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" + in result.stdout + ) @pytest.mark.skip(reason="Not implemented.") def test_message_forget(account_file: Path): - reason = None - channel = None - # private_key = None private_key_file = get_account(account_file) debug = "--no-debug" @@ -105,9 +93,8 @@ def test_message_forget(account_file: Path): [ "message", "forget", - "--reason", reason, - "--channel", channel, - "--private-key-file", private_key_file, + "--private-key-file", + private_key_file, debug, ], ) @@ -137,28 +124,23 @@ def test_message_get(): def test_message_post(account_file): - path = Path(os.path.join( - Path(__file__).parent.parent.parent, "fixtures", "post.json") - ).absolute().as_posix() + path = ( + Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")) + .absolute() + .as_posix() + ) message_type = "POST" - # ref = None - # channel = None - # private_key = None - # private_key_file = get_account(account_file) - # debug = "--no-debug" result = runner.invoke( app, [ "message", "post", - "--path", path, - "--type", message_type, - # "--ref", ref, - # "--channel", channel, - # "--private-key-file", private_key_file, - # debug + "--path", + path, + "--type", + message_type, ], ) @@ -166,22 +148,19 @@ def test_message_post(account_file): assert result.stdout -def test_message_sign(account_file): # TODO !!! - # private_key = None - private_key_file = get_account(account_file) - # message = get_test_message(private_key_file) - message = "A message to be sign" - # debug = "--no-debug" +def test_message_sign(account_file): + private_key_file = account_file + message = json.dumps(get_test_message(get_account(private_key_file))) result = runner.invoke( app, [ "message", "sign", - # "--message", json.dumps(message), - "--message", message, - "--private-key-file", private_key_file, - # debug + "--message", + message, + "--private-key-file", + private_key_file, ], ) @@ -191,19 +170,16 @@ def test_message_sign(account_file): # TODO !!! def test_message_sign_stdin(account_file): - # private_key = None - private_key_file = get_account(account_file) - message = get_test_message(private_key_file) - debug = "--no-debug" + private_key_file = account_file + message = get_test_message(get_account(private_key_file)) result = runner.invoke( app, [ "message", "sign", - "--message", message, - "--private-key-file", private_key_file, - debug + "--private-key-file", + private_key_file, ], input=json.dumps(message), ) @@ -214,17 +190,14 @@ def test_message_sign_stdin(account_file): def test_message_watch(account_file: Path): - indent = None - debug = "--no-debug" + debug = "--debug" + message_ref = "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" - result = runner.invoke( - app, - [ - "message", - "watch", - "--indent", indent, - debug - ], - ) + # runs indefinitely, we should just check if stdout prints something after some time and then kill the process + proc = subprocess.Popen(["aleph", "message", "watch", message_ref, debug]) + time.sleep(1) + proc.send_signal(signal.SIGINT) + # Wait for the process to complete + proc.wait() - assert result.exit_code == 0 + assert proc.returncode == 130 # SIGINT diff --git a/tests/unit/commands/test_node.py b/tests/unit/commands/test_node.py index 8ed4869b..d88a98c5 100644 --- a/tests/unit/commands/test_node.py +++ b/tests/unit/commands/test_node.py @@ -1,6 +1,4 @@ -import textwrap - -import re +import json from typer.testing import CliRunner @@ -10,67 +8,22 @@ def test_node_compute(): - json = "--no-json" - active = "--no-active" - address = None - debug = "--no-debug" - result = runner.invoke( - app, [ - "node", - "compute", - json, - active, - "--address", address, - debug - ] + app, ["node", "compute", "--json", "--no-active", "--no-debug"] ) assert result.exit_code == 0 - - pattern = textwrap.dedent( - '''\ - .*Compute Node Information.* - .* - .* Score ┃ Name ┃ Creation Time ┃ Decentralization ┃ Status.* - .* - │ [0-9]+\.[0-9]+% │ .* │ [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} │ .*[0-9]+\.[0-9]+% │ .* │ - [\s\S]* - ''' - ) - - assert re.fullmatch(pattern, result.stdout) + output = json.loads(result.stdout) + assert isinstance(output, list) + assert len(output) > 0 + assert isinstance(output[0], dict) -# TODO Stopped here!!! def test_node_core(): - json = "--no-json" - active = "--no-active" - address = None - debug = "--no-debug" - - result = runner.invoke( - app, [ - "node", - "core", - json, - active, - "--address", address, - debug - ] - ) + result = runner.invoke(app, ["node", "core", "--json", "--no-active", "--no-debug"]) assert result.exit_code == 0 - - pattern = textwrap.dedent( - '''\ - .*Core Channel Node Information.* - .* - .* Score ┃ Name ┃ Staked ┃ Linked ┃ Creation Time ┃ Status - .* - │ [0-9]+\.[0-9]+% │ .* │ .* │ [0-9]+ │ [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} │ .* │ - [\s\S]* - ''' - ) - - assert re.fullmatch(pattern, result.stdout) + output = json.loads(result.stdout) + assert isinstance(output, list) + assert len(output) > 0 + assert isinstance(output[0], dict) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 0de315f1..6dcbaa88 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -1,23 +1,30 @@ -import re import os -import tempfile +import re from pathlib import Path +import pytest from typer.testing import CliRunner from aleph_client.__main__ import app -import pytest runner = CliRunner() def test_program_upload(account_file: Path): - path = Path(os.path.join( - Path(__file__).parent.parent.parent, "fixtures", "example_program_upload.zip") - ).absolute().as_posix() + path = ( + Path( + os.path.join( + Path(__file__).parent.parent.parent, + "fixtures", + "example_program_upload.zip", + ) + ) + .absolute() + .as_posix() + ) entrypoint = "__init__:app" - # channel = "channel" + # channel = "TEST" # memory = "memory" # vcpus = "vcpus" # timeout_seconds = "timeout_seconds" @@ -34,10 +41,13 @@ def test_program_upload(account_file: Path): # ephemeral_volume = "ephemeral_volume" # immutable_volume = "immutable_volume" - result = runner.invoke( - app, [ - "program", "upload", path, entrypoint, + app, + [ + "program", + "upload", + path, + entrypoint, # "--channel", channel, # "--memory", memory, # "--vcpus", vcpus, @@ -46,14 +56,15 @@ def test_program_upload(account_file: Path): # print_messages, # print_code_message, # print_program_message, - "--runtime", runtime, + "--runtime", + runtime, # beta, # debug, # persistent, # "--persistent-volume", persistent_volume, # "--ephemeral-volume", ephemeral_volume, # "--immutable-volume", immutable_volume - ] + ], ) pattern = r"Your program has been uploaded on aleph.im" @@ -62,20 +73,26 @@ def test_program_upload(account_file: Path): @pytest.fixture def item_hash_upload(account_file: Path): - path = Path(os.path.join( - Path(__file__).parent.parent.parent, "fixtures", "example_program_upload.zip") - ).absolute().as_posix() + path = ( + Path( + os.path.join( + Path(__file__).parent.parent.parent, + "fixtures", + "example_program_upload.zip", + ) + ) + .absolute() + .as_posix() + ) entrypoint = "__init__:app" runtime = "f873715dc2feec3833074bd4b8745363a0e0093746b987b4c8191268883b2463" result = runner.invoke( - app, [ - "program", "upload", path, entrypoint, "--runtime", runtime - ] + app, ["program", "upload", path, entrypoint, "--runtime", runtime] ) - pattern = r'https://aleph\.sh/vm/(\w+)' + pattern = r"https://aleph\.sh/vm/(\w+)" match = re.findall(pattern, result.stdout) if match: item_hash = match[0] @@ -84,21 +101,30 @@ def item_hash_upload(account_file: Path): def test_program_update(account_file: Path, item_hash_upload): item_hash = item_hash_upload - path = Path(os.path.join( - Path(__file__).parent.parent.parent, "fixtures", "example_program_upload.zip") - ).absolute().as_posix() - # private_key = None + path = ( + Path( + os.path.join( + Path(__file__).parent.parent.parent, + "fixtures", + "example_program_upload.zip", + ) + ) + .absolute() + .as_posix() + ) private_key_file = str(account_file) - # print_message = "--print-message" # [--print-message|--no-print-message] - # debug = "--debug" # [--debug|--no-debug] result = runner.invoke( - app, [ - "program", "update", item_hash, path, - "--private-key-file", private_key_file - # print_message, - # debug, - ] + app, + [ + "program", + "update", + item_hash, + path, + "--private-key-file", + private_key_file, + "--debug", + ], ) assert result.exit_code == 0 @@ -107,18 +133,19 @@ def test_program_update(account_file: Path, item_hash_upload): def test_program_unpersist(account_file: Path, item_hash_upload): item_hash = item_hash_upload - private_key = None private_key_file = str(account_file) - # debug = "--debug" # [--debug|--no-debug] result = runner.invoke( - app, [ - "program", "unpersist", item_hash, - "--private-key-file", private_key_file, - # debug, - ] + app, + [ + "program", + "unpersist", + item_hash, + "--private-key-file", + private_key_file, + "--debug", + ], ) assert result.exit_code == 0 assert result.stdout - \ No newline at end of file diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index b23428f0..bdc06e03 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -7,13 +7,12 @@ https://pytest.org/latest/plugins.html """ import os - from pathlib import Path from tempfile import NamedTemporaryFile from typing import Generator import pytest -from aleph.sdk.chains.common import generate_key, get_public_key +from aleph.sdk.chains.common import generate_key @pytest.fixture @@ -32,15 +31,12 @@ def account_file(empty_account_file: Path) -> Path: @pytest.fixture def ssh_keys_files(empty_account_file: Path) -> dict[str, Path]: - private_key_file = Path(os.path.join( - Path(__file__).parent.parent, "fixtures", "example_ssh_key") + private_key_file = Path( + os.path.join(Path(__file__).parent.parent, "fixtures", "example_ssh_key") ).absolute() - public_key_file = Path(os.path.join( - Path(__file__).parent.parent, "fixtures", "example_ssh_key.pub") + public_key_file = Path( + os.path.join(Path(__file__).parent.parent, "fixtures", "example_ssh_key.pub") ).absolute() - return { - "private_key": private_key_file, - "public_key": public_key_file - } + return {"private_key": private_key_file, "public_key": public_key_file} diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 7813919c..253e3f6f 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,12 +1,6 @@ -from aleph_message.models import ( - AggregateMessage, - ForgetMessage, - MessageType, - PostMessage, - ProgramMessage, - StoreMessage, - InstanceMessage -) +from aleph_message.models import (AggregateMessage, ForgetMessage, + InstanceMessage, MessageType, PostMessage, + ProgramMessage, StoreMessage) from aleph_client.utils import get_message_type_value From f1b47d0e10e23020aa3608612941bfd54446c169 Mon Sep 17 00:00:00 2001 From: mhh Date: Thu, 2 May 2024 11:43:36 +0200 Subject: [PATCH 102/110] Fix exit_with_error_message --- src/aleph_client/exit_codes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aleph_client/exit_codes.py b/src/aleph_client/exit_codes.py index c2b7e525..010df76c 100644 --- a/src/aleph_client/exit_codes.py +++ b/src/aleph_client/exit_codes.py @@ -36,4 +36,4 @@ def exit_with_error_message(exit_code: int, message: Optional[str] = None) -> No typer.echo(f"{error_message}: {message}") else: typer.echo(error_message) - typer.Exit(exit_code) + raise typer.Exit(exit_code) From bfb346f280fe5d579c0eee9c6e84940cd1fe4226 Mon Sep 17 00:00:00 2001 From: mhh Date: Thu, 2 May 2024 11:43:44 +0200 Subject: [PATCH 103/110] Fix message amend and add content property for non-interactive mode --- src/aleph_client/commands/message.py | 49 +++++++---- tests/unit/commands/test_message.py | 116 ++++++++++++++------------- 2 files changed, 94 insertions(+), 71 deletions(-) diff --git a/src/aleph_client/commands/message.py b/src/aleph_client/commands/message.py index a8187a42..1776ad45 100644 --- a/src/aleph_client/commands/message.py +++ b/src/aleph_client/commands/message.py @@ -16,13 +16,14 @@ from aleph.sdk.query.responses import MessagesResponse from aleph.sdk.types import AccountFromPrivateKey, StorageEnum from aleph.sdk.utils import extended_json_encoder +from aleph.sdk.exceptions import MessageNotFoundError, ForgottenMessageError from aleph_message.models import (AlephMessage, ItemHash, MessageType, ProgramMessage) from aleph_client.commands import help_strings from aleph_client.commands.utils import (colorful_json, colorful_message_json, input_multiline, setup_logging, - str_to_datetime) + str_to_datetime, is_environment_interactive) from aleph_client.exit_codes import exit_with_error_message from aleph_client.utils import AsyncTyper @@ -165,6 +166,9 @@ async def post( @app.command() async def amend( item_hash: str = typer.Argument(..., help="Hash reference of the message to amend"), + content: Optional[str] = typer.Option( + None, help="Dictionary update to perform. If omitted, the editor will be opened." + ), private_key: Optional[str] = typer.Option( sdk_settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY ), @@ -179,24 +183,38 @@ async def amend( account: AccountFromPrivateKey = _load_account(private_key, private_key_file) - async with AlephHttpClient(api_server=sdk_settings.API_HOST) as client: - existing_message: AlephMessage = await client.get_message(item_hash=item_hash) - - editor: str = os.getenv("EDITOR", default="nano") - with tempfile.NamedTemporaryFile(suffix="json") as fd: - # Fill in message template - fd.write(existing_message.content.json(indent=4).encode()) - fd.seek(0) + try: + async with AlephHttpClient(api_server=sdk_settings.API_HOST) as client: + existing_message: AlephMessage = await client.get_message(item_hash=item_hash) + except MessageNotFoundError: + exit_with_error_message(os.EX_DATAERR, f"Message not found: {item_hash}") + except ForgottenMessageError: + exit_with_error_message(os.EX_DATAERR, f"Message is forgotten: {item_hash}") + + if content: + new_content_dict = existing_message.content.dict() + new_content_dict.update(json.loads(content)) + else: + if not is_environment_interactive(): + exit_with_error_message( + os.EX_USAGE, + "No content provided and non-interactive environment detected. Please provide content.", + ) + editor: str = os.getenv("EDITOR", default="nano") + with tempfile.NamedTemporaryFile(suffix=".json") as fd: + # Fill in message template + fd.write(existing_message.content.json(indent=4).encode()) + fd.seek(0) - # Launch editor - subprocess.run([editor, fd.name], check=True) + # Launch editor + subprocess.run([editor, fd.name], check=True) - # Read new message - fd.seek(0) - new_content_json = fd.read() + # Read new message + fd.seek(0) + new_content_json = fd.read() + new_content_dict = json.loads(new_content_json) content_type = type(existing_message).__annotations__["content"] - new_content_dict = json.loads(new_content_json) new_content = content_type(**new_content_dict) if isinstance(existing_message, ProgramMessage): @@ -207,7 +225,6 @@ async def amend( new_content.time = time.time() new_content.type = "amend" - typer.echo(new_content) async with AuthenticatedAlephHttpClient( account=account, api_server=sdk_settings.API_HOST ) as client: diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index 6b0f4157..376b4529 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -4,6 +4,7 @@ import subprocess import time from pathlib import Path +from typing import Union import pytest from aleph.sdk.chains.ethereum import ETHAccount @@ -30,62 +31,54 @@ def get_test_message(account: ETHAccount): } -@pytest.mark.skip(reason="Not implemented.") -def test_message_amend(account_file: Path): - # private_key = None - private_key_file = get_account(account_file) - debug = "--no-debug" - - result = runner.invoke( - app, - ["message", "amend", "--private-key-file", private_key_file, debug], +def create_test_message(private_key_file: Union[Path, str] = None): + path = ( + Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")) + .absolute() + .as_posix() ) - - assert result.exit_code == 0 - - -@pytest.mark.skip(reason="Not implemented.") -def test_message_find(): - pagination = 1 - page = 1 - hashes = "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" - chains = "ETH" - start_date = 1234 - ignore_invalid_messages = "--no-ignore-invalid-messages" - + message_type = "POST" + channel = "TEST" result = runner.invoke( app, [ "message", - "find", - "--pagination", - pagination, - "--page", - page, - "--hashes", - hashes, - "--chains", - chains, - "--start-date", - start_date, - "--ignore-invalid-messages", - ignore_invalid_messages, + "post", + "--path", + path, + "--type", + message_type, + "--channel", + channel, + "--private-key-file", + private_key_file, ], ) + return result + + +def test_message_post(account_file): + result = create_test_message(account_file) assert result.exit_code == 0 + assert result.stdout - assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout - assert ( - "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" - in result.stdout +def test_message_amend(account_file: Path): + result = create_test_message(account_file) + message = json.loads(result.stdout) + + result = runner.invoke( + app, + ["message", "amend", message["item_hash"], "--content", '{"content": {"hello": "my bro"}}', "--private-key-file", account_file], ) + assert result.exit_code == 0 + print(result.stdout) + assert json.loads(result.stdout)["content"]["content"]["hello"] == "my bro" -@pytest.mark.skip(reason="Not implemented.") def test_message_forget(account_file: Path): - private_key_file = get_account(account_file) + private_key_file = account_file debug = "--no-debug" result = runner.invoke( @@ -123,29 +116,42 @@ def test_message_get(): assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout -def test_message_post(account_file): - path = ( - Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")) - .absolute() - .as_posix() - ) - - message_type = "POST" +def test_message_find(): + pagination = 1 + page = 1 + hashes = "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" + chains = "ETH" + start_date = 1234 + ignore_invalid_messages = "--no-ignore-invalid-messages" result = runner.invoke( app, [ "message", - "post", - "--path", - path, - "--type", - message_type, + "find", + "--pagination", + pagination, + "--page", + page, + "--hashes", + hashes, + "--chains", + chains, + "--start-date", + start_date, + "--ignore-invalid-messages", + ignore_invalid_messages, ], ) assert result.exit_code == 0 - assert result.stdout + + assert "0x101d8D16372dBf5f1614adaE95Ee5CCE61998Fc9" in result.stdout + + assert ( + "bd79839bf96e595a06da5ac0b6ba51dea6f7e2591bb913deccded04d831d29f4" + in result.stdout + ) def test_message_sign(account_file): From e46068999b3728bfe45f90f593574d158ed47c0e Mon Sep 17 00:00:00 2001 From: mhh Date: Thu, 2 May 2024 11:43:57 +0200 Subject: [PATCH 104/110] Fix test_instance_delete --- tests/unit/commands/test_instance.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index 34775f78..cee1e124 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -51,10 +51,10 @@ def test_instance_create(account_file: Path, ssh_keys_files: Dict[str, Path]): def test_instance_delete(account_file: Path): - item_hash = "item_hash" + item_hash = "93eea5a38043f1eabd268704cccc1133394fda02cfdb8bc0a82a50e5e6eb41aa" # some super old instance hash reason = "reason" private_key_file = str(account_file) - print_messages = "--print-messages" # [--print-messages|--no-print-messages] + print_message = "--print-message" # [--print-message|--no-print-message] debug = "--debug" # [--debug|--no-debug] result = runner.invoke( @@ -62,17 +62,12 @@ def test_instance_delete(account_file: Path): [ "instance", "delete", + print_message, item_hash, - "--reason", - reason, - "--private-key-file", - private_key_file, - print_messages, - debug, ], ) - assert result.exit_code == os.EX_NOINPUT + assert result.exit_code == os.EX_NOPERM assert result.stdout From f7b43d95fafb2b046a6f1e3c84576c4dfd8fe4aa Mon Sep 17 00:00:00 2001 From: mhh Date: Thu, 2 May 2024 11:46:16 +0200 Subject: [PATCH 105/110] Fix test_message_forget --- tests/unit/commands/test_message.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index 376b4529..d978f021 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -54,6 +54,7 @@ def create_test_message(private_key_file: Union[Path, str] = None): private_key_file, ], ) + time.sleep(1) return result @@ -78,17 +79,16 @@ def test_message_amend(account_file: Path): def test_message_forget(account_file: Path): - private_key_file = account_file - debug = "--no-debug" + result = json.loads(create_test_message(account_file).stdout) result = runner.invoke( app, [ "message", "forget", + result["item_hash"], "--private-key-file", - private_key_file, - debug, + account_file, ], ) From 4785527343d4225f6fcc5f9fdc60f31e87397041 Mon Sep 17 00:00:00 2001 From: mhh Date: Thu, 2 May 2024 12:21:24 +0200 Subject: [PATCH 106/110] Fix update program command --- src/aleph_client/commands/program.py | 2 +- tests/unit/commands/test_program.py | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/aleph_client/commands/program.py b/src/aleph_client/commands/program.py index 32b00daf..d0fc2aac 100644 --- a/src/aleph_client/commands/program.py +++ b/src/aleph_client/commands/program.py @@ -221,7 +221,7 @@ async def update( # TODO: Read in lazy mode instead of copying everything in memory file_content = fd.read() logger.debug("Uploading file") - message, status = client.create_store( + message, status = await client.create_store( file_content=file_content, storage_engine=StorageEnum(code_message.content.item_type), channel=code_message.channel, diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index 6dcbaa88..b6b85ff5 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -112,7 +112,6 @@ def test_program_update(account_file: Path, item_hash_upload): .absolute() .as_posix() ) - private_key_file = str(account_file) result = runner.invoke( app, @@ -120,10 +119,9 @@ def test_program_update(account_file: Path, item_hash_upload): "program", "update", item_hash, - path, + str(path), "--private-key-file", - private_key_file, - "--debug", + account_file, ], ) From ff078e8b3f42a48cf50c2395ccf4b028863ea909 Mon Sep 17 00:00:00 2001 From: mhh Date: Thu, 2 May 2024 12:21:41 +0200 Subject: [PATCH 107/110] Fix create instance command --- src/aleph_client/commands/instance.py | 1 + tests/unit/commands/test_instance.py | 9 ++------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/aleph_client/commands/instance.py b/src/aleph_client/commands/instance.py index 35595bd8..f6307521 100644 --- a/src/aleph_client/commands/instance.py +++ b/src/aleph_client/commands/instance.py @@ -187,6 +187,7 @@ def validate_ssh_pubkey_file(file: Union[str, Path]) -> Path: sync=True, rootfs=rootfs, rootfs_size=rootfs_size, + rootfs_name=os_map.get(rootfs, "custom"), storage_engine=StorageEnum.storage, channel=channel, memory=memory, diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index cee1e124..52dde4be 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -5,6 +5,7 @@ from typer.testing import CliRunner from aleph_client.__main__ import app +from aleph_client.exit_codes import EX_NOFUNDS runner = CliRunner() item_hash = None @@ -47,28 +48,22 @@ def test_instance_create(account_file: Path, ssh_keys_files: Dict[str, Path]): ], ) - assert result.exit_code == 0 + assert result.exit_code == EX_NOFUNDS def test_instance_delete(account_file: Path): item_hash = "93eea5a38043f1eabd268704cccc1133394fda02cfdb8bc0a82a50e5e6eb41aa" # some super old instance hash - reason = "reason" - private_key_file = str(account_file) - print_message = "--print-message" # [--print-message|--no-print-message] - debug = "--debug" # [--debug|--no-debug] result = runner.invoke( app, [ "instance", "delete", - print_message, item_hash, ], ) assert result.exit_code == os.EX_NOPERM - assert result.stdout def test_instance_list(account_file: Path): From cae2c93c37a79920bfc6b6eed48ecb15c2882181 Mon Sep 17 00:00:00 2001 From: mhh Date: Thu, 2 May 2024 12:36:14 +0200 Subject: [PATCH 108/110] Remove unnecessary account path test --- tests/unit/commands/test_account.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index 7061150c..ed372a13 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -88,13 +88,6 @@ def test_account_export_private_key(account_file: Path): assert len(result.stdout.strip()) == 66 -def test_account_path(): - result = runner.invoke(app, ["account", "path"]) - - pattern = r".*.aleph-im/private-keys/ethereum\.key" - assert re.match(pattern, result.stdout) - - def test_sign_bytes_raw(account_file: Path): message = "some message" # private_key = None From 941c3e871e97087b5c7835763e830f3050516ecd Mon Sep 17 00:00:00 2001 From: mhh Date: Thu, 2 May 2024 12:36:36 +0200 Subject: [PATCH 109/110] Allow -2 as SIGINT exit code because GH action!? --- tests/unit/commands/test_message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index d978f021..b6c34756 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -206,4 +206,4 @@ def test_message_watch(account_file: Path): # Wait for the process to complete proc.wait() - assert proc.returncode == 130 # SIGINT + assert proc.returncode in [130, -2] # 130 is the return code for SIGINT on most systems, -2 is the return code for SIGINT on Windows From 1a1f4e3bc5b95cada1bc42771073999fd38f8858 Mon Sep 17 00:00:00 2001 From: mhh Date: Thu, 2 May 2024 12:51:11 +0200 Subject: [PATCH 110/110] Fix mypy issues --- tests/unit/commands/test_account.py | 14 ++++---------- tests/unit/commands/test_aggregate.py | 12 +++--------- tests/unit/commands/test_domain.py | 23 ++++++----------------- tests/unit/commands/test_file.py | 24 ++++++------------------ tests/unit/commands/test_instance.py | 10 +++++----- tests/unit/commands/test_message.py | 18 +++++++++--------- tests/unit/commands/test_program.py | 6 +++--- 7 files changed, 36 insertions(+), 71 deletions(-) diff --git a/tests/unit/commands/test_account.py b/tests/unit/commands/test_account.py index ed372a13..1868fa5a 100644 --- a/tests/unit/commands/test_account.py +++ b/tests/unit/commands/test_account.py @@ -26,19 +26,13 @@ def test_account_address(account_file: Path): # TODO Verify if the output message ""Failed to retrieve balance..." is ok!!! def test_account_balance(account_file: Path): - address = None - # private_key = None - private_key_file = str(account_file) - result = runner.invoke( app, [ "account", "balance", - "--address", - address, "--private-key-file", - private_key_file, + str(account_file), ], ) @@ -58,7 +52,7 @@ def test_account_create(account_file: Path): result = runner.invoke( app, - ["account", "create", "--private-key-file", private_key_file, replace, debug], + ["account", "create", "--private-key-file", str(private_key_file), replace, debug], ) assert result.exit_code == 0, result.stdout @@ -102,7 +96,7 @@ def test_sign_bytes_raw(account_file: Path): "--message", message, "--private-key-file", - private_key_file, + str(private_key_file), debug, ], ) @@ -121,7 +115,7 @@ def test_sign_bytes_raw_stdin(account_file: Path): result = runner.invoke( app, - ["account", "sign-bytes", "--private-key-file", private_key_file, debug], + ["account", "sign-bytes", "--private-key-file", str(private_key_file), debug], input=message, ) diff --git a/tests/unit/commands/test_aggregate.py b/tests/unit/commands/test_aggregate.py index cafb0ca3..a20b2e01 100644 --- a/tests/unit/commands/test_aggregate.py +++ b/tests/unit/commands/test_aggregate.py @@ -16,7 +16,6 @@ def fixture_aggregate_post(account_file: Path): key = "key" content = """{"c": 3, "d": "test"}""" - address = None channel = "TEST" inline = "--no-inline" sync = "--no-sync" @@ -30,12 +29,10 @@ def fixture_aggregate_post(account_file: Path): "post", key, content, - "--address", - address, "--channel", channel, "--private-key-file", - private_key_file, + str(private_key_file), inline, sync, debug, @@ -87,7 +84,6 @@ def test_aggregate_post(fixture_aggregate_post): def test_aggregate_get(account_file: Path, fixture_aggregate_post): key = "key" - address = None private_key_file = str(account_file) debug = "--no-debug" @@ -97,10 +93,8 @@ def test_aggregate_get(account_file: Path, fixture_aggregate_post): "aggregate", "get", key, - "--address", - address, "--private-key-file", - private_key_file, + str(private_key_file), debug, ], ) @@ -134,7 +128,7 @@ def test_aggregate_forget(account_file: Path, fixture_aggregate_post): "--reason", reason, "--private-key-file", - private_key_file, + str(private_key_file), debug, ], ) diff --git a/tests/unit/commands/test_domain.py b/tests/unit/commands/test_domain.py index 7bf37a49..b748cde5 100644 --- a/tests/unit/commands/test_domain.py +++ b/tests/unit/commands/test_domain.py @@ -12,11 +12,9 @@ @pytest.mark.skip(reason="Not implemented.") def test_domain_add_ipfs(account_file: Path): fqdn = "aleph.im" - # private_key = None private_key_file = str(account_file) target = "ipfs" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - owner = None result = runner.invoke( app, @@ -25,13 +23,11 @@ def test_domain_add_ipfs(account_file: Path): "add", fqdn, "--private-key-file", - private_key_file, + str(private_key_file), "--target", target, "--item-hash", item_hash, - "--owner", - owner, ], ) @@ -43,11 +39,9 @@ def test_domain_add_ipfs(account_file: Path): @pytest.mark.skip(reason="Not implemented.") def test_domain_add_program(account_file: Path): fqdn = "aleph.im" # domain - # private_key = None private_key_file = str(account_file) target = "program" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - owner = None result = runner.invoke( app, @@ -56,13 +50,11 @@ def test_domain_add_program(account_file: Path): "add", fqdn, "--private-key-file", - private_key_file, + str(private_key_file), "--target", target, "--item-hash", item_hash, - "--owner", - owner, ], ) @@ -78,7 +70,6 @@ def test_domain_add_instance(account_file: Path): private_key_file = str(account_file) target = "instance" # {ipfs|program|instance} item_hash = "098f6bcd4621d373cade4e832627b4f6" - owner = None result = runner.invoke( app, @@ -87,13 +78,11 @@ def test_domain_add_instance(account_file: Path): "add", fqdn, "--private-key-file", - private_key_file, + str(private_key_file), "--target", target, "--item-hash", item_hash, - "--owner", - owner, ], ) @@ -115,7 +104,7 @@ def test_domain_attach(account_file: Path): "attach", fqdn, "--private-key-file", - private_key_file, + str(private_key_file), "--item-hash", item_hash, ], @@ -141,7 +130,7 @@ def test_domain_detach(account_file: Path): "detach", fqdn, "--private-key-file", - private_key_file, + str(private_key_file), ], ) @@ -164,7 +153,7 @@ def test_domain_info(account_file: Path): "info", fqdn, "--private-key-file", - private_key_file, + str(private_key_file), ], ) diff --git a/tests/unit/commands/test_file.py b/tests/unit/commands/test_file.py index 13ca7c56..78dd1e92 100644 --- a/tests/unit/commands/test_file.py +++ b/tests/unit/commands/test_file.py @@ -27,7 +27,7 @@ def test_file_upload(account_file: Path): "upload", path, "--private-key-file", - private_key_file, + str(private_key_file), ], ) @@ -39,8 +39,6 @@ def test_file_download(): hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" use_ipfs = "--no-use-ipfs" output_path = "." - file_name = None - file_extension = None debug = "--no-debug" result = runner.invoke( @@ -52,10 +50,6 @@ def test_file_download(): use_ipfs, "--output-path", output_path, - "--file-name", - file_name, - "--file-extension", - file_extension, debug, ], # 5 bytes file ) @@ -82,7 +76,7 @@ def test_file_forget(account_file: Path): "--channel", channel, "--private-key-file", - private_key_file, + str(private_key_file), debug, ], ) @@ -96,8 +90,6 @@ def test_file_list(account_file: Path): private_key_file = str(account_file) pagination = 100 page = 1 - sort_order = -1 - json = "--no-json" result = runner.invoke( app, @@ -105,15 +97,11 @@ def test_file_list(account_file: Path): "file", "list", "--private-key-file", - private_key_file, + str(private_key_file), "--pagination", - pagination, + str(pagination), "--page", - page, - "--sort-order", - sort_order, - "--json", - json, + str(page), ], ) @@ -136,7 +124,7 @@ def test_file_pin(account_file: Path): "--channel", channel, "--private-key-file", - private_key_file, + str(private_key_file), ], ) diff --git a/tests/unit/commands/test_instance.py b/tests/unit/commands/test_instance.py index 52dde4be..8949cb15 100644 --- a/tests/unit/commands/test_instance.py +++ b/tests/unit/commands/test_instance.py @@ -30,11 +30,11 @@ def test_instance_create(account_file: Path, ssh_keys_files: Dict[str, Path]): "--channel", channel, "--memory", - memory, + str(memory), "--vcpus", - vcpus, + str(vcpus), "--timeout-seconds", - timeout_seconds, + str(timeout_seconds), "--private-key-file", private_key_file, "--ssh-pubkey-file", @@ -43,7 +43,7 @@ def test_instance_create(account_file: Path, ssh_keys_files: Dict[str, Path]): "--rootfs", rootfs, "--rootfs-size", - rootfs_size, + str(rootfs_size), "--debug", ], ) @@ -80,7 +80,7 @@ def test_instance_list(account_file: Path): "--address", address, "--private-key-file", - private_key_file, + str(private_key_file), json, debug, ], diff --git a/tests/unit/commands/test_message.py b/tests/unit/commands/test_message.py index b6c34756..6dd335d5 100644 --- a/tests/unit/commands/test_message.py +++ b/tests/unit/commands/test_message.py @@ -31,7 +31,7 @@ def get_test_message(account: ETHAccount): } -def create_test_message(private_key_file: Union[Path, str] = None): +def create_test_message(private_key_file: Union[Path, str]): path = ( Path(os.path.join(Path(__file__).parent.parent.parent, "fixtures", "post.json")) .absolute() @@ -51,7 +51,7 @@ def create_test_message(private_key_file: Union[Path, str] = None): "--channel", channel, "--private-key-file", - private_key_file, + str(private_key_file), ], ) time.sleep(1) @@ -71,7 +71,7 @@ def test_message_amend(account_file: Path): result = runner.invoke( app, - ["message", "amend", message["item_hash"], "--content", '{"content": {"hello": "my bro"}}', "--private-key-file", account_file], + ["message", "amend", message["item_hash"], "--content", '{"content": {"hello": "my bro"}}', "--private-key-file", str(account_file)], ) assert result.exit_code == 0 print(result.stdout) @@ -88,7 +88,7 @@ def test_message_forget(account_file: Path): "forget", result["item_hash"], "--private-key-file", - account_file, + str(account_file), ], ) @@ -130,15 +130,15 @@ def test_message_find(): "message", "find", "--pagination", - pagination, + str(pagination), "--page", - page, + str(page), "--hashes", hashes, "--chains", chains, "--start-date", - start_date, + str(start_date), "--ignore-invalid-messages", ignore_invalid_messages, ], @@ -166,7 +166,7 @@ def test_message_sign(account_file): "--message", message, "--private-key-file", - private_key_file, + str(private_key_file), ], ) @@ -185,7 +185,7 @@ def test_message_sign_stdin(account_file): "message", "sign", "--private-key-file", - private_key_file, + str(private_key_file), ], input=json.dumps(message), ) diff --git a/tests/unit/commands/test_program.py b/tests/unit/commands/test_program.py index b6b85ff5..d65248f2 100644 --- a/tests/unit/commands/test_program.py +++ b/tests/unit/commands/test_program.py @@ -52,7 +52,7 @@ def test_program_upload(account_file: Path): # "--memory", memory, # "--vcpus", vcpus, # "--timeout-seconds", timeout_seconds, - # "--private-key-file", private_key_file, + # "--private-key-file", str(private_key_file), # print_messages, # print_code_message, # print_program_message, @@ -121,7 +121,7 @@ def test_program_update(account_file: Path, item_hash_upload): item_hash, str(path), "--private-key-file", - account_file, + str(account_file), ], ) @@ -140,7 +140,7 @@ def test_program_unpersist(account_file: Path, item_hash_upload): "unpersist", item_hash, "--private-key-file", - private_key_file, + str(private_key_file), "--debug", ], )