[Tests] Fix tests for new model

This commit is contained in:
2026-07-27 17:01:01 +02:00
committed by ahtlon
parent 4b87603e06
commit 1efbad1db3
4 changed files with 44 additions and 34 deletions

View File

@@ -3,6 +3,7 @@ from fastapi.testclient import TestClient
from sqlmodel import Session, create_engine, SQLModel from sqlmodel import Session, create_engine, SQLModel
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool from sqlalchemy.pool import StaticPool
from datetime import time
from app.main import app from app.main import app
from app.model.models import UserDB, Card, GroupDB, AccessAuthorizationDB, Timetable, AaGroupLink from app.model.models import UserDB, Card, GroupDB, AccessAuthorizationDB, Timetable, AaGroupLink
@@ -107,11 +108,18 @@ def test_card(db_session, test_group):
@pytest.fixture @pytest.fixture
def test_aa(db_session): def test_aa_tt(db_session):
"""Create a test access authorization.""" """Create a test access authorization with timetable."""
tt = Timetable(
weekday=1,
starttime=time(1, 0, 0, 0),
duration=50
)
aa = AccessAuthorizationDB( aa = AccessAuthorizationDB(
name="Test AA", name="Test AA",
is_active=True is_active=True,
type="timetable",
timetables=[tt]
) )
db_session.add(aa) db_session.add(aa)
db_session.commit() db_session.commit()

View File

@@ -41,7 +41,7 @@ def test_group_models():
def test_access_authorization_models(): def test_access_authorization_models():
"""Test access authorization model creation and validation.""" """Test access authorization model creation and validation."""
# Test AccessAuthorizationBase # 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.name == "Test AA"
assert aa_base.is_active is True 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) timetable_create = TimetableCreate(weekday=1, starttime="08:00", duration=60)
aa_create = AccessAuthorizationCreate( aa_create = AccessAuthorizationCreate(
name="New AA", name="New AA",
is_active=False, is_active=False,
type="timetable",
timetables=[timetable_create] timetables=[timetable_create]
) )
assert aa_create.name == "New AA" assert aa_create.name == "New AA"

View File

@@ -6,6 +6,7 @@ def test_create_access_auth(client, auth_headers):
"""Test creating a new access authorization.""" """Test creating a new access authorization."""
aa_data = { aa_data = {
"name": "New AA", "name": "New AA",
"type": "timetable",
"is_active": True, "is_active": True,
"timetables": [ "timetables": [
{"weekday": 1, "starttime": "08:00", "duration": 60}, {"weekday": 1, "starttime": "08:00", "duration": 60},
@@ -23,7 +24,7 @@ def test_create_access_auth(client, auth_headers):
assert len(data["timetables"]) == 2 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.""" """Test retrieving all access authorizations."""
response = client.get("/api/v1/aa/", headers=auth_headers) response = client.get("/api/v1/aa/", headers=auth_headers)
assert response.status_code == 200 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 assert len(aa_list) >= 1
aa_names = [aa["name"] for aa in aa_list] 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.""" """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 assert response.status_code == 200
data = response.json() data = response.json()
assert data["id"] == test_aa.id assert data["id"] == test_aa_tt.id
assert data["name"] == test_aa.name assert data["name"] == test_aa_tt.name
def test_get_nonexistent_access_auth(client, auth_headers): 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 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.""" """Test assigning an access authorization to a group."""
response = client.put( 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 headers=auth_headers
) )
assert response.status_code == 200 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 # 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.""" """Test assigning an already assigned access authorization."""
# First assignment # 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 # Second assignment should indicate it's already assigned
response = client.put( 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 headers=auth_headers
) )
# According to the code, this returns 409 with "already assigned" message # 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() 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.""" """Test unassigning an access authorization from a group."""
# First assign # 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 # Then unassign
response = client.put( 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 headers=auth_headers
) )
assert response.status_code == 200 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.""" """Test unassigning a non-existent assignment."""
response = client.put( 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 headers=auth_headers
) )
assert response.status_code == 404 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.""" """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 assert response.status_code == 404
@@ -114,7 +115,7 @@ def test_assign_nonexistent_aa(client, auth_headers, test_group):
assert response.status_code == 404 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.""" """Test updating an access authorization."""
update_data = { update_data = {
"name": "Updated AA", "name": "Updated AA",
@@ -122,7 +123,7 @@ def test_update_access_auth(client, auth_headers, test_aa):
} }
response = client.patch( response = client.patch(
f"/api/v1/aa/{test_aa.id}", f"/api/v1/aa/{test_aa_tt.id}",
json=update_data, json=update_data,
headers=auth_headers headers=auth_headers
) )
@@ -133,7 +134,7 @@ def test_update_access_auth(client, auth_headers, test_aa):
assert data["is_active"] is False 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.""" """Test updating an access authorization with new timetables."""
update_data = { update_data = {
"timetables": [ "timetables": [
@@ -142,7 +143,7 @@ def test_update_access_auth_with_timetables(client, auth_headers, test_aa):
} }
response = client.patch( response = client.patch(
f"/api/v1/aa/{test_aa.id}", f"/api/v1/aa/{test_aa_tt.id}",
json=update_data, json=update_data,
headers=auth_headers headers=auth_headers
) )
@@ -161,14 +162,14 @@ def test_update_nonexistent_access_auth(client, auth_headers):
assert response.status_code == 404 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.""" """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 response.status_code == 200
assert "deleted successfully" in response.json()["message"].lower() assert "deleted successfully" in response.json()["message"].lower()
# Verify AA is deleted # 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 assert response.status_code == 404
@@ -178,7 +179,7 @@ def test_delete_nonexistent_access_auth(client, auth_headers):
assert response.status_code == 404 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.""" """Test that non-admin users cannot perform AA operations."""
# Try to create an AA # Try to create an AA
response = client.post( 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 assert response.status_code == 403
# Try to assign AA # 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 assert response.status_code == 403

View File

@@ -19,7 +19,7 @@ def test_check_access_with_valid_timetable(db_session):
) )
db_session.add(timetable) 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) db_session.add(aa)
aa.timetables = [timetable] aa.timetables = [timetable]
group.accessauths = [aa] group.accessauths = [aa]
@@ -46,7 +46,7 @@ def test_check_access_outside_hours(db_session):
) )
db_session.add(timetable) 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) db_session.add(aa)
aa.timetables = [timetable] aa.timetables = [timetable]
group.accessauths = [aa] group.accessauths = [aa]