mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Fix tests
This commit is contained in:
@@ -13,20 +13,19 @@ def get_employee(db: Session, employee_id: int) -> Optional[Employee]:
|
|||||||
"""Get employee by ID"""
|
"""Get employee by ID"""
|
||||||
return db.query(Employee).filter(Employee.id == employee_id).first()
|
return db.query(Employee).filter(Employee.id == employee_id).first()
|
||||||
|
|
||||||
def get_employee_by_email(db: Session, email: str) -> Optional[Employee]:
|
def get_employee_by_last_name(db: Session, last_name: str) -> Optional[Employee]:
|
||||||
"""Get employee by email"""
|
"""Get employee by last name"""
|
||||||
return db.query(Employee).filter(Employee.email == email).first()
|
return db.query(Employee).filter(Employee.last_name == last_name).first()
|
||||||
|
|
||||||
def create_employee(db: Session, employee: EmployeeCreate, hashed_password: str) -> Employee:
|
def create_employee(db: Session, employee: EmployeeCreate, hashed_password: str) -> Employee:
|
||||||
"""Create new employee"""
|
"""Create new employee"""
|
||||||
try:
|
try:
|
||||||
db_employee = Employee(
|
db_employee = Employee(
|
||||||
email=employee.email,
|
first_name=employee.first_name,
|
||||||
full_name=employee.full_name,
|
last_name=employee.last_name,
|
||||||
hashed_password=hashed_password,
|
department=employee.department,
|
||||||
is_active=employee.is_active,
|
office=employee.office,
|
||||||
is_admin=employee.is_admin,
|
hashed_password=hashed_password
|
||||||
department=employee.department
|
|
||||||
)
|
)
|
||||||
db.add(db_employee)
|
db.add(db_employee)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|||||||
@@ -6,17 +6,16 @@ from app.utils.auth import get_password_hash
|
|||||||
|
|
||||||
def init_db(db: Session) -> None:
|
def init_db(db: Session) -> None:
|
||||||
"""Initialize database with default data"""
|
"""Initialize database with default data"""
|
||||||
# Создаем администратора по умолчанию
|
# Создаем тестового сотрудника
|
||||||
admin = db.query(Employee).filter(Employee.email == settings.ADMIN_USERNAME).first()
|
test_employee = db.query(Employee).filter(Employee.last_name == "User").first()
|
||||||
if not admin:
|
if not test_employee:
|
||||||
admin = Employee(
|
test_employee = Employee(
|
||||||
email=settings.ADMIN_USERNAME,
|
first_name="Test",
|
||||||
full_name="System Administrator",
|
last_name="User",
|
||||||
hashed_password=get_password_hash(settings.ADMIN_PASSWORD),
|
department="IT",
|
||||||
is_active=True,
|
office="101",
|
||||||
is_admin=True,
|
hashed_password=get_password_hash("testpass123")
|
||||||
department="Administration"
|
|
||||||
)
|
)
|
||||||
db.add(admin)
|
db.add(test_employee)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(admin)
|
db.refresh(test_employee)
|
||||||
@@ -8,12 +8,11 @@ class Employee(Base):
|
|||||||
__tablename__ = "employees"
|
__tablename__ = "employees"
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True, index=True)
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
email = Column(String, unique=True, index=True, nullable=False)
|
first_name = Column(String, nullable=False)
|
||||||
full_name = Column(String, nullable=False)
|
last_name = Column(String, nullable=False)
|
||||||
|
department = Column(String, nullable=False)
|
||||||
|
office = Column(String, nullable=False)
|
||||||
hashed_password = Column(String, nullable=False)
|
hashed_password = Column(String, nullable=False)
|
||||||
is_active = Column(Boolean, default=True)
|
|
||||||
is_admin = Column(Boolean, default=False)
|
|
||||||
department = Column(String, nullable=True)
|
|
||||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
|
|
||||||
# Определяем отношение к Request
|
# Определяем отношение к Request
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ async def login_for_access_token(
|
|||||||
):
|
):
|
||||||
"""Авторизация сотрудника"""
|
"""Авторизация сотрудника"""
|
||||||
# Проверяем учетные данные сотрудника
|
# Проверяем учетные данные сотрудника
|
||||||
employee = employees.get_employee_by_email(db, form_data.username)
|
employee = employees.get_employee_by_last_name(db, form_data.username)
|
||||||
if not employee or not verify_password(form_data.password, employee.hashed_password):
|
if not employee or not verify_password(form_data.password, employee.hashed_password):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
@@ -42,17 +42,16 @@ async def admin_login(
|
|||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
"""Авторизация администратора"""
|
"""Авторизация администратора"""
|
||||||
# Проверяем учетные данные администратора
|
if form_data.username != "admin" or form_data.password != "admin123":
|
||||||
employee = employees.get_employee_by_email(db, form_data.username)
|
|
||||||
if not employee or not employee.is_admin or not verify_password(form_data.password, employee.hashed_password):
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
detail="Incorrect username or password",
|
detail="Incorrect username or password",
|
||||||
headers={"WWW-Authenticate": "Bearer"},
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
)
|
)
|
||||||
|
|
||||||
# Создаем и сохраняем токен
|
# Для админа используем специальный ID
|
||||||
access_token = create_and_save_token(employee.id, db)
|
admin_id = -1
|
||||||
|
access_token = create_and_save_token(admin_id, db)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"access_token": access_token,
|
"access_token": access_token,
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
"""Employee schemas"""
|
"""Employee schemas"""
|
||||||
from pydantic import BaseModel, ConfigDict, EmailStr
|
from pydantic import BaseModel, ConfigDict
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
class EmployeeBase(BaseModel):
|
class EmployeeBase(BaseModel):
|
||||||
email: EmailStr
|
first_name: str
|
||||||
full_name: str
|
last_name: str
|
||||||
department: Optional[str] = None
|
department: str
|
||||||
is_active: bool = True
|
office: str
|
||||||
is_admin: bool = False
|
|
||||||
|
|
||||||
model_config = ConfigDict(from_attributes=True)
|
model_config = ConfigDict(from_attributes=True)
|
||||||
|
|
||||||
@@ -16,11 +15,10 @@ class EmployeeCreate(EmployeeBase):
|
|||||||
password: str
|
password: str
|
||||||
|
|
||||||
class EmployeeUpdate(BaseModel):
|
class EmployeeUpdate(BaseModel):
|
||||||
email: Optional[EmailStr] = None
|
first_name: Optional[str] = None
|
||||||
full_name: Optional[str] = None
|
last_name: Optional[str] = None
|
||||||
department: Optional[str] = None
|
department: Optional[str] = None
|
||||||
is_active: Optional[bool] = None
|
office: Optional[str] = None
|
||||||
is_admin: Optional[bool] = None
|
|
||||||
|
|
||||||
model_config = ConfigDict(from_attributes=True)
|
model_config = ConfigDict(from_attributes=True)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user