From 841a61fc305ed12852f4193de8dc1a4c944291fe Mon Sep 17 00:00:00 2001 From: Vladimir Avdeev Date: Thu, 10 Nov 2022 16:29:52 +0300 Subject: [PATCH] Add unit tests for utils Signed-off-by: Vladimir Avdeev --- src/neofs_testlib/utils/wallet.py | 2 +- tests/test_ulils.py | 74 +++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/test_ulils.py diff --git a/src/neofs_testlib/utils/wallet.py b/src/neofs_testlib/utils/wallet.py index 656c41f..9cd248b 100644 --- a/src/neofs_testlib/utils/wallet.py +++ b/src/neofs_testlib/utils/wallet.py @@ -34,4 +34,4 @@ def get_last_address_from_wallet(wallet_path: str, wallet_password: str): wallet = neo3_wallet.Wallet.from_json(json.load(wallet_file), password=wallet_password) address = wallet.accounts[-1].address logger.info(f"got address: {address}") - return address \ No newline at end of file + return address diff --git a/tests/test_ulils.py b/tests/test_ulils.py new file mode 100644 index 0000000..50cf5fc --- /dev/null +++ b/tests/test_ulils.py @@ -0,0 +1,74 @@ +import json +import os +from unittest import TestCase +from uuid import uuid4 + +from neo3 import wallet as neo3_wallet + +from neofs_testlib.utils import converters, wallet + + +class TestUtils(TestCase): + def test_converters_str_to_ascii_hex(self): + source_str = "" + result_str = "" + self.assertEqual(converters.str_to_ascii_hex(source_str), result_str) + + source_str = '"test_data" f0r ^convert*' + result_str = "22746573745f646174612220663072205e636f6e766572742a" + self.assertEqual(converters.str_to_ascii_hex(source_str), result_str) + + source_str = "" + result_bytes = b"" + self.assertEqual(converters.ascii_hex_to_str(source_str), result_bytes) + + source_str = "22746573745f646174612220663072205e636f6e766572742a" + result_bytes = b'"test_data" f0r ^convert*' + self.assertEqual(converters.ascii_hex_to_str(source_str), result_bytes) + + def test_process_b64_bytearray_reverse(self): + source_str = "" + result_bytes = b"" + self.assertEqual(converters.process_b64_bytearray_reverse(source_str), result_bytes) + + source_str = "InRlc3RfZGF0YSIgZjByIF5jb252ZXJ0Kg==" + result_bytes = b"2a747265766e6f635e207230662022617461645f7473657422" + self.assertEqual(converters.process_b64_bytearray_reverse(source_str), result_bytes) + + def test_process_b64_bytearray(self): + source_str = "" + result_bytes = b"" + self.assertEqual(converters.process_b64_bytearray(source_str), result_bytes) + + source_str = "InRlc3RfZGF0YSIgZjByIF5jb252ZXJ0Kg==" + result_bytes = b"22746573745f646174612220663072205e636f6e766572742a" + self.assertEqual(converters.process_b64_bytearray(source_str), result_bytes) + + def test_contract_hash_to_address(self): + source_str = "d01a381aae45f1ed181db9d554cc5ccc69c69f4e" + result_str = "NT5hJ5peVmvYdZCsFKUM5MTcEGw5TB4k89" + self.assertEqual(converters.contract_hash_to_address(source_str), result_str) + + def test_init_wallet(self): + wallet_file_path = f"{str(uuid4())}.json" + for password in ("", "password"): + wrong_password = "wrong_password" + wallet.init_wallet(wallet_file_path, password) + self.assertTrue(os.path.exists(wallet_file_path)) + with open(wallet_file_path, "r") as wallet_file: + neo3_wallet.Wallet.from_json(json.load(wallet_file), password=password) + with self.assertRaises(Exception): + neo3_wallet.Wallet.from_json(json.load(wallet_file), password=wrong_password) + os.unlink(wallet_file_path) + + def test_get_last_address_from_wallet(self): + wallet_file_path = f"{str(uuid4())}.json" + for password in ("", "password"): + wallet.init_wallet(wallet_file_path, password) + with open(wallet_file_path, "r") as wallet_file: + wlt = neo3_wallet.Wallet.from_json(json.load(wallet_file), password=password) + last_address = wlt.accounts[-1].address + self.assertEqual( + wallet.get_last_address_from_wallet(wallet_file_path, password), last_address + ) + os.unlink(wallet_file_path)