|
1 | 1 | """ Test that exceptions are translated/rendered correctly """
|
| 2 | +import pytest |
| 3 | + |
2 | 4 | from ledgerblue.commException import CommException
|
3 | 5 |
|
4 | 6 | from ledgereth.comms import dongle_send
|
|
14 | 16 |
|
15 | 17 | def test_comms_exception(yield_dongle):
|
16 | 18 | with yield_dongle(exception=CommException("TEST", 0xFFFF, 0x00)) as dongle:
|
17 |
| - try: |
| 19 | + with pytest.raises(LedgerError) as err: |
18 | 20 | dongle_send(dongle, "GET_CONFIGURATION")
|
19 |
| - assert False |
20 |
| - except Exception as err: |
21 |
| - assert isinstance(err, LedgerError), f"Unexpected exception: {type(err)}" |
| 21 | + |
| 22 | + assert "Unexpected error" in str(err.value) |
22 | 23 |
|
23 | 24 |
|
24 | 25 | def test_comms_exception_invalid(yield_dongle):
|
25 | 26 | with yield_dongle(exception=CommException("TEST", 0x6A80, 0x00)) as dongle:
|
26 |
| - try: |
| 27 | + with pytest.raises(LedgerInvalid) as err: |
27 | 28 | dongle_send(dongle, "SIGN_TX_FIRST_DATA")
|
28 |
| - assert False |
29 |
| - except Exception as err: |
30 |
| - assert isinstance(err, LedgerInvalid), f"Unexpected exception: {type(err)}" |
| 29 | + |
| 30 | + assert "Invalid data" in str(err.value) |
31 | 31 |
|
32 | 32 |
|
33 | 33 | def test_comms_not_found(yield_dongle):
|
34 | 34 | with yield_dongle(exception=CommException("TEST", 0x6F00, 0x00)) as dongle:
|
35 |
| - try: |
| 35 | + with pytest.raises(LedgerNotFound) as err: |
36 | 36 | dongle_send(dongle, "GET_DEFAULT_ADDRESS_NO_CONFIRM")
|
37 |
| - assert False |
38 |
| - except Exception as err: |
39 |
| - assert isinstance(err, LedgerNotFound), f"Unexpected exception: {type(err)}" |
| 37 | + |
| 38 | + assert "Unable to find Ledger" in str(err.value) |
40 | 39 |
|
41 | 40 |
|
42 | 41 | def test_comms_exception_locked(yield_dongle):
|
43 | 42 | with yield_dongle(exception=CommException("TEST", 0x6B0C, 0x00)) as dongle:
|
44 |
| - try: |
| 43 | + with pytest.raises(LedgerLocked) as err: |
45 | 44 | dongle_send(dongle, "SIGN_TX_FIRST_DATA")
|
46 |
| - assert False |
47 |
| - except Exception as err: |
48 |
| - assert isinstance(err, LedgerLocked), f"Unexpected exception: {type(err)}" |
| 45 | + |
| 46 | + assert "locked" in str(err.value) |
49 | 47 |
|
50 | 48 |
|
51 | 49 | def test_comms_app_not_open(yield_dongle):
|
52 | 50 | with yield_dongle(exception=CommException("TEST", 0x6804, 0x00)) as dongle:
|
53 |
| - try: |
| 51 | + with pytest.raises(LedgerAppNotOpened) as err: |
54 | 52 | dongle_send(dongle, "SIGN_TX_FIRST_DATA")
|
55 |
| - assert False |
56 |
| - except Exception as err: |
57 |
| - assert isinstance( |
58 |
| - err, LedgerAppNotOpened |
59 |
| - ), f"Unexpected exception: {type(err)}" |
| 53 | + |
| 54 | + assert "Ethereum app" in str(err.value) |
60 | 55 |
|
61 | 56 | with yield_dongle(exception=CommException("TEST", 0x6D00, 0x00)) as dongle:
|
62 |
| - try: |
| 57 | + with pytest.raises(LedgerAppNotOpened) as err: |
63 | 58 | dongle_send(dongle, "SIGN_TX_FIRST_DATA")
|
64 |
| - assert False |
65 |
| - except Exception as err: |
66 |
| - assert isinstance( |
67 |
| - err, LedgerAppNotOpened |
68 |
| - ), f"Unexpected exception: {type(err)}" |
| 59 | + |
| 60 | + assert "Ethereum app" in str(err.value) |
69 | 61 |
|
70 | 62 | with yield_dongle(exception=CommException("TEST", 0x6D02, 0x00)) as dongle:
|
71 |
| - try: |
| 63 | + with pytest.raises(LedgerAppNotOpened) as err: |
72 | 64 | dongle_send(dongle, "SIGN_TX_FIRST_DATA")
|
73 |
| - assert False |
74 |
| - except Exception as err: |
75 |
| - assert isinstance( |
76 |
| - err, LedgerAppNotOpened |
77 |
| - ), f"Unexpected exception: {type(err)}" |
| 65 | + |
| 66 | + assert "Ethereum app" in str(err.value) |
78 | 67 |
|
79 | 68 |
|
80 | 69 | def test_comms_user_cancel(yield_dongle):
|
81 | 70 | with yield_dongle(exception=CommException("TEST", 0x6982, 0x00)) as dongle:
|
82 |
| - try: |
| 71 | + with pytest.raises(LedgerCancel) as err: |
83 | 72 | dongle_send(dongle, "SIGN_TX_FIRST_DATA")
|
84 |
| - assert False |
85 |
| - except Exception as err: |
86 |
| - assert isinstance(err, LedgerCancel), f"Unexpected exception: {type(err)}" |
| 73 | + |
| 74 | + assert "cancelled" in str(err.value) |
87 | 75 |
|
88 | 76 | with yield_dongle(exception=CommException("TEST", 0x6985, 0x00)) as dongle:
|
89 |
| - try: |
| 77 | + with pytest.raises(LedgerCancel) as err: |
90 | 78 | dongle_send(dongle, "SIGN_TX_FIRST_DATA")
|
91 |
| - assert False |
92 |
| - except Exception as err: |
93 |
| - assert isinstance(err, LedgerCancel), f"Unexpected exception: {type(err)}" |
| 79 | + |
| 80 | + assert "cancelled" in str(err.value) |
94 | 81 |
|
95 | 82 |
|
96 | 83 | def test_comms_unknown_error(yield_dongle):
|
97 | 84 | with yield_dongle(exception=CommException("TEST", 0xEEEE, 0x00)) as dongle:
|
98 |
| - try: |
| 85 | + with pytest.raises(LedgerError) as err: |
99 | 86 | dongle_send(dongle, "SIGN_TX_FIRST_DATA")
|
100 |
| - assert False |
101 |
| - except Exception as err: |
102 |
| - assert "UNKNOWN" in str(err) |
103 |
| - assert isinstance(err, LedgerError), f"Unexpected exception: {type(err)}" |
| 87 | + |
| 88 | + assert "UNKNOWN" in str(err.value) |
0 commit comments