2026-03-16 22:46:48 +01:00
|
|
|
"""Tests for phone workout verification, phone check, and unlock operations."""
|
2026-04-10 18:11:30 +02:00
|
|
|
# pylint: disable=protected-access,unused-argument
|
2026-03-16 22:46:48 +01:00
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
2026-04-09 21:44:13 +02:00
|
|
|
from unittest.mock import MagicMock, patch
|
2026-03-16 22:46:48 +01:00
|
|
|
|
2026-05-28 07:43:06 +02:00
|
|
|
from screen_locker.tests.conftest import create_locker
|
2026-03-16 22:46:48 +01:00
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestVerifyPhoneWorkout:
|
|
|
|
|
"""Tests for _verify_phone_workout method."""
|
|
|
|
|
|
|
|
|
|
def test_verified(
|
|
|
|
|
self,
|
|
|
|
|
mock_tk: MagicMock,
|
Reduce per-file-ignores by fixing lint violations across codebase
Fix ruff violations in ~15 source files and ~60+ test files to minimize
per-file-ignores in pyproject.toml. Remaining ignores are justified with
comments explaining why each suppression is necessary.
Source fixes: FBT003 (keyword args), S310 (URL validation), SLF001
(private access), T201 (print→logging), C901 (complexity), E501 (line
length), E402 (import order).
Test fixes: SIM117 (combined with), FBT (boolean args), PERF203 (try in
loop), S310/S607 (URLs/executables), E402/E501 (imports/lines), S108
(tmp paths), PLR0913 (too many args), ARG (unused args), ANN (type
annotations), RUF059 (unused unpacked vars), PT019 (fixture naming).
Remaining per-file-ignores (with justifications):
- Tests: ARG, D, PLC0415, PLR2004, S101, SLF001
- music_gen sources: PLC0415 (heavy ML lazy imports)
- moviepy_showcase: PLC0415 (circular dependency)
- generate_images: PLR0913 (matplotlib helpers need many params)
- praca_magisterska_video: E501, E402 (long paths, mpl.use)
2026-03-25 18:58:05 +01:00
|
|
|
mock_sys_exit: MagicMock,
|
2026-03-16 22:46:48 +01:00
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
2026-04-09 21:44:13 +02:00
|
|
|
"""Test workout verified on phone with sufficient duration."""
|
2026-03-16 22:46:48 +01:00
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
2026-03-18 22:20:05 +01:00
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_is_phone_connected",
|
2026-04-09 21:44:13 +02:00
|
|
|
MagicMock(return_value=True),
|
2026-03-16 22:46:48 +01:00
|
|
|
)
|
2026-03-18 22:20:05 +01:00
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_pull_stronglifts_db",
|
2026-04-09 21:44:13 +02:00
|
|
|
MagicMock(return_value=tmp_path / "sl.db"),
|
2026-03-16 22:46:48 +01:00
|
|
|
)
|
2026-03-18 22:20:05 +01:00
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_count_today_workouts",
|
2026-04-09 21:44:13 +02:00
|
|
|
MagicMock(return_value=2),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_is_workout_finish_recent",
|
|
|
|
|
MagicMock(return_value=True),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_get_today_exercise_count",
|
|
|
|
|
MagicMock(return_value=3),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_get_today_workout_duration_minutes",
|
|
|
|
|
MagicMock(return_value=65.0),
|
2026-03-16 22:46:48 +01:00
|
|
|
)
|
|
|
|
|
|
2026-04-09 21:44:13 +02:00
|
|
|
with patch(
|
2026-05-28 07:43:06 +02:00
|
|
|
"screen_locker._phone_verification.check_clock_skew",
|
2026-04-09 21:44:13 +02:00
|
|
|
return_value=(True, "Clock OK"),
|
|
|
|
|
):
|
|
|
|
|
status, message = locker._verify_phone_workout()
|
2026-03-16 22:46:48 +01:00
|
|
|
|
|
|
|
|
assert status == "verified"
|
|
|
|
|
assert "2 session" in message
|
2026-04-09 21:44:13 +02:00
|
|
|
assert "65 min" in message
|
|
|
|
|
assert "3 exercise" in message
|
2026-03-16 22:46:48 +01:00
|
|
|
|
|
|
|
|
def test_not_verified(
|
|
|
|
|
self,
|
|
|
|
|
mock_tk: MagicMock,
|
Reduce per-file-ignores by fixing lint violations across codebase
Fix ruff violations in ~15 source files and ~60+ test files to minimize
per-file-ignores in pyproject.toml. Remaining ignores are justified with
comments explaining why each suppression is necessary.
Source fixes: FBT003 (keyword args), S310 (URL validation), SLF001
(private access), T201 (print→logging), C901 (complexity), E501 (line
length), E402 (import order).
Test fixes: SIM117 (combined with), FBT (boolean args), PERF203 (try in
loop), S310/S607 (URLs/executables), E402/E501 (imports/lines), S108
(tmp paths), PLR0913 (too many args), ARG (unused args), ANN (type
annotations), RUF059 (unused unpacked vars), PT019 (fixture naming).
Remaining per-file-ignores (with justifications):
- Tests: ARG, D, PLC0415, PLR2004, S101, SLF001
- music_gen sources: PLC0415 (heavy ML lazy imports)
- moviepy_showcase: PLC0415 (circular dependency)
- generate_images: PLR0913 (matplotlib helpers need many params)
- praca_magisterska_video: E501, E402 (long paths, mpl.use)
2026-03-25 18:58:05 +01:00
|
|
|
mock_sys_exit: MagicMock,
|
2026-03-16 22:46:48 +01:00
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test no workout found on phone."""
|
|
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
2026-03-18 22:20:05 +01:00
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_is_phone_connected",
|
2026-04-09 21:44:13 +02:00
|
|
|
MagicMock(return_value=True),
|
2026-03-16 22:46:48 +01:00
|
|
|
)
|
2026-03-18 22:20:05 +01:00
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_pull_stronglifts_db",
|
2026-04-09 21:44:13 +02:00
|
|
|
MagicMock(return_value=tmp_path / "sl.db"),
|
2026-03-16 22:46:48 +01:00
|
|
|
)
|
2026-03-18 22:20:05 +01:00
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_count_today_workouts",
|
2026-04-09 21:44:13 +02:00
|
|
|
MagicMock(return_value=0),
|
2026-03-16 22:46:48 +01:00
|
|
|
)
|
|
|
|
|
|
2026-04-09 21:44:13 +02:00
|
|
|
with patch(
|
2026-05-28 07:43:06 +02:00
|
|
|
"screen_locker._phone_verification.check_clock_skew",
|
2026-04-09 21:44:13 +02:00
|
|
|
return_value=(True, "Clock OK"),
|
|
|
|
|
):
|
|
|
|
|
status, message = locker._verify_phone_workout()
|
2026-03-16 22:46:48 +01:00
|
|
|
|
|
|
|
|
assert status == "not_verified"
|
|
|
|
|
assert "No workout" in message
|
|
|
|
|
|
2026-04-09 21:44:13 +02:00
|
|
|
def test_too_short(
|
|
|
|
|
self,
|
|
|
|
|
mock_tk: MagicMock,
|
|
|
|
|
mock_sys_exit: MagicMock,
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test workout found but too short."""
|
|
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_is_phone_connected",
|
|
|
|
|
MagicMock(return_value=True),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_pull_stronglifts_db",
|
|
|
|
|
MagicMock(return_value=tmp_path / "sl.db"),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_count_today_workouts",
|
|
|
|
|
MagicMock(return_value=1),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_is_workout_finish_recent",
|
|
|
|
|
MagicMock(return_value=True),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_get_today_exercise_count",
|
|
|
|
|
MagicMock(return_value=3),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_get_today_workout_duration_minutes",
|
|
|
|
|
MagicMock(return_value=25.0),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with patch(
|
2026-05-28 07:43:06 +02:00
|
|
|
"screen_locker._phone_verification.check_clock_skew",
|
2026-04-09 21:44:13 +02:00
|
|
|
return_value=(True, "Clock OK"),
|
|
|
|
|
):
|
|
|
|
|
status, message = locker._verify_phone_workout()
|
|
|
|
|
|
|
|
|
|
assert status == "too_short"
|
|
|
|
|
assert "25 min" in message
|
|
|
|
|
assert "50 min" in message
|
|
|
|
|
|
2026-03-16 22:46:48 +01:00
|
|
|
def test_no_phone(
|
|
|
|
|
self,
|
|
|
|
|
mock_tk: MagicMock,
|
Reduce per-file-ignores by fixing lint violations across codebase
Fix ruff violations in ~15 source files and ~60+ test files to minimize
per-file-ignores in pyproject.toml. Remaining ignores are justified with
comments explaining why each suppression is necessary.
Source fixes: FBT003 (keyword args), S310 (URL validation), SLF001
(private access), T201 (print→logging), C901 (complexity), E501 (line
length), E402 (import order).
Test fixes: SIM117 (combined with), FBT (boolean args), PERF203 (try in
loop), S310/S607 (URLs/executables), E402/E501 (imports/lines), S108
(tmp paths), PLR0913 (too many args), ARG (unused args), ANN (type
annotations), RUF059 (unused unpacked vars), PT019 (fixture naming).
Remaining per-file-ignores (with justifications):
- Tests: ARG, D, PLC0415, PLR2004, S101, SLF001
- music_gen sources: PLC0415 (heavy ML lazy imports)
- moviepy_showcase: PLC0415 (circular dependency)
- generate_images: PLR0913 (matplotlib helpers need many params)
- praca_magisterska_video: E501, E402 (long paths, mpl.use)
2026-03-25 18:58:05 +01:00
|
|
|
mock_sys_exit: MagicMock,
|
2026-03-16 22:46:48 +01:00
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test no phone connected."""
|
|
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
2026-03-18 22:20:05 +01:00
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_is_phone_connected",
|
2026-04-09 21:44:13 +02:00
|
|
|
MagicMock(return_value=False),
|
2026-03-16 22:46:48 +01:00
|
|
|
)
|
|
|
|
|
|
2026-04-09 21:44:13 +02:00
|
|
|
with patch(
|
2026-05-28 07:43:06 +02:00
|
|
|
"screen_locker._phone_verification.check_clock_skew",
|
2026-04-09 21:44:13 +02:00
|
|
|
return_value=(True, "Clock OK"),
|
|
|
|
|
):
|
|
|
|
|
status, _ = locker._verify_phone_workout()
|
2026-03-16 22:46:48 +01:00
|
|
|
|
|
|
|
|
assert status == "no_phone"
|
|
|
|
|
|
|
|
|
|
def test_error_no_db(
|
|
|
|
|
self,
|
|
|
|
|
mock_tk: MagicMock,
|
Reduce per-file-ignores by fixing lint violations across codebase
Fix ruff violations in ~15 source files and ~60+ test files to minimize
per-file-ignores in pyproject.toml. Remaining ignores are justified with
comments explaining why each suppression is necessary.
Source fixes: FBT003 (keyword args), S310 (URL validation), SLF001
(private access), T201 (print→logging), C901 (complexity), E501 (line
length), E402 (import order).
Test fixes: SIM117 (combined with), FBT (boolean args), PERF203 (try in
loop), S310/S607 (URLs/executables), E402/E501 (imports/lines), S108
(tmp paths), PLR0913 (too many args), ARG (unused args), ANN (type
annotations), RUF059 (unused unpacked vars), PT019 (fixture naming).
Remaining per-file-ignores (with justifications):
- Tests: ARG, D, PLC0415, PLR2004, S101, SLF001
- music_gen sources: PLC0415 (heavy ML lazy imports)
- moviepy_showcase: PLC0415 (circular dependency)
- generate_images: PLR0913 (matplotlib helpers need many params)
- praca_magisterska_video: E501, E402 (long paths, mpl.use)
2026-03-25 18:58:05 +01:00
|
|
|
mock_sys_exit: MagicMock,
|
2026-03-16 22:46:48 +01:00
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test error when StrongLifts DB cannot be pulled."""
|
|
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
2026-03-18 22:20:05 +01:00
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_is_phone_connected",
|
2026-04-09 21:44:13 +02:00
|
|
|
MagicMock(return_value=True),
|
2026-03-16 22:46:48 +01:00
|
|
|
)
|
2026-03-18 22:20:05 +01:00
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_pull_stronglifts_db",
|
2026-04-09 21:44:13 +02:00
|
|
|
MagicMock(return_value=None),
|
2026-03-16 22:46:48 +01:00
|
|
|
)
|
|
|
|
|
|
2026-04-09 21:44:13 +02:00
|
|
|
with patch(
|
2026-05-28 07:43:06 +02:00
|
|
|
"screen_locker._phone_verification.check_clock_skew",
|
2026-04-09 21:44:13 +02:00
|
|
|
return_value=(True, "Clock OK"),
|
|
|
|
|
):
|
|
|
|
|
status, message = locker._verify_phone_workout()
|
2026-03-16 22:46:48 +01:00
|
|
|
|
|
|
|
|
assert status == "error"
|
|
|
|
|
assert "database" in message.lower()
|
|
|
|
|
|
2026-04-09 21:44:13 +02:00
|
|
|
def test_clock_tampered(
|
|
|
|
|
self,
|
|
|
|
|
mock_tk: MagicMock,
|
|
|
|
|
mock_sys_exit: MagicMock,
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test clock_tampered when NTP check fails."""
|
|
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
|
|
|
|
|
|
|
|
with patch(
|
2026-05-28 07:43:06 +02:00
|
|
|
"screen_locker._phone_verification.check_clock_skew",
|
2026-04-09 21:44:13 +02:00
|
|
|
return_value=(False, "System clock is 600s ahead"),
|
|
|
|
|
):
|
|
|
|
|
status, message = locker._verify_phone_workout()
|
|
|
|
|
|
|
|
|
|
assert status == "clock_tampered"
|
|
|
|
|
assert "600s" in message
|
|
|
|
|
|
|
|
|
|
def test_stale_workout(
|
|
|
|
|
self,
|
|
|
|
|
mock_tk: MagicMock,
|
|
|
|
|
mock_sys_exit: MagicMock,
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test stale status when workout finish is not recent."""
|
|
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_is_phone_connected",
|
|
|
|
|
MagicMock(return_value=True),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_pull_stronglifts_db",
|
|
|
|
|
MagicMock(return_value=tmp_path / "sl.db"),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_count_today_workouts",
|
|
|
|
|
MagicMock(return_value=1),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_is_workout_finish_recent",
|
|
|
|
|
MagicMock(return_value=False),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with patch(
|
2026-05-28 07:43:06 +02:00
|
|
|
"screen_locker._phone_verification.check_clock_skew",
|
2026-04-09 21:44:13 +02:00
|
|
|
return_value=(True, "Clock OK"),
|
|
|
|
|
):
|
|
|
|
|
status, message = locker._verify_phone_workout()
|
|
|
|
|
|
|
|
|
|
assert status == "stale"
|
|
|
|
|
assert "old" in message.lower()
|
|
|
|
|
|
|
|
|
|
def test_no_exercises(
|
|
|
|
|
self,
|
|
|
|
|
mock_tk: MagicMock,
|
|
|
|
|
mock_sys_exit: MagicMock,
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test no_exercises when workout has no exercise data."""
|
|
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_is_phone_connected",
|
|
|
|
|
MagicMock(return_value=True),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_pull_stronglifts_db",
|
|
|
|
|
MagicMock(return_value=tmp_path / "sl.db"),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_count_today_workouts",
|
|
|
|
|
MagicMock(return_value=1),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_is_workout_finish_recent",
|
|
|
|
|
MagicMock(return_value=True),
|
|
|
|
|
)
|
|
|
|
|
object.__setattr__(
|
|
|
|
|
locker,
|
|
|
|
|
"_get_today_exercise_count",
|
|
|
|
|
MagicMock(return_value=0),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with patch(
|
2026-05-28 07:43:06 +02:00
|
|
|
"screen_locker._phone_verification.check_clock_skew",
|
2026-04-09 21:44:13 +02:00
|
|
|
return_value=(True, "Clock OK"),
|
|
|
|
|
):
|
|
|
|
|
status, message = locker._verify_phone_workout()
|
|
|
|
|
|
|
|
|
|
assert status == "no_exercises"
|
|
|
|
|
assert "exercise" in message.lower()
|