Check for disabled card and add tests
This commit is contained in:
@@ -2,7 +2,6 @@ import logging
|
||||
from datetime import date, datetime, timedelta
|
||||
from time import sleep
|
||||
|
||||
import lgpio
|
||||
from sqlalchemy import exc
|
||||
from sqlmodel import select
|
||||
|
||||
@@ -28,6 +27,7 @@ class DoorController:
|
||||
self._chip = None
|
||||
|
||||
if not mock_factory:
|
||||
import lgpio
|
||||
self._chip = lgpio.gpiochip_open(0)
|
||||
lgpio.gpio_claim_output(self._chip, unlock_pin, 1)
|
||||
lgpio.gpio_claim_output(self._chip, lock_pin, 1)
|
||||
@@ -42,7 +42,7 @@ class DoorController:
|
||||
def open(self):
|
||||
if self._mock:
|
||||
self._is_open = True
|
||||
logger.info("Dor unlocked.[MOCK]")
|
||||
logger.info("Door unlocked.[MOCK]")
|
||||
return
|
||||
|
||||
lgpio.gpio_write(self._chip, self._unlock_pin, 0)
|
||||
@@ -52,6 +52,11 @@ class DoorController:
|
||||
logger.info("Door unlocked!")
|
||||
|
||||
def close(self):
|
||||
if self._mock:
|
||||
self._is_open = False
|
||||
logger.info("Door locked.[MOCK]")
|
||||
return
|
||||
|
||||
lgpio.gpio_write(self._chip, self._lock_pin, 0)
|
||||
sleep(0.4)
|
||||
lgpio.gpio_write(self._chip, self._lock_pin, 1)
|
||||
@@ -101,6 +106,8 @@ def checkAccess(key: str, db: Session):
|
||||
current_weekday = datetime.weekday(date.today())
|
||||
current_time = datetime.now()
|
||||
card = db.exec(select(Card).where(Card.key == key)).one()
|
||||
if not card.enabled:
|
||||
return False
|
||||
for auth in card.group.accessauths:
|
||||
logger.info(f"checking auth: {auth.name}")
|
||||
if not auth.is_active:
|
||||
|
||||
@@ -112,3 +112,65 @@ def test_check_access_invalid_card(db_session):
|
||||
# Should raise exception for non-existent card
|
||||
with pytest.raises(Exception):
|
||||
checkAccess("non-existent-key", db_session)
|
||||
|
||||
def test_check_access_with_inactive_aa(db_session):
|
||||
# Setup: create card with valid access
|
||||
group = GroupDB(name="Test Group")
|
||||
db_session.add(group)
|
||||
db_session.commit()
|
||||
|
||||
card = Card(
|
||||
key="test-key-123",
|
||||
group_id=group.id,
|
||||
enabled=True,
|
||||
name="test_card",
|
||||
card_serial="00:00:00:00:00:00:00",
|
||||
)
|
||||
db_session.add(card)
|
||||
|
||||
timetable = Timetable(
|
||||
weekday=datetime.datetime.weekday(datetime.date.today()),
|
||||
starttime=datetime.datetime.now().time(),
|
||||
duration=120, # 2 hours
|
||||
)
|
||||
db_session.add(timetable)
|
||||
|
||||
aa = AccessAuthorizationDB(name="Test AA", is_active=False, type="timetable")
|
||||
db_session.add(aa)
|
||||
aa.timetables = [timetable]
|
||||
group.accessauths = [aa]
|
||||
|
||||
db_session.commit()
|
||||
|
||||
assert checkAccess("test-key-123", db_session) == False
|
||||
|
||||
def test_check_access_with_inactive_card(db_session):
|
||||
# Setup: create card with valid access
|
||||
group = GroupDB(name="Test Group")
|
||||
db_session.add(group)
|
||||
db_session.commit()
|
||||
|
||||
card = Card(
|
||||
key="test-key-123",
|
||||
group_id=group.id,
|
||||
enabled=False,
|
||||
name="test_card",
|
||||
card_serial="00:00:00:00:00:00:00",
|
||||
)
|
||||
db_session.add(card)
|
||||
|
||||
timetable = Timetable(
|
||||
weekday=datetime.datetime.weekday(datetime.date.today()),
|
||||
starttime=datetime.datetime.now().time(),
|
||||
duration=120, # 2 hours
|
||||
)
|
||||
db_session.add(timetable)
|
||||
|
||||
aa = AccessAuthorizationDB(name="Test AA", is_active=True, type="timetable")
|
||||
db_session.add(aa)
|
||||
aa.timetables = [timetable]
|
||||
group.accessauths = [aa]
|
||||
|
||||
db_session.commit()
|
||||
|
||||
assert checkAccess("test-key-123", db_session) == False
|
||||
Reference in New Issue
Block a user