57eb412b6d
The UI only noticed a removed card when some user action ran a real card command (e.g. Check status); /api/status is a cached-state read that kept returning the old card, and _handle_card_disconnect() did not clear app.card/rs. - start_card_monitor() registers a pyscard CardObserver for our reader; it only polls SCardGetStatusChange (no APDU, no connection, no extra process), and on removal sets server.card_present=False and calls _handle_card_disconnect() under _CARD_LOCK - /api/status now exposes connected (session usable) and card_present (physically inserted) and masks card/profile/atr/selection when not connected; _CARD_CONNECTED is initialized from card presence instead of being unconditionally True - the 2s UI poll includes /api/status; on disconnect it switches to the existing 'No card detected. Insert card and click Equip' state, or the new 'Card inserted — press Equip' hint when the card is back; the old _hadData heuristic is gone Tests for the observer (filtering, removal, insertion) and the UI state transitions. SW cache v90 -> v91.
63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Tests for the passive PC/SC card-presence observer."""
|
|
|
|
import sys
|
|
import types
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
PROJECTS = Path(__file__).resolve().parents[2]
|
|
PY_SIM = PROJECTS / 'pysim'
|
|
if str(PY_SIM) not in sys.path:
|
|
sys.path.insert(0, str(PY_SIM))
|
|
|
|
import pysim_otaman_server.server as S
|
|
|
|
|
|
class FakeCard:
|
|
def __init__(self, reader):
|
|
self.reader = reader
|
|
|
|
|
|
class TestCardPresenceObserver(unittest.TestCase):
|
|
def setUp(self):
|
|
self.observer = S._CardPresenceObserver('Test Reader 00 00')
|
|
self.server = types.SimpleNamespace(card_present=True)
|
|
self.saved_ref = S._server_ref
|
|
S._server_ref = self.server
|
|
self.disconnects = []
|
|
self.patcher = mock.patch.object(
|
|
S, '_handle_card_disconnect',
|
|
side_effect=lambda: self.disconnects.append(True))
|
|
self.patcher.start()
|
|
|
|
def tearDown(self):
|
|
self.patcher.stop()
|
|
S._server_ref = self.saved_ref
|
|
|
|
def test_removal_of_our_reader_disconnects(self):
|
|
self.observer.update(None, ([], [FakeCard('Test Reader 00 00')]))
|
|
self.assertFalse(self.server.card_present)
|
|
self.assertEqual(len(self.disconnects), 1)
|
|
|
|
def test_removal_of_other_reader_ignored(self):
|
|
self.observer.update(None, ([], [FakeCard('Other Reader 00 00')]))
|
|
self.assertTrue(self.server.card_present)
|
|
self.assertEqual(self.disconnects, [])
|
|
|
|
def test_insertion_sets_card_present(self):
|
|
self.server.card_present = False
|
|
self.observer.update(None, ([FakeCard('Test Reader 00 00')], []))
|
|
self.assertTrue(self.server.card_present)
|
|
self.assertEqual(self.disconnects, [])
|
|
|
|
def test_missing_reader_attribute_is_ignored(self):
|
|
self.observer.update(None, ([], [types.SimpleNamespace()]))
|
|
self.assertTrue(self.server.card_present)
|
|
self.assertEqual(self.disconnects, [])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|