tests: make a fixture out of client

also implement Cancel tests
pull/25/head
matejcik 6 years ago
parent 7673ebf952
commit 269eaa298f

@ -16,28 +16,22 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import os
from . import conftest
from trezorlib import coins
from trezorlib import tx_api
from trezorlib.client import TrezorClientDebugLink
from trezorlib.transport import get_transport
tests_dir = os.path.dirname(os.path.abspath(__file__))
tx_api.cache_dir = os.path.join(tests_dir, '../txcache')
def get_device():
path = os.environ.get('TREZOR_PATH')
return get_transport(path)
class TrezorTest:
def setup_method(self, method):
wirelink = get_device()
wirelink = conftest.get_device()
debuglink = wirelink.find_debug()
self.client = TrezorClientDebugLink(wirelink)
self.client.set_debuglink(debuglink)

@ -1,12 +1,19 @@
import functools
import os
import pytest
from . import common
from trezorlib.client import TrezorClient
from trezorlib import log
from trezorlib.transport import get_transport
from trezorlib.client import TrezorClient, TrezorClientDebugLink
from trezorlib import log, coins
def get_device():
path = os.environ.get('TREZOR_PATH')
return get_transport(path)
def device_version():
device = common.get_device()
device = get_device()
if not device:
raise RuntimeError()
client = TrezorClient(device)
@ -16,11 +23,38 @@ def device_version():
return 1
try:
TREZOR_VERSION = device_version()
except:
raise
TREZOR_VERSION = None
TREZOR_VERSION = device_version()
@pytest.fixture(scope="function")
def client():
wirelink = get_device()
debuglink = wirelink.find_debug()
client = TrezorClientDebugLink(wirelink)
client.set_debuglink(debuglink)
client.set_tx_api(coins.tx_api['Bitcoin'])
client.wipe_device()
client.transport.session_begin()
yield client
client.transport.session_end()
def setup_client(mnemonic=None, pin='', passphrase=False):
if mnemonic is None:
mnemonic = ' '.join(['all'] * 12)
if pin is True:
pin = '1234'
def client_decorator(function):
@functools.wraps(function)
def wrapper(client, *args, **kwargs):
client.load_device_by_mnemonic(mnemonic=mnemonic, pin=pin, passphrase_protection=passphrase, label='test', language='english')
return function(client, *args, **kwargs)
return wrapper
return client_decorator
def pytest_configure(config):

@ -0,0 +1,27 @@
import pytest
from .conftest import setup_client
import trezorlib.messages as m
@setup_client()
@pytest.mark.parametrize("message", [
m.Ping(message="hello", button_protection=True),
m.GetAddress(
address_n=[0],
coin_name="Bitcoin",
script_type=m.InputScriptType.SPENDADDRESS,
show_display=True
),
])
def test_cancel_ping(client, message):
resp = client.call_raw(message)
assert isinstance(resp, m.ButtonRequest)
client.transport.write(m.ButtonAck())
client.transport.write(m.Cancel())
resp = client.transport.read()
assert isinstance(resp, m.Failure)
assert resp.code == m.FailureType.ActionCancelled
Loading…
Cancel
Save