File size: 3,735 Bytes
d99646e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from app.core.database import Base
from app.models.department import Department
from app.models.user import User
from app.schemas.admin import AdminDepartmentUpdate, AdminUserUpdate
from app.schemas.auth import SignupRequest
from app.services.admin_service import (
create_department,
deactivate_department,
deactivate_user,
list_departments,
list_users,
update_department,
update_user,
)
from app.services.auth_service import create_user
def _make_session():
engine = create_engine(
"sqlite+pysqlite:///:memory:",
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base.metadata.create_all(bind=engine)
return engine, TestingSessionLocal()
def test_admin_can_create_update_and_deactivate_department():
engine, db = _make_session()
try:
created = create_department(
db,
name="Library",
code="LIB",
description="Library department",
keywords="books,study",
)
assert created.name == "Library"
assert created.is_active is True
updated = update_department(
db,
department_id=created.id,
payload=AdminDepartmentUpdate(
name="Library Services",
code="LIB-S",
description="Updated library department",
keywords="books, study, catalog",
is_active=True,
),
)
assert updated.name == "Library Services"
assert updated.code == "LIB-S"
assert updated.keywords == "books, study, catalog"
deactivated = deactivate_department(db, department_id=created.id)
assert deactivated.is_active is False
departments = list_departments(db)
assert departments == []
finally:
db.close()
Base.metadata.drop_all(bind=engine)
def test_admin_can_list_and_update_user_department():
engine, db = _make_session()
try:
finance = Department(
name="Finance Department",
code="FIN",
description="Finance work",
keywords="finance, budget",
)
exam = Department(
name="Examination Department",
code="EXAM",
description="Exam work",
keywords="exam, timetable",
)
db.add_all([finance, exam])
db.commit()
db.refresh(finance)
db.refresh(exam)
user = create_user(
db,
SignupRequest(
email="staff@example.com",
password="password123",
full_name="Finance Staff",
department_name="Finance Department",
),
)
users = list_users(db)
assert len(users) == 1
assert users[0].department_name == "Finance Department"
updated = update_user(
db,
user_id=user.id,
payload=AdminUserUpdate(
full_name="Exam Staff",
department_name="Examination Department",
is_active=False,
),
)
assert updated.full_name == "Exam Staff"
assert updated.role == "Examination Department"
assert updated.department_id == exam.id
assert updated.is_active is False
deactivated = deactivate_user(db, user_id=user.id)
assert deactivated.is_active is False
finally:
db.close()
Base.metadata.drop_all(bind=engine)
|