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

Тесты для бекенда

This commit is contained in:
MoonTestUse1
2025-01-04 03:22:23 +06:00
parent 0d543ed4f6
commit 2bde43c076
28 changed files with 740 additions and 990 deletions

View File

@@ -1,22 +1,35 @@
import os
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from fastapi.testclient import TestClient
from unittest.mock import Mock, patch
from ..database import Base, get_db
from ..main import app
from ..utils.jwt import create_and_save_token
from ..crud import employees
from ..utils.auth import get_password_hash
# Получаем URL базы данных из переменной окружения или используем значение по умолчанию
SQLALCHEMY_DATABASE_URL = os.getenv(
"DATABASE_URL",
"postgresql://postgres:postgres@localhost:5432/support_test"
)
engine = create_engine(SQLALCHEMY_DATABASE_URL)
# Используем SQLite для тестов
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Мок для Redis
class MockRedis:
def __init__(self):
self.data = {}
def setex(self, name, time, value):
self.data[name] = value
return True
def get(self, name):
return self.data.get(name)
@pytest.fixture(autouse=True)
def mock_redis():
with patch("app.utils.jwt.redis", MockRedis()):
yield
@pytest.fixture(scope="function")
def test_db():
# Создаем таблицы
@@ -33,14 +46,14 @@ def test_db():
@pytest.fixture(scope="function")
def test_employee(test_db):
hashed_password = get_password_hash("testpass123")
employee_data = {
"first_name": "Test",
"last_name": "User",
"department": "IT",
"office": "101",
"password": "testpass123"
"office": "101"
}
employee = employees.create_employee(test_db, employee_data)
employee = employees.create_employee(test_db, employee_data, hashed_password=hashed_password)
return employee
@pytest.fixture(scope="function")

View File

@@ -16,9 +16,9 @@ def test_login_success(test_db: Session):
"first_name": "Test",
"last_name": "User",
"department": "IT",
"office": "101",
"password": "testpass123"
}
"office": "101"
},
hashed_password=hashed_password
)
response = client.post(
@@ -34,10 +34,23 @@ def test_login_success(test_db: Session):
assert response.json()["token_type"] == "bearer"
def test_login_wrong_password(test_db: Session):
# Создаем тестового сотрудника с известным паролем
hashed_password = get_password_hash("testpass123")
employee = employees.create_employee(
test_db,
{
"first_name": "Test",
"last_name": "WrongPass",
"department": "IT",
"office": "101"
},
hashed_password=hashed_password
)
response = client.post(
"/api/auth/login",
data={
"username": "User",
"username": "WrongPass",
"password": "wrongpass"
}
)

View File

@@ -3,7 +3,7 @@ from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from ..main import app
from ..crud import employees
from ..utils.auth import verify_password
from ..utils.auth import verify_password, get_password_hash
client = TestClient(app)
@@ -49,15 +49,16 @@ def test_create_employee_unauthorized():
def test_get_employees(test_db: Session, admin_token: str):
# Создаем несколько тестовых сотрудников
for i in range(3):
hashed_password = get_password_hash("testpass123")
employees.create_employee(
test_db,
{
"first_name": f"Test{i}",
"last_name": f"User{i}",
"department": "IT",
"office": f"10{i}",
"password": "testpass123"
}
"office": f"10{i}"
},
hashed_password=hashed_password
)
response = client.get(
@@ -71,15 +72,16 @@ def test_get_employees(test_db: Session, admin_token: str):
def test_get_employee_by_id(test_db: Session, admin_token: str):
# Создаем тестового сотрудника
hashed_password = get_password_hash("testpass123")
employee = employees.create_employee(
test_db,
{
"first_name": "Test",
"last_name": "User",
"department": "IT",
"office": "101",
"password": "testpass123"
}
"office": "101"
},
hashed_password=hashed_password
)
response = client.get(
@@ -95,15 +97,16 @@ def test_get_employee_by_id(test_db: Session, admin_token: str):
def test_update_employee(test_db: Session, admin_token: str):
# Создаем тестового сотрудника
hashed_password = get_password_hash("testpass123")
employee = employees.create_employee(
test_db,
{
"first_name": "Test",
"last_name": "User",
"department": "IT",
"office": "101",
"password": "testpass123"
}
"office": "101"
},
hashed_password=hashed_password
)
update_data = {
@@ -124,15 +127,16 @@ def test_update_employee(test_db: Session, admin_token: str):
def test_delete_employee(test_db: Session, admin_token: str):
# Создаем тестового сотрудника
hashed_password = get_password_hash("testpass123")
employee = employees.create_employee(
test_db,
{
"first_name": "Test",
"last_name": "User",
"department": "IT",
"office": "101",
"password": "testpass123"
}
"office": "101"
},
hashed_password=hashed_password
)
response = client.delete(

View File

@@ -2,17 +2,18 @@ import pytest
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from ..main import app
from ..models.request import RequestStatus, RequestPriority
from ..crud import requests, employees
from ..models.request import RequestStatus
from ..utils.auth import get_password_hash
client = TestClient(app)
def test_create_request(test_db: Session, test_token: str):
request_data = {
"title": "Test Request",
"description": "Test Description",
"priority": "low",
"status": "new"
"department": "IT",
"request_type": "hardware",
"priority": RequestPriority.LOW.value,
"description": "Test Description"
}
response = client.post(
@@ -23,16 +24,17 @@ def test_create_request(test_db: Session, test_token: str):
assert response.status_code == 200
data = response.json()
assert data["title"] == request_data["title"]
assert data["department"] == request_data["department"]
assert data["description"] == request_data["description"]
assert data["priority"] == request_data["priority"]
assert data["priority"] == RequestPriority.LOW.value
assert data["status"] == RequestStatus.NEW.value
def test_create_request_unauthorized():
request_data = {
"title": "Test Request",
"description": "Test Description",
"priority": "low"
"department": "IT",
"request_type": "hardware",
"priority": RequestPriority.LOW.value,
"description": "Test Description"
}
response = client.post(
@@ -48,10 +50,10 @@ def test_get_employee_requests(test_db: Session, test_token: str, test_employee_
requests.create_request(
test_db,
{
"title": f"Test Request {i}",
"description": f"Test Description {i}",
"priority": "low",
"status": "new"
"department": "IT",
"request_type": f"hardware_{i}",
"priority": RequestPriority.LOW.value,
"description": f"Test Description {i}"
},
test_employee_id
)
@@ -67,32 +69,34 @@ def test_get_employee_requests(test_db: Session, test_token: str, test_employee_
assert all(req["employee_id"] == test_employee_id for req in data)
def test_update_request_status(test_db: Session, admin_token: str):
# Создаем тестовую заявку
# Создаем тестового сотрудника
hashed_password = get_password_hash("testpass123")
employee = employees.create_employee(
test_db,
{
"first_name": "Test",
"last_name": "User",
"department": "IT",
"office": "101",
"password": "testpass123"
}
"office": "101"
},
hashed_password=hashed_password
)
# Создаем тестовую заявку
request = requests.create_request(
test_db,
{
"title": "Test Request",
"description": "Test Description",
"priority": "low",
"status": "new"
"department": "IT",
"request_type": "hardware",
"priority": RequestPriority.LOW.value,
"description": "Test Description"
},
employee.id
)
response = client.put(
f"/api/requests/{request.id}",
json={"status": "in_progress"},
json={"status": RequestStatus.IN_PROGRESS.value},
headers={"Authorization": f"Bearer {admin_token}"}
)
@@ -107,8 +111,9 @@ def test_get_request_statistics(test_db: Session, admin_token: str):
assert response.status_code == 200
data = response.json()
assert "total" in data
assert "new" in data
assert "in_progress" in data
assert "completed" in data
assert "rejected" in data
assert "total_requests" in data
assert "by_status" in data
assert RequestStatus.NEW.value in data["by_status"]
assert RequestStatus.IN_PROGRESS.value in data["by_status"]
assert RequestStatus.COMPLETED.value in data["by_status"]
assert RequestStatus.REJECTED.value in data["by_status"]