From 80225df9199104db4c8e7b0382c5e53acef34b0e Mon Sep 17 00:00:00 2001
From: kmochocki <kmochocki@syncad.com>
Date: Mon, 3 Mar 2025 16:14:23 +0000
Subject: [PATCH] Add tests for detectable errors

---
 .../interface/test_errors/__init__.py         |  0
 .../interface/test_errors/conftest.py         | 40 +++++++++++++++++++
 .../test_errors/test_invalid_password.py      | 27 +++++++++++++
 .../test_errors/test_invalid_private_key.py   | 28 +++++++++++++
 .../test_errors/test_invalid_wallet.py        | 37 +++++++++++++++++
 .../test_errors/test_missing_stm_prefix.py    | 23 +++++++++++
 .../test_no_wallet_with_such_name.py          | 23 +++++++++++
 .../test_errors/test_not_existing_key.py      | 36 +++++++++++++++++
 ...st_wallet_with_such_name_already_exists.py | 34 ++++++++++++++++
 9 files changed, 248 insertions(+)
 create mode 100644 tests/beekeepy_test/interface/test_errors/__init__.py
 create mode 100644 tests/beekeepy_test/interface/test_errors/conftest.py
 create mode 100644 tests/beekeepy_test/interface/test_errors/test_invalid_password.py
 create mode 100644 tests/beekeepy_test/interface/test_errors/test_invalid_private_key.py
 create mode 100644 tests/beekeepy_test/interface/test_errors/test_invalid_wallet.py
 create mode 100644 tests/beekeepy_test/interface/test_errors/test_missing_stm_prefix.py
 create mode 100644 tests/beekeepy_test/interface/test_errors/test_no_wallet_with_such_name.py
 create mode 100644 tests/beekeepy_test/interface/test_errors/test_not_existing_key.py
 create mode 100644 tests/beekeepy_test/interface/test_errors/test_wallet_with_such_name_already_exists.py

diff --git a/tests/beekeepy_test/interface/test_errors/__init__.py b/tests/beekeepy_test/interface/test_errors/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/tests/beekeepy_test/interface/test_errors/conftest.py b/tests/beekeepy_test/interface/test_errors/conftest.py
new file mode 100644
index 00000000..8bd6d38e
--- /dev/null
+++ b/tests/beekeepy_test/interface/test_errors/conftest.py
@@ -0,0 +1,40 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+import pytest
+
+from beekeepy import Beekeeper
+
+if TYPE_CHECKING:
+    from typing import Iterator
+
+    from local_tools.beekeepy.models import SettingsFactory
+
+    from beekeepy import Session, UnlockedWallet, Wallet
+
+
+@pytest.fixture()
+def beekeeper(settings: SettingsFactory) -> Iterator[Beekeeper]:
+    with Beekeeper.factory(settings=settings()) as bk:
+        yield bk
+
+
+@pytest.fixture()
+def session(beekeeper: Beekeeper) -> Iterator[Session]:
+    with beekeeper.create_session() as ss:
+        yield ss
+
+
+@pytest.fixture()
+def wallet(session: Session) -> Wallet:
+    with session.create_wallet(name="wallet", password="wallet"):  # noqa: S106
+        pass
+
+    return session.open_wallet(name="wallet")
+
+
+@pytest.fixture()
+def unlocked_wallet(wallet: Wallet) -> Iterator[UnlockedWallet]:
+    with wallet.unlock(password="wallet") as uw:  # noqa: S106
+        yield uw
diff --git a/tests/beekeepy_test/interface/test_errors/test_invalid_password.py b/tests/beekeepy_test/interface/test_errors/test_invalid_password.py
new file mode 100644
index 00000000..e9641f4b
--- /dev/null
+++ b/tests/beekeepy_test/interface/test_errors/test_invalid_password.py
@@ -0,0 +1,27 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+import pytest
+
+from beekeepy.exceptions.detectable import InvalidPasswordError
+from helpy.exceptions import InvalidPasswordError as HelpyInvalidPasswordError
+
+if TYPE_CHECKING:
+    from beekeepy import Wallet
+
+
+def test_basic_detection() -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(InvalidPasswordError), InvalidPasswordError(wallet_name="wallet"):
+        raise HelpyInvalidPasswordError(
+            url="",
+            request="",
+            request_id=None,
+        )
+
+
+def test_test_invalid_private_key(wallet: Wallet) -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(InvalidPasswordError):
+        wallet.unlock(password="invalid")  # noqa: S106
diff --git a/tests/beekeepy_test/interface/test_errors/test_invalid_private_key.py b/tests/beekeepy_test/interface/test_errors/test_invalid_private_key.py
new file mode 100644
index 00000000..e1207a07
--- /dev/null
+++ b/tests/beekeepy_test/interface/test_errors/test_invalid_private_key.py
@@ -0,0 +1,28 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+import pytest
+
+from beekeepy.exceptions.detectable import InvalidPrivateKeyError
+from helpy.exceptions import ErrorInResponseError
+
+if TYPE_CHECKING:
+    from beekeepy import UnlockedWallet
+
+
+def test_basic_detection() -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(InvalidPrivateKeyError), InvalidPrivateKeyError(wifs="aaa"):
+        raise ErrorInResponseError(
+            url="",
+            request="",
+            request_id=None,
+            response="Assert Exception:false: Key can't be constructed",
+        )
+
+
+def test_invalid_private_key(unlocked_wallet: UnlockedWallet) -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(InvalidPrivateKeyError):
+        unlocked_wallet.import_key(private_key="key" * 17)
diff --git a/tests/beekeepy_test/interface/test_errors/test_invalid_wallet.py b/tests/beekeepy_test/interface/test_errors/test_invalid_wallet.py
new file mode 100644
index 00000000..4cd7f997
--- /dev/null
+++ b/tests/beekeepy_test/interface/test_errors/test_invalid_wallet.py
@@ -0,0 +1,37 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+import pytest
+
+from beekeepy.exceptions.detectable import InvalidWalletError
+from helpy.exceptions import ErrorInResponseError
+
+if TYPE_CHECKING:
+    from beekeepy import Session
+
+
+@pytest.mark.parametrize(
+    "message",
+    [
+        "Name of wallet is incorrect. Is empty.",
+        "Name of wallet is incorrect. Name: #####. Only alphanumeric and '._-@' chars are allowed",
+        "Name of wallet is incorrect. Name: #####. File creation with given name is impossible.",
+    ],
+    ids=[1, 2, 3],
+)
+def test_basic_detection(message: str) -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(InvalidWalletError), InvalidWalletError(wallet_name="#####"):
+        raise ErrorInResponseError(
+            url="",
+            request="",
+            request_id=None,
+            response=message,
+        )
+
+
+def test_not_existing_key(session: Session) -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(InvalidWalletError):
+        session.create_wallet(name="####", password="wallet")  # noqa: S106
diff --git a/tests/beekeepy_test/interface/test_errors/test_missing_stm_prefix.py b/tests/beekeepy_test/interface/test_errors/test_missing_stm_prefix.py
new file mode 100644
index 00000000..0f67e86e
--- /dev/null
+++ b/tests/beekeepy_test/interface/test_errors/test_missing_stm_prefix.py
@@ -0,0 +1,23 @@
+from __future__ import annotations
+
+import pytest
+
+from beekeepy.exceptions.detectable import MissingSTMPrefixError
+from helpy.exceptions import ErrorInResponseError
+
+
+def test_basic_detection() -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(MissingSTMPrefixError), MissingSTMPrefixError(
+        public_key="8Ya14mz5HiZ3JiEhoad4uoSjuK17fMJgJVVuY4991qrf6tbNdH"
+    ):
+        raise ErrorInResponseError(
+            url="",
+            request="",
+            request_id=None,
+            response=(
+                "Assert Exception:source.substr( 0, prefix.size() ) == prefix: "
+                "public key requires STM prefix, but was given "
+                "`8Ya14mz5HiZ3JiEhoad4uoSjuK17fMJgJVVuY4991qrf6tbNdH`"
+            ),
+        )
diff --git a/tests/beekeepy_test/interface/test_errors/test_no_wallet_with_such_name.py b/tests/beekeepy_test/interface/test_errors/test_no_wallet_with_such_name.py
new file mode 100644
index 00000000..879a467b
--- /dev/null
+++ b/tests/beekeepy_test/interface/test_errors/test_no_wallet_with_such_name.py
@@ -0,0 +1,23 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+import pytest
+
+from beekeepy.exceptions.detectable import NoWalletWithSuchNameError
+from helpy.exceptions import UnableToOpenWalletError
+
+if TYPE_CHECKING:
+    from beekeepy import Session
+
+
+def test_basic_detection() -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(NoWalletWithSuchNameError), NoWalletWithSuchNameError(wallet_name="some-wallet"):
+        raise UnableToOpenWalletError(url="", request="", request_id=None)
+
+
+def test_no_wallet_with_such_name(session: Session) -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(NoWalletWithSuchNameError):
+        session.open_wallet(name="not-existing")
diff --git a/tests/beekeepy_test/interface/test_errors/test_not_existing_key.py b/tests/beekeepy_test/interface/test_errors/test_not_existing_key.py
new file mode 100644
index 00000000..1defc198
--- /dev/null
+++ b/tests/beekeepy_test/interface/test_errors/test_not_existing_key.py
@@ -0,0 +1,36 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+import pytest
+
+from beekeepy.exceptions.detectable import NotExistingKeyError
+from helpy.exceptions import ErrorInResponseError
+
+if TYPE_CHECKING:
+    from beekeepy import Session, UnlockedWallet
+
+
+@pytest.mark.parametrize("wallet_name", ["wallet", None])
+@pytest.mark.parametrize(
+    "message",
+    ["Assert Exception:false: Key not in wallet", "Assert Exception:false: Public key aaa not found in wallet wallet"],
+)
+def test_basic_detection(message: str, wallet_name: str | None) -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(NotExistingKeyError), NotExistingKeyError(public_key="aaa", wallet_name=wallet_name):
+        raise ErrorInResponseError(
+            url="",
+            request="",
+            request_id=None,
+            response=message,
+        )
+
+
+def test_not_existing_key(session: Session, unlocked_wallet: UnlockedWallet) -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(NotExistingKeyError):
+        unlocked_wallet.sign_digest(sig_digest="aaa", key="STM8Ya14mz5HiZ3JiEhoad4uoSjuK17fMJgJVVuY4991qrf6tbNdH")
+
+    with pytest.raises(NotExistingKeyError):
+        session.sign_digest(sig_digest="aaa", key="STM8Ya14mz5HiZ3JiEhoad4uoSjuK17fMJgJVVuY4991qrf6tbNdH")
diff --git a/tests/beekeepy_test/interface/test_errors/test_wallet_with_such_name_already_exists.py b/tests/beekeepy_test/interface/test_errors/test_wallet_with_such_name_already_exists.py
new file mode 100644
index 00000000..6a5a13ae
--- /dev/null
+++ b/tests/beekeepy_test/interface/test_errors/test_wallet_with_such_name_already_exists.py
@@ -0,0 +1,34 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+import pytest
+
+from beekeepy.exceptions import WalletWithSuchNameAlreadyExistsError
+from helpy.exceptions import ErrorInResponseError
+
+if TYPE_CHECKING:
+    from beekeepy import Session
+
+
+def test_basic_detection() -> None:
+    # ARRANGE, ACT & ASSERT
+    with pytest.raises(WalletWithSuchNameAlreadyExistsError), WalletWithSuchNameAlreadyExistsError(wallet_name="aaa"):
+        raise ErrorInResponseError(
+            url="",
+            request="",
+            request_id=None,
+            response=(
+                "Assert Exception:!fc::exists( wallet_file_name ): "
+                "Wallet with name: 'aaa' already exists at /some/path/to/aaa.wallet"
+            ),
+        )
+
+
+def test_wallet_with_such_name_already_exists(session: Session) -> None:
+    # ARRANGE
+    session.create_wallet(name="aaa", password="wallet")  # noqa: S106
+
+    # ACT & ASSERT
+    with pytest.raises(WalletWithSuchNameAlreadyExistsError):
+        session.create_wallet(name="aaa", password="wallet")  # noqa: S106
-- 
GitLab