diff --git a/tests/slots/pyqt/widgets/test_account.py b/tests/slots/pyqt/widgets/test_account.py index fb379e062b47820dff12471ed42795fa38740d2b..1dc23b91ed78e745ece4aedc96f890f67f98996e 100644 --- a/tests/slots/pyqt/widgets/test_account.py +++ b/tests/slots/pyqt/widgets/test_account.py @@ -14,21 +14,152 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. from PyQt5.QtCore import QMutex from pytestqt.qtbot import QtBot +from substrateinterface import Keypair, KeypairType from tikka.domains.application import Application from tikka.domains.entities.account import Account -from tikka.slots.pyqt.entities.worker import AsyncQWorker from tikka.slots.pyqt.widgets.account import AccountWidget +from tikka.slots.pyqt.windows.account_unlock import AccountUnlockWindow +from tikka.slots.pyqt.windows.transfer import TransferWindow -def test_account( - qtbot: QtBot, application: Application, account: Account, monkeypatch +def call_count(count): + """ + Decorator to add the call_count attribute to a function + + :param count: Set call_count value to count + :return: + """ + + def wrapper(function): + function.call_count = count + return function + + return wrapper + + +def test_account_transfer_button_on_locked_account( + qtbot: QtBot, + application: Application, + account: Account, + wallet_mnemonic: str, + wallet_password: str, + monkeypatch, ): # pylint: disable=missing-function-docstring application.accounts.add(account) - monkeypatch.setattr(AsyncQWorker, "start", lambda *args: None) + # create wallet + keypair = Keypair.create_from_mnemonic( + mnemonic=wallet_mnemonic, + language_code="en", + crypto_type=KeypairType.SR25519, + ss58_format=application.currencies.get_current().ss58_format, + ) + wallet = application.wallets.create(keypair, wallet_password) + application.wallets.add(wallet) widget = AccountWidget(application, account, QMutex()) qtbot.addWidget(widget) assert widget.addressLabel.text() == account.address + + # TEST ACCOUNT UNLOCK IS SUCCESSFUL + + # if unlock window is opened, count call, unlock account and accept window + @call_count(0) + def unlock_window_success_exec_mock(dialog): + assert isinstance(dialog, AccountUnlockWindow) + unlock_window_success_exec_mock.call_count = 1 + dialog.application.accounts.unlock(dialog.account, wallet_password) + dialog.accept() + + monkeypatch.setattr(AccountUnlockWindow, "exec_", unlock_window_success_exec_mock) + + # if transfer window is opened, count call, accept window + @call_count(0) + def transfer_window_exec_mock(dialog): + assert isinstance(dialog, TransferWindow) + transfer_window_exec_mock.call_count = 1 + dialog.accept() + + monkeypatch.setattr(TransferWindow, "exec_", transfer_window_exec_mock) + + widget.transferButton.click() + + # check if unlock window is called, then the transfer window if the account is unlocked + assert unlock_window_success_exec_mock.call_count == 1 + assert transfer_window_exec_mock.call_count == 1 + + # reset account to locked status + application.accounts.lock(account) + + # TEST ACCOUNT UNLOCK IS CANCELED + transfer_window_exec_mock.call_count = 0 + + # if unlock window is opened, count call, unlock account and accept window + @call_count(0) + def unlock_window_cancel_exec_mock(dialog): + assert isinstance(dialog, AccountUnlockWindow) + unlock_window_cancel_exec_mock.call_count = 1 + dialog.reject() + + monkeypatch.setattr(AccountUnlockWindow, "exec_", unlock_window_cancel_exec_mock) + + widget.transferButton.click() + + # check if unlock window is called, then the transfer window if the account is unlocked + assert unlock_window_cancel_exec_mock.call_count == 1 + assert transfer_window_exec_mock.call_count == 0 + + +def test_account_transfer_button_on_unlocked_account( + qtbot: QtBot, + application: Application, + account: Account, + wallet_mnemonic: str, + wallet_password: str, + monkeypatch, +): # pylint: disable=missing-function-docstring + application.accounts.add(account) + + # create wallet + keypair = Keypair.create_from_mnemonic( + mnemonic=wallet_mnemonic, + language_code="en", + crypto_type=KeypairType.SR25519, + ss58_format=application.currencies.get_current().ss58_format, + ) + wallet = application.wallets.create(keypair, wallet_password) + application.wallets.add(wallet) + + # unlock account + application.accounts.unlock(account, wallet_password) + + widget = AccountWidget(application, account, QMutex()) + qtbot.addWidget(widget) + + assert widget.addressLabel.text() == account.address + + # if unlock window is opened, count call, unlock account and accept window + @call_count(0) + def unlock_window_exec_mock(dialog): + assert isinstance(dialog, AccountUnlockWindow) + unlock_window_exec_mock.call_count = 1 + dialog.accept() + + monkeypatch.setattr(AccountUnlockWindow, "exec_", unlock_window_exec_mock) + + # if transfer window is opened, count call, accept window + @call_count(0) + def transfer_window_exec_mock(dialog): + assert isinstance(dialog, TransferWindow) + transfer_window_exec_mock.call_count = 1 + dialog.accept() + + monkeypatch.setattr(TransferWindow, "exec_", transfer_window_exec_mock) + + widget.transferButton.click() + + # check if unlock window is called (no on unlocked account), then check the transfer window + assert unlock_window_exec_mock.call_count == 0 + assert transfer_window_exec_mock.call_count == 1 diff --git a/tikka/slots/pyqt/widgets/account.py b/tikka/slots/pyqt/widgets/account.py index b684ef0f0e53732b18a72a7d66249e1f43b4e144..a59e69b88ea898b563558571d01e9616442d6a07 100644 --- a/tikka/slots/pyqt/widgets/account.py +++ b/tikka/slots/pyqt/widgets/account.py @@ -105,13 +105,15 @@ class AccountWidget(QWidget, Ui_AccountWidget): :return: """ - # account wallet is unlocked ? + # account wallet is not unlocked ? if not self.application.wallets.is_unlocked(self.account.address): # open unlock account window AccountUnlockWindow(self.application, self.account, self.parent()).exec_() - parent = self.parent() - TransferWindow(self.application, self.account, self.mutex, parent).exec_() + if self.application.wallets.is_unlocked(self.account.address): + TransferWindow( + self.application, self.account, self.mutex, self.parent() + ).exec_() def _on_transfer_sent(self): """