diff --git a/test/conftest.py b/test/conftest.py index 70bf024..a60fa9d 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -3,6 +3,7 @@ from fastapi.testclient import TestClient from sqlmodel import Session, create_engine, SQLModel from sqlalchemy.orm import sessionmaker from sqlalchemy.pool import StaticPool +from datetime import time from app.main import app from app.model.models import UserDB, Card, GroupDB, AccessAuthorizationDB, Timetable, AaGroupLink @@ -107,11 +108,18 @@ def test_card(db_session, test_group): @pytest.fixture -def test_aa(db_session): - """Create a test access authorization.""" +def test_aa_tt(db_session): + """Create a test access authorization with timetable.""" + tt = Timetable( + weekday=1, + starttime=time(1, 0, 0, 0), + duration=50 + ) aa = AccessAuthorizationDB( name="Test AA", - is_active=True + is_active=True, + type="timetable", + timetables=[tt] ) db_session.add(aa) db_session.commit() diff --git a/test/test_models.py b/test/test_models.py index 85e3ac9..f30710f 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -41,7 +41,7 @@ def test_group_models(): def test_access_authorization_models(): """Test access authorization model creation and validation.""" # Test AccessAuthorizationBase - aa_base = AccessAuthorizationBase(name="Test AA", is_active=True) + aa_base = AccessAuthorizationBase(name="Test AA", is_active=True, type="timetable") assert aa_base.name == "Test AA" assert aa_base.is_active is True @@ -49,7 +49,8 @@ def test_access_authorization_models(): timetable_create = TimetableCreate(weekday=1, starttime="08:00", duration=60) aa_create = AccessAuthorizationCreate( name="New AA", - is_active=False, + is_active=False, + type="timetable", timetables=[timetable_create] ) assert aa_create.name == "New AA" diff --git a/test/test_services/test_aa_manager.py b/test/test_services/test_aa_manager.py index 3bfdb3c..0fae33e 100644 --- a/test/test_services/test_aa_manager.py +++ b/test/test_services/test_aa_manager.py @@ -6,6 +6,7 @@ def test_create_access_auth(client, auth_headers): """Test creating a new access authorization.""" aa_data = { "name": "New AA", + "type": "timetable", "is_active": True, "timetables": [ {"weekday": 1, "starttime": "08:00", "duration": 60}, @@ -23,7 +24,7 @@ def test_create_access_auth(client, auth_headers): assert len(data["timetables"]) == 2 -def test_get_all_access_auths(client, auth_headers, test_aa): +def test_get_all_access_auths(client, auth_headers, test_aa_tt): """Test retrieving all access authorizations.""" response = client.get("/api/v1/aa/", headers=auth_headers) assert response.status_code == 200 @@ -32,17 +33,17 @@ def test_get_all_access_auths(client, auth_headers, test_aa): assert len(aa_list) >= 1 aa_names = [aa["name"] for aa in aa_list] - assert test_aa.name in aa_names + assert test_aa_tt.name in aa_names -def test_get_access_auth_by_id(client, auth_headers, test_aa): +def test_get_access_auth_by_id(client, auth_headers, test_aa_tt): """Test retrieving a specific access authorization by ID.""" - response = client.get(f"/api/v1/aa/{test_aa.id}", headers=auth_headers) + response = client.get(f"/api/v1/aa/{test_aa_tt.id}", headers=auth_headers) assert response.status_code == 200 data = response.json() - assert data["id"] == test_aa.id - assert data["name"] == test_aa.name + assert data["id"] == test_aa_tt.id + assert data["name"] == test_aa_tt.name def test_get_nonexistent_access_auth(client, auth_headers): @@ -51,10 +52,10 @@ def test_get_nonexistent_access_auth(client, auth_headers): assert response.status_code == 404 -def test_assign_access_auth_to_group(client, auth_headers, test_group, test_aa): +def test_assign_access_auth_to_group(client, auth_headers, test_group, test_aa_tt): """Test assigning an access authorization to a group.""" response = client.put( - f"/api/v1/aa/assign/{test_group.id}/{test_aa.id}", + f"/api/v1/aa/assign/{test_group.id}/{test_aa_tt.id}", headers=auth_headers ) assert response.status_code == 200 @@ -65,14 +66,14 @@ def test_assign_access_auth_to_group(client, auth_headers, test_group, test_aa): # Note: The response model might not include the full relationship -def test_assign_already_assigned_access_auth(client, auth_headers, test_group, test_aa): +def test_assign_already_assigned_access_auth(client, auth_headers, test_group, test_aa_tt): """Test assigning an already assigned access authorization.""" # First assignment - client.put(f"/api/v1/aa/assign/{test_group.id}/{test_aa.id}", headers=auth_headers) + client.put(f"/api/v1/aa/assign/{test_group.id}/{test_aa_tt.id}", headers=auth_headers) # Second assignment should indicate it's already assigned response = client.put( - f"/api/v1/aa/assign/{test_group.id}/{test_aa.id}", + f"/api/v1/aa/assign/{test_group.id}/{test_aa_tt.id}", headers=auth_headers ) # According to the code, this returns 409 with "already assigned" message @@ -80,31 +81,31 @@ def test_assign_already_assigned_access_auth(client, auth_headers, test_group, t assert "already assigned" in response.json()["detail"].lower() -def test_unassign_access_auth_from_group(client, auth_headers, test_group, test_aa): +def test_unassign_access_auth_from_group(client, auth_headers, test_group, test_aa_tt): """Test unassigning an access authorization from a group.""" # First assign - client.put(f"/api/v1/aa/assign/{test_group.id}/{test_aa.id}", headers=auth_headers) + client.put(f"/api/v1/aa/assign/{test_group.id}/{test_aa_tt.id}", headers=auth_headers) # Then unassign response = client.put( - f"/api/v1/aa/unassign/{test_group.id}/{test_aa.id}", + f"/api/v1/aa/unassign/{test_group.id}/{test_aa_tt.id}", headers=auth_headers ) assert response.status_code == 200 -def test_unassign_nonexistent_assignment(client, auth_headers, test_group, test_aa): +def test_unassign_nonexistent_assignment(client, auth_headers, test_group, test_aa_tt): """Test unassigning a non-existent assignment.""" response = client.put( - f"/api/v1/aa/unassign/{test_group.id}/{test_aa.id}", + f"/api/v1/aa/unassign/{test_group.id}/{test_aa_tt.id}", headers=auth_headers ) assert response.status_code == 404 -def test_assign_to_nonexistent_group(client, auth_headers, test_aa): +def test_assign_to_nonexistent_group(client, auth_headers, test_aa_tt): """Test assigning an AA to a non-existent group.""" - response = client.put(f"/api/v1/aa/assign/99999/{test_aa.id}", headers=auth_headers) + response = client.put(f"/api/v1/aa/assign/99999/{test_aa_tt.id}", headers=auth_headers) assert response.status_code == 404 @@ -114,7 +115,7 @@ def test_assign_nonexistent_aa(client, auth_headers, test_group): assert response.status_code == 404 -def test_update_access_auth(client, auth_headers, test_aa): +def test_update_access_auth(client, auth_headers, test_aa_tt): """Test updating an access authorization.""" update_data = { "name": "Updated AA", @@ -122,7 +123,7 @@ def test_update_access_auth(client, auth_headers, test_aa): } response = client.patch( - f"/api/v1/aa/{test_aa.id}", + f"/api/v1/aa/{test_aa_tt.id}", json=update_data, headers=auth_headers ) @@ -133,7 +134,7 @@ def test_update_access_auth(client, auth_headers, test_aa): assert data["is_active"] is False -def test_update_access_auth_with_timetables(client, auth_headers, test_aa): +def test_update_access_auth_with_timetables(client, auth_headers, test_aa_tt): """Test updating an access authorization with new timetables.""" update_data = { "timetables": [ @@ -142,7 +143,7 @@ def test_update_access_auth_with_timetables(client, auth_headers, test_aa): } response = client.patch( - f"/api/v1/aa/{test_aa.id}", + f"/api/v1/aa/{test_aa_tt.id}", json=update_data, headers=auth_headers ) @@ -161,14 +162,14 @@ def test_update_nonexistent_access_auth(client, auth_headers): assert response.status_code == 404 -def test_delete_access_auth(client, auth_headers, test_aa): +def test_delete_access_auth(client, auth_headers, test_aa_tt): """Test deleting an access authorization.""" - response = client.delete(f"/api/v1/aa/{test_aa.id}", headers=auth_headers) + response = client.delete(f"/api/v1/aa/{test_aa_tt.id}", headers=auth_headers) assert response.status_code == 200 assert "deleted successfully" in response.json()["message"].lower() # Verify AA is deleted - response = client.get(f"/api/v1/aa/{test_aa.id}", headers=auth_headers) + response = client.get(f"/api/v1/aa/{test_aa_tt.id}", headers=auth_headers) assert response.status_code == 404 @@ -178,7 +179,7 @@ def test_delete_nonexistent_access_auth(client, auth_headers): assert response.status_code == 404 -def test_aa_operations_by_non_admin(client, test_aa, user_auth_headers): +def test_aa_tt_operations_by_non_admin(client, test_aa_tt, user_auth_headers): """Test that non-admin users cannot perform AA operations.""" # Try to create an AA response = client.post( @@ -193,5 +194,5 @@ def test_aa_operations_by_non_admin(client, test_aa, user_auth_headers): assert response.status_code == 403 # Try to assign AA - response = client.put(f"/api/v1/aa/assign/1/{test_aa.id}", headers=user_auth_headers) + response = client.put(f"/api/v1/aa/assign/1/{test_aa_tt.id}", headers=user_auth_headers) assert response.status_code == 403 diff --git a/test/test_services/test_door.py b/test/test_services/test_door.py index 166f3e0..589c7df 100644 --- a/test/test_services/test_door.py +++ b/test/test_services/test_door.py @@ -19,7 +19,7 @@ def test_check_access_with_valid_timetable(db_session): ) db_session.add(timetable) - aa = AccessAuthorizationDB(name="Test AA", is_active=True) + aa = AccessAuthorizationDB(name="Test AA", is_active=True, type="timetable") db_session.add(aa) aa.timetables = [timetable] group.accessauths = [aa] @@ -46,7 +46,7 @@ def test_check_access_outside_hours(db_session): ) db_session.add(timetable) - aa = AccessAuthorizationDB(name="Test AA", is_active=True) + aa = AccessAuthorizationDB(name="Test AA", is_active=True, type="timetable") db_session.add(aa) aa.timetables = [timetable] group.accessauths = [aa]