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

Создание чата9testt

This commit is contained in:
MoonTestUse1
2025-01-05 06:32:34 +06:00
parent 9ba671bdaa
commit 7f7838a0d3
28 changed files with 653 additions and 721 deletions

View File

@@ -1,117 +1,93 @@
"""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.core.auth import create_access_token
from app.models.user import User
client = TestClient(app)
pytestmark = pytest.mark.asyncio
def test_create_employee(test_db: Session, admin_auth_header):
"""Test creating a new employee"""
@pytest.mark.asyncio
async def test_create_employee(client: TestClient, test_admin: dict, db: Session):
"""Test creating a new employee."""
access_token = create_access_token(
data={"sub": test_admin["email"], "is_admin": True}
)
employee_data = {
"first_name": "John",
"last_name": "Doe",
"department": "IT",
"office": "B205",
"password": "test123"
"email": "new@example.com",
"password": "newpass123",
"full_name": "New Employee",
"is_admin": False
}
response = client.post(
"/api/employees/",
json=employee_data,
headers=admin_auth_header
headers={"Authorization": f"Bearer {access_token}"},
json=employee_data
)
assert response.status_code == 200
data = response.json()
assert data["email"] == employee_data["email"]
assert data["full_name"] == employee_data["full_name"]
assert not data["is_admin"]
@pytest.mark.asyncio
async def test_create_employee_unauthorized(client: TestClient, test_user: dict):
"""Test creating an employee without admin rights."""
access_token = create_access_token(
data={"sub": test_user["email"], "is_admin": False}
)
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
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"
"email": "new@example.com",
"password": "newpass123",
"full_name": "New Employee",
"is_admin": False
}
response = client.post("/api/employees/", json=employee_data)
assert response.status_code == 401 # Unauthorized
response = client.post(
"/api/employees/",
headers={"Authorization": f"Bearer {access_token}"},
json=employee_data
)
assert response.status_code == 403
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"""
response = client.get(
f"/api/employees/{test_employee.id}",
headers=admin_auth_header
@pytest.mark.asyncio
async def test_get_employee(client: TestClient, test_admin: dict, test_user: dict, db: Session):
"""Test getting an employee by ID."""
access_token = create_access_token(
data={"sub": test_admin["email"], "is_admin": True}
)
# Получаем ID тестового пользователя
user = db.query(User).filter(User.email == test_user["email"]).first()
response = client.get(
f"/api/employees/{user.id}",
headers={"Authorization": f"Bearer {access_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["department"] == test_employee.department
assert data["office"] == test_employee.office
assert "password" not in data
assert data["email"] == test_user["email"]
assert data["full_name"] == test_user["full_name"]
def test_update_employee(test_db: Session, test_employee, admin_auth_header):
"""Test updating employee data"""
@pytest.mark.asyncio
async def test_update_employee_me(client: TestClient, test_user: dict):
"""Test updating current employee info."""
access_token = create_access_token(
data={"sub": test_user["email"], "is_admin": False}
)
update_data = {
"first_name": "Updated",
"last_name": "Name",
"department": "HR",
"office": "B202"
"full_name": "Updated Name"
}
response = client.put(
f"/api/employees/{test_employee.id}",
json=update_data,
headers=admin_auth_header
"/api/employees/me",
headers={"Authorization": f"Bearer {access_token}"},
json=update_data
)
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
def test_delete_employee(test_db: Session, test_employee, admin_auth_header):
"""Test deleting employee"""
response = client.delete(
f"/api/employees/{test_employee.id}",
headers=admin_auth_header
)
assert response.status_code == 200
# Verify employee is deleted
get_response = client.get(
f"/api/employees/{test_employee.id}",
headers=admin_auth_header
)
assert get_response.status_code == 404
assert data["full_name"] == update_data["full_name"]