1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00

testing pipe

This commit is contained in:
MoonTestUse1
2025-01-06 05:24:29 +06:00
parent fba5b250a7
commit 2f7bf9c28c
21 changed files with 582 additions and 475 deletions

View File

@@ -1,117 +1,135 @@
"""Employee tests."""
import pytest
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from app.main import app
from app.crud import employees
from app.utils.auth import get_password_hash
from app.schemas.employee import EmployeeCreate
from app.models.employee import Employee
client = TestClient(app)
def test_create_employee(test_db: Session, admin_auth_header):
"""Test creating a new employee"""
employee_data = {
"first_name": "John",
"last_name": "Doe",
"department": "IT",
"office": "B205",
"password": "test123"
}
def test_create_employee(client: TestClient, admin_token: str, db: Session):
"""Тест создания сотрудника."""
response = client.post(
"/api/employees/",
json=employee_data,
headers=admin_auth_header
"/api/employees",
headers={"Authorization": f"Bearer {admin_token}"},
json={
"email": "new@example.com",
"password": "newpassword",
"full_name": "New Employee",
"department": "IT",
"is_active": True,
"is_admin": False
}
)
assert response.status_code == 201
data = response.json()
assert data["email"] == "new@example.com"
assert data["full_name"] == "New Employee"
assert data["department"] == "IT"
assert "id" in data
def test_create_employee_unauthorized(client: TestClient):
"""Тест создания сотрудника без авторизации."""
response = client.post(
"/api/employees",
json={
"email": "new@example.com",
"password": "newpassword",
"full_name": "New Employee",
"is_active": True,
"is_admin": False
}
)
assert response.status_code == 401
assert response.json()["detail"] == "Not authenticated"
def test_get_employees(client: TestClient, admin_token: str, test_employee: Employee, db: Session):
"""Тест получения списка сотрудников."""
response = client.get(
"/api/employees",
headers={"Authorization": f"Bearer {admin_token}"}
)
assert response.status_code == 200
data = response.json()
assert data["first_name"] == employee_data["first_name"]
assert data["last_name"] == employee_data["last_name"]
assert data["department"] == employee_data["department"]
assert data["office"] == employee_data["office"]
assert "password" not in data
assert isinstance(data, list)
assert len(data) > 0
assert "email" in data[0]
assert "full_name" in data[0]
assert "department" in data[0]
def test_get_employees(test_db: Session, test_employee, admin_auth_header):
"""Test getting list of employees"""
response = client.get("/api/employees/", headers=admin_auth_header)
assert response.status_code == 200
data = response.json()
assert len(data) >= 1
assert data[0]["first_name"] == test_employee.first_name
assert data[0]["last_name"] == test_employee.last_name
assert data[0]["department"] == test_employee.department
assert data[0]["office"] == test_employee.office
assert "password" not in data[0]
def test_create_employee_unauthorized(test_db: Session):
"""Test creating employee without authorization"""
employee_data = {
"first_name": "John",
"last_name": "Doe",
"department": "IT",
"office": "B205",
"password": "test123"
}
response = client.post("/api/employees/", json=employee_data)
assert response.status_code == 401 # Unauthorized
def test_get_employees_unauthorized(test_db: Session):
"""Test getting employees list without authorization"""
response = client.get("/api/employees/")
assert response.status_code == 401 # Unauthorized
def test_get_employee_by_id(test_db: Session, test_employee, admin_auth_header):
"""Test getting employee by ID"""
def test_get_employee_by_id(client: TestClient, admin_token: str, test_employee: Employee, db: Session):
"""Тест получения сотрудника по ID."""
response = client.get(
f"/api/employees/{test_employee.id}",
headers=admin_auth_header
headers={"Authorization": f"Bearer {admin_token}"}
)
assert response.status_code == 200
data = response.json()
assert data["first_name"] == test_employee.first_name
assert data["last_name"] == test_employee.last_name
assert data["email"] == test_employee.email
assert data["full_name"] == test_employee.full_name
assert data["department"] == test_employee.department
assert data["office"] == test_employee.office
assert "password" not in data
def test_update_employee(test_db: Session, test_employee, admin_auth_header):
"""Test updating employee data"""
update_data = {
"first_name": "Updated",
"last_name": "Name",
"department": "HR",
"office": "B202"
}
def test_get_nonexistent_employee(client: TestClient, admin_token: str):
"""Тест получения несуществующего сотрудника."""
response = client.get(
"/api/employees/999",
headers={"Authorization": f"Bearer {admin_token}"}
)
assert response.status_code == 404
assert response.json()["detail"] == "Employee not found"
def test_update_employee(client: TestClient, admin_token: str, test_employee: Employee, db: Session):
"""Тест обновления данных сотрудника."""
response = client.put(
f"/api/employees/{test_employee.id}",
json=update_data,
headers=admin_auth_header
headers={"Authorization": f"Bearer {admin_token}"},
json={
"email": "updated@example.com",
"full_name": "Updated Employee",
"department": "HR",
"is_active": True,
"is_admin": False
}
)
assert response.status_code == 200
data = response.json()
assert data["first_name"] == update_data["first_name"]
assert data["last_name"] == update_data["last_name"]
assert data["department"] == update_data["department"]
assert data["office"] == update_data["office"]
assert "password" not in data
assert data["email"] == "updated@example.com"
assert data["full_name"] == "Updated Employee"
assert data["department"] == "HR"
def test_delete_employee(test_db: Session, test_employee, admin_auth_header):
"""Test deleting employee"""
def test_delete_employee(client: TestClient, admin_token: str, test_employee: Employee, db: Session):
"""Тест удаления сотрудника."""
response = client.delete(
f"/api/employees/{test_employee.id}",
headers=admin_auth_header
headers={"Authorization": f"Bearer {admin_token}"}
)
assert response.status_code == 200
# Verify employee is deleted
get_response = client.get(
f"/api/employees/{test_employee.id}",
headers=admin_auth_header
data = response.json()
assert data["email"] == test_employee.email
assert data["full_name"] == test_employee.full_name
assert data["department"] == test_employee.department
def test_employee_me(client: TestClient, employee_token: str, test_employee: Employee, db: Session):
"""Тест получения информации о текущем сотруднике."""
response = client.get(
"/api/employees/me",
headers={"Authorization": f"Bearer {employee_token}"}
)
assert get_response.status_code == 404
assert response.status_code == 200
data = response.json()
assert data["email"] == test_employee.email
assert data["full_name"] == test_employee.full_name
assert data["department"] == test_employee.department
def test_update_me(client: TestClient, employee_token: str, test_employee: Employee, db: Session):
"""Тест обновления информации о текущем сотруднике."""
response = client.put(
"/api/employees/me",
headers={"Authorization": f"Bearer {employee_token}"},
json={
"full_name": "Updated Name",
"department": "Support"
}
)
assert response.status_code == 200
data = response.json()
assert data["full_name"] == "Updated Name"
assert data["email"] == test_employee.email
assert data["department"] == "Support"