Skip to content

Commit

Permalink
increase tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
odudex committed Jul 1, 2024
1 parent a833a24 commit fef4961
Show file tree
Hide file tree
Showing 6 changed files with 1,852 additions and 1,792 deletions.
3,492 changes: 1,746 additions & 1,746 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def dock(monkeypatch, mp_modules):
import sys

monkeypatch.setitem(sys.modules, "board", board_dock())
monkeypatch.setitem(sys.modules, "pmu", None)
reset_krux_modules()


Expand Down
21 changes: 20 additions & 1 deletion tests/pages/home_pages/test_wallet_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ def test_wallet(mocker, m5stickv, tdata):
None,
[BUTTON_ENTER, BUTTON_ENTER, BUTTON_ENTER],
),
# 1 Load, from SD card, good data, accept
(
False,
tdata.SINGLESIG_12_WORD_KEY,
tdata.SPECTER_SINGLESIG_WALLET_DATA,
None,
[BUTTON_ENTER, BUTTON_PAGE, BUTTON_ENTER, BUTTON_ENTER],
),
]

num = 0
Expand All @@ -135,6 +143,7 @@ def test_wallet(mocker, m5stickv, tdata):

ctx = create_ctx(mocker, case[4], wallet, case[3])
wallet_descriptor = WalletDescriptor(ctx)
mocker.patch.object(wallet_descriptor, "has_sd_card", return_value=True)
mocker.patch.object(
wallet_descriptor, "capture_qr_code", new=lambda: (case[2], FORMAT_PMOFN)
)
Expand All @@ -146,13 +155,23 @@ def test_wallet(mocker, m5stickv, tdata):
mocker.spy(wallet_descriptor, "capture_qr_code")
mocker.spy(wallet_descriptor, "display_wallet")

# Mock SD card descriptor loading
if case[4][:3] == [BUTTON_ENTER, BUTTON_PAGE, BUTTON_ENTER]:
mock_utils = mocker.patch("krux.pages.utils.Utils")
mock_utils.return_value.load_file.return_value = (None, case[2])

wallet_descriptor.wallet()

if case[0]:
wallet_descriptor.display_wallet.assert_called_once()
else:
if case[4][0] == BUTTON_ENTER:
# If accepted the message and choose to load from camera
if case[4][:2] == [BUTTON_ENTER, BUTTON_ENTER]:
wallet_descriptor.capture_qr_code.assert_called_once()
if case[2] is not None and case[2] != "{}":
wallet_descriptor.display_wallet.assert_called_once()
# If accepted the message and choose to load from SD
elif case[4][:3] == [BUTTON_ENTER, BUTTON_PAGE, BUTTON_ENTER]:
if case[2] is not None and case[2] != "{}":
wallet_descriptor.display_wallet.assert_called_once()
assert ctx.input.wait_for_button.call_count == len(case[4])
58 changes: 35 additions & 23 deletions tests/pages/test_tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from unittest.mock import patch
from ..shared_mocks import mock_context
from . import create_ctx


SEEDS_JSON = """{
Expand Down Expand Up @@ -31,15 +31,14 @@ def mock_file_operations(mocker):
def test_tools_menu(m5stickv, mocker):
from krux.pages.tools import Tools
from krux.pages import MENU_EXIT
from krux.input import BUTTON_ENTER, BUTTON_PAGE, BUTTON_PAGE_PREV
from krux.input import BUTTON_ENTER, BUTTON_PAGE_PREV

BTN_SEQUENCE = [
BUTTON_PAGE_PREV, # Go to Back
BUTTON_ENTER, # Leave tools menu
]

ctx = mock_context(mocker)
ctx.input.wait_for_button = mocker.MagicMock(side_effect=BTN_SEQUENCE)
ctx = create_ctx(mocker, BTN_SEQUENCE)
while True:
if Tools(ctx).run() == MENU_EXIT:
break
Expand All @@ -58,30 +57,26 @@ def test_delete_mnemonic_from_flash(m5stickv, mocker):
BUTTON_ENTER, # Leave
]

ctx = mock_context(mocker)
ctx.input.wait_for_button = mocker.MagicMock(side_effect=BTN_SEQUENCE)
ctx = create_ctx(mocker, BTN_SEQUENCE)
with patch("krux.encryption.open", new=mocker.mock_open(read_data=SEEDS_JSON)) as m:
tool = Tools(ctx)
tool.rm_stored_mnemonic()
# First mnemonic in the list (ECB) will be deleted
# Assert only CBC remains
m().write.assert_called_once_with(CBC_ONLY_JSON)
assert ctx.input.wait_for_button.call_count == len(BTN_SEQUENCE)


def test_sd_check_no_sd(m5stickv, mocker):
from krux.pages.tools import Tools
from krux.input import BUTTON_PAGE
from unittest.mock import ANY

BTN_SEQUENCE = [
BUTTON_PAGE, # Leave
]
mocker.patch(
"uos.statvfs",
new=mocker.MagicMock(return_value=[0, 4096, 4096, 0, 1024]),
)
ctx = mock_context(mocker)
ctx.input.wait_for_button = mocker.MagicMock(side_effect=BTN_SEQUENCE)
ctx = create_ctx(mocker, None)
tool = Tools(ctx)
tool.flash_text = mocker.MagicMock()
tool.sd_check()
Expand All @@ -99,13 +94,13 @@ def test_sd_check(m5stickv, mocker, mock_file_operations):
"uos.statvfs",
new=mocker.MagicMock(return_value=[0, 4096, 4096, 0, 1024]),
)
ctx = mock_context(mocker)
ctx.input.wait_for_button = mocker.MagicMock(side_effect=BTN_SEQUENCE)
ctx = create_ctx(mocker, BTN_SEQUENCE)
tool = Tools(ctx)
tool.sd_check()
ctx.display.draw_hcentered_text.assert_has_calls(
[mocker.call("SD card\n\nSize: 16 MB\n\nUsed: 12 MB\n\nFree: 4 MB")]
)
assert ctx.input.wait_for_button.call_count == len(BTN_SEQUENCE)


def test_delete_mnemonic_from_sd(m5stickv, mocker, mock_file_operations):
Expand All @@ -124,15 +119,15 @@ def test_delete_mnemonic_from_sd(m5stickv, mocker, mock_file_operations):
BUTTON_ENTER, # Leave
]

ctx = mock_context(mocker)
ctx.input.wait_for_button = mocker.MagicMock(side_effect=BTN_SEQUENCE)
ctx = create_ctx(mocker, BTN_SEQUENCE)
with patch("krux.sd_card.open", new=mocker.mock_open(read_data=SEEDS_JSON)) as m:
tool = Tools(ctx)
tool.rm_stored_mnemonic()
# First mnemonic in the list (ECB) will be deleted
# Assert only CBC remains
padding_size = len(SEEDS_JSON) - len(CBC_ONLY_JSON)
m().write.assert_called_once_with(CBC_ONLY_JSON + " " * padding_size)
assert ctx.input.wait_for_button.call_count == len(BTN_SEQUENCE)


def test_wipe_device(amigo, mocker):
Expand All @@ -143,45 +138,62 @@ def test_wipe_device(amigo, mocker):
BTN_SEQUENCE = [BUTTON_ENTER] # Confirm wipe

mocker.spy(Tools, "erase_spiffs")
ctx = mock_context(mocker)
ctx.input.wait_for_button = mocker.MagicMock(side_effect=BTN_SEQUENCE)
ctx = create_ctx(mocker, BTN_SEQUENCE)
test_tools = Tools(ctx)
test_tools.wipe_device()

assert test_tools.erase_spiffs.call_count == 1
assert ctx.input.wait_for_button.call_count == len(BTN_SEQUENCE)


def test_printer_test_tool(amigo, mocker):
"""Test that the print tool is called with the correct text"""
from krux.pages.tools import Tools
from krux.themes import theme
from krux.input import BUTTON_ENTER, BUTTON_PAGE
from krux.input import BUTTON_ENTER

BTN_SEQUENCE = [BUTTON_ENTER] # Confirm print, then leave

with patch("krux.pages.print_page.PrintPage.print_qr") as mocked_print_qr:
ctx = mock_context(mocker)
ctx.input.wait_for_button = mocker.MagicMock(side_effect=BTN_SEQUENCE)
ctx = create_ctx(mocker, BTN_SEQUENCE)
test_tools = Tools(ctx)
test_tools.print_test()

mocked_print_qr.assert_called_with(
"Krux Printer Test QR", title="Krux Printer Test QR"
)
assert ctx.input.wait_for_button.call_count == len(BTN_SEQUENCE)


def test_create_qr(amigo, mocker):
"""Test that QR creation tool is called with the correct text"""
from krux.pages.tools import Tools
from krux.input import BUTTON_ENTER, BUTTON_PAGE
from krux.input import BUTTON_ENTER

BTN_SEQUENCE = [BUTTON_ENTER]

with patch("krux.pages.qr_view.SeedQRView") as Mocked_QRView:
ctx = mock_context(mocker)
ctx.input.wait_for_button = mocker.MagicMock(side_effect=BTN_SEQUENCE)
ctx = create_ctx(mocker, BTN_SEQUENCE)

test_tools = Tools(ctx)
test_tools.capture_from_keypad = mocker.MagicMock(return_value="test")
test_tools.create_qr()

Mocked_QRView.assert_called_with(ctx, data="test", title="Custom QR Code")
assert ctx.input.wait_for_button.call_count == len(BTN_SEQUENCE)


def test_load_descriptor_adresses(m5stickv, mocker):
from krux.pages.tools import Tools
from krux.input import BUTTON_ENTER, BUTTON_PAGE_PREV

BTN_SEQUENCE = [
BUTTON_ENTER, # Confirm intro
BUTTON_PAGE_PREV, # Go to Back
BUTTON_ENTER, # Leave
]

ctx = create_ctx(mocker, BTN_SEQUENCE)
tool = Tools(ctx)
tool.descriptor_addresses()
assert ctx.input.wait_for_button.call_count == len(BTN_SEQUENCE)
33 changes: 33 additions & 0 deletions tests/test_auto_shutdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from .shared_mocks import mock_context
import pytest


def test_auto_shutdown(m5stickv, mocker):
from krux.auto_shutdown import auto_shutdown, seconds_counter

ctx = mock_context(mocker)
auto_shutdown.add_ctx(ctx)

# Set the timeout to a value that will cause a shutdown in 2 seconds
auto_shutdown.time_out = 1

# Call the seconds_counter method to simulate the timer callback
# Check if SystemExit is raised
with pytest.raises(SystemExit):
# Mocking timer calls:
# Decrement the time_out value to 0
seconds_counter("dummy timer argument")
# Next timer call will shutdown the device
seconds_counter("dummy timer argument")


def test_feed(m5stickv, mocker):
from krux.auto_shutdown import auto_shutdown
from krux.krux_settings import Settings

auto_shutdown.time_out = 1000

# Call the feed method to reset the timeout
auto_shutdown.feed()
# Check if the timeout is reset to the value set in the settings
assert auto_shutdown.time_out == Settings().security.auto_shutdown * 60
39 changes: 17 additions & 22 deletions tests/test_power.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
def test_init(mocker, m5stickv):
def test_init(mocker, all_devices):
from krux.power import PowerManager

manager = PowerManager()

assert isinstance(manager, PowerManager)


def test_init_with_amigo(mocker, amigo):
def test_pmu(mocker, all_devices):
from krux.power import PowerManager
import board

manager = PowerManager()

assert isinstance(manager, PowerManager)

if board.config["type"] == "dock":
assert manager.pmu is None
manager.has_battery() is False
else:
assert manager.pmu is not None
manager.has_battery() is True

def test_init_without_pmu(mocker, m5stickv):
import sys

if "pmu" in sys.modules:
del sys.modules["pmu"]
def test_charge_remaining(mocker, all_devices):
from krux.power import PowerManager
import board

manager = PowerManager()

assert isinstance(manager, PowerManager)
assert manager.pmu is None


def test_init_with_amigo_without_pmu(mocker, amigo):
import sys

if "pmu" in sys.modules:
del sys.modules["pmu"]
from krux.power import PowerManager

manager = PowerManager()
if manager.pmu is not None:
manager.pmu.get_battery_voltage = mocker.MagicMock(return_value=4000)

assert isinstance(manager, PowerManager)
assert manager.pmu is None
if board.config["type"] == "amigo":
assert manager.battery_charge_remaining() == 0.9
elif board.config["type"] in ("m5stickv", "cube"):
assert round(manager.battery_charge_remaining(), 2) == 0.75


def test_shutdown(mocker, m5stickv):
Expand Down

0 comments on commit fef4961

Please sign in to comment.