Skip to content
Snippets Groups Projects
Commit d8b175d5 authored by Vincent Texier's avatar Vincent Texier
Browse files

[fix] fix transfer window opened even if unlock window canceled

parent d000bc14
No related branches found
No related tags found
No related merge requests found
Pipeline #18391 waiting for manual action
......@@ -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
......@@ -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):
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment