From ea2a1f916d2a1b50d93753a751736d923e5957da Mon Sep 17 00:00:00 2001 From: ahtlon Date: Mon, 27 Jul 2026 17:18:04 +0200 Subject: [PATCH] [Tests] add new tests for oneshot type aa --- test/test_services/test_aa_manager.py | 39 ++++++++++++++++++++++++--- test/test_services/test_door.py | 29 +++++++++++++++++++- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/test/test_services/test_aa_manager.py b/test/test_services/test_aa_manager.py index 0fae33e..1714257 100644 --- a/test/test_services/test_aa_manager.py +++ b/test/test_services/test_aa_manager.py @@ -2,10 +2,10 @@ import pytest from fastapi import status -def test_create_access_auth(client, auth_headers): +def test_create_access_auth_tt(client, auth_headers): """Test creating a new access authorization.""" aa_data = { - "name": "New AA", + "name": "New tt_AA", "type": "timetable", "is_active": True, "timetables": [ @@ -18,11 +18,44 @@ def test_create_access_auth(client, auth_headers): assert response.status_code == 200 data = response.json() - assert data["name"] == "New AA" + assert data["name"] == "New tt_AA" assert data["is_active"] is True + assert data["type"] == "timetable" assert "id" in data assert len(data["timetables"]) == 2 +def test_create_access_auth_os(client, auth_headers): + """Test creating a new access authorization with oneshot type.""" + aa_data = { + "name": "New os_AA", + "type": "oneshot", + "is_active": True, + "oneshot": { + "uses": 1, + "ends_at": "2029-07-27" + } + } + + response = client.post("/api/v1/aa/", json=aa_data, headers=auth_headers) + assert response.status_code == 200 + + data = response.json() + assert data["name"] == "New os_AA" + assert data["type"] == "oneshot" + assert data["is_active"] is True + assert "id" in data + assert data["oneshot"]["uses"] == 1 + +def test_create_wrong_aa_type(client, auth_headers): + """Test creating a new access authorization with oneshot type.""" + aa_data = { + "name": "New AA", + "type": "wrong", + "is_active": True, + } + + response = client.post("/api/v1/aa/", json=aa_data, headers=auth_headers) + assert response.status_code == 422 def test_get_all_access_auths(client, auth_headers, test_aa_tt): """Test retrieving all access authorizations.""" diff --git a/test/test_services/test_door.py b/test/test_services/test_door.py index 589c7df..7e332e8 100644 --- a/test/test_services/test_door.py +++ b/test/test_services/test_door.py @@ -1,7 +1,7 @@ import pytest import datetime from app.services.door import checkAccess -from app.model.models import Card, GroupDB, AccessAuthorizationDB, Timetable +from app.model.models import Card, GroupDB, AccessAuthorizationDB, Timetable, OneShotAccess def test_check_access_with_valid_timetable(db_session): # Setup: create card with valid access @@ -55,6 +55,33 @@ def test_check_access_outside_hours(db_session): result = checkAccess("test-key-123", db_session) assert result == False +def test_check_access_with_valid_oneshot(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) + + oneshot = OneShotAccess( + uses=1, + ends_at=datetime.datetime.now() + datetime.timedelta(days=1) + ) + db_session.add(oneshot) + + aa = AccessAuthorizationDB(name="Test AA", is_active=True, type="oneshot") + db_session.add(aa) + aa.oneshot = oneshot + group.accessauths = [aa] + + db_session.commit() + + # Test: access should be granted within time window + result = checkAccess("test-key-123", db_session) + assert result == True + assert aa.oneshot.uses == 0 + def test_check_access_invalid_card(db_session): # Should raise exception for non-existent card with pytest.raises(Exception):