Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add grant and revoke tests #5216

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pytests/iroha_cli_tests/common/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Stderr(Enum):
REPETITION = "Repetition"
TOO_LONG = "Name length violation"
FAILED_TO_FIND_DOMAIN = "Failed to find domain"
FAILED_TO_FIND_ACCOUNT = "Failed to find account"
INVALID_CHARACTER = "Failed to parse"
INVALID_TYPE = "should be either `Store` or `Numeric`"
RESERVED_CHARACTER = (
Expand All @@ -27,6 +28,8 @@ class Stderr(Enum):
)
WHITESPACES = "White space not allowed"
INSUFFICIENT_FUNDS = "Not enough quantity to transfer/burn"
NOT_PERMITTED = "Operation is not permitted: This operation is only allowed inside the genesis block"
UNKNOWN_PERMISSION = "Unknown permission"


class ReservedChars(Enum):
Expand Down
7 changes: 4 additions & 3 deletions pytests/iroha_cli_tests/common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

def extract_hash(stdout):
"""
Extracts a SHA-256 hash from the given string.
Extracts SHA-256 hash from the given string.
:param stdout: The string from which to extract the hash.
:return: The extracted hash if found, otherwise None.
Expand Down Expand Up @@ -54,15 +54,16 @@ def read_isi_from_json(file_path):

def write_isi_to_json(isi_data, file_path):
"""
Writes ISI instruction to a JSON file.
Writes ISI instruction to a JSON file, ensuring the directory exists.
:param isi_data: Dictionary with ISI instruction.
:param file_path: Path to save the JSON file.
"""
os.makedirs(os.path.dirname(file_path), exist_ok=True)
if not isinstance(isi_data, list):
isi_data = [isi_data]
with open(file_path, "w", encoding="utf-8") as file:
json.dump(isi_data, file, indent=4)
file.write(json.dumps(isi_data, indent=4, ensure_ascii=False))


def generate_random_string_with_reserved_char():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"Grant": {
"Permission": {
"object": {"name": "CanRegisterDomain", "payload": null},
"destination": "ed0120CE7FA46C9DCE7EA4B125E2E36BDB63EA33073E7590AC92816AE1E861B7048B03@wonderland"
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"Revoke": {
"Permission": {
"object": {"name": "CanRegisterDomain", "payload": null},
"destination": "ed0120CE7FA46C9DCE7EA4B125E2E36BDB63EA33073E7590AC92816AE1E861B7048B03@wonderland"
}
}
}
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[{
"Unregister": {
"Asset": {
[
{
"Unregister": {
"Asset": {
"object": "rose##ed0120CE7FA46C9DCE7EA4B125E2E36BDB63EA33073E7590AC92816AE1E861B7048B03@wonderland"
}
}
}
}]
}
]
57 changes: 42 additions & 15 deletions pytests/iroha_cli_tests/src/iroha_cli/have.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"""

import json
from typing import Any, Callable, Optional

import allure # type: ignore

from . import iroha_cli, iroha, match
from ...common.helpers import extract_hash


def expected_in_actual(expected, actual) -> bool:
def expected_in_actual(expected: Any, actual: Any) -> bool:
"""
Check if the expected result is present in the actual result.
Expand All @@ -18,7 +20,9 @@ def expected_in_actual(expected, actual) -> bool:
:return: True if expected is in actual, False otherwise.
"""
allure.attach(
json.dumps(actual), name="actual", attachment_type=allure.attachment_type.JSON
json.dumps(actual),
name="actual",
attachment_type=allure.attachment_type.JSON,
)
allure.attach(
json.dumps(expected),
Expand All @@ -29,12 +33,12 @@ def expected_in_actual(expected, actual) -> bool:
return expected in actual


def domain(expected, owned_by=None):
def domain(expected: Any, owned_by: Optional[Any] = None) -> bool:
"""
Check if the expected domain is present in the list of domains.
Optionally checks if the domain is owned by a specific owner.
:param expected: The expected domain object.
:param expected: The expected domain identifier.
:param owned_by: The owner of the domain, default is None.
:return: True if the domain is present and owned by the specified owner if provided.
"""
Expand All @@ -47,17 +51,16 @@ def domain_in_domains() -> bool:
domain_info = domains.get(expected)
if not domain_info or domain_info.get("owned_by") != str(owned_by):
return False

return True

return iroha_cli.wait_for(domain_in_domains)


def account(expected):
def account(expected: Any) -> bool:
"""
Check if the expected account is present in the list of accounts.
:param expected: The expected account object.
:param expected: The expected account identifier.
:return: True if the account is present, False otherwise.
"""

Expand All @@ -68,11 +71,11 @@ def account_in_accounts() -> bool:
return iroha_cli.wait_for(account_in_accounts)


def asset_definition(expected):
def asset_definition(expected: Any) -> bool:
"""
Check if the expected asset definition is present in the list of asset definitions.
:param expected: The expected asset definition object.
:param expected: The expected asset definition identifier.
:return: True if the asset definition is present, False otherwise.
"""

Expand All @@ -85,11 +88,11 @@ def asset_definition_in_asset_definitions() -> bool:
return iroha_cli.wait_for(asset_definition_in_asset_definitions)


def asset(expected):
def asset(expected: Any) -> bool:
"""
Check if the expected asset is present in the list of assets.
:param expected: The expected asset object.
:param expected: The expected asset identifier.
:return: True if the asset is present, False otherwise.
"""

Expand All @@ -100,7 +103,7 @@ def asset_in_assets() -> bool:
return iroha_cli.wait_for(asset_in_assets)


def asset_has_quantity(expected_asset_id, expected_quantity):
def asset_has_quantity(expected_asset_id: Any, expected_quantity: str) -> bool:
"""
Check if the expected asset quantity is present in the list of assets.
Expand All @@ -116,7 +119,9 @@ def check_quantity() -> bool:
actual_quantity = None
for asset_item in assets:
if asset_item == expected_asset_id:
actual_quantity = assets.get(expected_asset_id, {})["value"]["Numeric"]
actual_quantity = (
assets.get(expected_asset_id, {}).get("value", {}).get("Numeric")
)
break
if actual_quantity is None:
raise ValueError(f"Asset with ID {expected_asset_id} not found.")
Expand All @@ -137,11 +142,33 @@ def check_quantity() -> bool:
return iroha_cli.wait_for(check_quantity)


def error(expected):
def error(expected: str) -> bool:
"""
Check if the expected error is present in the iroha_cli stderr.
:param expected: The expected error message.
:return: True if the error is present, False otherwise.
"""
return match.iroha_cli_have_error(expected=expected, actual=iroha_cli.stderr)
stderr = iroha_cli.stderr
if stderr is None:
allure.attach(
"stderr is None",
name="actual",
attachment_type=allure.attachment_type.TEXT,
)
allure.attach(
expected,
name="expected",
attachment_type=allure.attachment_type.TEXT,
)
return False
return match.iroha_cli_have_error(expected=expected, actual=stderr)


def transaction_hash() -> str:
"""
Extract and return the transaction hash from iroha_cli.
:return: The transaction hash as a string.
"""
return extract_hash(iroha_cli.transaction_hash)
Loading
Loading